var theTimeout;
function fixLeftNavHeight(newHeight){//makes the left nav and content sections equal heights
	var navHeight = $("div.leftNav").height();//height of the left nav
	if (navHeight <= newHeight){//make 'em equal
	   $("div.leftNav").css("height", newHeight+"px");
	}
}
function showProdDetails(el, vertPos, horizPos){//shows the product close up on rollover
	$(el).css('top',vertPos).css('left',horizPos).show();
}
function thumbRoll(hdnEl){
    var $currentUL = hdnEl.parent("li").parent("ul.productList");//the ul.productList we're in
	var ulCount = $("div#theProducts").children("ul.productList").size();// how many ul.productList's
	var ulIndex = $("ul.productList").index($currentUL);//the index of this ul.productList
	var liCount = $currentUL.children("li").size();//how many li's
	var liIndex = $currentUL.children("li").index(hdnEl.parent("li"));//the index of this li
//    alert(liIndex);
//	alert("ulIndex: " + ulIndex + ", ulCount: " + ulCount + ", liIndex: " + liIndex + ", liCount: " + liCount);
	 if($currentUL.width() == "334"){//the hero image listing page
	    if (ulCount = 1){
	        switch(liIndex){
	        case 0:
	            showProdDetails(hdnEl,"-187px",0);
	            break;
	        case 1:
	            showProdDetails(hdnEl,"-187px","-167px");
	            break;
	        case 2:
	            showProdDetails(hdnEl,"-374px",0);
	            break;
	        case 3:
	            showProdDetails(hdnEl,"-374px","-167px");
	            break; 
	        }
	    }else{
	        switch(liIndex){
	        case 0:
	            showProdDetails(hdnEl,"-187px",0);
	            break;
	        case 2:
	            showProdDetails(hdnEl,"-187px",0);
	            break;
	        default:
	            showProdDetails(hdnEl,"-187px","-167px");	    
	        }
	    }
	}else if ((ulCount-1) > ulIndex){// if we're not in the last ul.productList
		if (((liIndex + 1) ==  liCount) && (liCount > 1)){//theTimeout = setTimeout(function(){showProdDetails(theHidden,"-185px","-167px")}, 200);//show details down from the top and to the left
		    showProdDetails(hdnEl,"-187px","-167px");//show details down from the top and to the left
		}else{
		    showProdDetails(hdnEl,"-187px",0);//show details down from the top and to the right
		}
	}else if((ulCount == 1) && (liCount == 1)){//only one product
	    showProdDetails(hdnEl,"-187px",0);//show details down from the top and to the right
	}else if(ulCount == 1){//only one row
		if (((liIndex + 1) ==  liCount) && (liCount > 1)){//we're on the last item in the list
		    showProdDetails(hdnEl,"-187px","-167px");//show details down from the top and to the left
		}else{
		    showProdDetails(hdnEl,"-187px",0);//show details down from the top and to the right
		}
	}else if((ulCount-1) == ulIndex){
		if (((liIndex + 1) ==  liCount) && (liCount > 1)){//we're on the last item in the list
		    showProdDetails(hdnEl,"-373px","-167px");//show details up from the bottom up and to the left
		}else{
		    showProdDetails(hdnEl,"-373px",0);//show details up from the bottom up and to the right
		}
	}
}
$(function(){//on document ready
	$("body").pngFix();//heart ie6
	fixLeftNavHeight($("div.contentWhiteBG").height());
	$("div.collectionSlides").easySlider(//for the rest of the collection
		{
			prevId: 'prevCollect',
			prevText: '',	
			nextText: '',
			nextId: 'nextCollect'
		}
	);
});
$(window).load(function(){//on after elements have loaded
	$("ul#topLevelLeftNav > li > a").click(function(){//toggles the left hand navigation on clicking category titles
		$("ul#topLevelLeftNav > li > a").each(function(){//remove highlight class from all
		    $(this).removeClass("highlight");
		});
		$("ul.leftSubLevelNav").each(function(){//hide them all
		    $(this).hide();
		});
		$(this).siblings("ul.leftSubLevelNav").show();//show this one
		var addHeight = $(this).siblings("ul.leftSubLevelNav").height();
		var newHeight = $("div.leftNav").height() + addHeight;
		if (newHeight > $("div.productContent").height()){
		    fixLeftNavHeight(newHeight);
		}else{
		    fixLeftNavHeight($("div.productContent").height());
		}
		$(this).toggleClass("highlight");//add highlight class to this one
		return false;
	});
	$("a#sortDropArw").click(function(){
		$("div#hiddenSort").toggle();
		return false;								  
	});
	$("div#lstSortDrop a").click(function(){
		$("div#hiddenSort").toggle();
	});
//	$("div.hdnContainer, div#theProducts, div#container").mouseout(function(){// hide the details when you mouse out of it
//		$("div.hdnContainer").hide();
//		
//	});
	$("ul.productList li").hover(function(){//show the hidden product info in the top left
		//$("div.hdnContainer").hide();
		clearTimeout(theTimeout);
		var $theHidden = $(this).children("div.hdnContainer");//the div we're going to show
		theTimeout = setTimeout(function(){thumbRoll($theHidden)}, 500);
	}, function(){
	    clearTimeout(theTimeout);
	    $("div.hdnContainer").hide();
	});
});