﻿$(document).ready(function() {
    if (typeof waitTime == "undefined" || waitTime == null)
        waitTime = 2500; //2.5 seconds default

    //sleep, then run slideRight function
    setTimeout("slideRight(GetWindowSizeCoords())", waitTime);

    $(document).keypress(function(e) {
        if (e.keyCode == 27) {
            CloseEmailPopup();
        }
        else if (e.keyCode == 113) {
            slideRight(GetWindowSizeCoords()); //for debugging (opens popup when pressing F2)
        }
    });

    //enter keypress tracking
    $('#txt_jQueryEmail').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_jqueryEmail').click();
            return false;
        }
    });

    $('#txt_jQueryEmailInvalid').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_jQueryEmailInvalid').click();
            return false;
        }
    });

    $('#txt_StaticEmail').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_StaticEmail').click();
            return false;
        }
    });

    $('#txt_EmailInvalid').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_EmailInvalid').click();
            return false;
        }
    });

    $('#txt_headerEmailInvalid').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_headerEmailInvalid').click();
            return false;
        }
    });
    
    $('#txt_headerEmail').keypress(function(e) {
        if (e.keyCode == 13) {
            $('#a_headerEmail').click();
            return false;
        }
    });
});

var xmlhttp = GetXmlHttpObject();

function CaptureEmail(site, emailTxt, prefix) {
    var email = document.getElementById(emailTxt).value;

    if (xmlhttp == null) {
        alert("Your browser does not support HTTP Request");
        return;
    }
    var url = "http://" + location.host + "/EmailService.asmx/CaptureEmail?email=" + email + "&refUrl=" + document.referrer + "&locUrl=" + location.href + "&optInFlag=true&site=" + site;
    //xmlhttp.onreadystatechange=StateChanged;
    xmlhttp.onreadystatechange = function () { StateChanged(prefix); };
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
}

function StateChanged(prefix) {
    if (xmlhttp.readyState == 4) {
        //0 = new
        //1 = updated
        //2 = exists
        //3 = invalid email
        //4 = general error

        if (xmlhttp.responseText == "0" || xmlhttp.responseText == "1") {
            //thank you display
            $('.' + prefix + 'email_registerNow').css("display", "none");
            $('.' + prefix + 'email_invalidEmail').css("display", "none");
            $('.' + prefix + 'email_thankyouLogo').css("display", "block");

            //CloseEmailPopupDelay(2);
        }
        else if (xmlhttp.responseText == "2") {
            //already exists display
            $('.' + prefix + 'email_registerNow').css("display", "none");
            $('.' + prefix + 'email_invalidEmail').css("display", "none");
            $('.' + prefix + 'email_exists').css("display", "block");
        }
        else if (xmlhttp.responseText == "3") {
            //invalid email display
            $('.' + prefix + 'email_registerNow').css("display", "none");
            $('.' + prefix + 'email_invalidEmail').css("display", "block");
            $('.' + prefix + 'email_thankyouLogo').css("display", "none");
            $('#txt_jQueryEmailInvalid').focus();
        }
        else {
            //default display
            $('.' + prefix + 'email_registerNow').css("display", "block");
            $('.' + prefix + 'email_invalidEmail').css("display", "none");
            $('.' + prefix + 'email_thankyouLogo').css("display", "none");
        }


        //omniture event tracking (for new and updated status)
        if (xmlhttp.responseText == "0" || xmlhttp.responseText == "1") {
            s.events = "event5";
        }
    }
}

function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }

    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }

    return null;
}

function OpenPrivacyPolicy(root) {
    window.open(root + 'privacy.htm', 'Privacy_Policy', 'width=500,height=600,scrollbars=yes');
}

function OpenGuarantee(root) {
    window.open(root + 'guarantee.htm', '100%_Guarantee', 'width=660,height=600,scrollbars=yes');
}

function CloseEmailPopupDelay(timeDelay) {
    setTimeout("CloseEmailPopup()", timeDelay * 1000);
}

function CloseEmailPopup() {
    $('.div_jQueryEmail').animate({ left: -800 });
}

function SwitchPopupText() {
    $('.footer_emailsignup_txt').css({ "display": "" });
    $('.emailCapture_error').css({ "display": "none" });
}

function GetWindowSizeCoords() {
    //returns width for left positioning
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var popupHeight = $(".div_jQueryEmail").height();
    var popupWidth = $(".div_jQueryEmail").width();

    //offset = (body.width - overallbody.width) / 2 -- centered so must be divided
    var divWidth = $("#overallbody").css("width");
    var offset_x = (parseInt(divWidth) - windowWidth) / 2;

    //centering
    $(".div_jQueryEmail").css({
        "position": "absolute",
        "top": (windowHeight / 2 - popupHeight / 2)
    });

    //returns positioning for window to 'dock' to left side of screen
    //return a zero offset if the window is sized smaller than the design of the overallbody wrapper div
    return offset_x < 0 ? offset_x : 0;
}

function slideRight(leftTargetCoord) {
    //If the left css property equals 0, move the element to the left as 
    //many pixels as it is wide (including padding and border); 
    //if not, move it back to 0.
    var $lefty = $('.div_jQueryEmail');
    $lefty.animate({
//        left: parseInt($lefty.css('left'), 10) < 0 ?
//        leftTargetCoord :
    //        0
left:0
    });
}
