// JavaScript Document

function initSideNav(){ //assigns behaviors to the side nav on document load

	//create the accordian effect actions
	links1 = $$('.sideNav1');
	links2 = $$('.sideNav2');


	links1.each(function(e){
		e.observe('click',function(){accordianNav1(e.id)})
	});
	
	links2.each(function(e){
		e.observe('click',function(){accordianNav2(e.id)})
	});	
	
	/*
		for (var i=0; i<links1.length; i++){
			addEventToObject(links1[i],'onclick',function(){accordianNav1(this.id);});
		}
	
		for (var i=0; i<links2.length; i++){
			addEventToObject(links2[i],'onclick',function(){accordianNav2(this.id);});
		}
	*/

	//assign image swap actions
	navImages = $$('.navImage');

	navImages.each(function(e){

		e.observe('mouseover',function(){MM_swapImage(e.id,'','/lib/layout/home/images/side-nav/'+e.id.replace('navImage','')+'_f2.jpg',1);});
		e.observe('mouseout',function(){MM_swapImgRestore()});

	});

/*
	for (var i=0; i<navImages.length; i++){
		addEventToObject(navImages[i],'onmouseover',function(){MM_swapImage(this.id,'','/lib/layout/home/images/side-nav/'+this.id.replace('navImage','')+'_f2.jpg',1);});
		addEventToObject(navImages[i],'onmouseout',function(){MM_swapImgRestore()});
	}
*/
	MM_preloadImages('/lib/layout/home/images/side-nav/1_f2.jpg','/lib/layout/home/images/side-nav/2_f2.jpg','/lib/layout/home/images/side-nav/3_f2.jpg','/lib/layout/home/images/side-nav/4_f2.jpg','/lib/layout/home/images/side-nav/5_f2.jpg','/lib/layout/home/images/side-nav/6_f2.jpg','/lib/layout/home/images/side-nav/7_f2.jpg','/lib/layout/home/images/side-nav/8_f2.jpg','/lib/layout/home/images/side-nav/9_f2.jpg');
}

function accordianNav1(id){
//open nav item (1st level) when clicked on

	//if you are on the home page, expand the nav to cover up the promobox advertisement
	if($('promobox')){ //if the promobox exists, then you are on the home page
		$('promobox').style.display = 'none';
		$('side-nav').style.minHeight = '460px';

		//this is dumb stuff you have to do to make it work with IE
		if(navigator.userAgent.toLowerCase().indexOf("msie")>-1){
			$('side-nav').style.overflow = 'visible';
			$('side-nav').style.height = '460px';
			setTimeout(function(){$('side-nav').style.background = 'url(/lib/layout/home/images/side-nav/bg.jpg) top left repeat'},50);
		}
	}

	if($('subnav-'+id).style.display != 'block'){
		new Effect.toggle('subnav-'+id, 'blind', {duration:.2});
	}

	//close all other 1st level nav items
	var mySubnav;
	for (var k=0;k<links1.length;k++){
		mySubnav = $('subnav-'+links1[k].id);

		if (mySubnav.id!='subnav-'+id && mySubnav.style.display != 'none' ){
			new Effect.BlindUp(mySubnav.id,{duration:.2});
		}
		
	}	
}

function accordianNav2(id){
//open nav item (2nd level) when clicked on

	if($('subnav-'+id).style.display != 'block'){
		new Effect.toggle('subnav-'+id, 'blind', {duration:.2});
	}
	
	//close all other 2nd level nav items
	var mySubnav;
	for (var k=0;k<links2.length;k++){
		mySubnav = $('subnav-'+links2[k].id);

		if (mySubnav.id!='subnav-'+id && mySubnav.style.display != 'none' ){
			new Effect.BlindUp(mySubnav.id,{duration:.2});
		}
		
	}
}

function openNavItem(){
// open nav item on page load.  first it performs an 
// ajax sql query to find out what nav item needs to be opened

	//if you are not on the home page, expand the side nav to a height of 460px
	if($('side-nav') && (!$('promobox') || (window.location.href.indexOf('#') != -1 && window.location.href.indexOf('#1') == -1 ))){
		$('side-nav').style.minHeight = '460px';		

		//this is dumb stuff you have to do to make it work with IE
		if(navigator.userAgent.toLowerCase().indexOf("msie")>-1){
			$('side-nav').style.overflow = 'visible';
			$('side-nav').style.height = '460px';
		}
	}

	var mylocation = window.location.href;
	if (mylocation.indexOf('#1')!=-1){mylocation = mylocation.split('#1')[0]}

	var url = '/lib/layout/home/whereami.asp'
	var pars = 'location='+encodeURIComponent(mylocation);

	var myAjax = new Ajax.Request( url, {
		  method: 'post', 
		  parameters: pars, 
		  onComplete: openNavItem2
	  });
}

function openNavItem2(originalRequest){
//opens the nav item after the ajax sql query returns

	//closes all nav items
	subnavs = $$('.subnav');
	for (var j=0; j<subnavs.length; j++){
		subnavs[j].style.display = 'none';
	}
	
	//opens the first tier items
	var currentCategory = originalRequest.responseText.split(',')[0];
	var currentSubCategory = originalRequest.responseText.split(',')[1];

	if (currentCategory !=''){
		if(currentCategory == "gloves") { 

			if ($("subnav-gloves").style.display == 'none' ){
				$("subnav-gloves").style.display = 'block'; 
			}
		}else if(currentCategory == "apparel") { 
			if ($("subnav-apparel").style.display == 'none' ){
				$("subnav-apparel").style.display = 'block';
			}
		}else{
			if ($("subnav-prodCat"+currentCategory).style.display == 'none' ){
				$("subnav-prodCat"+currentCategory).style.display = 'block'; 		
			}
		}
	}

	//opens the second tier items	
	if (currentSubCategory !=''){
		if ($("subnav-prodCat"+currentSubCategory).style.display == 'none' ){	
			$("subnav-prodCat"+currentSubCategory).style.display = 'block'; 
		}
	}
}

Event.observe(window,'load',initSideNav);
Event.observe(window,'load',openNavItem);