/* Featured Specials Slider v0.1 JS by Josh Lizarraga */
/* Copyright 2009 Autofusion.com */

(function(){

/* AF Library */

if(typeof(YAHOO.AUTOFUSION) == "undefined"){
	YAHOO.namespace("AUTOFUSION");
}
if(typeof(YAHOO.AUTOFUSION.items) == "undefined"){
	YAHOO.AUTOFUSION.items = new Object();
}
var AF = YAHOO.AUTOFUSION;

/* Featured Specials Slider */

// Featured Specials Slider Object
AF.FeaturedSpecialsSlider = new Object;
var FSS = AF.FeaturedSpecialsSlider;

// Private Methods
//var PM = new Object;

// Vertical Method
FSS.Vertical = function(afID, afConfig){
	// FSS.Vertical Properties:
	this.setInterval = window.setInterval;
	this.id = (afID) ? afID : false;
	this.config = (afConfig) ? afConfig : false;
	this.container = document.getElementById(this.id);
	this.slider = YAHOO.util.Dom.getElementsByClassName("fss-slider", "div", this.container)[0];
	this.upArrow = false;
	this.downArrow = false;
	this.upInterval = false;
	this.downInterval = false;
	this.autoInterval = false;
	this.autoplay = true;
	this.results = false;
	this.total = 0;
	this.animationSpeed = .25;
	this.expandEnd = 250;
	this.autoScrollSpeed = 50;
	this.userScrollSpeed = 5;
	this.scrollIncrement = 1;
	if(this.config != false){
		for(var i in this.config){
			this[i] = this.config[i];
		}
	}
	var that = this;
	// FSS.Vertical Methods:
	this.autoDownLoop = function(){
		var oTop = parseInt(YAHOO.util.Dom.getStyle(that.slider, "top")); // Slider's "top" position.
		var oT = oTop; // We need a "top" value we can mess with.
		var oSH = parseInt(that.slider.offsetHeight); // Slider height.
		var oCH = parseInt(that.container.offsetHeight); // Container height.
		var oHD = oSH - oCH; // Difference in heights.
		if(oT < 0){
			// Convert T to positive for comparison:
			oT = -(oT);
		}
		if(oT < oHD){
			YAHOO.util.Dom.setStyle(that.slider, "top", (oTop - that.scrollIncrement) + "px");
		} else {
			clearInterval(that.autoInterval);
			that.autoInterval = setInterval(that.autoUpLoop, that.autoScrollSpeed);
		}
	}; // this.autoDownLoop()
	this.autoUpLoop = function(){
		var oTop = parseInt(YAHOO.util.Dom.getStyle(that.slider, "top")); // Slider's "top" position.
		if(oTop < 0){
			YAHOO.util.Dom.setStyle(that.slider, "top", (oTop + that.scrollIncrement) + "px");
		} else {
			clearInterval(that.autoInterval);
			that.autoInterval = setInterval(that.autoDownLoop, that.autoScrollSpeed);
		}
	}; // this.autoUpLoop()
	this.downLoop = function(){
		var oTop = parseInt(YAHOO.util.Dom.getStyle(that.slider, "top")); // Slider's "top" position.
		var oT = oTop; // We need a "top" value we can mess with.
		var oSH = parseInt(that.slider.offsetHeight); // Slider height.
		var oCH = parseInt(that.container.offsetHeight); // Container height.
		var oHD = oSH - oCH; // Difference in heights.
		if(oT < 0){
			// Convert T to positive for comparison:
			oT = -(oT);
		}
		if(oT < oHD){
			YAHOO.util.Dom.setStyle(that.slider, "top", (oTop - that.scrollIncrement) + "px");
		}
	}; // this.downLoop()
	this.upLoop = function(){
		var oTop = parseInt(YAHOO.util.Dom.getStyle(that.slider, "top")); // Slider's "top" position.
		if(oTop < 0){
			YAHOO.util.Dom.setStyle(that.slider, "top", (oTop + that.scrollIncrement) + "px");
		}
	}; // this.upLoop()
	this.downOver = function(e){
		clearInterval(that.autoInterval);
		that.downInterval = setInterval(that.downLoop, that.userScrollSpeed);
	}; // this.downOver()
	this.upOver = function(e){
		clearInterval(that.autoInterval);
		that.upInterval = setInterval(that.upLoop, that.userScrollSpeed);
	}; // this.upOver()
	this.downOut = function(e){
		clearInterval(that.downInterval);
	}; // this.downOut()
	this.upOut = function(e){
		clearInterval(that.upInterval);
	}; // this.upOut()
	this.linkOver = function(e){
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.nodeName.toUpperCase() != "A"){
			oTarget = YAHOO.util.Dom.getAncestorByTagName(oTarget, "a");
		}
		// Hide/Show:
		var oResult = document.getElementById("fss-result-" + oTarget.rel);
		YAHOO.util.Dom.setStyle(that.results, "display", "none");
		YAHOO.util.Dom.setStyle(oResult, "display", "block");
		// Animation:
		var oSPAN = oTarget.getElementsByTagName("span")[0];
		var oExpand = new YAHOO.util.Anim(oTarget, {
				width: { to: that.expandEnd }
			}, that.animationSpeed, YAHOO.util.Easing.easeOut);
		oExpand.animate();
		var oFadeIn = new YAHOO.util.Anim(oSPAN, {
				opacity: { to: 1 }
			}, that.animationSpeed, YAHOO.util.Easing.easeOut);
		oFadeIn.animate();
	}; // this.linkOver()
	this.linkOut = function(e){
		var oTarget = YAHOO.util.Event.getTarget(e);
		if(oTarget.nodeName.toUpperCase() != "A"){
			oTarget = YAHOO.util.Dom.getAncestorByTagName(oTarget, "a");
		}
		var oSPAN = oTarget.getElementsByTagName("span")[0];
		var oExpand = new YAHOO.util.Anim(oTarget, {
				width: { to: 0 }
			}, that.animationSpeed, YAHOO.util.Easing.easeOut);
		oExpand.animate();
		var oFadeIn = new YAHOO.util.Anim(oSPAN, {
				opacity: { to: 0 }
			}, that.animationSpeed, YAHOO.util.Easing.easeOut);
		oFadeIn.animate();
	}; // this.linkOut()
	this.init = function(){
		that.results = YAHOO.util.Dom.getElementsByClassName("fss-result", "div", that.container);
		var oLinks = that.slider.getElementsByTagName("a");
		that.total = oLinks.length;
		that.upArrow = YAHOO.util.Dom.getElementsByClassName("fss-up", "img", that.container)[0];
		that.downArrow = YAHOO.util.Dom.getElementsByClassName("fss-down", "img", that.container)[0];
		// Set SPANs' opacity to 0:
		for(var i=0; i<that.total; i++){
			var oSPAN = oLinks[i].getElementsByTagName("span");
			YAHOO.util.Dom.setStyle(oSPAN, "opacity", "0");
		}
		// Event listeners:
		YAHOO.util.Event.addListener(that.upArrow, "mouseover", that.upOver);
		YAHOO.util.Event.addListener(that.upArrow, "mouseout", that.upOut);
		YAHOO.util.Event.addListener(that.downArrow, "mouseover", that.downOver);
		YAHOO.util.Event.addListener(that.downArrow, "mouseout", that.downOut);
		YAHOO.util.Event.addListener(oLinks, "mouseover", that.linkOver);
		YAHOO.util.Event.addListener(oLinks, "mouseout", that.linkOut);
		// Autoplay:
		if(that.autoplay == true){
			that.autoInterval = setInterval(that.autoDownLoop, that.autoScrollSpeed);
		}
	}; // this.init()
}; // FSS.Vertical()

})();