$(window).load(function() {
	//var thing = $("li.leaf:last");
	//var position = thing.position();
	placeTopNav();
});
function placeTopNav(){
	// this function sets the positions of the top nav dropdowns
	$(".topNavInner li.leaf").each(function(){
		var th = $(this);
		var position = th.position();
		var t = Math.round(position.top);
		var l = Math.round(position.left);
		var w = th.width();
		var r = l+w;
		//console.log(w + " - " + l);
		// now get the dropdown
		var dropper = $(this).children(".cutemenu-dropdown-wrapper");
		var dropperW = dropper.width();
		
		if(dropperW != null){
			//console.log(dropperW);
			var dropperL = r - dropperW + 20;
			$(dropper).css({left: dropperL+"px"});
		}
	});	
}
//   #################################################
//  #                                                 #
//  #    expanding nav                                #
//  #                                                 #
//   #################################################

isExpanded = new Array();

//  ######################################
//  #  product page minis
//  ######################################

otherNavReady = false;

function expandAlt(exParent,exBlock,changer)
{
	document.getElementById(changer).blur();
	if (otherNavReady)
	{
		otherNavReady = true;	
		blockHeightAlt = getElementHeight(exBlock);
		if (isExpanded[exParent])
		{
			document.getElementById(changer).innerHTML = "[ + ]";
			smoothOpacity(exBlock,100,0,10,30,0,"#000000");
			growObject(exParent,false,blockHeightAlt,false,0,1,1,30,3,80,"makeReady");
		} else {
			document.getElementById(changer).innerHTML = "[ &ndash; ]";
			setOpacity(exBlock,0);
			smoothOpacity(exBlock,0,100,10,50,200,"#000000");
			growObject(exParent,false,0,false,blockHeightAlt,1,1,30,3,0,"makeReady");
		}
		isExpanded[exParent] = !isExpanded[exParent];
	}
}

function setThisHidden(which)
{
	otherNavReady = true;
	document.getElementById("blockInner"+which).className = "innerListGeneric";
}

function setThisVisible(which)
{
	//alert("huh");	
	otherNavReady = true;
	blockHeightAlt = getElementHeight("blockInner"+which);
	//blockHeightAlt = $("#blockInner"+which).height();
	document.getElementById("blockOuter"+which).className = "blockHidden";
	document.getElementById("blockInner"+which).className = "innerListGeneric";
	document.getElementById("blockOuter"+which).style.height = blockHeightAlt+"px";
	isExpanded["blockOuter"+which] = true;

}






//  ######################################
//  #  side nav
//  ######################################

oldParent = 0;
oldBlock = 0;
oldChanger = 0;
navReady = false;
function expand(exParent,exBlock,changer)
{
	document.getElementById(changer).blur();
	if (navReady)
	{
		navReady = false;
		if (oldParent != 0)
		{
			document.getElementById(oldChanger).innerHTML = "[ + ]";
			smoothOpacity(oldBlock,100,0,10,30,0,"#000000");
			growObject(oldParent,false,blockHeight,false,0,1,1,30,3,100,"makeReady");
			isExpanded[oldParent] = !isExpanded[oldParent];
		}
		blockHeight = getElementHeight(exBlock);
		if (exParent == oldParent)
		{
			oldParent = 0;
		} else {
			document.getElementById(changer).innerHTML = "[ &ndash; ]";
			setOpacity(exBlock,0);
			smoothOpacity(exBlock,0,100,10,50,200,"#000000");
			growObject(exParent,false,0,false,blockHeight,1,1,30,3,0,"makeReady");
			oldParent = exParent;
			oldBlock = exBlock;
			oldChanger = changer;
		}
		isExpanded[exParent] = !isExpanded[exParent];
	}
}

function makeReady()
{
	navReady = true;
}

createLoadListener(noOpenStarter);

function noOpenStarter()
{
	navReady = true;
}






//  ######################################
//  #  gallery open
//  ######################################

galleryTitle = new Array();
galleryCaption = new Array();
gallerySource = new Array();
galleryX = new Array();
galleryY = new Array();
galleryPos = 0;
galleryItems = 0;
capHeight = 1;
function newGalleryItem(which,whichTitle,whichCaption,whichFile,x,y)
{
	galleryTitle[which] = whichTitle;
	galleryCaption[which] = whichCaption;
	gallerySource[which] = whichFile;
	galleryX[which] = x;
	galleryY[which] = y;
	imagePreload("/catalogFiles/",whichFile);
}


function showGallery(which)
{
	//var bigone = which + 1;
	$('.heroShot').attr("src","/cms/sites/all/themes/gorillastandstheme/catalogFiles/"+which);
}

function showGalleryInfo()
{
	document.getElementById("galleryTitle").innerHTML = galleryTitle[galleryPos];
	document.getElementById("galleryCaption").innerHTML = galleryCaption[galleryPos];
	capHeight = getElementHeight("galleryCaption");
	smoothOpacity("galleryCaptionInner",0,100,10,25,200,"#000000","showGalleryEnd");
	growObject("galleryCaptionInner",false,1,false,capHeight,1,1,15,3);
}

