﻿$(document).ready(function () {
    var tabContainers = $('div.podWrapper > div.newProdContent');
    tabContainers.hide().filter(':first').show();

    $('ul#navtabs a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('ul#navtabs a').removeClass('hilitea');
        $('ul#navtabs li').removeClass('hilite');
        $(this).addClass('hilitea');
        $(this).parent('li').addClass('hilite');
        return false;
    });

    $("#footer a").mouseover(function (e) {
        e.preventDefault();
        var footerTop = $("div#footer").offset().top;
        var footerLeft = $("div#footer").offset().left;
        $("#mapplacerholder").attr("src", $(this).attr('href'));
        $("#mapplacerholder").css({ "top": footerTop - 287, "left": footerLeft });
        $("#mapplacerholder").toggle();
    }).mouseout(function () {
        $("#mapplacerholder").toggle();
        $("#mapplacerholder").css({ "top": "-1000px", "left": "-1000px" });
    });
    $("#footer a").click(function (e) {
        e.preventDefault();
    });

    $("#signup").click(function (e) {
        addSubscriber();
        e.preventDefault();
    });

    $("a.close").click(function () {
        $(".handle").click();
    });

    $("#hotSpot a").click(function () {
        //check which anchor is visible and change div
        if ($("#hotSpot #signupTabOpen").is(":visible")) {
            $("#signupTab").animate({ "height": "225px" }, 500, function () {
                $("#signupTab #signUpform").fadeIn(500);
                $("#hotSpot a").toggle();
            });

        } else {
            $("#signupTab #signUpform, #signupTab #subscribed, #signupTab #alreadysubscribed, #signupTab #error").fadeOut(500, function () {
                $("#signupTab").animate({ "height": "34px" }, 500);
            });
            $("#hotSpot a").toggle();
        }
    });
});

function addSubscriber() {
    $("input").removeClass("error");
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    var flagTest = true;
    if (!$("#txtName").val()) {
        $("#txtName").addClass("error");
        flagTest = false;
    }
    if (!filter.test($("#txtEmail").val())) {
        $("#txtEmail").addClass("error");
        flagTest = false;
    }

    if (flagTest) {
        $.ajax({ type: "POST",
            async: false,
            url: "/WebService.asmx/addSubscriber",
            data: "name=" + $("#txtName").val() + "&email=" + $("#txtEmail").val() + "&preferredStore=" + $("#preferredStore").val() + "&signupType=website",
            success: function (xml) {
                var returnOutput = $(xml).find('boolean').text();
                if (returnOutput == "true") {
                    $("#signUpform").hide();
                    $("#subscribed").show();
                    $("#alreadysubscribed").hide();
                    $("#txtName").val("");
                    $("#txtEmail").val("");
                    $("#preferrStore").val("0");
                } else {
                    $("#signUpform").hide();
                    $("#subscribed").hide();
                    $("#alreadysubscribed").show();
                    $("#txtName").val("");
                    $("#txtEmail").val("");
                    $("#preferrStore").val("0");
                }
            },
            error: function() {
                $("#signUpform").hide();
                $("#subscribed").hide();
                $("#alreadysubscribed").hide();
                $("#error").show();
                $("#txtName").val("");
                $("#txtEmail").val("");
                $("#preferrStore").val("0");
            }
        });
    } else {
    return false;
    }
}


startList = function () {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("siteNav");
        for (i = 0; i < navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function () {
                    this.className += " over";
                }
                node.onmouseout = function () {
                    this.className = this.className.replace(" over", "");
                }
            }
        }
    }
}
window.onload = startList;
