/*
 * Copyright 2009 Palm, Inc. All rights reserved.
 */
/*global Mojo, Class, $L */
/*jslint rhino:true, undef:true */

var PageProgressAnimation = Class.create({

	initialize: function(details) {
		
		Mojo.require(details.controller, "PageProgressAnimation requires a controller");
		Mojo.require(details.model, "PageProgressAnimation requires a model");

		this._controller = details.controller;
		this._model = details.model;
		this._iconClass = details.model.icon;
		this._iconSelector = '.' + details.model.icon;
		this._frameHeight = details.frameHeight || 48;

		this._currentFrameIndex = 0;
		this._currentPercentImage = 0;
	},
	
	_getElement: function() {

		var icon = this._controller.select(this._iconSelector);
		if (icon) {
			icon = icon[0];
		}

		return icon;
	},
	
	start: function(progress) {
		
		var element = this._getElement();		
		if (element) {
			// Get a handle onto the main animation queue.
			this._animationQueue = Mojo.Animation.queueForElement(element);
			this.setProgress(progress);
		}		
	},
	
	stop: function() {

		if (this._loadProgressAnimator) {
			this._loadProgressAnimator.cancel();
			delete this._loadProgressAnimator;
		}
	},
	
	/**
	 * Set the page load progress state.
	 *
	 * @param {Object} percent 0..100.
	 */	
	setProgress: function(percent) {

		try {
			if (percent > 100) {
				percent = 100;
			}
			else if (percent < 0) {
				percent = 0;
			}
			
			// Convert the percentage complete to an image number
			// Image must be from 0 to 25 (26 images available)
			var image = Math.round(percent / 3.85);
			if (image > 25) {
				image = 25;
			}
			
			// Has the progress changed?
			if (this._animationQueue && (this._currentPercentImage < image)) {
				// Cancel the existing animator if there is one
				if (this._loadProgressAnimator) {
					this._loadProgressAnimator.cancel();
					delete this._loadProgressAnimator;
				}
				
				// Animate from the current value to the new value
				this._loadProgressAnimator = Mojo.Animation.animateValue(this._animationQueue, 
					"linear", 
					this._updateLoadProgress.bind(this), 
					{
						from: this._currentPercentImage,
						to: image,
						duration: 0.5
					});
			}
		} 
		catch (e) {
			Mojo.Log.logException(e, "#setProgress");
		}
	},

	/**
	 * Update the load progress icon
	 *
	 * @param {Object} 0..23
	 */
	_updateLoadProgress: function(image) {
		
		try {
			// Find the progress image
			image = Math.round(image);
			
			// Don't do anything if the progress is already displayed
			if (this._currentPercentImage == image) {
				return;
			}
			
			var element = this._getElement();
			if (element) {
				if (this._model._progress) {
					element.removeClassName(this._model._progress);					
				}
				var progress = 'progress-' + image;
				element.addClassName(progress);
				this._model.icon = this._iconClass + ' ' +progress;
				this._model._progress = progress;
			}			
			this._currentPercentImage = image;
			
		} catch (e) {
			Mojo.Log.logException(e, "#updateLoadProgress()");
		}
	}
});
