var Login = Class.create();
Login.prototype = {
	initialize: function() {

		var oBody = document.getElementsByTagName("body").item(0);

		// Add overlay
		var oOverlay = document.createElement("div");
		oOverlay.onclick = function() { MyLogin.end(); return false; }
		oOverlay.setAttribute( 'id','overlay' );
		oOverlay.style.display = 'none';
		oBody.appendChild( oOverlay );

		// Resize overlay to full browser
		var aSize = getPageSize();
		oOverlay.style.width = aSize[0] + 'px';
		oOverlay.style.height = aSize[1] + 'px';	

		// Fade in overlay
		new Effect.Appear('overlay', { duration: 0.5, from: 0.0, to: 0.5 });

		// Add login window
		this.oLogin = document.createElement( 'div' );
		this.oLogin.reference = this;
		this.oLogin.setAttribute( 'id', 'auth' );
		oBody.appendChild( this.oLogin );
	
		// Add HTML to login screen
		new Ajax.Request('ajax/html_login.php', {
			onSuccess: this.onHtmlSuccess,
			onFailure: this.onHtmlFailure } );
	},

	onHtmlSuccess: function( p_oResult ) {
		// Update login html.
		$('auth').update( p_oResult.responseText );
		$('close').onclick = function() { MyLogin.end(); return false; }
		$('button-login').onclick = function() { MyLogin.login(); return false; }
	},

	onHtmlFailure: function() {
		alert( 'An error occured' );
	},

	end: function() {
		// Remove the login overlay.
		Element.remove( 'auth' );
		new Effect.Fade( 'overlay', {duration: 0.3} );
	},
	
	login: function() {
		// Get Username and password and login.
		var sUsername = Field.getValue( 'username' );
		var sPassword = Field.getValue( 'password' );
		var s = Field.getValue( 'password' );

		new Ajax.Request('ajax/login.php', {
				method: 'post',
				parameters: { username: sUsername, password: sPassword }, 
				onSuccess: this.onLoginSuccess,
				onFailure: this.onLoginFailure } );	
		
	},

	loginError: function( p_sLanguage )
	{
		this.oLogin.morph( "background: #9d0c15;", {duration: 0.2} );
		var eMessage = $( 'auth-left' );
		while( eMessage.hasChildNodes() )
			eMessage.removeChild( eMessage.firstChild );

		if( p_sLanguage == "NL" )
		{
			var eHeader = document.createElement( "h2" );
			eHeader.appendChild( document.createTextNode( "Er is een fout opgetreden. U heeft geen account." ) );
			eMessage.appendChild( eHeader );

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "Sorry, u moet een account maken om in te loggen anders kunt u geen producten bestellen." ) );
			eMessage.appendChild( ePar );

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "Als u uw inlog gegevens kwijt ben laat het mij weten dan stuur ik deze op." ) );
			eMessage.appendChild( ePar )

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "Anders probeert u nog een keer in te loggen." ) );
			eMessage.appendChild( ePar )
		}
		else
		{
			var eHeader = document.createElement( "h2" );
			eHeader.appendChild( document.createTextNode( "Failure, no account or login" ) );
			eMessage.appendChild( eHeader );

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "Sorry, you'll have to create an account or need to login before you can order products." ) );
			eMessage.appendChild( ePar );

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "If you lost your login and/or password send me an e-mail so I could send it to you." ) );
			eMessage.appendChild( ePar )

			var ePar = document.createElement( "p" );
			ePar.appendChild( document.createTextNode( "Otherwise try again to login." ) );
			eMessage.appendChild( ePar )
		}
	},

	onLoginSuccess: function( p_oResult ) {
		l_aResult = p_oResult.responseText.parseJSON();
		// If error occured, show message.
		if( !l_aResult["status"] ) {
			MyLogin.loginError( l_aResult["language"] );
		} else {
			// Reload page.
			location.reload(true);
		}			
	},

	onLoginFailure: function() {
		alert( 'Error occured' );
	}
}