﻿$(function () {

    var flip = 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
    $("#imgFindStores").live("click", function () {
        if (validZipPostalCode($("#txtPanelZip").val()) && !($("#txtPanelZip").val() == ' Zip/Postal Code')) {
            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#txtPanelZip").val();
            var value = $("#txtPanelZip").val()+"";
            value = value.replace(/\s/g,"%20");
            $("#find-store-content").load("/StoreLocator/Store-Finder-Widget.aspx?ZipCode=" + value);
        }

        return false;
    });

    $("#txtPanelZip").live("keyup", function () {
        if (validZipPostalCode($("#txtPanelZip").val())) {
            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#txtPanelZip").val();
            var value = $("#txtPanelZip").val()+"";
            value = value.replace(/\s/g,"%20");
            $("#find-store-content").load("/StoreLocator/Store-Finder-Widget.aspx?ZipCode=" + value);
        }

        return false;
    });
    $("#txtPanelZip").live("keypress", function (e) {
        if (e.keyCode == 13) return false;
    });

//    //txtPanelZipIntl
//    $("#imgFindStores").live("click", function () {
//        if ($("#txtPanelZipIntl").val().length == 5 && !($("#txtPanelZipIntl").val() == 'Enter')) {
//            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#txtPanelZip").val();
//            $("#find-store-content").load("/StoreLocator/Store-Finder-Widget.aspx?ZipCode=" + $("#txtPanelZipIntl").val());
//        }

//        return false;
//    });

//    $("#txtPanelZipIntl").live("keyup", function () {
//        if ($("#txtPanelZipIntl").val().length == 5) {
//            //window.location = "/Stores/Store-Results.aspx?ZipCode=" + $("#txtPanelZip").val();
//            $("#find-store-content").load("/StoreLocator/Store-Finder-Widget.aspx?ZipCode=" + $("#txtPanelZipIntl").val());
//        }

//        return false;
//    });
//    $("#txtPanelZipIntl").live("keypress", function (e) {
//        if (e.keyCode == 13) return false;
//    });

    $(".find-store, .close-button").click(function () {
        if (flip % 2 == 0) {
            //$(".arrow").attr("src", "/images/find-store/find_store_arrow_up.png");
            if ($("#Referrer").val().length > 0 && $("#Referrer").val() != "lanefurniture.com") {
                //$("#find-store-content").hide();
                window.location = $("#Referrer").val();
                return false;
            }
            else {
                $("#find-store-content").load("/StoreLocator/Store-Finder-Widget.aspx");
            }
        }
        else {
            //$(".arrow").attr("src", "/images/find-store/find_store_arrow_down.png");
        }
        $("#find-store-content").slideToggle("fast", function () {
            // animation complete
            flip++;
        });
        return false;
    });

    var $stickyHeight = 75;
    var $padding = 0;
    var $topOffset = 75;
    var $footerHeight = 83;
    var obj = $("#find-a-store");
    var objSaf = $(".primaryStoreCSS");


    function scrollSticky() {
        if ($(window).height() >= $stickyHeight) {
            var aOffset = $(obj).offset();

            if ($(window).scrollTop() + $padding > $topOffset) {
                $(obj).attr('style', 'position:fixed; top:-8px; margin-left:-336px; z-index:1500px;');
            } else {
                $(obj).attr('style', 'position:relative; margin-left:-336px;');
            }
        }
    }

    $(window).scroll(function () {
        scrollSticky()
    });

    // Is this a version of Safari?
    if ($.browser.safari) {
        $(objSaf).attr('style', 'position:relative;left:-545px;');
        //alert('test');

    }

});

function validZipPostalCode(zip) {
    if (zip.match(/^[0-9]{5}$/)) {
        return true;
    }
    zip = zip.toUpperCase();
    if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
        return true;
    }
    if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
        return true;
    }
    return false;
}
