﻿$(document).ready(function () {
    var flip = 0;
    var bStoresLoaded = 0;

    // fix for IE z-index issue
    /*
    if ($.browser.msie) {
    $("#header-container").css("z-index", 5000);
    $("#footer-container").css("z-index", 1);
    }
    */

    // MUST use the .live function to handle AJAX'ed content properly that was not present at original DOM load
    /*
    $(".close-button").live("click", function () {
    $("#find-store-content").slideToggle("fast", function () {
    // animation complete
    flip++;
    });
    return false;
    });
    */

    // MUST use the .live function to handle AJAX'ed content properly that was not present at original DOM load

    $("#zip-header-go").live("click", function () {
        if ($("#zip-header-input").val().length == 5 && !($("#zip-header-input").val() == 'Enter')) {
            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#zip-header-input").val();
            $("div#find-store div.bg-wrap").load("/Store-Locator/Store-Finder-Widget.aspx?ZipCode=" + $("#zip-header-input").val() + " #widgetresults");
        }

        return false;
    });



    $("#zip-header-input").live("keyup", function () {
        if ($("#zip-header-input").val().length == 5) {
            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#zip-header-input").val();
            $("div#find-store div.bg-wrap").load("/Store-Locator/Store-Finder-Widget.aspx?ZipCode=" + $("#zip-header-input").val() + " #widgetresults");
        }

        return false;
    });

    $("#zip-header-input").live("keypress", function (e) {
        if (e.keyCode == 13) return false;
    });

    // on-page lookup (eg., My Favorites)
    $(".fs-zip .zip2 .zip-input").live("keyup", function () {
        if ($(".fs-zip .zip2 .zip-input").val().length == 5) {
            window.location = "/Store-Locator/?ZipCode=" + $(".fs-zip .zip2 .zip-input").val();
            //$("div#find-store div.bg-wrap").load("/Store-Locator/Store-Finder-Widget.aspx?ZipCode=" + $("#zip-header-input").val() + " #widgetresults");
        }

        return false;
    });

    $(".fs-zip .zip2 .zip-input").live("keypress", function (e) {
        if (e.keyCode == 13) return false;
    });
    // end on-page lookup code
    

    $(".store-header").click(function () {
        //if (flip % 2 == 0) {
        if ($("#Referrer").val().length > 0 && $("#Referrer").val() != "broyhillfurniture.com") {
            //$("#find-store-content").hide();
            window.location = $("#Referrer").val();
            return false;
        }
        else {
            $("div#find-store div.bg-wrap").load("/Store-Locator/Store-Finder-Widget.aspx #widgetresults", function (response, status, xhr) {
                if (status == "success") {
                    //alert("THIS HAS ALL OF THE TEXT, INCLUDING <FORM> ... IGNORE THAT. " + xhr.responseText);
                }
            });            
            $('.complex-flyout').css('visibility', 'visible');
            bStoresLoaded = 1;
        }
        //}
        //else {
        ////$(".arrow").attr("src", "/images/find-store/find_store_arrow_down.png");
        //}
        //$("#complex-flyout").slideToggle("fast", function () {
        //    flip++;
        //});
        return false;
    });

    $("div#find-store").mouseenter(function () {
        if (bStoresLoaded == 1) { $('.complex-flyout').css('visibility', 'visible'); }
    });

    $("div#find-store").mouseleave(function () {
        $('.complex-flyout').css('visibility', 'hidden');
    });

});
