﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_TR_You_must_login, cc_resx_SITE_URL, cc_resx_TR_AUTH_AJAX_ERROR, 
cc_resx_TR_A_Verification_Link_Mailed, cc_resx_TR_Redirecting, cc_resx_TR_A_Verification_Link_Mailed, 
cc_resx_TR_Logging_In, cc_resx_TR_Please_wait
*/

//  
//  Author: Kevin Burkitt
//  Date:   2010-03-22
//
//  Functions for controlling authorisation and sign up overlays
//
cc.auth = function() {

    return {
        submitInProgress: false,
        redirecting: false,

        init: function(container) {
            if (typeof container === 'undefined') {
                container = 'body';
            }

            /*
            *  !! REMOVED LOGIN HOVER TEMPORARILY TO EXPEDITE LAUNCH
            */
            jQuery('.cc-login').unbind('click').click(function() {
                var prevUrl = escape(window.location);

                if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                    var currentUrl = escape(window.location);
                    window.location = 'http://www.zootoday.com/home/login?forwardDestination=' + currentUrl;
                }
                else {

                    cc.ui.colorbox("/login-join/login/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                }
                return false;
            });



            jQuery('.cc-acount-linking').unbind('click').click(function() {

                cc.ui.colorbox("/acccount-link/", cc.auth.initAuthOverlay);

                return false;
            });




            jQuery('.cc-signup').unbind('click').click(function() {
                /* this used to be /join/ */
                var prevUrl = escape(window.location);
                if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                    var currentUrl = escape(window.location);
                    window.location = 'http://www.zootoday.com/home/registerUser1?forwardDestination=' + currentUrl;
                }
                else {
                    cc.ui.colorbox("/login-join/register/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                }
                return false;
            });

            //REGISTER BUTTON
            jQuery('.cc-register', container).unbind('click').click(function() {

                if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                    var currentUrl = escape(window.location);
                    window.location = 'http://www.zootoday.com/home/registerUser1?forwardDestination=' + currentUrl;
                }
                else {
                    cc.auth.showJoin();
                }
            });

            jQuery('.cc-tourjoin', container).unbind('click').click(function() {
                location.href = '/tourjoin';
            });

            jQuery('#form-auth', container).unbind('submit').submit(function() { return cc.auth.authFormSubmit(); });
            jQuery('#no-password', container).unbind('submit').submit(function() { return cc.auth.authFormSubmit(); });

            if (container === 'body') {
                var returnUrl = cc.helpers.getQueryStringParam("ReturnUrl");
                
                if (typeof returnUrl !== 'undefined' && returnUrl !== '') {
                    var msg = cc.helpers.getQueryStringParam("ReturnUrl");
                    
                    if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                        var currentUrl = escape(window.location);
                        window.location = 'http://www.zootoday.com/home/login?forwardDestination=' + currentUrl;
                    }
                    
                    if (typeof msg === 'undefined' || msg === '') {
                        msg = cc_resx_TR_You_must_login;
                    }
                    cc.ui.colorbox('/login/?msg=' + cc.helpers.encodeQsParam(msg), cc.auth.initAuthOverlay);
                }
            }
        },

        showLogin: function(msg) {


            if (cc_resx_SITE_NAME === 'CandyCrib') {
                //CANDYCRIB
                if (msg === '') {
                    msg = 'null';
                }
                var prevUrl = escape(window.location);
                if (jQuery('.ao-content').size() > 0) {
                    jQuery('.ao-content').fadeOut(300, function() {
                        if (typeof msg !== "undefined" && typeof msg !== "object") {
                            cc.ui.colorbox("/login-join/login/" + escape(msg) + "/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                        } else {
                            cc.ui.colorbox("/login-join/login/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                        }
                    });
                } else {
                    if (typeof msg !== "undefined") {
                        cc.ui.colorbox("/login-join/login/" + escape(msg) + "/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                    } else {
                        cc.ui.colorbox("/login-join/login/?xurl=" + prevUrl, cc.auth.initAuthOverlay);
                    }
                }
            }
            else if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                var currentUrl = escape(window.location);
                window.location = 'http://www.zootoday.com/home/login?forwardDestination=' + currentUrl;
            } else {
                //ULTRAVIXENS
                if (jQuery('.ao-content').size() > 0) {
                    jQuery('.ao-content').fadeOut(300, function() {
                        if (typeof msg !== "undefined" && typeof msg !== "object") {
                            cc.ui.colorbox("/login/" + escape(msg), cc.auth.initAuthOverlay);
                        } else {
                            cc.ui.colorbox("/login/", cc.auth.initAuthOverlay);
                        }
                    });
                } else {
                    if (typeof msg !== "undefined") {
                        cc.ui.colorbox("/login/" + escape(msg), cc.auth.initAuthOverlay);
                    } else {
                        cc.ui.colorbox("/login/", cc.auth.initAuthOverlay);
                    }
                }
            }


            return false;
        },




        // REGISTER BUTTON
        ShowChangeEmail: function() {
            cc.ui.colorbox("/join/change-email/", cc.auth.initAuthOverlay);
        },



        showJoin: function() {

            if (cc_resx_SITE_NAME === 'ZOORealGirls') {
                var currentUrl = escape(window.location);
                window.location = 'http://www.zootoday.com/home/registerUser1?forwardDestination=' + currentUrl;
            }
            else {
                var prevUrl = escape(window.location);
                if (jQuery('.ao-content').size() > 0) {
                    jQuery('.ao-content').fadeOut(300, function() {
                        cc.ui.colorbox("/join/", cc.auth.initAuthOverlay);
                    });
                } else {
                    jQuery('#username').focus();

                    cc.ui.colorbox("/login-join/register/?xurl=" + prevUrl + "/", cc.auth.initAuthOverlay);
                    jQuery('#username').focus();
                }
                return false;

            }
        },




        showModelJoin: function() {
            if (jQuery('.ao-content').size() > 0) {
                jQuery('.ao-content').fadeOut(300, function() {
                    cc.ui.colorbox("/join/model/", cc.auth.initAuthOverlay);
                });
            } else {
                cc.ui.colorbox("/join/model/", cc.auth.initAuthOverlay);
            }
            return false;
        },

        initAuthOverlay: function() {
            // Init Links:
            jQuery('.ao-content .cc-login').unbind('click').click(cc.auth.showLogin);
            jQuery('.ao-content .cc-signup').unbind('click').click(cc.auth.showJoin);
            jQuery('.ao-content .cc-signup-girls').unbind('click').click(cc.auth.showModelJoin);

            jQuery('.ao-content #username').keypress(function() {
                setTimeout(function() {
                    jQuery('#form-auth .auth-url').html(cc_resx_SITE_URL + jQuery('.ao-content #username').val());
                }, 10);
            });
        },

        authFormSubmit: function() {
            var url = '';
            url = jQuery('#form-auth').attr('action');


            url += '?format=json';

            if (cc.auth.submitInProgress === true) {
                return false;
            }
            cc.auth.submitInProgress = true;

            // Clear Error Message
            jQuery('.ao-message').html('');

            // Show Please Wait Overlay
            cc.auth.showWait();

            // Submit Form
            cc.ajax.formSubmit(
                '#form-auth',
                {
                    target: '#form-auth',
                    dataType: 'json',
                    url: url
                },
                function(data, statusText) {


                    if (typeof data.istour !== 'undefined' && data.istour !== '' && data.istour === true) {


                        if (typeof data.message !== 'undefined' && data.message !== '') {
                            alert(data.message);
                            return;
                        }
                        else {
                            if (typeof data.memberid !== 'undefined' && data.memberid !== '') {
                                if (cc_resx_SITE_NAME === 'CandyCrib') {
                                    jQuery('.x_memberid').val(data.memberid);
                                    cc.payments.epochTransactionStart(jQuery('#form-tourjoinpay'));
                                }
                                else {
                                    var productId = '0';
                                    productId = jQuery('#pi_code').value;
                                    if (jQuery('.pi_code').length > 3) {
                                        for (var i = 0; i < jQuery('.pi_code').length; i++) {
                                            if (jQuery('.pi_code')[i].checked) {
                                                productId = jQuery('.pi_code')[i].value;
                                            }
                                            else {


                                            }

                                        }
                                        cc.payments.subscribe(productId, jQuery('#PageSessionId').val(), '1');
                                    }
                                    else {
                                        cc.payments.UserRegisterPay('uv-bt-userregister', '' + data.memberid + '');
                                    }


                                }
                            }
                        }
                    }

                    if (typeof data.redirectresp !== 'undefined' && data.redirect !== '200') {
                        jQuery('.ao-message').addClass('error');
                        jQuery('.ao-message').html(cc_resx_TR_AUTH_AJAX_ERROR);
                        return;
                    }

                    if (typeof data.redirectresp !== 'undefined' && data.redirect !== '') {
                        location.href = data.redirectresp;
                        return;
                    }
                    if (typeof data.success !== 'undefined' && data.success === false) {

                        if (typeof data.userexpired !== 'undefined' && data.userexpired === true) {
                            if (typeof data.message !== 'undefined' && data.message !== '') {
                                cc.auth.showExpiredUserMessage(data.message);
                            }
                        }
                        else {
                            jQuery('.ao-message').addClass('error');
                            jQuery('.ao-message').html('');
                        }

                    } else {
                        jQuery('.ao-message').removeClass('error');
                    }
                    if (typeof data.message !== 'undefined' && data.message !== '') {
                        jQuery('.ao-message').html(data.message);

                        if (typeof data.ecCity !== 'undefined' && data.ecCity === 'success') {
                            //clear sent to a friend textboxes
                            jQuery('.girlofweekinput').val('');

                        }

                    }

                    if (typeof data.messageok !== 'undefined' && data.messageok != '') {
                        cc.ui.showModalOK(data.messageok, null, true);
                    }



                    if (typeof data.reload !== 'undefined' && data.reload === true) {
                        var returnUrl = cc.helpers.getQueryStringParam("ReturnUrl");

                        if (typeof returnUrl !== 'undefined' && returnUrl !== '') {
                            if (typeof data.isjoin !== 'undefined' && data.isjoin === true) {
                                cc.ui.showModalOK(cc_resx_TR_A_Verification_Link_Mailed, null, true);
                            }
                            else {
                                cc.auth.showWait(cc_resx_TR_Redirecting);
                                cc.auth.redirecting = true;
                                location.href = returnUrl;
                            }
                        } else {
                            if (typeof data.isjoin !== 'undefined' && data.isjoin === true) {
                                cc.ui.showModalOK(cc_resx_TR_A_Verification_Link_Mailed, null, true);
                            }
                            else {
                                cc.auth.showWait(cc_resx_TR_Logging_In);
                                cc.auth.redirecting = true;
                                location.reload(true);
                            }
                        }
                    }





                    if (typeof data.redirect !== 'undefined' && data.redirect !== '') {
                        cc.auth.showWait(cc_resx_TR_Redirecting);
                        cc.auth.redirecting = true;
                        location.href = data.redirect;
                    }

                    if (typeof data.overlay !== 'undefined' && data.overlay !== '') {

                        cc.ui.colorbox(data.overlay, cc.auth.initAuthOverlay);
                    }




                    if (typeof data.errors !== 'undefined' && data.errors.length > 0) {
                        if (data.errors.length > 0) {
                            for (var i = 0; i < data.errors.length; i++) {
                                cc.form.showFormError(data.errors[i].id, data.errors[i].message);
                            }

                            jQuery('.error:first').focus();
                        }
                    }


                },
                function() {
                    cc.auth.submitInProgress = false;
                    if (cc.auth.redirecting !== true) {
                        cc.auth.hideWait();
                    }
                });

            return false;
        },










        zooLogout: function() {

            var currentUrl = escape(window.location);
            window.location = 'http://www.zootoday.com/home/logoutSubmit?forwardDestination=' + currentUrl;

        },






        authLoginSubmit: function(formid) {
            var url = '';

            url = jQuery('#' + formid).attr('action');


            url += '?format=json';

            if (cc.auth.submitInProgress === true) {
                return false;
            }
            cc.auth.submitInProgress = true;

            // Clear Error Message
            jQuery('.ao-message').html('');

            // Show Please Wait Overlay
            cc.auth.showWait();

            // Submit Form
            cc.ajax.formSubmit(
                '#' + formid,
                {
                    target: '#' + formid,
                    dataType: 'json',
                    url: url
                },
                function(data, statusText) {

                    if (typeof data.success !== 'undefined' && data.success === false) {

                        jQuery('.ao-message').addClass('error');

                        if (typeof data.message !== 'undefined' && data.message !== '') {
                            jQuery('.ao-message').html(data.message);
                        }



                    } else {
                        jQuery('.ao-message').removeClass('error');
                    }

                    if (typeof data.redirect !== 'undefined' && data.redirect !== '') {
                        cc.auth.showWait(cc_resx_TR_Redirecting);
                        cc.auth.redirecting = true;
                        location.href = data.redirect;
                    }

                    if (typeof data.reload !== 'undefined' && data.reload === true) {
                        var returnUrl = cc.helpers.getQueryStringParam("ReturnUrl");
                        if (typeof returnUrl !== 'undefined' && returnUrl !== '') {
                            if (typeof data.isjoin !== 'undefined' && data.isjoin === true) {
                                cc.ui.showModalOK(cc_resx_TR_A_Verification_Link_Mailed, null, true);
                            }
                            else {
                                cc.auth.showWait(cc_resx_TR_Redirecting);
                                cc.auth.redirecting = true;
                                location.href = returnUrl;
                            }
                        } else {
                            if (typeof data.isjoin !== 'undefined' && data.isjoin === true) {
                                cc.ui.showModalOK(cc_resx_TR_A_Verification_Link_Mailed, null, true);
                            }
                            else {
                                cc.auth.showWait(cc_resx_TR_Logging_In);
                                cc.auth.redirecting = true;
                                location.reload(true);
                            }
                        }
                    }

                },
                function() {
                    cc.auth.submitInProgress = false;
                    if (cc.auth.redirecting !== true) {
                        cc.auth.hideWait();
                    }
                });

            return false;
        },


        showExpiredUserMessage: function(memberid) {

            var msg = '<p class="header">Your Membership Subscription has expired!</p><br/><a onclick=\"cc.payments.UserRegisterPay(\'uv-bt-userregister\', \'' + memberid + '\');"  ><span class="button sprite">Renew</span></a><a href="/">Go Back to Homepage</a>'
            jQuery('body').append("<div style='display:none'><div id='inline-error'><div class='bg'></div><div class='expired-user msg'></div></div></div>");


            jQuery('#inline-error .bg').css('height', '140px');
            jQuery('#inline-error .bg').css('width', '530px');
            jQuery('#inline-error').css('background-color', '#000000');
            jQuery('#cboxClose').css('background-color', '#000000');



            jQuery('#inline-error .msg').html(msg);

            jQuery.fn.colorbox({
                href: '#inline-error',
                inline: true,

                close: 'X',
                onClosed: function() {
                    // If error message box initiated from qs param ?emsg=... is being closed, then 
                    // reload page without the param in the url (so it doesn't keep reappearing)
                    if (cc.helpers.getQueryStringParam('emsg') !== '') {
                        location.href = cc.helpers.stripParamFromQs('emsg');
                    }l
                }
            });
        },


        authSelectGender: function(value) {
        if (value === '1') {
            //girl
            jQuery('#gender').val("female");
            if (cc_resx_SITE_NAME === 'UltraVixens') {
                jQuery('#auth-register-head').css("color", "#eb3700");
                document.getElementById('auth-girl').src = cc_resx_CDN_URL + "images/auth/Stage2Girl.png";
                document.getElementById('auth-guy').src = cc_resx_CDN_URL + "images/auth/Stage1Boy.png";
            } else {
                jQuery('#auth-register-head').css("color", "#ED0AED");
                document.getElementById('auth-girl').src = cc_resx_CDN_URL + "images/Stage2Girl.png";
                document.getElementById('auth-guy').src = cc_resx_CDN_URL + "images/Stage1Boy.png";
            }

            
        }
        else {
            //guy
            jQuery('#gender').val("male");
            if (cc_resx_SITE_NAME === 'UltraVixens') {
                jQuery('#auth-register-head').css("color", "#eb3700");
                document.getElementById('auth-girl').src = cc_resx_CDN_URL + "images/auth/Stage1Girl.png";
                document.getElementById('auth-guy').src = cc_resx_CDN_URL + "images/auth/Stage2Boy.png";
            } else {
                jQuery('#auth-register-head').css("color", "#0494D9");
                document.getElementById('auth-girl').src = cc_resx_CDN_URL + "images/Stage1Girl.png";
                document.getElementById('auth-guy').src = cc_resx_CDN_URL + "images/State2Boy.png";
            }
           
        }



        },


        ZooForgotPassword: function() {

            var currentUrl = escape('http://realgirls.zootoday.com/myprofile/info/');
            window.location = 'http://www.zootoday.com/home/resend.htm?forwardDestination=' + currentUrl;


        },



        showWait: function(msg) {
            if (typeof msg === 'undefined' || msg === '') {
                msg = cc_resx_TR_Please_wait;
            }
            jQuery('#ao-wait-msg').html(msg);
            jQuery('.ao-wait-bg').show();
            jQuery('.ao-wait').show();
        },

        hideWait: function() {
            jQuery('.ao-wait-bg').hide();
            jQuery('.ao-wait').hide();
        }

    };

} ();

