/* -------------------- INTRO -------------------- */

function startIntro( _video )
{
	// Show intro and hide content
	$("#intro_video").show();
	$("#intro_skip").show();
	$("#intro_text").hide();
	var left = $("#intro_video").offset().left + $("#intro_video").width() - $("#intro_skip").width() - 10;
	var top  = $("#intro_video").offset().top + 5;
	$('#intro_skip').css( { 'left': left + 'px', 'top': top + 'px' });

	// Init player
	swf.addParam('allowfullscreen','false');
	swf.addParam('allowscriptaccess','always');
	swf.addParam('wmode','opaque');
	swf.addVariable('autostart','true');
	swf.addVariable('controlbar','none');
	swf.addVariable('volume','70');
	swf.addVariable('icons','false');
	swf.addVariable('enablejs','true');
	swf.addVariable('file',_video);
	swf.write('intro_video');
}

function playerReady(obj)
{
	player = document.getElementById('mpl');
	player.addModelListener('STATE', 'stateMonitor');
};

function stateMonitor(obj)
{
	if( obj.newstate == 'IDLE' || obj.newstate == 'COMPLETED' )
	{
		$("#intro_skip").hide();
		$('#intro_video').animate({opacity: 'hide'}, 'slow', function() {
			$('#intro_text').animate({opacity: 'show'}, 'slow');
		});
	}
};

/* -------------------- SLOGAN SLIDER -------------------- */

function slideSlogan()
{
	$('#slogans').animate({opacity: 'hide'}, 'slow', function() {
	    sloganIndex = sloganIndex == slogans.length - 1 ? 0 : sloganIndex + 1;
		$('#slogans').html( slogans[ sloganIndex ] );
		$('#slogans').animate({opacity: 'show'}, 'slow');
	});
	setTimeout(slideSlogan, slideSloganTime);
}

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

function initLoginForm()
{
	// Add login functionality
	$( "#login_submit" ).bind( 'click', function() {
		
		$( '#login_submit' ).val( _LANG_PLEASE_WAIT );
		$( '#login_submit' ).attr( 'disabled', true );

		var params = { _view: "login",
				       username: $( "#login_username" ).attr( "value" ),
					   password: $( "#login_password" ).attr( "value" )
		};

		$.getJSON( _INTERFACE_URL, params, function( _return ) {
			$( '#login_submit' ).val( _LANG_LOGIN );
			$( '#login_submit' ).attr( 'disabled', false );
			
			if ( _return.error ) {
				$( "#login_username" ).focus();
				alert( _return.error_msg );
			} else
				window.location.href( _return.location ); 
		} );
	} );	
}

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

function initContactForm()
{
	var cForm = document.getElementById( 'contact_form' );
	
	$( '#contact_submit' ).bind( 'click', function() {

		$( '#contact_submit' ).val( _LANG_PLEASE_WAIT );
		$( '#contact_submit' ).attr( 'disabled', true );
		
		var params = { _view: "contact_form",
				      title: $( '#contact_title' ).attr( 'value' ),
				      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' ),
				      message: $( '#contact_message' ).attr( 'value' ),
					  robot: 'norobot'
		};

		$.getJSON( _INTERFACE_URL, params, function( _return ) {
			$( '#contact_submit' ).val( _LANG_SEND );
			$( '#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();
	} );
}