$(document).ready(function(){
	
	// Get Text in AJAX
	$.post(
		baseUrl,
		{
			ajax: "get_text",
			force_language: language
		},
		function(data){
			all_texts = data;
		},
		'json'
	);
	
	newsletter_state = 1;
	$("#bt-signup").click(function () {
		//$(".p-updated").toggle("normal");
		if(newsletter_state == 1)
		{
			$('#newsletter_state1').slideUp({
				duration:600,
				easing:'easeInCubic'
			});
			newsletter_state = 2;
		}
		else
		{
			$('#newsletter_state1').slideDown({
				duration:600,
				easing:'easeInCubic'
			});
			newsletter_state = 1;
		}
	});
	
	$("#sendSignup").click(function () {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var emailToVal = $("#email").val();
		var nameVal = $("#name").val();
		if(!emailReg.test(emailToVal) || emailToVal=="" || nameVal=="") {
			if(!emailReg.test(emailToVal) || emailToVal==""){
				$("#enteremail").replaceWith('<p id="enteremail" class="p-updated" style="color:#F00;">' + getText('newsletter_enterEmail2') + '</p>');
			}else{
				$("#enteremail").replaceWith('<p id="enteremail" class="p-updated" style="color:#000;">' + getText('newsletter_enterEmail') + '</p>');	
			}
			if(nameVal==""){
				$("#entername").replaceWith('<p id="entername" class="p-updated" style="color:#F00;">' + getText('newsletter_enterName2') + '</p>');
			}else{
				$("#entername").replaceWith('<p id="entername" class="p-updated" style="color:#000;">' + getText('newsletter_enterName') + '</p>');
			}
		}else{
			$("#enteremail").replaceWith('<p class="p-updated" style="color:#0B0;">Veuillez patientez...</p>');
			$("#entername").remove();
			$("#name").remove();
			$("#email").remove();
			$("#sendSignup").remove();
			
			$.post(
				baseUrl,
				{
					ajax: 'signup',
					name: nameVal,
					email: emailToVal
				},
				function(data){
					if(data.success == 1)
					{
						
						$("#newsletter_state2").replaceWith('<div class="sent-subscribe" style="color:#0b0;">' + getText('newsletter_returnSuccess') + '</div>');
					}
					else
					{
						
						$("#newsletter_state2").replaceWith('<div class="sent-subscribe" style="color:#f00;">' + getText('newsletter_returnSuccess2') + '</div>');
					}
				},
				'json'
			);
		}
	});	
	
	
	// START Slideshow
	$('#slideshow_control').append('<span class="nav"></span>');
	var counter = 1;
	$('#slideshow > div').each(function(){
		var addClass = '';
		if($(this).hasClass('active'))
		{
			addClass = ' class="active"';
		}
		
		$('#slideshow_control > span.nav').append('<a href="#"'+addClass+' rel="slide_'+counter+'">' + counter + '</a>');
		$(this).attr('id', 'slide_'+counter);
		counter++;
	});
	
	$('#slideshow_control > span.nav > a').click(function(){
		clearInterval(slideInterval);
		slideSwitch($(this).attr('rel'));
	});

	
	var slideInterval = setInterval("slideSwitch(null)", 10000);
	// END Slideshow
	

	// Prevent default action on # link
	$('a[href="#"]').click(function(e){
		e.preventDefault();
		return false;
	});
	
	//function that show hidding control for slideshow
	var controlerDown = $("#slideshow_control").css("bottom");
	var controlerUp = "0px";
	$("#slideshow_header").hover(function(){
		$("#slideshow_control").stop().animate({ 
			bottom: controlerUp
		},
		{
			duration: 400
		});
	},function(){
		$("#slideshow_control").stop().animate({ 
			bottom: controlerDown
		},
		{
			duration: 400
		});
	});
});

function slideSwitch(slideIdToShow) {
	var $active = $('#slideshow div.active');
	
	if ( $active.length == 0 ) $active = $('#slideshow div:last');
	
	if(slideIdToShow == null)
	{
		// use this to pull the divs in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()	: $('#slideshow div').first();
	}
	else
	{
		var $next = $('#slideshow div#'+slideIdToShow);
	}
	
	if($next.hasClass('active'))
	{
		return false;
	}
	
	$("#slideshow_control span.title").html("");
	$('#slideshow_control > span.nav > a').removeClass('active');
	
	$active.addClass('last-active');
	
	$("#slideshow_control span.title").html($next.attr("title"));
	$('#slideshow_control > span.nav > a[rel='+$next.attr('id')+']').addClass('active');
	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 500, function() {
			$active.removeClass('active last-active');
		});
	
}

function getText(id)
{
	return eval('all_texts.'+id);
}
