window.onload = init;

function init() {
 	if(document.getElementById("apparelIntro")) {
		document.getElementById("apparelIntro").style.display='block';
		rotate();		
	}
}

var adImages = new Array("images/apparel_previews/live-naked-1.jpg","images/apparel_previews/venus-gold-1.jpg","images/apparel_previews/logo-1.jpg","images/apparel_previews/three-birds-1.jpg","images/apparel_previews/flowers-creme-1.jpg","images/apparel_previews/flowers-fusica-1.jpg","images/apparel_previews/bird-on-wire-1.jpg","images/apparel_previews/trees-1.jpg","images/apparel_previews/faces-1.jpg","images/apparel_previews/trippy-1.jpg");
var adLinks = new Array("/product.php?productid=7","/product.php?productid=19","#","/product.php?productid=15","/product.php?productid=13","/product.php?productid=14","/product.php?productid=18","/product.php?productid=11","/product.php?productid=12","/product.php?productid=10");

var thisAd = Math.floor(Math.random()*adImages.length);
var couponClick = false;

function rotate() {
	thisAd++;
	if (thisAd == adImages.length) {
		thisAd = 0;
	}

	if (couponClick == false) {
	 	document.getElementById("apparelSwap").src = adImages[thisAd];
	 	document.getElementById("apparelLink").href = adLinks[thisAd];
	 	
		setTimeout("rotate()", 5 * 1000);
	}
}

function showCoupon() {
	document.getElementById("apparelSwap").src = "images/apparel_previews/live-naked-coupon-1.jpg";
	document.getElementById("apparelLink").href = "#";
	couponClick = true;
	
	setTimeout("restartRotate()", 15 * 1000);
}

function restartRotate() {
	couponClick = false;
	rotate();
}


/****************** apparel side links *****************/

asl = {
	apparelID: 'apparel',
	homeID: 'home',
	
	sideLinkHeader: 'HeaderClick',
	headerLinkClass: 'headerLink',
	activeClass: 'active',
	
	hideClass: 'hide',
	blueClass: 'blue',
	
	broadMinded: 'broad',
	sale: 'sale',
	
	divArray: new Array(),
	linkArray: new Array(),
	
	init:function() {
	 	var apparel=document.getElementById(asl.apparelID); //look to the apparel div
		var links=apparel.getElementsByTagName('a'); //grab all the links in the document
		if(!links) {return;}
		
		//loop through all the links
		for(var i=0;i<links.length;i++) {
		 	//alert(links[i].id);
		 	if(DOMhelp.cssjs('check',links[i],asl.headerLinkClass)) { //make sure it's of the headerLink class		 	 
		 	 	var divName = links[i].id.replace(asl.sideLinkHeader,''); //remove the sideLinkHeader
		 	 	
		 	 	//alert(divName);
		 	 	asl.divArray[asl.divArray.length] = divName;
		 	 	asl.linkArray[asl.linkArray.length] = links[i].id;
				
				var numSubLinks = 0;
				var subLinkDiv = document.getElementById(divName); //hide the sublinks
				if (subLinkDiv) {
				 	if(!DOMhelp.cssjs('check',subLinkDiv,asl.activeClass)) { //check if active class
						DOMhelp.cssjs('add',subLinkDiv,asl.hideClass);
					} else { //if not do not hide and add blue
						DOMhelp.cssjs('add',links[i],asl.blueClass);
					}
					
					var subLinks = subLinkDiv.getElementsByTagName('a'); //get all the sublinks
					var numSubLinks = subLinks.length;
				}
				
		 	 	if(divName != asl.homeID) {//add the click fix and event to the header link, not the home link
		 	 		DOMhelp.addEvent(links[i],'click',asl.headerClick,false); //handle the click
		 	 		
		 	 		if(!links[i].id.toLowerCase().match(asl.broadMinded) && !links[i].id.toLowerCase().match(asl.sale)) {
						links[i].onclick=DOMhelp.safariClickFix;
					}
				}			
			}
		}
	},
	headerClick:function(e) {
	 	var selectedDIV=DOMhelp.getTarget(e); //get selected division
	 	
	 	while(selectedDIV.nodeName.toLowerCase()!='a') { //move up the nodes til the A is active
			selectedDIV=selectedDIV.parentNode;
		}
		
		var selectedLink = selectedDIV;
		
	 	while(selectedDIV.nodeName.toLowerCase()!='div') { //move through the nodes til the DIV is active
			selectedDIV=selectedDIV.nextSibling;
		}	
		
		for(var i=0;i<asl.divArray.length;i++) {
			var closeDIV = document.getElementById(asl.divArray[i]); //get each DIV
			var closeLink = document.getElementById(asl.linkArray[i]); //get each link
			DOMhelp.cssjs('add',closeDIV,asl.hideClass); //hide all the DIV
			DOMhelp.cssjs('remove',closeLink,asl.blueClass); //remove the blue from all the links
		}	
	
		DOMhelp.cssjs('remove',selectedDIV,asl.hideClass); //remove the hide from the selected DIV
		DOMhelp.cssjs('add',selectedLink,asl.blueClass); //add the blue to the selected link
	}
}
DOMhelp.addEvent(window,'load',asl.init,false); //asl - Apparel Side Links