function showGalleryEnd()
{
	galleryClick = true;
	if (galleryItems > 1)
	{
		document.getElementById("galleryNext").style.height = (galleryY[galleryPos]+12)+"px";
		document.getElementById("galleryPrevious").style.height = (galleryY[galleryPos]+12)+"px";
		document.getElementById("galleryNext").style.display = "block";
		document.getElementById("galleryPrevious").style.display = "block";	
		setOpacity("galleryNext",50);
		setOpacity("galleryPrevious",50);
	}
}

//  ######################################
//  #  gallery navigate
//  ######################################

function galleryChange(change)
{
	if (galleryClick)
	{
		document.getElementById("galleryTitle").innerHTML = "";
		//setOpacity("galleryNext",50);
		//setOpacity("galleryPrevious",50);
		document.getElementById("galleryNext").style.display = "none";
		document.getElementById("galleryPrevious").style.display = "none";
		galleryClick = false;
		newPos = galleryPos + change;
		if (newPos < 1) newPos = galleryItems;
		if (newPos > galleryItems) newPos = 1;
		smoothOpacity("galleryCaptionInner",100,0,8,25,0,"#000000");
		smoothOpacity("galleryPhoto",100,0,8,1,0,false,"galleryChangeTwo");
	}
}

function galleryChangeTwo()
{
	oldX = galleryX[galleryPos]+12;
	newX = galleryX[newPos]+12;
	oldY = galleryY[galleryPos];
	newY = galleryY[newPos];
	growObject("galleryCaptionInner",false,capHeight,false,1,1,1,15,3);
	growObject("galleryBlockInner",oldX,false,newX,false,1,1,20,3);
	growObject("galleryPhoto",false,oldY,false,newY,1,1,20,3,0,"galleryChangeThree");
}

function galleryChangeThree()
{
	setOpacity("galleryPhoto",0);
	document.getElementById("galleryPhoto").style.backgroundImage = "url('/catalogFiles/"+gallerySource[newPos]+"')";
	document.getElementById("galleryPhoto").style.height = (galleryY[newPos])+"px";
	document.getElementById("galleryBlock").style.display = "block";
	galleryPos = newPos;
	smoothOpacity("galleryPhoto",0,100,8,1,0,false,"showGalleryInfo");
}

function galleryPreviousOver()
{
	setOpacity("galleryPrevious",100);
	//document.getElementById("galleryPrevious").style.backgroundImage = "url(/templates/images/galleryPrevious.gif)";
}

function galleryPreviousOut()
{
	setOpacity("galleryPrevious",50);
	//document.getElementById("galleryPrevious").style.backgroundImage = "url(/templates/images/nothing.gif)";
}

function galleryNextOver()
{
	setOpacity("galleryNext",100);
	//document.getElementById("galleryNext").style.backgroundImage = "url(/templates/images/galleryNext.gif)";
}

function galleryNextOut()
{
	setOpacity("galleryNext",50);
	//document.getElementById("galleryNext").style.backgroundImage = "url(/templates/images/nothing.gif)";
}











//  ######################################
//  #  gallery close
//  ######################################

function hideGallery()
{
	document.getElementById("galleryBlock").style.display = "none";
	smoothOpacity("alertShaderInner",80,0,6,1,0,false,"hideGalleryFinish");
}

function hideGalleryFinish()
{
	destroyAlertShader();
	document.getElementById("galleryTitle").innerHTML = "";
	document.getElementById("galleryCaption").innerHTML = "";
	document.getElementById("galleryCaptionInner").style.height = "1px";
}









//  ######################################
//  #  pop down menus
//  ######################################

popDownCurrent = 0;
popDownAction = 0;
popDown = new Array();


function popDownAlive(which)
{
	popDown[which] = false;
}

function xpopDownOn(which)
{
	navRight = getElementLeft("navTop"+which);
	navWidth = getElementWidth("navTop"+which);
	otherWidth = getElementWidth("topNavSubInner"+which);
	alert("navRight: "+navRight+"\nnavWidth: "+navWidth+"\notherWidth: "+otherWidth);
}

function popDownOn(which)
{
	popDownAction++;
	realWhich = (typeof(popDown[which]) != "undefined") ? which : 0;
	if ((popDownCurrent != 0) && (popDownCurrent != realWhich))
	{
		document.getElementById("topNavSub"+popDownCurrent).style.display = "none";
		popDownCurrent = 0;
	}
	if ((realWhich != 0) && (popDownCurrent != realWhich))
	{
		document.getElementById("topNavSub"+realWhich).style.visibility = "hidden";
		document.getElementById("topNavSub"+realWhich).style.display = "block";
		navPosX = (getElementLeft("navTop"+realWhich) + getElementWidth("navTop"+realWhich)) - (getElementWidth("topNavSubInner"+realWhich) + 26);
		navPosY = getElementTop("navTop"+realWhich) + 19;
		document.getElementById("topNavSub"+realWhich).style.top = navPosY+"px";
		document.getElementById("topNavSub"+realWhich).style.left = navPosX+"px";
		document.getElementById("topNavSub"+realWhich).style.visibility = "visible";
		popDownCurrent = realWhich;	
	}
}

