﻿var sCurrentTab = "";
var bShowZyngaAlert = true;
var GigyaTimerID;

function GigyaAutoLogin() {
    if (gigya != null) {
        gigya.services.socialize.plugins.login.providerClick('gigya-signin', 'facebook');
        window.clearInterval(GigyaTimerID);
    }
}

function InitGlobalUserTab() {
    //FB.Bootstrap.requireFeatures(["Connect"], function () {
    //    FB.init('' + sConnectAPIKey + '', '/xd_receiver.htm', { 'reloadIfSessionStateChanged': true });
    //    if ((ReadCookie('bAuthenticated') != "true") && (ReadCookie('bShowConnect') == "true") && (FB.Connect.get_loggedInUser() > 0)) {
    //        ShowGlobalUserModal('connect', 'modalConnect');
    //    }
    //    else if ((ReadCookie('bAuthenticated') == "true") && (ReadCookie('bShowConnectLink') == "true") && (!(FB.Connect.get_loggedInUser() > 0))) {
    //        ShowGlobalUserModal('connectLink', 'modalConnectLink');
    //    }
    //    else if ((ReadCookie('bAuthenticated') == "true") && (ReadCookie('bShowConnectLinkAuth') == "true") && ((FB.Connect.get_loggedInUser() > 0))) {
    //        ShowGlobalUserModal('connect', 'modalConnectLinkAuth');
    //    }
    //});

    InitEvents();
}

function InitEvents() {
    // VARIOUS EVENTS

    //jQuery('#join').click(function () {
    //    ShowGlobalUserModal('join', 'modalJoin');
    //});

    //jQuery('#signIn').click(function () {
    //    ShowGlobalUserModal('signIn', 'modalSignIn');
    //});

    //jQuery('#signInCancel').click(function (event) {
    //    HideGlobalUserModal();
    //});

    //jQuery('#modalGlobalUser select').mouseenter(function (event) {
    //    event.stopPropagation();
    //});

    //jQuery('#modalGlobalUser select').mouseleave(function (event) {
    //    event.stopPropagation();
    //});

    //jQuery('.forgotPassword').click(function (event) {
    //    ShowGlobalUserModal('signIn', 'modalPassword');
    //});

    //jQuery('#signInJoin').click(function (event) {
    //    ShowGlobalUserModal('join', 'modalJoin');
    //});

    //jQuery('#connectJoin').click(function (event) {
    //    ShowGlobalUserModal('join', 'modalJoin');
    //});

    //jQuery('#connectNo').click(function (event) {
    //    EraseCookie('bShowConnect');
    //    HideGlobalUserModal();
    //});

    //jQuery('#connectIcon').click(function (event) {
    //    if (ReadCookie('bAuthenticated') != "true") {
    //        ShowGlobalUserModal('connect', 'modalConnect');
    //    }
    //});

    //jQuery('#joinCancel').click(function (event) {
    //    HideGlobalUserModal();
    //});

    //jQuery('#connectLinkNo').click(function (event) {
    //    EraseCookie('bShowConnectLink');
    //    HideGlobalUserModal();
    //});

    //jQuery('#connectLinkAuthNo').click(function (event) {
    //    EraseCookie('bShowConnectLinkAuth');
    //    HideGlobalUserModal();
    //});

    //jQuery('#modalGlobalUser').mouseleave(function (event) {
    //    if ((sCurrentTab != 'connect') && (sCurrentTab != 'connectLink') && (sCurrentTab != 'join') && (sCurrentTab != 'signIn')) {
    //        HideGlobalUserModal();
    //    }
    //});

    jQuery('#ctl00_txtRewardsCode1').focus(function () {
        if (ReadCookie('bAuthenticated') == "true") {
            // This is a temporary alert
            //if (bShowZyngaAlert) {
            //    alert('Virtual Zynga gifts have been added to the Slurpee Nation rewards catalog. You can either enter your cup code here and get your Zynga item with your rewards points, or you can enter your cup code directly at BuyEarnPlay.com and redeem.');
            //    bShowZyngaAlert = false;
            //}

            ClearText(this);
            EraseCookie('bShowConnectLink');
            ShowRewardsCodeModal('modalCodeEntry');            
        }
        else {
            // The normal message is going away for maintenance.
            alert('You must be a Slurpee Nation member to participate in the rewards program.');

            // Temporary maintenance message
            //alert('Please excuse us while we perform some routine maintenance. Try entering your code in a couple hours.');
        }
    });

    jQuery('#ctl00_txtRewardsCode1').blur(function () {
        FillText(this);
    });

    jQuery('#ctl00_txtRewardsCode2').focus(function () {
        ClearText(this);
        if (ReadCookie('bAuthenticated') == "true") {
            EraseCookie('bShowConnectLink');
            ShowRewardsCodeModal('modalCodeEntry');
        }
        else {
            alert('You must be a Slurpee Nation member to participate in the rewards program.');
        }
    });

    jQuery('#ctl00_txtRewardsCode2').blur(function () {
        FillText(this);
    });

    jQuery('#ctl00_txtRewardsCode3').focus(function () {
        ClearText(this);
        if (ReadCookie('bAuthenticated') == "true") {
            EraseCookie('bShowConnectLink');
            ShowRewardsCodeModal('modalCodeEntry');
        }
        else {
            alert('You must be a Slurpee Nation member to participate in the rewards program.');
        }
    });

    jQuery('#ctl00_txtRewardsCode3').blur(function () {
        FillText(this);
    });

    jQuery('#codePlacementLink').click(function (event) {
        jQuery('#modalCodePlacement').css('display', 'block');
    });

    jQuery('#modalCodePlacement').click(function (event) {
        jQuery('#modalCodePlacement').css('display', 'none');
    });

    //jQuery('#modalRewardsCode').mouseleave(function(event) {
    //    HideRewardsCodeModal();
    //});
    jQuery('#codeEntryCancel').click(function (event) {
        HideRewardsCodeModal();
    });

    jQuery('#ctl00_txtRewardsCode1').alpha();
    jQuery('#ctl00_txtRewardsCode2').alpha();
    jQuery('#ctl00_txtRewardsCode3').alpha();

    jQuery('#ctl00_txtRewardsCode1').keyup(function (event) {
        jQuery(this).val(jQuery(this).val().toUpperCase());
        if (jQuery(this).val().length == jQuery(this).attr('maxlength')) {
            jQuery('#ctl00_txtRewardsCode2').focus();
        }
    });

    jQuery('#ctl00_txtRewardsCode2').keyup(function (event) {
        jQuery(this).val(jQuery(this).val().toUpperCase());
        if (jQuery(this).val().length == jQuery(this).attr('maxlength')) {
            jQuery('#ctl00_txtRewardsCode3').focus();
        }
    });

    jQuery('#ctl00_txtRewardsCode3').keyup(function (event) {
        jQuery(this).val(jQuery(this).val().toUpperCase());
    });
}

