var currentRotateBox = "rotateBox1";
var currentAnimation = "";
var boxCounter = 1;
var rotateTimer = "";
var rotateTime = 5000;
var fadeTime = 750;


function rotateOut(box)
{
	var boxOut = dojo.animateProperty(
	{
	  node:box, 
	  duration:1, 
	  properties:{
	    left:{start:"0", end:"-692", unit:"px"}
	    }
	});
	var boxFade = dojo.fadeOut({node: box,duration: fadeTime});
	
	return dojo.fx.chain([boxFade, boxOut]);
}

function rotateIn(box)
{
	dojo.fadeOut({node: box,duration: 1}).play();
	var boxIn = dojo.animateProperty(
	{
	  node:box, 
	  duration:1, 
	  properties:{
	    left:{start:"692", end:"0", unit:"px"}
	    }
	});
	var boxFade = dojo.fadeIn({node: box,duration: fadeTime});
	
	return dojo.fx.chain([boxIn, boxFade]);
	
}

function rotateBoxes(boxOut, boxIn)
{
	if(!(dojo.byId(boxOut) && dojo.byId(boxIn)) || (boxOut == boxIn))
	{
		return 0;
	}
	
	
	if(currentAnimation && currentAnimation.status() == "playing")
	{	
		//maybe need to do something here..
    }
   
	currentAnimation = dojo.fx.chain([rotateOut(boxOut), rotateIn(boxIn)]).play();
	currentRotateBox = boxIn;
		
	
}

