/*
 * Copyright 2008-2009 Palm, Inc. All rights reserved.
 */

DownloadItem = {};

/**
 * Contains information for the item being currently downloaded via the
 * download manager. 
 */
DownloadItem = Class.create({
	
	/**
	 * Constructor.
	 * 
	 * @param {String} uri The resource being downloaded
	 */
	initialize: function(uri) {
		this.uri = uri;
		this.amountReceived = 0;
		this.totalSize = 0;
		this.mimeType = undefined;
		this.ticket = undefined;
		this.completed = false;
		this.canceling = false;
		this.canceled = false;
		this.error = undefined;
		this.localFile = undefined;
		this.rightIcon = 'item-cancel';
		this.title = DownloadItem.generateTitle(uri);
	},
	
	/**
	 * Return the current download progress as a percent 0..1.
	 */
	downloadProgress: function() {

		if (!this.completed) {
			if (this.totalSize === undefined || this.totalSize === 0) {
				return 0;
			}
			else {
				return this.amountReceived / this.totalSize;
			}
		}
		
		return 1;
	},
	
	/**
	 * Is this download complete.
	 */
	downloadComplete: function() {
		return this.completed;
	},
	
	/**
	 * Mark this download as completed.
	 */
	setDownloadComplete: function() {
		this.completed = true;
	}
});

DownloadItem.generateTitle = function(uri) {
	var result = uri.match(/^.*\/([^\/]+)$/);
	if (result !== null) {
		return result[1];
	}
	else {
		return uri;
	}
};