function popDownOff(which)
{

	popDownAction++;
	if (typeof(popDown[which]) != "undefined")
	{
		setTimeout("popDownKill('"+which+"',"+popDownAction+")",1000);
	}
}

function popDownKill(which,action)
{
	if (action == popDownAction)
	{
		document.getElementById("topNavSub"+which).style.display = "none";
		popDownCurrent = 0;
	}
}


















//  ######################################
//  #  featured products rotator
//  ######################################

rotatorImage = new Array();
rotatorLink = new Array();
rotatorActive = 1;
rotatorNext = 0;
rotatorAnimate = true;
rotatorDelay = 8000;

function rotatorNew(which,imageSrc,linkURL)
{
	rotatorImage[which] = imageSrc;
	rotatorLink[which] = linkURL;
	imagePreload("",imageSrc);
}

function rotatorClick()
{
	if (rotatorActive != 0)
	{
		rotatorAnimate = false;
		document.location = rotatorLink[rotatorActive];
	}
}

function rotatorOn(which)
{
	if (rotatorActive != 0)
	{
		rotatorAnimate = false;
		document.getElementById("rot"+which).blur();
		document.getElementById("featuredImage").style.backgroundImage = "url("+rotatorImage[which]+")";
		document.getElementById("rot"+which).className = "active";
		document.getElementById("rot"+rotatorActive).className = "inactive";
		rotatorActive = which;
	}
}

function rotatorStart()
{
	setTimeout("rotatorOut()",rotatorDelay);
}

function rotatorOut()
{
	if (rotatorAnimate)
	{
		rotatorNext = rotatorActive + 1;
		if (rotatorNext > rotatorTotal) rotatorNext = 1;
		smoothOpacity("featuredImage",100,0,10,50,0,"#000000","rotatorIn");
	}
}

function rotatorIn()
{
	if (rotatorAnimate)
	{
		document.getElementById("featuredImage").style.backgroundImage = "url("+rotatorImage[rotatorNext]+")";
		document.getElementById("rot"+rotatorActive).className = "inactive";
		rotatorActive = rotatorNext;
		document.getElementById("rot"+rotatorActive).className = "active";
		smoothOpacity("featuredImage",0,100,10,50,0,"#000000");
		setTimeout("rotatorOut()",rotatorDelay);
	}
}












//  ######################################
//  #  shopping guide
//  ######################################
	
checkSelect = new Array();
checkTotal = new Array();
checkAnswers = new Array();
checkAnswersTotal = new Array();
checkDocs = new Array();
checkDocsAlt = new Array();
checkDocsTotal = 0;

function checkDocsMaker(answerID,catalogID)
{

	if (typeof(checkAnswers[answerID]) == "undefined") 
	{
		checkAnswers[answerID] = new Array();
		checkAnswersTotal[answerID] = 0;
	}
	checkAnswers[answerID].push(catalogID);
	checkAnswersTotal[answerID]++;
	if (typeof(checkDocsAlt[catalogID]) == "undefined")
	{
		checkDocsAlt[catalogID] = true;
		checkDocs.push(catalogID);
		checkDocsTotal++;
	}
}

function shoppingGuide(which,question)
{
	answer = document.getElementById(question).value;
	if (answer == 0) 
	{
		checkSelect[which] = 0;
		document.getElementById(question).selectedIndex = 0;
	} else {
		checkSelect[which] = answer;
	}
	showOptions();	
}

function showOptions()
{
	validDocs = new Array();
	for (loop = 0; loop < checkDocsTotal; loop++)
	{
		validDocs[checkDocs[loop]] = true;
	}
	for (loop = 1; loop <= checkQuestions; loop++)
	{
		if (checkSelect[loop] != 0)
		{
			answer = checkSelect[loop];
			for (indx in validDocs)
			{
				dropThis = true
				for (catID in checkAnswers[answer])
				{
					if (indx == checkAnswers[answer][catID]) dropThis = false;
				}
				if (dropThis == true) validDocs[indx] = false;
			}
		}
	}
	realLoop = 0;
	newText = "";
	for (loop = 0; loop < checkDocsTotal; loop++)
	{
		if (validDocs[checkDocs[loop]] == true)
		{
			setClass("si"+checkDocs[loop],'shoppingItemOn');
		} else {
			setClass("si"+checkDocs[loop],'shoppingItemOff');
		}
	}
}

function goLink(which)
{
	document.location = which;
}

























//  ######################################
//  #  product image changer
//  ######################################

currentProduct = '';

function productPhotoPreloader(cpID,cpFile,cpPreload)
{
}

function swapProductImage()
{
	cpID = document.getElementById("productSwapper").value;
	if (currentProduct != cpID)
	{
		setClass('productShot'+currentProduct,'heroShotOff');
		currentProduct = cpID;
		setClass('productShot'+currentProduct,'heroShot');
	}
}
