// 
//  loginController.js
//  conceptoire.com
//  
//  Created by Christian Corsano on 2008-09-17.
//  Copyright 2008 Christian Corsano. All rights reserved.
// 

var LoginController = Class.create({
	loginFormDisplayed: false,
	isLogged: false,
	
	init: function(event){
		this.isLogged = $('lock_button').select('.open').size() == 1;
		if (! this.isLogged)
			$('lock_button').parentElement.observe('click', this.showLoginFormAction.bindAsEventListener(this) );
		else
			$('lock_button').parentElement.observe('click', this.showLogoutAction.bindAsEventListener(this) );
	},
	
	showLoginFormAction: function(event){
		this.loginFormDisplayed = true;
		var request = new Ajax.Request(
			'/sessions/new.js');
	},
	
	showLoginFormSuccess: function(form){
		Modalbox.show(form,{
			title:'Login :',
  			afterLoad: function(){
  				$('login_cancel_button').observe('click', Modalbox.hide.bind(Modalbox));
  			}});
	},

	logoutAction: function(event){
		new Ajax.Request('/sessions/destroy.js');
		Modalbox.hide({afterHide:location.reload.bind(location)});
	},
	
	showLogoutAction: function(event){
		var div = new Element('div');
		var button_cancel = new Element('input',{type:'button',value:'Cancel',id:'logout_cancel_button'});
		var button_ok = new Element('input',{type:'button',value:'Ok',id:'logout_ok_button'});
		div.appendChild(button_cancel);
		div.appendChild(button_ok);
		Modalbox.show(div,{title:'Confirm Logout :',
			afterLoad: function(){
				$('logout_ok_button').observe('click', LoginController.logoutAction.bindAsEventListener(LoginController));
				$('logout_cancel_button').observe('click', Modalbox.hide.bind(Modalbox));
			}});
	},
	
	cancelLoginAction: function(event){
		this.loginFormDisplayed = false;
	},
	
	submitLoginAction: function(event){
		
	},
	
	submitLoginSuccess: function(event){
		Modalbox.hide({afterHide:location.reload.bind(location)});
	},
	
	submitLoginFailure: function(event){
		
	}
	
});

LoginController = new LoginController();


document.observe('dom:loaded', LoginController.init.bindAsEventListener(LoginController) );