// JavaScript Document
$(document).ready(function () {

    var ANS_customer_id = "f525263a-4ad7-4900-bee2-79114b3dc9b6";


    //Disable the right click event
    /*
    $(function () {
    $(this).bind("contextmenu", function (e) {
    e.preventDefault();
    });
    }); */

    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    };

    $(".signUp_ascotBlog").click(function () {
        var intLeft = 715;
        var intTop = $("#main").height();   
        $("#overlaySubscribe").css("left", intLeft);
        $("#overlaySubscribe").css("position", "absolute");
        $("#overlaySubscribe").css("top", intTop);
        $("#overlaySubscribe").css("z-index", "99");
        $("#overlaySubscribe").show();
    });

    $(".signUp_ascot").click(function () {
        var intLeft = 715;
        var intTop = 460;
        $("#overlaySubscribe").css("left", intLeft);
        $("#overlaySubscribe").css("position", "absolute");
        $("#overlaySubscribe").css("top", intTop);
        $("#overlaySubscribe").css("z-index", "99");
        $("#overlaySubscribe").show();
    });

    $(".btn_submit").click(function () {
        if (isValidEmailAddress($('.inputSubscribe').attr('value'))) {
            $.getJSON('/DesktopModules/GDC.NewContactus/SignupNewsletter.aspx', 'mail=' + $('.inputSubscribe').attr('value'), function (data) {
                $(".subscribeForm").hide();
                $("#invalidEmail").hide();
                $(".subscribeMessage").show();
            });
        }
        else {
            $("#invalidEmail").show();
        }
    });

    $(".btn_submitBlog").click(function () {
        if (isValidEmailAddress($('.inputSubscribe').attr('value'))) {
            $.getJSON('../DesktopModules/GDC.NewContactus/SignupNewsletter.aspx', 'mail=' + $('.inputSubscribe').attr('value'), function (data) {
                $(".subscribeForm").hide();
                $("#invalidEmail").hide();
                $(".subscribeMessage").show();
            });
        }
        else {
            $("#invalidEmail").show();
        }
    });

    $("#close_ascot").click(function () {
        $("#overlaySubscribe").hide();
    });

    //Gallery
    if ($('a.galleryBox').length) {
        $('a.galleryBox').fancybox({ 'hideOnContentClick': false, 'width': 850, 'height': 510, 'padding': 0, 'overlayOpacity': 0.5, 'showCloseButton': true, 'type': 'iframe', 'titleShow': false });

        if (document.location.href.indexOf("showgallery") > -1) {
            $("a.galleryBox").trigger('click');
        }
    };

    //Email this item
    if ($('a.iframe').length) {
        $("a.iframe").fancybox({
            'hideOnContentClick': false,
            'width': 430,
            'height': 450,
            'padding': 0,
            'overlayOpacity': 0.0,
            'titleShow': false,
            'modal': true,
            'showCloseButton': false
        });
    };

    //Like on gallery
    $("a#likeButton").click(function () {
        $("#facebookBox").css("left", 700);
        $("#facebookBox").css("position", "absolute");
        $("#facebookBox").css("top", 435);
        $("#facebookBox").css("z-index", "99");
        $("#facebookBox").show();
    });

    $("#close_facebook").click(function () {
        $("#facebookBox").hide();
    });

    //Learn more on checkout
    $("a.taxInformation").click(function () {
        var final_position = $(this).position();

        $("#taxInformationBox").css("left", 530);
        $("#taxInformationBox").css("position", "absolute");
        $("#taxInformationBox").css("top", $(this).offset().top - 200);
        $("#taxInformationBox").css("z-index", "99");
        $("#taxInformationBox").show();
    });

    $("#closeTaxBox").click(function () {
        $("#taxInformationBox").hide();
    });

    var date = new Date(); // grabbing the date as of this second it gets run
    $.ajax({
        type: "GET",
        url: "/DesktopModules/GDC.PieceCatalog/totalInCart.aspx?=" + date.getTime(), //add the time stamp and specified format to the end
        dataType: "json",
        cache: false,
        contentType: "application/json",
        success: function (data) {
            $("#totalInCart").text(data.total);
        },
        error: function (xhr, status, error) {
            //alert(xhr.status);
        }
    });

    /* TRACK CHANGES ON THE PAGE */
    var pageChanged = false;

    var catcher = function () {
        if (pageChanged) {
            return 'One or more forms have changed!';
        }
    };
    $(function () {
        $(window).bind('beforeunload', catcher);
    });

    $('.trackChanges').keydown(function (event) {
        pageChanged = true;
    });
    $('.trackChanges').change(function (event) {
        pageChanged = true;
    });

    $('.continueButton').click(function () {
        pageChanged = false;
    });

});

