var currentDesignerIndex = 0;
var t;

$(document).ready(function(){

    $(".hoverable").each(function(){
        MakeHoverable($(this),"hover");
    });
    
    $(".clickable").each(function(){
        MakeElementClickable($(this),true);
    });
    
    $(".clearer").each(function(){
        $(this).append("<div class=\"clear\">.</div>");
    });
    
    $("#ForgottenPassword").hide();
    
    $("#ForgottenPasswordButton").click(function(e){
    
        e.preventDefault();
        $("#ForgottenPassword").show();
        
    });
    
    $("#PricePointFilter").bind("mouseenter", function(){
        $(this).find(".pricePointOptions").show();
        $(this).addClass("hover");
    });
    
    $("#PricePointFilter").bind("mouseleave", function(){
        $(this).find(".pricePointOptions").hide();
        $(this).removeClass("hover");
    });
    
    $("#SortBy").bind("mouseenter", function(){
        $(this).find(".sortByOptions").show();
        $(this).addClass("hover");
    });
    
    $("#SortBy").bind("mouseleave", function(){
        $(this).find(".sortByOptions").hide();
        $(this).removeClass("hover");
    });
    
    $(".filterBar").bind("mouseenter", function(){
        $(this).find("ul").show();
        $(this).addClass("hover");
    });
    
    $(".filterBar").bind("mouseleave", function(){
        $(this).find("ul").hide();
        $(this).addClass("hover");
    });
    
    $(".productList .product").each(function(){
        MakeElementClickable($(this), false);
    });
    
    //category menu
    $("#Categories ul.level0 li.level0").bind("mouseenter", function(){
        $(this).find("ul").show();
    });
    
    $("#Categories ul.level0 li.level0").bind("mouseleave", function(){
        $(this).find("ul").hide();
    });
    
    $("#OurBlogButton").bind("mouseenter", function(){
        $("body").addClass("hover");
    });
    
    $("#OurBlogButton").bind("mouseleave", function(){
        $("body").removeClass("hover");
    });
    
    $(".searchBox").bind("focus", function(){
        $(".searchBox").val("");
    });
    
    $(".searchBox").bind("keydown", function(e){
        if (e.keyCode == 13) {
            e.preventDefault();
            $(".searchButton").trigger("click");           
        }
    });
    
    $(".searchButton").bind("click", function(e){
        e.preventDefault();
        window.location = "/Search.aspx?q=" + $("input.searchBox").val();
    });
    
    if($("#CategoryRotator").length > 0)
    {
        var homeCats = $("#CategoryRotator .homeCat");
        var CategoryRotator = $("#CategoryRotator");
        var homeCatImages = $(".homeCat .image");
        var homeCatSummaries = $(".homeCat .summary");
        var homeCatLinks = $(".homeCat .link");
        
        var homeImageHolder;
        
        homeCatImages.hide();
        homeCatSummaries.hide();
        homeCatLinks.hide();
        
        CategoryRotator.prepend("<div id=\"HomeImageHolder\"></div>");
        homeImageHolder = CategoryRotator.find("#HomeImageHolder");
        
        selectHomeCat(homeCats[0]);
        
        var homeCat = $(".homeCat");
        homeCat.bind("mouseenter", function(){
            selectHomeCat($(this));
        });
        
        homeCat.click(function(){
            window.location = $(this).find("a").attr("href");
        });
    }
    
    function selectHomeCat(homeCat)
    {
        $("#CategoryRotator .homeCat").removeClass("selected");
        $("#CategoryRotator .homeCat").removeClass("selectedHomeWedding");
        
        if($(homeCat).hasClass("homeWedding"))
        {            
            $(homeCat).addClass("selectedHomeWedding");            
        }
        else
        {
            $(homeCat).addClass("selected");            
        }
        
        homeImageHolder.html($(homeCat).find(".image").html());
    }    
    
    if($("#TopManufacturers").length > 0)
    {
        $("#TopManufacturers .homeManufacturer").each(function(){
            MakeElementClickable($(this),true);
        });
    }
    
    
});

//Gets an internal link and makes the whole item clickable
function MakeElementClickable(item, hideLink)
{
    var link = item.find("a").attr("href");
    item.bind("click", function(){window.location = link;});
    
    if(hideLink)
    {
        item.find("a").hide();
    }
}

//Gets an internal link and makes the whole item clickable
function MakeHoverable(item, mouseOverClass)
{
    item.bind("mouseenter", function(){$(this).addClass("hover")});
    item.bind("mouseleave", function(){$(this).removeClass("hover")});
}
