//Equal height columns
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	if(minHeightCheck() == true) {
		group.css("min-height",tallest + "px");
	}
	else {
		//browser does not support min-height so we must use height instead	
		group.css("height",tallest + "px");
	}
}

//We need to see if the browser supports min-height css property
function minHeightCheck() {
	var id = "minheightsupport" + (new Date).getTime();
	$("<div></div>").attr("id",id).css({ height : "1px", minHeight : "2px", overflow : "hidden", border : "none", padding : "0", margin : "0" }).appendTo("body");
	var iscorrectheight = document.getElementById(id).offsetHeight==2;
	$("#"+id).remove();
	return iscorrectheight;
}

function switchpage(select) {
    var index;

    for (index = 0; index < select.options.length; index++)
        if (select.options[index].selected) {
        if (select.options[index].value != "") {
            if (querySt("sort") != "") {
                window.location.href = replaceQueryString(window.location.href, "sort", select.options[index].value);
            }
            else {
                window.location.href = window.location.href + "?sort=" + select.options[index].value;
            }
        }
        break;
    }
}

function replaceQueryString(url, param, value) {
    var re = new RegExp("([?|&])" + param + "=.*?(&|$)", "i");

    if (url.match(re)) {
        return url.replace(re, '$1' + param + "=" + value + '$2');
    }
    else {
        var is_p = document.URL.indexOf('?page=');
        if (is_p != -1) {
            return url + '&' + param + "=" + value;
        }
        else {
            return url + '?' + param + "=" + value;
        }
    }
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}


$(document).ready(function() {
	//Remove text value in search field when focused
	$(".text-search").focus(function () {
		if($(this).attr("value") == "ENTER SEARCH") {
			$(this).attr({
				value: ""
			});
		}
	});
	
	//Shop Nav
	var currentSubNav;
	
	$(".categories").hide();
	//Hide sub nav items
	
	//Append + to top level categories with a sub nav
	$("#sub_nav h4 a").each(function (i) {
		if ($(this).parent().parent().find(".categories").length > 0) {
			$(this).append("<span class='plus'>+</span>");
		}
	});
	
	//Expand sub nav if navigating that section
	$(".eFashionZone #sub_nav ul.categories.fashion").show().addClass("open");
	$(".eFashionZone .sn-fashion a .plus").remove();
	$(".eFashionZone .sn-fashion a").append("<span class='minus'>-</span>");
	
	$(".eBeautyZone #sub_nav ul.categories.beauty").show().addClass("open");
	$(".eBeautyZone .sn-beauty a .plus").remove();
	$(".eBeautyZone .sn-beauty a").append("<span class='minus'>-</span>");
	
	$(".eLifestyleZone #sub_nav ul.categories.lifestyle").show().addClass("open");
	$(".eLifestyleZone .sn-lifestyle a .plus").remove();
	$(".eLifestyleZone .sn-lifestyle a").append("<span class='minus'>-</span>");
	
	$(".eRetailersZone #sub_nav ul.categories.eretailers").show().addClass("open");
	$(".eRetailersZone .sn-eretailers a .plus").remove();
	$(".eRetailersZone .sn-eretailers a").append("<span class='minus'>-</span>");

	$(".ePriceListingZone #sub_nav ul.categories.price-listing").show().addClass("open");
	$(".ePriceListingZone .sn-price-listing a .plus").remove();
	$(".ePriceListingZone .sn-price-listing a").append("<span class='minus'>-</span>");
	
	//Open sub nav when top level category is clicked
	$("#sub_nav h4 a").click(function () {
		//Get the current sub nav
		currentSubNav = $(this).parent().parent().find(".categories");
		
		//Check if there is a sub nav under this heading
		if(currentSubNav.length > 0) {
			//Check to see if sub nav is already expanded
			if($(currentSubNav).hasClass("open")) {
				$(currentSubNav).hide().removeClass("open"); 
				$(this).find(".minus").remove();
				$(this).append("<span class='plus'>+</span>");
			}
			else {
				$(currentSubNav).show().addClass("open"); 
				$(this).find(".plus").remove();
				//Made sure a sub nav is present before adding minus
				$(this).append("<span class='minus'>-</span>");
			}
			return false; //prevents anchor link redirect
		}
		equalHeight($(".equal-height")); //adjust height of content area
   });
	
	//Equalise column heights
	equalHeight($(".equal-height"));
	
	//Style product listing
	var numberInRow = 3;
	$("#product_listing ul:not(.pagination) li").each(function (i) {
			//alert(i);
			if((i + 1) % numberInRow == 0) {
				$(this).addClass('last');
			}
	});;
});

Cufon.replace('#nav li a', { hover: true, fontFamily: 'River Avenue' });
Cufon.replace('h1.cufon', { fontFamily: 'Neutra Display Titling' });
Cufon.replace('h2.cufon', { fontFamily: 'Neutra Display Titling' });
Cufon.replace('h3.cufon', { fontFamily: 'Neutra Display Titling' });
Cufon.replace('#sub_nav h3', { fontFamily: 'Neutra Display Titling' });