if (!document.trace) {
	this.trace = function(output) {
		//if (console.log && output) console.log(output);
	}
}

/**
 * User Status Module - view helper.
 * @uses mootools-release-1.11.js
 */

var UserStatus = new Class({
	
	
	initialize : function() {
	
		// State enumerations
		
		this.SIGNED_IN = "userSignedIn";
		this.SIGNED_OUT = "userSignedOut";
		this.GUEST = "userGuest";
		
		// Doc element static references
		
		this.ELEM_AVATAR = $('user_status_avatar');
		this.ELEM_USERNAME = $('user_status_player');
		this.ELEM_DESCRIPTION = $('user_status_description');
		this.ELEM_BUTTON = $('user_status_link');
		this.ELEM_POINTS = $('user_status_points');
		
		// Number of cycles to animate through when score updates
		
		this.cycles = 50;
		
		// Duration for fade transitions (milliseconds)
		
		this.fadeDuration = 700;
		
		// @private
		
		this.currentPoints = 0;
		this.newPoints = 0;
		this.increment = 0;
		this.loop = 0;
		this.positive = true;
	},
	
	/**
	 * Set the general state of the module.
	 * 
	 * @param 	state A valid state key to set the overall view state to.
  	 */
	setState : function(state) {
		
		switch (state) {
	
			case this.SIGNED_IN : 
				
				//this.updateButton("VIEW PROFILE", "#player");
				
				var fx = new Fx.Elements($$(this.ELEM_AVATAR, this.ELEM_POINTS), {duration: this.fadeDuration});
				
				fx.start({
    				'0' : {'opacity': 1.00},
    				'1' : {'opacity': 1.00}
				});
						
				break;
				
			case this.SIGNED_OUT :
				
				//this.updateButton("SIGN BACK IN", "#signin");

				var fx = new Fx.Elements($$(this.ELEM_AVATAR, this.ELEM_POINTS), {duration: this.fadeDuration});
				
				fx.start({
    				'0' : {'opacity': 0.40},
    				'1' : {'opacity': 0.40}
				});
				
				break;
			
			case this.GUEST : default :
			
				this.updateButton("SIGN-IN NOW", "#signin");
				
				var fx = new Fx.Elements($$(this.ELEM_AVATAR, this.ELEM_POINTS), {duration: this.fadeDuration});
				
				fx.start({
    				'0' : {'opacity': 1.00},
    				'1' : {'opacity': 1.00}
				});
				
				break;
		}
	},
	
	/**
	 * Update the avatar image for the view.
	 *
	 * @param 	image An avatar image URL.
     */
	updateAvatar : function(image) {
		if (image) this.ELEM_AVATAR.setProperty('src', image);
	},

	/**
	 * Update the user name field for the view.
	 *
	 * @param 	name (String) The user name to display (trimming managed by CSS).
	 */
	updateUsername : function(name) {
		if (name) this.ELEM_USERNAME.setText(name);
	},

	/**
	 * Update the description field for the view.
	 *
	 * @param 	description (String) The description to display (trimming managed by CSS).
	 */
	updateDescription : function(description) {
		if (description) this.ELEM_DESCRIPTION.setText(description);
	},

	/**
	 * Update the button element for the view.
	 * 
	 * @param 	label (String) The text label to display on the button.
	 * @param 	action (String) The HREF action to perform when the button is pressed.
	 */
	updateButton : function(label, action) {
		if (label) this.ELEM_BUTTON.setText(label);
		if (action) this.ELEM_BUTTON.setProperty('href', action);
	},

	/**
	 * Update the points element for the view.
	 * 
	 * @param 	points (Number) The new points value.
	 */ 
	updatePoints : function(points) {
		if (isNaN(points)) points = this.ELEM_POINTS.getText().toInt();
		this.onUpdatePoints(points);
	},

	
	onUpdatePoints : function(points) {
	
		this.newPoints = points;
		this.currentPoints = this.ELEM_POINTS.getText().toInt();
		
		var delta = this.newPoints - this.currentPoints;
		
		this.increment = Math.ceil(delta / this.cycles);
		
		//if (this.increment > 0 && this.increment < 1) this.increment = 1; //<< shouldn't need these with Math.ceil() result...
		//if (this.increment < 0 && this.increment > -1) this.increment = -1;
		
		this.positive = (this.increment > 0);
		
		this.loop = this.onUpdatePointsProgress.periodical(10, this);	
	},
	
	
	onUpdatePointsProgress : function() {
	
		this.currentPoints += this.increment;
		
		if (this.positive && this.currentPoints >= this.newPoints)
			this.onUpdatePointsComplete();
		else if (!this.positive && this.currentPoints <= this.newPoints)
			this.onUpdatePointsComplete();
		else
			this.commitPoints(this.currentPoints);
	},

	
	onUpdatePointsComplete : function() {
	
		this.loop = $clear(this.loop);
		this.commitPoints(this.newPoints)
	},

	
	commitPoints : function(points) {
		
		this.ELEM_POINTS.setText(points);
	},
	
	
	toString : function() {
		return "[UserStatus]";
	}
	
});


 