﻿// JSLint Config:
/*jslint browser: true */
/*global cc, jQuery, cc_resx_TR_AUTH_AJAX_ERROR */

//  
//  Author: Kevin Burkitt
//  Date:   2010-02-25
//  
//  Javascript controlling display/hide of header login menu
//
cc.auth.login = function() {
    return {

        overFg: false,
        overBg: false,
        inputFocus: false,
        fgBoxVisible: false,
        bgVisible: false,
        overFgTimeout: null,
        overBgTimeout: null,
        inputFocusTimeout: null,
        hoverTime: 300,
        escKeyPress: false,

        init: function() {

            jQuery('.cc-menu-login').unbind('click').click(function() {
                cc.ui.colorbox("/login/", cc.auth.initAuthOverlay);
                return false;
            });

            jQuery('.unsubcribemail').unbind('click').click(function() {
                return cc.auth.login.unsubscribeMail();
                
            });

            //jQuery('#unsubscribe-form', container).unbind('submit').submit(function() { return cc.auth.login.unsubscribeMail(); });



            /*
            *  !! REMOVED LOGIN HOVER TEMPORARILY TO EXPEDITE LAUNCH
            
            return;

            // Move to Header
            jQuery('#header-login-box').appendTo('.cc-menu-login');

            // Login Suggestions
            // jQuery("#username", '#header-login-box').labelOver("labelover");

            // Display on Hover
            jQuery('.cc-menu-login').hover(
            function() { if (cc.auth.login.overFgTimeout !== 0) { clearTimeout(cc.auth.login.overFgTimeout); } cc.auth.login.overFg = true; cc.log('OVER FGBOX'); },
            function() { cc.auth.login.overFgTimeout = setTimeout(function() { cc.auth.login.overFg = false; cc.log('OUT FGBOX'); }, cc.auth.login.hoverTime); }
            );

            // Background Moveover State
            jQuery('.login-bg-overlay', '#header-login').hover(
            function() { if (cc.auth.login.overBgTimeout !== 0) { clearTimeout(cc.auth.login.overBgTimeout); } cc.auth.login.overBg = true; cc.log('OVER FGBOX'); },
            function() { cc.auth.login.overBgTimeout = setTimeout(function() { cc.auth.login.overBg = false; cc.log('OUT FGBOX'); }, cc.auth.login.hoverTime); }
            );

            // Textbox display/blur - background fades in when textbox in focus
            jQuery('#username,#password,#submit,#rememberme', '#header-login').focus(
            function() { if (cc.auth.login.inputFocusTimeout !== 0) { clearTimeout(cc.auth.login.inputFocusTimeout); } cc.auth.login.inputFocus = true; cc.log('FOCUS INPUT'); }
            );
            jQuery('#username,#password,#submit,#rememberme', '#header-login').blur(
            function() { cc.auth.login.inputFocusTimeout = setTimeout(function() { cc.auth.login.inputFocus = false; cc.log('BLUR INPUT'); }, cc.auth.login.hoverTime); }
            );


            jQuery('#h-login').click(function() { return false; });

            jQuery(document).keyup(function(e) {
            // esc keypress
            if (e.keyCode === 27) {
            cc.auth.login.hide();
            }
            });

            jQuery('#header-login').submit(function() { return cc.auth.login.loginSubmit(); });

            setInterval(cc.auth.login.checkState, 50);
            
            */
        },

        checkState: function() {
            if (cc.auth.login.overFg && !cc.auth.login.fgBoxVisible) {
                cc.auth.login.fgBoxVisible = true;
                // cc.log('SET fgBoxVisible = true');
                jQuery("#header-login-box").stop(true, true).animate({ opacity: "show", top: "20px" }, 300, function() { cc.auth.login.fgBoxVisible = true; });
            }

            if (cc.auth.login.inputFocus && !cc.auth.login.bgVisible) {
                cc.auth.login.bgVisible = true;
                // cc.log('SET bgVisible = true');
                jQuery('<div/>').addClass('login-bg-overlay').appendTo('BODY').stop(true, true).animate({ opacity: "show" }, 1000);
            }

            if ((!cc.auth.login.overFg && !cc.auth.login.inputFocus) || cc.auth.login.escKeyPress) {
                if (cc.auth.login.bgVisible) {
                    // cc.log('HIDE bg');
                    jQuery('.login-bg-overlay').stop(true, true).animate({ opacity: "hide" }, 400, function() { jQuery(this).remove(); });
                    cc.auth.login.bgVisible = false;
                }
                if (cc.auth.login.fgBoxVisible) {
                    // cc.log('HIDE fgBox');
                    jQuery("#header-login-box").animate({ opacity: "hide", top: "30px" }, 200);
                    jQuery('#header-login-box .error').html('');
                    jQuery('#header-login-box .error').hide();
                    cc.auth.login.fgBoxVisible = false;
                }
            }
            cc.auth.login.escKeyPress = false;
        },

        hide: function() {
            jQuery('#username,#password,#submit').blur();
            cc.auth.login.escKeyPress = true;
        },




        unsubscribeMail: function() {
          
            cc.ajax.formSubmit(
                '#unsubscribe-form',
                {
                    target: '#header-login-box',
                    dataType: 'json',
                    url: '/unsubscribe'
                },
                function(data) {
                    if (typeof data !== 'undefined' && data.success === false) {
                        jQuery('#response').addClass('error');
                        jQuery('#response').html(data.message);
                        return;
                    }
                    else {
                        jQuery('#response').html(data.message);
                        jQuery('#response').addClass('success');
                         
                        return;
                    }

                });

            return false;
        },





        loginSubmit: function() {
            cc.ajax.formSubmit(
                '#header-login',
                {
                    target: '#header-login-box',
                    dataType: 'json',
                    url: '/login?format=json'
                },
                function(data, statusText) {
                    if (typeof data.redirectresp !== 'undefined' && data.redirect !== '200') {
                        jQuery('#header-login-box .message').addClass('error');
                        jQuery('#header-login-box .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) {
                        jQuery('#header-login-box .message').addClass('error');
                    } else {
                        jQuery('#header-login-box .message').removeClass('error');
                    }
                    if (typeof data.message !== 'undefined' && data.message !== '') {
                        jQuery('#header-login-box .error').html(data.message);
                        jQuery('#header-login-box .error').slideDown(300);
                    }
                    if (data.reload === true) {
                        location.reload(true);
                    }

                });

            return false;
        }
    };

} ();
