var initSubject;
var initMessage;
var cache = new Array();
var moving = 0;
	
$(document).ready(function(){
	
	var isIE = /*@cc_on!@*/false;

	var pages = new Array();
	pages.home = 1;
	pages.experiences = 2;
	pages.scolarship = 3;
	pages.skills = 4;
	pages.hobbies = 5;
	
	addNavEvents();
	addShortcuts();
	loadHomePage();
	forcePageDisplay();
	
	function loadHomePage()
	{
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=get_home",
			success: function(textResponse){
				$("#container1").html(textResponse);
				cache[1] = textResponse;
				preLoadAllPages();
				resetListeners();
			}
		});
	}
	
	function preLoadAllPages()
	{
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=get_experiences",
			success: function(textResponse){
				cache[2] = textResponse;
			}
		});
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=get_scolarship",
			success: function(textResponse){
				cache[3] = textResponse;
			}
		});
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=get_skills",
			success: function(textResponse){
				cache[4] = textResponse;
			}
		});
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=get_hobbies",
			success: function(textResponse){
				cache[5] = textResponse;
			}
		});
	}
	
	function moveTo(pageClicked)
	{
		if( moving == 0 )
		{
			moving = 1;
			var result =  pages[pageClicked] - pages[$('#currentPage').val()];
			if(result > 0)
				goToRight(pageClicked);
			else if(result < 0)
				goToLeft(pageClicked);
		}
	}
	
	
	function resetListeners()
	{
		$('#messageBox').removeClass('messageBox');
		$('#messageBox').html('');
		initSubject = $('#formSubject').val();
		initMessage = $('#formMessage').val();
		
		$('#container2').css('left', '-900px');
		$('#container1').css('opacity', '1');
		
		$("#previous").click(function(event) {
	        goToLeft($('#prevToLoad').val());
	    });
		$("#next").click(function(event) {
	        goToRight($('#nextToLoad').val());
	    });
	    $(".toggleable").click(function(event){
			makeToggle(this.id);
		});
		
		disableShortutsInForm();
	}
	
	function fadeOutPage(page)
	{
		$('#'+page).stop();
		$('#'+page).animate({"opacity": "0"}, "fast");
		$('#'+page).animate({"filter": "alpha(opacity=0)"}, "fast");
		$('#'+page).animate({"-moz-opacity": "0"}, "fast");
	}
	
	function forcePageDisplay(page)
	{
		$('#'+page).stop();
	}
	
	function goToLeft(pageToLoad)
	{
		if(!isIE)
		{
			forcePageDisplay();
			fadeOutPage('container1');
		}
	    $('#container2').css('left', '-900px');
		prepareLoading(pageToLoad);
	}

	function goToRight(pageToLoad)
	{
		if(!isIE)
		{
			forcePageDisplay();
			fadeOutPage('container1');
		}
        $('#container2').css('left', '900px');
		prepareLoading(pageToLoad);
	}
	
	function prepareLoading(pageToLoad)
	{
		if(cache[pages[pageToLoad]] != undefined)
		{
			fillContainer(cache[pages[pageToLoad]], cache[pages[pageToLoad]]);
		}
		else
		{
			$.ajax({
				type: "GET",
				url: "index.php",
				data: "do=get_"+pageToLoad,
				success: fillContainer
			});
		}
	}
	
	function fillContainer(textResponse, xmlResponse)
	{
		$('#container2').html(textResponse);
		$("#container2").animate({"left": "0"}, "fast", displayFinalPage);
	}
	
	function displayFinalPage()
	{
		if($('#container2').html() != "")
		{
			$('#container1').html($('#container2').html());
		}
		$('#container2').html('');
		
		if(!isIE)
		{
			forcePageDisplay('container1');
		}
		resetListeners();
	}
	
	function makeToggle(id)
	{
		$('#content_'+id).toggle("normal");
		if($('#'+id).html() == "masquer la description")
		{
			$('#'+id).html("en savoir plus");
		}
		else
		{
			$('#'+id).html("masquer la description");
		}
	}
	
	function addNavEvents()
	{
		$("#goToUp").click(function(event) {
			$.scrollTo( 0, 300);
		});
		$("#menuHome").click(function(event) {
			moveTo('home');
		});
		$("#menuExperiences").click(function(event) {
			moveTo('experiences');
		});
		$("#menuScolarship").click(function(event) {
			moveTo('scolarship');
		});
		$("#menuSkills").click(function(event) {
			moveTo('skills');
		});
		$("#menuHobbies").click(function(event) {
			moveTo('hobbies');
		});
	}
	
	function disableShortutsInForm()
	{
		$('#formEmail').focus(function(event){
			shortcut.remove("RIGHT");
		});
		$('#formEmail').blur(function(event){
			shortcut.add("RIGHT",function() {
				moveTo($('#nextToLoad').val());
			});
		});
		$('#formEmail').focus(function(event){
			shortcut.remove("LEFT");
		});
		$('#formEmail').blur(function(event){
			shortcut.add("LEFT",function() {
				moveTo($('#prevToLoad').val());
			});
		});
		
		$('#formSubject').focus(function(event){
			shortcut.remove("RIGHT");
		});
		$('#formSubject').blur(function(event){
			shortcut.add("RIGHT",function() {
				moveTo($('#nextToLoad').val());
			});
		});
		$('#formSubject').focus(function(event){
			shortcut.remove("LEFT");
		});
		$('#formSubject').blur(function(event){
			shortcut.add("LEFT",function() {
				moveTo($('#prevToLoad').val());
			});
		});
		
		$('#formMessage').focus(function(event){
			shortcut.remove("RIGHT");
		});
		$('#formMessage').blur(function(event){
			shortcut.add("RIGHT",function() {
				moveTo($('#nextToLoad').val());
			});
		});
		$('#formMessage').focus(function(event){
			shortcut.remove("LEFT");
		});
		$('#formMessage').blur(function(event){
			shortcut.add("LEFT",function() {
				moveTo($('#prevToLoad').val());
			});
		});
		
		moving = 0;
	}
	
	function addShortcuts()
	{
		shortcut.add("RIGHT",function() {
			moveTo($('#nextToLoad').val());
		});
		shortcut.add("LEFT",function() {
			moveTo($('#prevToLoad').val());
		});
		shortcut.add("1",function() {
			moveTo('home');
		});
		shortcut.add("2",function() {
			moveTo('experiences');
		});
		shortcut.add("3",function() {
			moveTo('scolarship');
		});
		shortcut.add("4",function() {
			moveTo('skills');
		});
		shortcut.add("5",function() {
			moveTo('hobbies');
		});
	}
});

function checkContactForm()
{
	var email = $('#formEmail').val();
	var subject = $('#formSubject').val();
	var message = $('#formMessage').val();
	var mailRegex = new RegExp(".+@.+\..+","g")
	var error = false;
	
	if( (!email.match(mailRegex)) )
	{
		$('#formEmail').css("color", "#dd2200");
		error = true;
	}
	else
	{
		$('#formEmail').css("color", "#888888");
	}
		
	if( (initSubject == subject) || (subject == "") || (!isNaN(subject)) )
	{
		$('#formSubject').css("color", "#dd2200");
		error = true;
	}
	else
	{
		$('#formSubject').css("color", "#888888");
	}
	
	if( (initMessage == message) || (message == "") || (!isNaN(message)) )
	{
		$('#formMessage').css("color", "#dd2200");
		error = true;
	}
	else
	{
		$('#formMessage').css("color", "#888888");
	}
	
	if(!error)
	{
		$.ajax({
			type: "GET",
			url: "index.php",
			data: "do=send_mail&email="+email+"&subject="+subject+"&message="+message,
			success: function(textResponse){
				if(textResponse != "")
				{
					$('#messageBox').addClass('messageBox');
					$('#messageBox').html(textResponse);
				}
			}
		});
	}
	return false;
}