// JavaScript Document
if ( ! window.Prototype )
	throw new Exception('Can\'t work without prototype');

if ( ! window.BrowserDetect )
	throw new Exception('Can\'t work without BrowserDetect');
	
//if ( ! window.Scriptaculous )
//	throw new Exception('Can\'t work without Scriptaculous');	

if ( ! window.hensam )
	var hensam = { Projects : { mehrdafon : {} } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { mehrdafon : {} } ;
else if ( ! hensam.Projects.mehrdafon  )
	hensam.Projects.mehrdafon = {};
	
hensam.Projects.mehrdafon.LoginForm = Class.create({
	loginLinkElement : 'login',
	loginHolder : null,
	loginCancelElement : 'loginCancel',
	ajaxWrapper : null,
	notificationDisplayTime : 1000,
	focusThisElement : null,
	
	loggedOutHandler : function(event) {
		this.loginHolder.update('<span class="headline_box">Sie sind wieder ausgeloggt!</span>');
		this.loginLinkElement.id = "login";
		window.setTimeout(this.hideLoginForm.bind(this, event), this.notificationDisplayTime);
	},
	
	popupLoginForm : function(event) {
		
		event.stop();
		if ( this.ajaxWrapper.content.down() != this.loginHolder)
		{
			this.ajaxWrapper.content.update(this.loginHolder);
		}
		
		if ( this.isLoggedOut && $('errormsgs') )
		{	
			$('errormsgs').hide();
		}
				
		if ( ! this.isLoggedOut )
		{
			this.loginHolder.update('<span class="headline_box">Sie werden nun ausgeloggt</span>');
			
			new Ajax.Request('/login.php?we_webUser_logout=1', {
				method : 'get',
				onSuccess : this.loggedOutHandler.bindAsEventListener(this)
			});
		}
		
		var inputs = this.loginHolder.select("input[tabindex]");
		var firstInput = null;
		if ( inputs && inputs.length > 0 )
		{
			firstInput = inputs[0];
			
			for ( var i = 1; i < inputs.length; i++ )
			{
				if ( firstInput.tabindex < inputs[i].tabindex )
				{
					firstInput = inputs[i];
				}
			}
			this.focusThisElement = firstInput;
		}
		
		this.ajaxWrapper.show();
		if ( this.focusThisElement && this.focusThisElement.focus)
		{
			this.focusThisElement.focus();
			this.focusThisElement = null;
		}
	},
		
	hideLoginForm : function(event) {
		if ( event && event.stop )
			event.stop();
		this.ajaxWrapper.hide();
		this.fillLoginHolder();		
	},
	
	loginSuccess : function() {
		window.location.reload();
	},
	
	loginCallback : function(transport) {
		var data = transport.responseText.evalJSON();
		if ( data.status == 'ok' )
		{
			this.loginHolder.update('<span class="headline_box">Sie haben sich erfolgreich eingeloggt!</span>');
			window.setTimeout(this.loginSuccess.bind(this), this.notificationDisplayTime);
		}
		else
		{
			$('errormsgs').update('Ihr Login ist leider nicht g&uuml;ltig').show();
		}
	},
	
	submitLogin : function(event) {
		event.stop();
		if ( event.keycode && event.keycode == Event.KEY_ESC )
		{
			return;
		}
		
        new Ajax.Request('/login.php?ajax=true', {
            onSuccess : this.loginCallback.bindAsEventListener(this),
            onFailure : function() { 
				this.ajaxWrapper.alert('Sorry there is a problem with transport :('); 
			},          
            method : 'post',
            postBody : (this.loginHolder.down('form').serialize())
        });
	},
	
	fillLoginHolderCompletion : function() {
		this.loginCancelElement = $(this.loginCancelElementId);
		this.isLoggedOut = this.loginHolder.down('form') ? true : false;
		
		if ( this.isLoggedOut)
		{
			this.loginHolder.down('form').observe('submit', this.submitLogin.bindAsEventListener(this));
		}
		if ( this.loginCancelElement)
		{
			this.loginCancelElement.observe('click', this.hideLoginForm.bindAsEventListener(this));
		}		
	},
	
	fillLoginHolder : function() {
		if ( false ) // BrowserDetect.browser == 'Explorer' )
		{
			this.loginHolder = new Element('div');
		}
		else
		{
			this.loginHolder = this.loginHolder || new Element('div');
		}
		new Ajax.Updater(this.loginHolder, '/login.php', {
			method : 'get',
			parameters : {
				'ajax' : 'true',
				'getform' : 'true'
			},
			onComplete : this.fillLoginHolderCompletion.bindAsEventListener(this)
		});
	},
	
	registerEvents : function(event) {
		this.ajaxWrapper = new hensam.Projects.mehrdafon.AjaxWrapper();
		
		if ( 'Explorer' == BrowserDetect.browser && BrowserDetect.version <= 6 )
		{
			return; // go away if the user uses IE6
		}
		
		this.loginLinkElement = $('login') || $('logoff');		
		this.loginLinkElement.observe('click', this.popupLoginForm.bindAsEventListener(this));
		
		this.fillLoginHolder();
	},
	
	initialize : function() {
		Event.observe(document, 'dom:loaded', this.registerEvents.bindAsEventListener(this));
	}
});

new hensam.Projects.mehrdafon.LoginForm();												  