function autoRotate()
{
	boxCounter++;
	if(boxCounter > 7)
	{
		boxCounter = 1;
	}
	rotateBoxes(currentRotateBox, "rotateBox" + boxCounter);
	rotateTimer = setTimeout("autoRotate()", rotateTime);
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


function clearTimer()
{
	clearTimeout(rotateTimer);
}

function startTimer()
{
	boxCounter = Right(currentRotateBox, 1);
	rotateTimer = setTimeout("autoRotate()", rotateTime);
}

function customFocus()
{
	if(dojo.byId("ct").style.backgroundImage == "url(css/images/tabs1.jpg)")
	{
		return 0;
	}
	
	dojo.byId("tab_box").style.height = dojo.byId("tab_box").offsetHeight + "px";
	dojo.byId("tab_box").innerHTML = "";
	
	dojo.animateProperty(
	{
	  node:"tab_box", 
	  duration:500, 
	  properties:{
	    height:{start:dojo.byId("tab_box").offsetHeight, end:dojo.byId("customTailor").offsetHeight, unit:"px"}
	    }
	}).play();

	setTimeout('dojo.byId("tab_box").innerHTML = dojo.byId("customTailor").innerHTML;', 500);
	dojo.byId("ct").style.backgroundImage = "url(css/images/tabs1.jpg)";
	dojo.byId("qs").style.backgroundImage = "url(css/images/tabs2.jpg)";
}
	
function quickShipFocus()
{
	if(dojo.byId("qs").style.backgroundImage == "url(css/images/tabs1.jpg)")
	{
		return 0;
	}
	dojo.byId("tab_box").style.height = dojo.byId("tab_box").offsetHeight + "px";
	dojo.byId("tab_box").innerHTML = "";
	
	dojo.animateProperty(
	{
	  node:"tab_box", 
	  duration:500, 
	  properties:{
	    height:{start:dojo.byId("tab_box").offsetHeight, end:dojo.byId("quickShip").offsetHeight, unit:"px"}
	    }
	}).play();
	setTimeout('dojo.byId("tab_box").innerHTML = dojo.byId("quickShip").innerHTML;', 500);
	dojo.byId("qs").style.backgroundImage = "url(css/images/tabs1.jpg)";
	dojo.byId("ct").style.backgroundImage = "url(css/images/tabs2.jpg)";
}

function removeItem(index)
{
	dojo.byId("removeIndex").value = index;
	dojo.byId("removeForm").submit();
}

function checkoutFocus() 
{
   //Do a query for the input nodes
   dojo.query(".checkoutTable input, .checkoutTable select, .checkoutTable textarea",
     dojo.byId("content"))
   .onfocus(function(evt){
     //change background when an input gets focus
       dojo.anim(getContainer(evt.target),{backgroundColor: "#CEC093"});
     })
   .onblur(function(evt){
     //Make the background white when an input loses focus
       dojo.anim(getContainer(evt.target), {backgroundColor: "#ffffff"});
     })
 }
 
function getContainer(node) 
{
   var body = dojo.body();
   while(node && node != body
         && !dojo.hasClass(node, "checkoutTable")) {
     node = node.parentNode;
   }
   if(dojo.hasClass(node, "checkoutTable")){
     return node;
   }
   return null;
 }
 
function sameAsBilling()
 {
 	
 	if(dojo.byId("sameAs").checked)
 	{
 		//do a query to find all the billing nodes
 		billing = dojo.query("select, input[type=text]", dojo.byId("billingAddress"));
 		shipping = dojo.query("select, input[type=text]", dojo.byId("shippingAddress"));
 		for(formIndex = 0; formIndex < billing.length; formIndex++)
 		{
 			try
 			{
 				shipping[formIndex].value = billing[formIndex].value;
 			}
 			catch(e)
 			{}
 		}
 	}
 }
 
 function dealerLocate()
 {
 	window.location = "?event=dealer.locateform";
 }
 
// smooth scrolling to anchor tags
function scrollToTag(name, container)
{
	var scrollContainer;
	
	if(container != null && container != '')
	{
		scrollContainer = dojo.byId(container);
	}
	else
	{
		scrollContainer = window;
	}
	
	// summary; searches for a <a name=""></a> attrib, and scrolls to it
	dojo.query('a[name="'+name+'"]').forEach(function(node){
		// first one wins
		
		var _tmp = dojo.coords(node);	
		var tg = { x: _tmp.l, y: _tmp.t - 15 };
		var anim = dojox.fx.smoothScroll({
			node:node,
			win:scrollContainer,
			duration:500,
			target: tg,
			easing:dojox.fx.easing.backOut
		}).play();
		return;
	});
	
}

function showFAQ()
{
	//if already out, hide
	if(dojo.byId("faqs").style.display == "block")
	{
		hideFAQ();
	}
	else
	{
		dojo.byId("faqs").style.display = "block";
		dojo.byId("faqs").style.height = "5px";
			
		var wgrow = dojo.animateProperty(
		{
		  node:"faqs", 
		  duration:500,
		  easing:dojox.fx.easing.expoOut, 
		  properties:{
		    width:{start:0, end:500, unit:"px"}
		    }
		});
		var hgrow =dojo.animateProperty(
		{
		  node:"faqs", 
		  duration:700,
		  easing:dojox.fx.easing.bounceOut, 
		  properties:{
		    height:{start:0, end:300, unit:"px"}
		    }
		});
		dojo.fx.chain([wgrow, hgrow]).play();
	}
}

function hideFAQ()
{
	
	var hshrink =dojo.animateProperty(
	{
	  node:"faqs", 
	  duration:700,
	  easing:dojox.fx.easing.backIn, 
	  properties:{
	    height:{start:300, end:5, unit:"px"}
	    }
	});
	var wshrink = dojo.animateProperty(
	{
	  node:"faqs", 
	  duration:500,
	  easing:dojox.fx.easing.expoIn,
	  onEnd:function()
	  		{
	  			dojo.byId("faqs").style.display = "none";
	   		},
	  properties:{
	    width:{start:500, end:0, unit:"px"}
	    }
	});
	dojo.fx.chain([hshrink, wshrink]).play();
	//setTimeout('dojo.byId("faqs").style.display = "none";', 1000);
}

function verifyPassword(password, verify, message, pass, fail, button)
{
	if(dojo.byId(password).value == dojo.byId(verify).value)
	{
		dojo.byId(message).innerHTML = dojo.byId(pass).innerHTML;
		dojo.byId(button).disabled = false;
	}
	else
	{
		dojo.byId(message).innerHTML = dojo.byId(fail).innerHTML;
		dojo.byId(button).disabled = true;
	}
}

function loadSSizes(catID, chest)
{
	dijit.byId("size_box").attr("loadingMessage", '<div style="text-align:center;"><img src="css/images/ajax-loader.gif" style="margin:50px auto"></div>');
	dojo.animateProperty({ 
		node: "size_box",
		properties: {height: {end:100}},
		duration:300,
		//easing: dojox.fx.easing.backOut,
		onEnd: function(){
			dijit.byId("size_box").attr("href", '?event=murphy.showSSeries&catID=' + catID + '&chest=' + chest);
			dijit.byId("size_box").attr("onLoad", function()
			{
				scrollToTag('shop');		
			});
			dojo.byId("size_box").style.height = "auto";
					
		}
	}).play();
	
	
}