// -----------------------------------------------------------------------------
// slideshow.js
// -----------------------------------------------------------------------------


/**
 *	Slideshow Written by Michael Kramer
 */
var Player = function() {
	var PLAY = 1;
	var PAUSE = 0;
	var SPEED = 10; // IN SECONDS
	return {
		/**
		 * Initialize The Player
		 */
		init: function(){
			this.status = PLAY;
			this.speed = SPEED * 1000;
			this.prev = dojo.byId('player_previous');
			this.action = dojo.byId('player_action');
			this.next = dojo.byId('player_next');
			//attach actions
			dojo.connect(this.prev, 'onclick', this, '_previous');
			dojo.connect(this.next, 'onclick', this, '_next');
			dojo.connect(this.action, 'onclick', this, '_toggleStatus');
			//Setup Slides
			this.t_current = null;
			this.li_current = null;
			this.old_target = null;
			this.oldTimer = null;
			dojo.byId('slide-2').style.display = 'none';
			dojo.byId('slide-3').style.display = 'none';
			dojo.byId('slide-4').style.display = 'none';
			this.t_current = 1;		
			//start the auto fading thingy
			setTimeout(function() { p_Player._next(); }, this.speed);
		},
		/**
		 * Toggle play/pause status
		 */
		_toggleStatus: function(){
			if(this.status == PLAY){
				console.log('Pausing');
				this._clearTimer();
				this.status = PAUSE;
				this.action.className = 'play';
			} else{
				console.log('Resuming Play');
				this.oldTimer = setTimeout(function() { p_Player._next(); }, this.speed);
				this.status = PLAY;
				this.action.className = 'pause';
			}
		},
		/**
		 * Goto Previous Slide
		 */
		_previous: function(){
			this._clearTimer();
			i_nextTarget = this.t_current - 1;
			if(null == dojo.byId('slide-' + i_nextTarget) ){
				i_nextTarget = 1;
				console.log('We are already at the first.');
				this.oldTimer = setTimeout(function() { p_Player._next(); }, this.speed);
				return;
			}
			if(this.status == PLAY){
				this.oldTimer = setTimeout(function() { p_Player._next(); }, this.speed);
			}
			this.old_target = dojo.byId('slide-'+this.t_current);
			this.fadeOut(dojo.byId('slide-'+this.t_current), 500, function(){ p_Player.old_target.style.display ='none'; });
			this.fadeIn(dojo.byId('slide-'+i_nextTarget), 1000);
			this.t_current = i_nextTarget;
		},
		/**
		 * Goto Next Slide
		 */
		_next: function(){
			this._clearTimer();
			i_nextTarget = this.t_current + 1;
			if(null == dojo.byId('slide-' + i_nextTarget) ){
				i_nextTarget = 1;
				console.log('Next target is undefined');
			}
			if(this.status == PLAY){
				this.oldTimer = setTimeout(function() { p_Player._next(); }, this.speed);
			}
			this.old_target = dojo.byId('slide-'+this.t_current);
			this.fadeOut(dojo.byId('slide-'+this.t_current), 500, function(){ p_Player.old_target.style.display ='none'; });
			this.fadeIn(dojo.byId('slide-'+i_nextTarget), 1000);
			this.t_current = i_nextTarget;
		},
		/**
		 * Clear the slide queue
		 */
		_clearTimer: function(){
			if(this.oldTimer != null){
				clearTimeout(this.oldTimer);
			}
		},
		/**
		 * Fade In a given target
		 *
		 * @param target The target we are Fading In
		 * @param d The duration of the Fade
		 */
		fadeIn: function(target, d){
			target.style.display = 'block';
			var args = { 
				node: target,
				duration: d,
				properties: { 
					opacity: {
						start:0.2,
						end:1.00
					}
			   }
			};
			dojo.animateProperty(args).play();
		},
		/**
		 * Fade Out a given target
		 *
		 * @param target The target we are Fading Out
		 * @param d The duration of the Fade
		 * @param end Function to run at the end of the fade
		 */
		fadeOut: function(target, d, end){
			var args = { 
				node: target,
				duration: d,
				properties: { 
					opacity: {
						start:1.00,
						end:0.0
					}
			   },
			   onEnd: end
			};
			dojo.animateProperty(args).play();
		}
	}
}
var p_Player = new Player();
dojo.addOnLoad(function(){ p_Player.init(); });