/************************************************************
 * Generates the sliding navigation similar to the iPhone	*
 ************************************************************/


// Create the constructor
var stcSlider = function() {
	
}

// Attach the properties and methods
stcSlider.prototype = {
	
	// Amount to nudge upon arrow click
	nudgePx: 200, // In pixels, of course
	nudgeDur: .3, // Duration in seconds
	
	// Initialize by attaching events to the arrows and handles
	init: function () {
		var arrows	= YAHOO.util.Dom.getElementsByClassName("sliderArrow");
		YAHOO.util.Event.addListener(arrows, "click", this.moveIt, this, true);
		var handles = YAHOO.util.Dom.getElementsByClassName("draggableSlider");
		YAHOO.util.Event.addListener(handles, "mousedown", this.dragIt, this, true);
		YAHOO.util.Event.addListener(handles, "mouseup", this.killDrag, this, true);
		var linkspans = YAHOO.util.Dom.getElementsByClassName("sliderLink");
		YAHOO.util.Event.addListener(linkspans, "click", this.goToPage, this, true);
		this.findSelected();
	},
	
	// Figure out which direction and how far to go
	moveIt: function(e) {
		var arrow	= e.target || e.srcElement;
		while(arrow.tagName.toUpperCase() != "DIV") {
			arrow = arrow.parentNode;
		}
		var arrowR = YAHOO.util.Dom.getRegion(arrow);
		var arrowW = arrowR.right-arrowR.left;
		var slide = arrow.parentNode;
		slide = slide.getElementsByTagName("ul")[0];
		while(slide.tagName.toUpperCase() != "UL") {
			slide = slide.parentNode;
		}
		var els = slide.getElementsByTagName("li");
		var lastEl = els[els.length-1];
		var lastElR = YAHOO.util.Dom.getRegion(lastEl);
		var slideR	= YAHOO.util.Dom.getRegion(slide);
		var cont	= slide.parentNode;
		var contR	= YAHOO.util.Dom.getRegion(cont);
		if(YAHOO.util.Dom.hasClass(arrow, "right")) {
				var from = slideR.left-contR.left;
				var to = from-this.nudgePx;
				var rtEdge = lastElR.right-contR.right-this.nudgePx;
				if(rtEdge<0) to = to-rtEdge-arrowW;
				if(lastElR.right-contR.right<0) to = from;
		}
		else if(YAHOO.util.Dom.hasClass(arrow, "left")) {
				var from = slideR.left-contR.left;
				var to = from+this.nudgePx;
				if(to>0) to = 0;
		}
		else {
				console.log("I dunno which way ya want me to go.");
				return false;
		}
		
		this.slideNudge(from, to, slide);
	},
	
	// Make your move
	slideNudge: function(from, to, slide) {
		var newAttributes = {
		   left: { from:from, to: to }
		};
		
		var newAnim = new YAHOO.util.Anim(slide, newAttributes);
		newAnim.duration = this.nudgeDur;
		newAnim.method = YAHOO.util.Easing.easeBoth;
		newAnim.animate();	

	},
	
	// This lets you drag the slider bar back and forth
	dragIt: function(e) {
		var handle	= e.target || e.srcElement;
		while(handle.tagName.toUpperCase() != "UL" && handle.tagName.toUpperCase() != "A") {
			handle = handle.parentNode;
		}
		if(handle.tagName.toUpperCase() == "A") {
			this.dragOn = false;
			return false;
		}
		this.dragTarget = handle;
		var thisObj = this;
		window.setTimeout(function(){
			thisObj.dragOn = true;
		}, 377);
		this.dragStart = YAHOO.util.Event.getXY(e)[0];
		YAHOO.util.Event.stopPropagation(e);
		YAHOO.util.Event.addListener(document.body, "mousedown", this.killDrag, this, true);
		YAHOO.util.Event.addListener(document.body, "mouseup", this.killDrag, this, true);
		YAHOO.util.Event.addListener(document.body, "mousemove", this.slideMove, this, true);
	},
	
	// Does some boundary checking, then moves the UL in relation to the mouse
	slideMove: function(e) {
		if(!this.dragOn || !this.dragTarget) return false;
		var arrowTemp = this.dragTarget.parentNode.getElementsByTagName("div");
		for(x=0;x<arrowTemp.length;x++) {
			if(YAHOO.util.Dom.hasClass(arrowTemp[x], "sliderArrow") && YAHOO.util.Dom.hasClass(arrowTemp[x], "right")) {
				var arrow = arrowTemp[x];
			}
		}
		var arrowR = YAHOO.util.Dom.getRegion(arrow);
		var arrowW = arrowR.right-arrowR.left;
		var mouseX = YAHOO.util.Event.getXY(e)[0];
		var mouseDiff = mouseX-this.dragStart;
		var handleR = YAHOO.util.Dom.getRegion(this.dragTarget);
		var contR = YAHOO.util.Dom.getRegion(this.dragTarget.parentNode);
		var els = this.dragTarget.getElementsByTagName("li");
		var lastEl = els[els.length-1];
		var lastElR = YAHOO.util.Dom.getRegion(lastEl);
		var newHandleX = handleR.left+mouseDiff-contR.left;
		if(newHandleX>0) newHandleX = 0;
		var rEdge = lastElR.right-contR.right+mouseDiff+arrowW;
		if(rEdge<=0) newHandleX = newHandleX-rEdge;
		YAHOO.util.Dom.setStyle(this.dragTarget, "left", newHandleX+"px");
		this.dragStart = mouseX;
		if(typeof(sliderDebug) !== "undefined") {
			console.log(newHandleX);
			console.log(rEdge);
			console.log(arrowW);
		}
	},
	
	// Stop dragging the bar around.
	killDrag: function(e) {
		if(!this.dragOn) return false;
		var thisObj = this;
		window.setTimeout(function(){
			thisObj.dragOn = false;
			thisObj.dragTarget = false;
		}, 20);
		YAHOO.util.Event.removeListener(document.body, "mousedown", this.killDrag);
		YAHOO.util.Event.removeListener(document.body, "mouseup", this.killDrag);
		YAHOO.util.Event.removeListener(document.body, "mousemove", this.slideMove);
	},
	
	// Make sure the selected tab is visible on load
	findSelected: function() {
		var selTabs = YAHOO.util.Dom.getElementsByClassName("sliderSelected");
		for(var x=0;x<selTabs.length;x++) {
			var slide = selTabs[x].parentNode;
			while(slide.tagName.toUpperCase() != "UL") {
				slide = slide.parentNode;
			}
			var arrowTemp = slide.parentNode.getElementsByTagName("div");
			for(i=0;i<arrowTemp.length;i++) {
				if(YAHOO.util.Dom.hasClass(arrowTemp[i], "sliderArrow") && YAHOO.util.Dom.hasClass(arrowTemp[i], "right")) {
					var arrow = arrowTemp[i];
				}
			}
			var arrowR = YAHOO.util.Dom.getRegion(arrow);
			var arrowW = arrowR.right-arrowR.left;
			var cont = slide.parentNode;
			var contR = YAHOO.util.Dom.getRegion(cont);
			var selEl = selTabs[x];
			var selElR = YAHOO.util.Dom.getRegion(selEl);
			var elDiff = selElR.right-contR.right;
			if(elDiff>0) {
				var to = (elDiff*-1)-arrowW;
				this.slideNudge(0,to,slide);
			}
		}
	},
	
	// Do the page changing
	goToPage: function(e) {
		if(this.dragOn) return;
		var linkSpan = e.target || e.srcElement;
		while(!YAHOO.util.Dom.hasClass(linkSpan, "sliderLink")){
			linkSpan = linkSpan.parentNode;
		}
		var linkArray = linkSpan.id.split(".");
		var linkURL = linkArray[0].split("-");
		var linkQS  = linkArray[1]?linkArray[1].split("-"):"";
		var linkPath="";
		var linkQuery="";
		for(var x=1;x<linkURL.length; x++) {
			linkPath += "/"+linkURL[x];
		}
		if(linkQS != ""){
			linkPath += "?";
			for(var q=0;q<linkQS.length; q++) {
				var linkPair = linkQS[q].split(":");
				linkPath += linkPair[0]+"="+linkPair[1]+"&";
			}
		}
		var thisObj = this;
		window.setTimeout(function(){
			thisObj.dragOn = false;
			thisObj.dragTarget = false;
		}, 380);
		if(typeof(sliderDebug) !== "undefined") {
			console.log(linkPath);
			return;
		}
		window.location=linkPath;
	}
}

YAHOO.util.Event.onDOMReady(function() {
	var slider = new stcSlider();
	slider.init();
});