// Functions
LoginPanel = {
    id: 'loginPanel',
    default_id: 'loginPanel',
    options: {
        duration: 0.5
    },
    throbber: 'throbber',
    your_style: false,
    showflag: true,
	shown: false,
	sessionTimeoutShown: false,
    loginPass: false,

    resetForm: function() {

        //console.info('reset form');
        new Ajax.Updater('loginPanelElements', this.loginPanelHref);
        LoginPanel.showflag = false;
    },

    show: function(id) {
		
	
		
        	
		if (!id) {
            // if no id is passed, use the default_id
            id = this.default_id;
        } else {
            // if an id is passed, assign it as the current id
            this.id = id;
        }
		
		/*if (!LoginPanel.shown) {
        	Effect.SlideDown(id, this.options);
		}*/
		Effect.SlideDown(id, this.options);
			
		$('loginBottomCap').show();

        //sets focus to login
		if ($('login_email')) {
			setTimeout(function() {
				Form.Element.focus("login_email");
			}, 100);
		}
		LoginPanel.shown = true;

    },

    hide: function(id) {
        // console.log("hide login panel");

        if (!id) {
            id = this.default_id;
        } else {
            this.id = id;
        }
		

        //console.info('Effect slide up');
        new Effect.SlideUp($(id), Object.merge(this.options, {
            afterFinish: this.resetForm.bind(this)
            }));
		$('loginBottomCap').hide();

    },

    showThrobber: function() {
        $(this.throbber).show();
    },

	hideThrobber: function() {
		$(this.throbber).hide();
	},
	
    handleSubmit: function(element, evt) {
        // show the throbber
        LoginPanel.showThrobber();

        // hide the form
        element.hide();
        
        //clear the error messages
        if($('errorMessage')) 
        	$('errorMessage').update('');

        // submit the form
        var ajaxOptions = {
            method: 'post',
            postBody: Form.serialize(element),
			onSuccess: function(response) {
				Ajax.Request.evalResponse(response); //Sometimes Firefox takes the response from the server as plain/text...so let's eval it.
			}
		};

        new Ajax.Request((element.action.indexOf('&button=') != -1 ? element.action: element.action + '&button=client'), ajaxOptions);
        LoginPanel.loginPass = true;
        return false;
    }
	
};

var behaviours2 = {

    

};


var behaviours1 = {
    'a.signup:click, #alreadyMember a:click': function(element, evt) {
        // Fix for IE6, loginPanelWrapper got loaded in DOM after by login_panel_ie6_additions.js. Putting a timeout makes sure the loginPanelWrapper is created.
		Event.stop(evt);
		if ($('loginPanelWrapper')) {
			
            if (LoginPanel.showflag) {
               if (!LoginPanel.shown) {
			   	 	LoginPanel.show();
			   }
            } else {
				LoginPanel.show();
			}
        } else {
			
            if (LoginPanel.showflag) {
				if (!LoginPanel.shown) {
					setTimeout(function() {
						LoginPanel.show();
					}, 1000);
				}
				
            } else {

				/*if (LoginPanel.shown) {
					LoginPanel.show();
				}*/
				
				LoginPanel.show();
				
			}
        }
        window.scroll(0,0);	// Make IE show the top of the page
    },
	
	'#sessionTimeout, #showLoginPanel': function(element,evt) {
		if (!LoginPanel.shown && !LoginPanel.sessionTimeoutShown) {
			setTimeout(function() {
				LoginPanel.show();
				LoginPanel.sessionTimeoutShown = true;
			}, 1000);
		}
	},
	
	'#signUp a:click, #myJennySession.noSession h3 a:click': function(element, evt) {
		Event.stop(evt);
        LoginPanel.show();
		LoginPanel.showflag = false;
    },
	'#login_with_email_and_password': function(element) {
        // store the action of the login form for later form refreshing
        LoginPanel.loginPanelHref = element.action;
    },

    '#loginPanel .forgotPwdLink a:click': function(element, evt) {
		
		if(element.hasClassName("external")) {
			return true;
		} else {	
				Event.stop(evt);
			return new Ajax.Updater('loginPanelElements', element.href + "?email=" + document.getElementById('login_email').value);
			
		}
	
        
        
    },
	
	'ul.forgot_password a:click': function(element, evt) {
		
		if(element.hasClassName("external")) {
			return true;
		} else {	
				Event.stop(evt);
			return new Ajax.Updater('loginPanelElements', element.href + "?email=" + document.getElementById('login_email').value);
			
		}
	  
        
    },

    '.login_cancel_img:click, .password_retrieval_cancel_img:click, #login_cancel:click, #password_retrieval_cancel:click, #loginPanel h3 a:click, #closelogin:click': function(element, evt) {
        Event.stop(evt);
		LoginPanel.hide();
		LoginPanel.shown = false;
		LoginPanel.showflag = true;
        
        if (LoginPanel.loginPass == true) {
            LoginPanel.loginPass = false;
            location.reload(true);
        }
    },
	

    '#login_not_a_client:click': function(element, evt) {
        history.go();
        Event.stop(evt);
    },

    '#loginPanel form': function(element) {
        element.onsubmit = function(evt) {
            return LoginPanel.handleSubmit(element, evt);
        };
    },

    '#login_non_client:click': function(element, evt) {
        var theForm = $('login_with_account_number');

        theForm.action += '&button=nonClient';
        theForm.onsubmit();

        Event.stop(evt);
    },

    '#login_email, #login_password': function(element, evt) {

        element.onkeypress = function() {

            if (/MSIE/.test(navigator.userAgent) != false)
                if (event.keyCode == 13) {
                $('login_login').click();
                //$('login_with_email_and_password').submit();
                return false;
            }
        };

    }
};
EventSelectors.register(behaviours1);