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

DownloadModel = Class.create({

	status: 'idle',
	value: '0',
	total: '0',
	
	initialize: function(url) {

		this.title = this._makeTitleFromUrl(url);
	},
	
	_makeTitleFromUrl: function(url) {
		if (url) {
		
			var result = url.match(/^.*\/([^\/]+)$/);
			if ((result !== null) && (result.length > 1)) {
				return result[1];
			}
			else {
				return url;
			}
		}	
	},
	
	start: function(ticket, file) {
		
		// Extract the ticket for later communications with the download manager
		this.ticket = ticket;
		if (file) {
			this.title = file.substring(file.lastIndexOf("\/")+1);
		}

		this.status = 'download';
	},
	
	error: function() {
		
		this.status = 'cancelled';
		// On an error HI requirement is to present a 'full' pill.
		this.value = 1;
		this.icon = 'action-icon warning';
	},
	
	progress: function(received, total) {
		
		total = total === 0 ? received : total;		
		this.value = received / total;
		this.received = received;
		this.total = total;
		this.status = 'download';
	},
	
	complete: function(mimetype, file) {
	
		this.mimetype = mimetype;
		this.file = file;
		// Set the value to undefined now
		// makes the whole button clickable.
		this.value = undefined;
		this.icon = 'action-icon complete';
		this.status = 'complete';
	},
	
	abort: function(mimetype, file) {
	
		this.mimetype = mimetype;
		// On an abort HI requirement is to present a 'full' pill.
		this.value = 1;
		this.file = file;
		this.icon = 'action-icon warning';
		this.status = 'cancelled';
	}
});
