// JavaScript Document

var contentPage = null;
var showPage = false;
var currentPage = null;
var animate = true;
	
// JavaScript Document
var pageWidth = 700;

$(document).ready(function() {

	$("#Main #Menu li").bind('click', function(event) {
		
		if(animate == true) {
			animate = false;
			var offset = $("#Main #Menu").offset();
			
			$("#Main #Menu").css({position: 'relative'}).css({ top: offset.top - 100 }).animate({ top: -100 }, 1000);
			
			$("#Main #Menu #First").animate({left: 0, top: 30, 'font-size':  '12pt'}, 1000).children().children().animate({width: 85}, function() {$(this).css({ fontSize : '12pt'}); });
			$("#Main #Menu #Second").animate({left: 100 + 'px', top: 30, 'font-size': '12pt'}, 1000).children().children().animate({width: 85}, function() {$(this).css({ fontSize : '12pt'}); });
			$("#Main #Menu #Third").animate({left: 450 + 'px', top: 30, 'font-size':  '12pt'}, 1000).children().children().animate({width: 85}, function() {$(this).css({ fontSize : '12pt'}); });
			$("#Main #Menu #Fourth").animate({left: 550 + 'px', top: 30, 'font-size':  '12pt'}, 1000).children().children().animate({width: 85}, function() {$(this).css({ fontSize : '12pt'}); });
			
			$("body").attr('id', '');
			$("h2#Prolongation").fadeOut();	
			$("#Titre1418").css({position: 'relative'}).animate({opacity: 0, top: -70 + 'px'});
			
			var offsetLogo = $("#LogoHome").offset();
			$("#LogoHome").css({position: 'absolute', left: 112 + 'px'}).animate({top: 0, left: 243, marginTop: 0, width: 214, height: 87}, 1000, function() {
				if(showPage == false && contentPage != null) {
				
					$("#PageText").animate({opacity: 1});
					fillPage(contentPage, currentPage);
					showPage = true;
				}
			}).bind('click', function() {
				document.location.href = 'http://www.jeparticipe.ch';
			});
		}
	});
	
	
	
	$('ul#Menu li').click(function(event) {

		var newPage = $(this).attr('rel');

		$("#PageText").animate({opacity: 0.2});
		
		changePage(newPage);
	});

});

function changePage(newPage) {
	
	$.get('./ajax.php', {pajax: newPage, noHeaders: true}, function(content) {

		contentPage = content;
		currentPage = newPage;
		if(showPage == true) {
			$("#PageText").dequeue().animate({opacity: 0}, function() {
				fillPage(content, currentPage);
			}).animate({opacity: 1});
		}

	});

}

function fillPage(contentPage, currentPage) {
	$("#PageText").html(contentPage).find("form").bind('submit', function() {

		var data = $(this).serializeArray();
		data.push({name: 'pajax', value: currentPage});		
		data.push({name: 'noHeaders', value: 'true'});		
		
		$.ajax({
			
			data: data,
			url: 'ajax.php',
			timeout: 10000,
			type: 'post',
			
			success: function(resultFromForm, xhr) {
				fillPage(resultFromForm, currentPage);
			}  
		});

		return false;						   
	});
	
	
	$("textarea").growfield();
	
	$(".Slidable h2").bind('click', function() {
											 
		$(".Slidable h2").not(this).next().slideUp();
		$(this).next().slideDown();	 
	});
	
	
	$(".updateCaptcha").click(function(event) {
		event.preventDefault();
		var rel = $(this).attr('rel');
		
		var data = jsonCaptcha[rel];
		data.noHeaders = true;
		data.pajax = 'newCaptcha';
		
		var captcha = $(this).parent().parent().find('img');
		$.post('ajax.php', data, function(resp) {
			$(captcha).attr('src', resp);
		});
	});
	
	$("a.Ajax").bind('click', function() {
		
		var rel = $(this).attr('rel');
		changePage(rel);
	});
}