function ShowGlobalUserModal(sTab, sModal) {
    sCurrentTab = sTab;

    HideAllModals();
    HideAllTabs();

    jQuery('#tabGlobalUser').css('background', 'url(/Images/tabGlobalUser_ModalTop.png)');

    jQuery('#' + sTab).css('background', 'url(/Images/tab' + sTab + '.gif) no-repeat');
    jQuery('#' + sModal).css('display', 'block');

    if ((sCurrentTab != 'connect') && (sCurrentTab != 'connectLink') && (sCurrentTab != 'join')) {
        jQuery('#blanket').css('display', 'block');
    }

    jQuery('#modalGlobalUser').fadeIn();

    jQuery(document).keypress(function (event) {
        DisableEnterKey(event);
    });
}

function HideGlobalUserModal() {
    HideAllTabs();
    jQuery('#tabGlobalUser').css('background', 'url(/Images/tabGlobalUser.png)');
    jQuery('#blanket').css('display', 'none');
    jQuery('#modalGlobalUser').fadeOut();
}

function ShowRewardsCodeModal(sModal) {
    HideAllModals();
    HideAllTabs();

    jQuery('#tabGlobalUser').css('background', 'url(/Images/tabGlobalUser.png)');
    jQuery('#' + sModal).css('display', 'block');

    //jQuery('#blanket').css('display', 'block');
    jQuery('#modalRewardsCode').fadeIn();

    jQuery(document).keypress(function (event) {
        DisableEnterKey(event);
    });
}

function HideRewardsCodeModal() {
    jQuery('#blanket').css('display', 'none');
    jQuery('#modalRewardsCode').fadeOut();
}

function HideAllTabs() {
    jQuery('#signIn').css('background', 'none');
    jQuery('#join').css('background', 'none');
    jQuery('#connect').css('background', 'none');
    jQuery('#connectLink').css('background', 'none');
}

function HideAllModals() {
    jQuery('#modalJoin').css('display', 'none');
    jQuery('#modalSignIn').css('display', 'none');
    jQuery('#modalPassword').css('display', 'none');
    jQuery('#modalConnect').css('display', 'none');
    jQuery('#modalConnectLink').css('display', 'none');
    jQuery('#modalConnectLinkAuth').css('display', 'none');

    // Rewards modals
    jQuery('#modalCodeEntry').css('display', 'none');
}
