/**************************************************************

	Script		: CoolBoxLoginManager
	Version		: 1.0
	Authors		: Marcin Olszowy

**************************************************************/

var CoolBoxLoginManager = new Class({

	initialize : function() {
		setTimeout("$('cbl_uname').focus()",500);
		setTimeout("CBLM.addKeydownEvent()",500);
	},
	
	addKeydownEvent : function() {
		$('cbl_passwd').onkeydown = function(event) {
			var event = new Event(event);
			if(event.key == 'enter') {
				CBLM.login.request('index.php?link_id=start');
			}
		}
	},
			
	login : { 
	
		dblclickcatch : false,
	
		request: function() {
			
			if(!CBLM.login.dblclickcatch) {
				CBLM.login.dblclickcatch = true;
				$('cbl_loginloadingicon').setProperty('src','images/loading.gif');
				$('cbl_loginloadingicon').setStyle('visibility','visible');
				$('cbl_loginloadingicon').setStyle('opacity',1);
				new Request.JSON({
					method: 'post',
					data: {username: $('cbl_uname').get('value').trim(), password: $('cbl_passwd').get('value').trim(), t: $('loginbutton').get('t').trim()},
					url: '_ajax/default.requestlogin.ajax.php',
					onFailure: function(exc) {
						alert(Localization.network_error);
						$('cbl_loginloadingicon').setStyle('visibility','hidden');
					},
					onError: function() {
						alert(Localization.network_error);
						$('cbl_loginloadingicon').setStyle('visibility','hidden');
					},
					onSuccess: function(o) {
						switch(o.success) {
							case(-1):
								UI.login.error(o.message);
								break;
							case(0):
								CBLM.login.error(o);
								if($chk(o.t)) {
									$('loginbutton').setProperty('t',o.t);	
								}
								break;
							case(1):
								CBLM.login.success();
								break;
						}
					}
				}).send();
				
			}
				
		},
		
		success: function() {
			Cookie.write('username', $('username').get('value').trim(), {duration:365});
			//alert(UI.CAB.params.successpath);
			if($chk(UI.CAB.params.successpath)) {
				location.href = UI.CAB.params.successpath;
			} else {
				location.href = 'index.php';
			}
		},
		
		error: function(o) {
			$('cbl_loginloadingicon').setProperty('src','images/icons/cross.png');
			var fadeout = function() {
				$('cbl_loginloadingicon').fade('out');	
				unsetdblclickcatch.delay(500);
			};
			fadeout.delay(2500);
			var unsetdblclickcatch = function() {
				CBLM.login.dblclickcatch = false;
			};
			
			if(o.errortype == 2) {
				UI.viewHTMLBox('coolbox.activationrequired.ajax.php',Localization.coolbox_activationrequired);
			}
			
		}
		
	}
	
});