/* YUI Slideshow Widget v0.1.1 by Josh Lizarraga @ FreshCutSD.com */
/* Based on jQuery Cycle: http://malsup.com/jquery/cycle/ */
/* Copyright 2009 FreshCutSD.com */

(function(){

if(typeof(YAHOO.widget.Slideshow) == "undefined"){

// Transitions
var T = new Object;
T.none = {
	cssStart: false,
	cssEnd: false,
	animation: false
};
T.staticTL = {
	cssStart: {
		top: "0px",
		left: "0px"
	},
	cssEnd: false,
	animation: false
};
T.staticTR = {
	cssStart: {
		top: "0px",
		right: "0px"
	},
	cssEnd: false,
	animation: false
};
T.staticBL = {
	cssStart: {
		bottom: "0px",
		left: "0px"
	},
	cssEnd: false,
	animation: false
};
T.staticBR = {
	cssStart: {
		bottom: "0px",
		right: "0px"
	},
	cssEnd: false,
	animation: false
};
T.fadeIn = {
	cssStart: false,
	cssEnd: false,
	animation: {
		opacity: { from: 0, to: 1 }
	}
};
T.fadeOut = {
	cssStart: false,
	cssEnd: false,
	animation: {
		opacity: { from: 1, to: 0 }
	}
};
T.swipeInLTR = {
	cssStart: false,
	cssEnd: false,
	animation: {
		left: { from: "-cW", to: 0 }
	}
};
T.swipeOutLTR = {
	cssStart: false,
	cssEnd: false,
	animation: {
		left: { from: 0, to: "cW" }
	}
};
T.swipeInRTL = {
	cssStart: false,
	cssEnd: false,
	animation: {
		right: { from: "-cW", to: 0 }
	}
};
T.swipeOutRTL = {
	cssStart: false,
	cssEnd: false,
	animation: {
		right: { from: 0, to: "cW" }
	}
};
T.swipeInTTB = {
	cssStart: false,
	cssEnd: false,
	animation: {
		top: { from: "-cH", to: 0 }
	}
};
T.swipeOutTTB = {
	cssStart: false,
	cssEnd: false,
	animation: {
		top: { from: 0, to: "cH" }
	}
};
T.swipeInBTT = {
	cssStart: false,
	cssEnd: false,
	animation: {
		bottom: { from: "-cH", to: 0 }
	}
};
T.swipeOutBTT = {
	cssStart: false,
	cssEnd: false,
	animation: {
		bottom: { from: 0, to: "cH" }
	}
};

YAHOO.widget.Slideshow = function(yswID, yswConfig){
	// Properties:
	this.id = (yswID) ? yswID : false;
	this.config = (yswConfig) ? yswConfig : false;
	this.container = document.getElementById(this.id);
	this.slides = YAHOO.util.Dom.getChildren(this.container);
	this.currentSlide = 0;
	this.zIndex = {
		container: "1",
		slides: "2",
		nextSlide: "3",
		currentSlide: "4"
	};
	this.loop = false;
	this.autoplay = true;
	this.timer = 3000;
	this.speed = 0.5;
	this.transitionIn = "fadeIn";
	this.transitionOut = "fadeOut";
	this.easingIn = "easeOut";
	this.easingOut = "easeOut";
	this.previousButton = false;
	this.nextButton = false;
	this.playButton = false;
	this.pauseButton = false;
	this.stopOnUser = true;
	this.debug = false; // debug + autoplay = timeout instead of interval.
	if(this.config != false){
		for(var i in this.config){
			this[i] = this.config[i];
		}
	}
	var that = this;
	// Methods:
	this.cssValueCheck = function(pTarget, pValue){
		switch(pValue){
			case "cW":
				return parseInt(pTarget.parentNode.offsetWidth) + "px";
				break;
			case "-cW":
				return (parseInt(pTarget.parentNode.offsetWidth) * -1) + "px";
				break;
			case "cH":
				return parseInt(pTarget.parentNode.offsetHeight) + "px";
				break;
			case "-cH":
				return (parseInt(pTarget.parentNode.offsetHeight) * -1) + "px";
				break;
			case "sW":
				return parseInt(pTarget.offsetWidth) + "px";
				break;
			case "-sW":
				return (parseInt(pTarget.offsetWidth) * -1) + "px";
				break;
			case "sH":
				return parseInt(pTarget.offsetHeight) + "px";
				break;
			case "-sH":
				return (parseInt(pTarget.offsetHeight) * -1) + "px";
				break;
			default:
				return pValue;
		}
	}; // this.cssValueCheck()
	this.animValueCheck = function(pTarget, pValue){
		switch(pValue){
			case "cW":
				return parseInt(pTarget.parentNode.offsetWidth);
				break;
			case "-cW":
				return parseInt(pTarget.parentNode.offsetWidth) * -1;
				break;
			case "cH":
				return parseInt(pTarget.parentNode.offsetHeight);
				break;
			case "-cH":
				return parseInt(pTarget.parentNode.offsetHeight) * -1;
				break;
			case "sW":
				return parseInt(pTarget.offsetWidth);
				break;
			case "-sW":
				return parseInt(pTarget.offsetWidth) * -1;
				break;
			case "sH":
				return parseInt(pTarget.offsetHeight);
				break;
			case "-sH":
				return parseInt(pTarget.offsetHeight) * -1;
				break;
			default:
				return pValue;
		}
	}; // this.animValueCheck()
	this.transIn = function(pTarget, pSpeed, pEasing, pZ, pTrans){
		YAHOO.util.Dom.setStyle(pTarget, "z-index", pZ.nextSlide);
		if(pTrans.cssStart != false){
			for(var i in pTrans.cssStart){
				YAHOO.util.Dom.setStyle(pTarget, i, that.cssValueCheck(pTarget, pTrans.cssStart[i]));
			}
		}
		var oAnimation = new Object;
		for(var i in pTrans.animation){ // "opacity", "left", "height", etc.
			oAnimation[i] = new Object;
			for(var j in pTrans.animation[i]){ // "to", "from", "by", etc.
				oAnimation[i][j] = that.animValueCheck(pTarget, pTrans.animation[i][j]);
			}
		}
		var oTransIn = new YAHOO.util.Anim(pTarget, oAnimation, pSpeed, pEasing);
		oTransIn.animate();
		oTransIn.onComplete.subscribe(function(){
			YAHOO.util.Dom.setStyle(pTarget, "z-index", pZ.currentSlide);
			if(pTrans.cssEnd != false){
				for(var i in pTrans.cssEnd){
					YAHOO.util.Dom.setStyle(pTarget, i, that.cssValueCheck(pTarget, pTrans.cssEnd[i]));
				}
			}
		});
	}; // this.transIn()
	this.transOut = function(pTarget, pSpeed, pEasing, pZ, pTrans){
		if(pTrans.cssStart != false){
			for(var i in pTrans.cssStart){
				YAHOO.util.Dom.setStyle(pTarget, i, that.cssValueCheck(pTarget, pTrans.cssStart[i]));
			}
		}
		var oAnimation = new Object;
		for(var i in pTrans.animation){ // "opacity", "left", "height", etc.
			oAnimation[i] = new Object;
			for(var j in pTrans.animation[i]){ // "to", "from", "by", etc.
				oAnimation[i][j] = that.animValueCheck(pTarget, pTrans.animation[i][j]);
			}
		}
		var oTransOut = new YAHOO.util.Anim(pTarget, oAnimation, pSpeed, pEasing);
		oTransOut.animate();
		oTransOut.onComplete.subscribe(function(){
			YAHOO.util.Dom.setStyle(pTarget, "z-index", pZ.slides);
			if(pTrans.cssEnd != false){
				for(var i in pTrans.cssEnd){
					YAHOO.util.Dom.setStyle(pTarget, i, that.cssValueCheck(pTarget, pTrans.cssEnd[i]));
				}
			}
		});
	}; // this.transOut()
	this.slideChange = function(pWhich){
		// Current Slide:
		var oEasingOutFunction;
		var oTransOutObject;
		var oPrevSlide = that.currentSlide;
		var oContinue = true;
		switch(typeof(that.easingOut)){
			case "string":
				oEasingOutFunction = YAHOO.util.Easing[that.easingOut];
				break;
			case "function":
				oEasingOutFunction = that.easingOut;
				break;
			default:
				alert("Error: 'easingOut' must be the name of a YUI easing function or a custom function.");
		}
		switch(typeof(that.transitionOut)){
			case "string":
				oTransOutObject = T[that.transitionOut];
				break;
			case "object":
				oTransOutObject = that.transitionOut;
				break;
			default:
				alert("Error: 'transitionOut' must be the name of a preset transition or a custom object.");
		}
		//that.transOut(that.slides[that.currentSlide], that.speed, oEasingOutFunction, that.zIndex, oTransOutObject);
		// Incoming Slide:
		var oEasingInFunction;
		var oTransInObject;
		switch(typeof(that.easingIn)){
			case "string":
				oEasingInFunction = YAHOO.util.Easing[that.easingIn];
				break;
			case "function":
				oEasingInFunction = that.easingIn;
				break;
			default:
				alert("Error: 'easingIn' must be the name of a YUI easing function or a custom function.");
		}
		switch(typeof(that.transitionIn)){
			case "string":
				oTransInObject = T[that.transitionIn];
				break;
			case "object":
				oTransInObject = that.transitionIn;
				break;
			default:
				alert("Error: 'transitionIn' must be the name of a preset transition or a custom object.");
		}
		// Increment or Decrement the currentSlide:
		switch(pWhich){
			case "next":
				if(that.currentSlide + 1 < that.slides.length){
					that.currentSlide++;
				} else {
					that.currentSlide = 0;
				}
				break;
			case "previous":
				if(that.currentSlide - 1 > -1){
					that.currentSlide--;
				} else {
					that.currentSlide = that.slides.length - 1;
				}
				break;
			default:
				// We are assuming this is a slide index.
				if(that.currentSlide != parseInt(pWhich)){
					that.currentSlide = parseInt(pWhich);
				} else {
					oContinue = false;
				}
		}
		if(oContinue == true){
			that.transOut(that.slides[oPrevSlide], that.speed, oEasingOutFunction, that.zIndex, oTransOutObject);
			that.transIn(that.slides[that.currentSlide], that.speed, oEasingInFunction, that.zIndex, oTransInObject);
		}
	}; // this.slideChange()
	this.startLoop = function(){
		if(that.autoplay == true && that.debug == false){
			that.loop = setInterval(function(){that.slideChange("next");}, that.timer);
		} else if(that.autoplay == true && that.debug == true){
			setTimeout(function(){that.slideChange("next");}, that.timer)
		}
	};
	// Set positioning and z-indexes:
	YAHOO.util.Dom.setStyle(this.container, "position", "relative");
	YAHOO.util.Dom.setStyle(this.slides, "position", "absolute");
	YAHOO.util.Dom.setStyle(this.container, "z-index", this.zIndex.container);
	YAHOO.util.Dom.setStyle(this.slides, "z-index", this.zIndex.slides);
	YAHOO.util.Dom.setStyle(this.slides[this.currentSlide], "z-index", this.zIndex.currentSlide);
	YAHOO.util.Dom.setStyle(this.slides[this.currentSlide + 1], "z-index", this.zIndex.nextSlide);
	// Start loop:
	this.startLoop();
	// Controls:
	if(this.previousButton != false){
		YAHOO.util.Event.addListener(this.previousButton, "click", function(){
			if(that.loop != false && that.stopOnUser == true){
				clearInterval(that.loop);
			}
			that.slideChange("previous");
		});
	}
	if(this.nextButton != false){
		YAHOO.util.Event.addListener(this.nextButton, "click", function(){
			if(that.loop != false && that.stopOnUser == true){
				clearInterval(that.loop);
			}
			that.slideChange("next");
		});
	}
	if(this.playButton != false){
		YAHOO.util.Event.addListener(this.playButton, "click", function(){
			that.startLoop();
		});
	}
	if(this.pauseButton != false){
		YAHOO.util.Event.addListener(this.pauseButton, "click", function(){
			if(that.loop != false){
				clearInterval(that.loop);
			}
		});
	}
}; // YAHOO.widget.Slideshow()

} // if()

})();
