(function ($) {
$.fn.vAlign = function() 
{
    return this.each(function(i){
    var ah = $(this).height();
    var ph = $(this).parent().height();
    if (ph > ah)
      {
        var mh = (ph - ah) / 2;
        $(this).css('padding-top', mh);
      }
      else
      {
        $(this).parent().css('height', ah);
      }
  });
};
})(jQuery);

function CloseBrandsMenu(brandsHeight, utilityHeight) {
    $(".brandsMenu").css("height", "0px");
    $(".brandsMenu").css("top", "-" + brandsHeight + "px");
    $("#OurBrandsLink").css("color", "#EBC894");
    $("#OurBrandsLink").attr("class", "");
    $("#utility").css("height", utilityHeight + "px");
}

function ClosePartnersMenu(partnersHeight, utilityHeight) {
    $(".partnersMenu").css("height", "0px");
    $(".partnersMenu").css("top", "-" + partnersHeight + "px");
    $("#OurPartnersLink").css("color", "#EBC894");
    $("#OurPartnersLink").attr("class", "");
    $("#utility").css("height", utilityHeight + "px");
}


function clearTextSearch() {
    if ($("#txtsearch").val() == "Search")
        $("#txtsearch").val("");
    if ($("#txtsearchtop").val() == "Search")
        $("#txtsearchtop").val("");
} 

function resetTextSearch() {
    if ($("#txtsearch").val() == "")
        $("#txtsearch").val("Search");
    if ($("#txtsearchtop").val() == "")
        $("#txtsearchtop").val("Search");
}

function doSearch(inputName)
{    
    var url = window.location.protocol + "//" + window.location.host;
    if (window.location.href.search("search?") > -1)
        window.location.href = "search.aspx?search=" + $("#" + inputName).val();
    else
        window.location.href = url + "/search.aspx?search=" + $("#" + inputName).val();
}

function search_updateFormAction(theForm)
{
  theForm.action = window.location.protocol + "//" + window.location.host  + "/search.aspx";
}

$(document).ready(function()
{ 
  /*
  $("#lead_text_nav .slide2");
  setInterval(function() {
    $("#lead_text_nav .slide2").filter(':visible').fadeOut(2000,function() {
      if( $(this).next("#lead_text_nav li.slide2").size()) {
        $(this).next().fadeIn(1000);
      }
      else {
        $("#lead_text_nav .slide2").eq(0).fadeIn(1000);     
      }
    });
  },1000);*/

    var utilityHeight = $("#utility").height();
    var brandsHeight = $(".brandsMenu").height() + 25 + 25;
    var partnersHeight = $(".partnersMenu").height() + 25 + 25;
    var tileLengthFromTop = utilityHeight + $("#tile").height();

    $(".brandsMenu").css("top", "-" + (brandsHeight+20) + "px");
    $(".brandsMenu").css("height", "0px");
    $(".partnersMenu").css("top", "-327px");
    $(".partnersMenu").css("height", "0px");

    $("#OurBrandsLink").click(function() {
        if($(".brandsMenu").height() > 0)
        {
            CloseBrandsMenu(brandsHeight, utilityHeight);
            $("#tile").css("top", utilityHeight + "px");
        }
        else
        {
            ClosePartnersMenu(brandsHeight, utilityHeight);

            $(".brandsMenu").css("height", "auto");
            $(".brandsMenu").css("top", "0px");
            $(this).css("color", "#790000");
            $(this).attr("class", "selected");
            $("#utility").css("height", $(".brandsMenu").height() + "px");
            $("#tile").css("top", $(".brandsMenu").height() + "px");
        }
    });

    $("#BrandsMenuCloseButton").click(function() {
        CloseBrandsMenu(brandsHeight, utilityHeight);
        $("#tile").css("top", utilityHeight + "px");
    });

    $("#OurPartnersLink").click(function() {
        if($(".partnersMenu").height() > 0)
        {
            ClosePartnersMenu(brandsHeight, utilityHeight);
            $("#tile").css("top", utilityHeight + "px");
        }
        else
        {
            CloseBrandsMenu(brandsHeight, utilityHeight);

            $(".partnersMenu").css("height", "auto");
            $(".partnersMenu").css("top", "0px");
            $(this).css("color", "#790000");
            $(this).attr("class", "selected");
            $("#utility").css("height", $(".partnersMenu").height() + "px");
            $("#tile").css("top", $(".partnersMenu").height() + "px");
        }
    });

    $("#PartnersMenuCloseButton").click(function() {
        ClosePartnersMenu(brandsHeight, utilityHeight);
        $("#tile").css("top", utilityHeight + "px");
    });
    
    if (window.location.href.search("search?") > -1)
        $("#topsearch").css("display", "none");  
    else
        $("#txtsearchtop").val("Search");
        
});

jQuery(window).load(function() {
    $(".brandsMenu").show();
    $(".partnersMenu").show();
    /* Show first then vertical align */
    $(".brandsMenuItem img").vAlign();
});
