var _INTERFACE_URL = "interface.php";
var _LOGIN_CONTAINER_FOCUSED = false;
var _LOGIN_USERNAME_FOCUSED = false;
var _LOGIN_PASSWORD_FOCUSED = false;
var _LOGIN_LOADING = false;

/* -------------------- LOGIN -------------------- */

function initLogin()
{
	// Add login functionality
	$( "#login_button" ).bind( "click", function() {
		_LOGIN_LOADING = true;
		var params = { _view: "login",
				       _username: $( "#login_username" ).attr( "value" ),
					   _password: $( "#login_password" ).attr( "value" )
		};
		$( "#login_loader" ).css( "display", "block" ); // Show loader
		$.getJSON( _INTERFACE_URL, params, function( _return ) {
			$( "#login_loader" ).css( "display", "none" ); // Hide loader
			if ( _return.error ) {
				$( "#login_username" ).focus();
				alert( _return.error_msg );
			} else
				window.location.href( _return.location ); 
			_LOGIN_LOADING = false;
		} );
	} );
	
	// Add slider functionality
	$( "#login_link" ).bind( "mouseover", "", showLogin );
	$( "#login_container" ).bind( "mouseover", function() {
		_LOGIN_CONTAINER_FOCUSED = true;
	} );
	$( "#login_container" ).bind( "mouseout", function() {
		_LOGIN_CONTAINER_FOCUSED = false;
	} );
	$( "#login_container" ).bind( "mouseleave", "", hideLogin );
	$( "#login_username" ).bind( "focus", "", function() {
		_LOGIN_USERNAME_FOCUSED = true;
		$( "#login_username_label" ).css( "display", "none" );
	} );
	$( "#login_username" ).bind( "blur", function() {
		_LOGIN_USERNAME_FOCUSED = false;
		if ( $( this ).attr( "value" ) == '' )
			$( "#login_username_label" ).css( "display", "block" );
		hideLogin();
	} );
	$( "#login_password" ).bind( "focus", function() {
		_LOGIN_PASSWORD_FOCUSED = true;
		$( "#login_password_label" ).css( "display", "none" );
	} );
	$( "#login_password" ).bind( "blur", function() {
		_LOGIN_PASSWORD_FOCUSED = false;
		if ( $( this ).attr( "value" ) == '' )
			$( "#login_password_label" ).css( "display", "block" );
		hideLogin();
	} );
	$( "#login_username_label" ).bind( "click", function() {
		$( "#login_username" ).focus();
		$( this ).css( "display", "none" );
	} );
	$( "#login_password_label" ).bind( "click", function() {
		$( "#login_password" ).focus();
		$( this ).css( "display", "none" );
	} );
	
	// Check if input labels must be hidden
	if ( $( "#login_username" ).attr( "value" ) != '' )
		$( "#login_username_label" ).css( "display", "none" );
	if ( $( "#login_password" ).attr( "value" ) != '' )
		$( "#login_password_label" ).css( "display", "none" );
}
function showLogin()
{
	$( "#login_form" ).slideDown( 200 );
	$( "#login_link" ).attr( "class", "hover" );
}
function hideLogin( _perform  )
{
	if ( _LOGIN_CONTAINER_FOCUSED || _LOGIN_USERNAME_FOCUSED || _LOGIN_PASSWORD_FOCUSED || _LOGIN_LOADING )
		return;
	if ( _perform == true ) {
		$( "#login_form" ).slideUp( 200 );
		$( "#login_link" ).attr( "class", "" );
	} else
		setTimeout( "hideLogin(true)", 400 );
}

/* -------------------- CONTACT -------------------- */

function initContact()
{
	var cForm = document.getElementById( 'contact' );
	
	$( '#contact_submit' ).bind( 'click', function() {
		
		$( '#contact_submit' ).val( 'Bitte warten..' );
		$( '#contact_submit' ).attr( 'disabled', true );
		
		var params = { _view: "contact",
				      _firstname: $( '#contact_firstname' ).attr( 'value' ),
				      _surname: $( '#contact_surname' ).attr( 'value' ),
				      _company: $( '#contact_company' ).attr( 'value' ),
				      _phone: $( '#contact_phone' ).attr( 'value' ),
				      _email: $( '#contact_email' ).attr( 'value' ),
				      _subject: $( '#contact_subject' ).attr( 'value' ),
				      _message: $( '#contact_message' ).attr( 'value' )
		};
		
		$.getJSON( _INTERFACE_URL, params, function( _return ) {
			$( '#contact_submit' ).val( 'Abschicken' );
			$( '#contact_submit' ).attr( 'disabled', false );
			
			if ( _return.error ) {
				var fieldID = '#contact_' + _return.field; 
				alert( _return.error_msg );
				$( fieldID ).focus();
			} else {
				alert( _return.msg );
				cForm.reset();				
			}
		} );
	} );
	$( '#contact_abort' ).bind( 'click', function() {
		cForm.reset();
	} );
}