//Adds the top-border to topNav drop down item group (ul)
function topNavBorder(){
	//Detects number of lvl1 menu items
	var menuTotal = $("#topNav ul.lvl1").children("li").length;
	//Traverse through lvl1 menu items
	var i=0;
	for (i=0;i<menuTotal;i++)	{
		//Find width of each lvl1 menu item
		var menuWidth = $("#topNav ul.lvl1").children("li:eq(" + i + ")").width();
		//Find width of sub item group (ul) 
		var listWidth = $("#topNav ul.lvl1 li ul").width();
		//Add "2px" to include left and right border
		listWidth = listWidth + 2;
		//Calculate correct size for spacer img
		menuWidth = listWidth - menuWidth;
		//Find with of spacer img
		var imgWidth = $("#topNav ul.lvl1").children("li:eq(" + i + ")").find("ul li:first-child img").width();
		//If a spacer img exists i.e. if sub-menu exists
		if (imgWidth == 1) {
			//Set width of spacer img (other attributes set is CSS)
			 $("#topNav ul.lvl1").children("li:eq(" + i + ")").find("ul li:first-child img").css("width", menuWidth);
		}
	}
}
//Detect leaf elements and add corresponding css class
function setLeaf() {
	//Add class "leaf" to all elements without a sub-nav
	$("#leftNav li.live:not(:has(ul))").addClass("leaf");
	//Remove class "live" from all "leaf" elements
	$("#leftNav li.leaf").removeClass("live");
}
//Detect number of LeftNav levels and set class accordingly ready for formatting
function leftFormat(){
	if ( $("body").is("#plp") || $("body").is("#pdp") ) {
		tmp = $("#leftNav ul:has(li.live)").size();
		if(tmp == 1){
			$("#leftNav").addClass("twoLevels");
		}
		if(tmp == 2){
			$("#leftNav").addClass("threeLevels");
		}
		if(tmp == 3){
			$("#leftNav").addClass("fourLevels");
		}
	}
}
//Set position of Print button on compare page
function comparePrint(){
	var itemTotal = $("#products").children("div").length;
	if((itemTotal == 1) || (itemTotal == 0)){
		$("#compare .back img").addClass("total1");
	}
	if(itemTotal == 2){
		$("#compare .back img").addClass("total2");
	}
	if(itemTotal == 3){
		$("#compare .back img").addClass("total3");
	}
	if(itemTotal == 4){
		$("#compare .back img").addClass("total4");
	}
	if(itemTotal == 5){
		$("#compare .back img").addClass("total5");
	}
}

//page initialisation
function jqueryInit() {
	topNavBorder();
	setLeaf();
	leftFormat();
    comparePrint();
}

function showCompareClone() {
	$("#comparePanelClone").removeClass("compare");
	$("#comparePanelClone").addClass("compareLive");
	$("#topBanner .basket .comparePanelClone").slideDown("normal");
}
function hideCompareClone() {
	$("#topBanner .basket .comparePanelClone").css("display", "none");
	$("#comparePanelClone").removeClass("compareLive");
	$("#comparePanelClone").addClass("compare");
}

function showCompare() {
    $("#comparePanel").removeClass("compare");
    $("#comparePanel").addClass("compareLive");
    $("#topBanner .basket .comparePanel").slideDown("normal");
}
function hideCompare() {
    GLOBAL_COMPARE_CALLER = "";
    $("#topBanner .basket .comparePanel").css("display", "none");
	$("#comparePanel").removeClass("compareLive");
	$("#comparePanel").addClass("compare");
}
function toggleCompare(){    
    GLOBAL_COMPARE_CALLER = "";
    $("#comparePanelClone").remove();
    if ( $("#comparePanel").hasClass("compare") ) {
        showCompare();
    }
	else if ( $("#comparePanel").hasClass("compareLive") ) {
        hideCompare();
    }
}
//Toggle hide/show of Search leftNav components
function toggleSearchFilter(filterElement) {
	elementID = filterElement.parent().attr("id");
	elementClass = filterElement.parent().attr("class")
	if (elementClass == "active") {
		$("#" + elementID + "Container").css("display", "none");
		$("#" + elementID).removeClass("active");
		$("#" + elementID).addClass("inactive");
	}
	else if (elementClass == "inactive") {
		$("#" + elementID + "Container").slideDown("normal");
		$("#" + elementID).removeClass("inactive");
		$("#" + elementID).addClass("active");
	}
}