$(document).ready(function(){

	//Hide default
	$(".loading").hide();
	$('#send_to_friend').hide();
	$('#send_to_friend').css("visibility","visible");
		
	// Start Fancybox
	$("a.fancybox").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow': false
	});
	
		
	// disable link on main menu if javascript is enabled and disable # link
	$("#menu ul li a[rel]").click(function(e){
		e.preventDefault();
		return false;
	});
	$("a[href=#]").click(function(e){
		e.preventDefault();
		return false;
	});
	
	
	// Submenu management
	delay = 250;
	$("#menu div.smenu").fadeTo(0, 0);
	
	$("#menu div.smenu").each(function(){
		//alert($(this).css('left'));
		//alert($("#menu ul li a[for="+$(this).attr('id')+"]").text());
	});
	
	$("#menu ul li a").hover(
		function(){
			var el = "#"+$(this).attr("rel");
			if(el.length > 1)
			{
				$(el).show();
				$(el).stop();
				$(el).fadeTo(delay, 1);
			}
		},
		function(){
			var el = "#"+$(this).attr("rel");
			if(el.length > 1)
			{
				$(el).stop();
				$(el).fadeTo(delay, 0, function(){$(el).hide();});
			}
		}
	);
	
	$("#menu div.smenu").hover(
		function(){
			$(this).stop();
			$(this).fadeTo(delay, 1);
		},
		function(){
			$(this).stop();
			$(this).fadeTo(delay, 0, function(){$(this).hide();});
		}
	);
	// END submenu
	
	
	// Code for digits only in field
	$("input.digit").keypress(function(e){
		if(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
		{
			return false;
		}
	});
	
	
	// banner on each page
	$('#banner_container').html("");
	$('#banner_container').flash({
		swf: swf_url,
		height: 190,
		width: 1000,
		flashvars: {
			url_favorite: url_favorite,
			url_language: url_language,
			url_send: url_send,
			url_logo: url_logo
		}
	});//END banner
	
	
	// peeling ad
	$('#ad_peel').mouseenter(function(){
		var swf=$('#ad_peel').children()[0];
		swf.peelOff();
		$(this).css("width","490px");
		$(this).css("height","490px");
	});
	$('#ad_peel').mouseleave(function(){
		var swf=$('#ad_peel').children()[0];
		swf.peelOff();
	});
	$('#ad_peel').click(function(e){
		window.open(url_ad_peel);
		e.preventDefault();
		return false;
	});// END peeling ad
	

	// ticker on home page
	$("#partners .images_box").easySlider({
		controlsShow:	false,
		speed: 			800,
		auto:			true,
		pause:			5000,
		continuous:		true
	});
	
	
	
	// closes the send to a friend window
	$(".close_window").click(function(e){
		$("#send_to_friend").fadeOut("slow");
		e.preventDefault();
		return false;
	})//END close window



	// submit the send to a friend form
	$("#submit").click(function(e){

		$(".error").hide();
		var has_error = false;
		var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		var email_self_val = $("#email_self").val();
		if(email_self_val == '') {
			$("#email_self").after('<span class="error">'+error_email_empty+'</span>');
			has_error = true;
		} else if(!email_reg.test(email_self_val)) {
			$("#email_self").after('<span class="error">'+error_email_invalid+'</span>');
			has_error = true;
		}
		
		
		var email_friend_val = $("#email_friend").val();
		if(email_friend_val == '') {
			$("#email_friend").after('<span class="error">'+error_email_empty+'</span>');
			has_error = true;
		} else if(!email_reg.test(email_friend_val)) {
			$("#email_friend").after('<span class="error">'+error_email_invalid+'</span>');
			has_error = true;
		}
		
		var subject_self_val = $("#subject_self").val();
		if(subject_self_val == '') {
			$("#subject_self").after('<span class="error">'+error_subject_empty+'</span>');
			has_error = true;
		}
		
		var subject_friend_val = $("#subject_friend").val();
		if(subject_friend_val == '') {
			$("#subject_friend").after('<span class="error">'+error_subject_empty+'</span>');
			has_error = true;
		}
		
		var action_val = $("#action").val();
		
		if(has_error == false) {
			$(this).hide();
			$(".loading").show();
			
			$.post(base_url+"ajax/",
				{
					ajax: "mailto_ami",
					action:action_val,
					email_self: email_self_val,
					email_friend: email_friend_val,
					subject_friend: subject_friend_val,
					subject_self: subject_self_val,
					language:language
				},
				function(data){
					if(parseInt(data) == 0)
					{
						// ca marche
						$("#send_to_friend_container").slideUp("normal", function() {
							$("#send_to_friend_container").before('<p class="feedback">'+success+'</p>');
						});
						
						setTimeout("closeFriend();",3000);
						
					}
					else
					{
						// ca marche pas
						$("#send_to_friend_container").slideUp("normal", function() {
							$("#send_to_friend_container").before('<p class="feedback">Code d\'erreur '+data+'</p><p>'+failure+'</p>');
						});
						
						setTimeout("closeFriend();",3000);
					}
				},
				"text"
			);
			
		}
		
		e.preventDefault();
		return false;

	});//END send to a friend form
		
	
	// youtube video on each page
	$('#youtube').html("");
	$('#youtube').flash({
		swf: "http://www.youtube.com/v/tSvFWR6NQgk&amp;hl=en_US&amp;fs=1&amp;",
		height: 220,
		width: 270,
		params: {
			allowFullScreen: false,
			allowscriptaccess: "always"
		}
	});//END youtube video


	// advertisement
	$("#big").html('<iframe src="'+base_url+ad_top_large+'" width="750" height="90" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" background-color="transparent"></iframe>');
	$("#small").html('<iframe src="'+base_url+ad_top_small+'" width="200" height="90" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" background-color="transparent"></iframe>');
	$("#ad_side_1").html('<iframe src="'+base_url+ad_side_1+'" width="300" height="250" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" background-color="transparent"></iframe>');
	$("#ad_side_2").html('<iframe src="'+base_url+ad_side_2+'" width="300" height="250" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" background-color="transparent"></iframe>');
	$("#ad_side_3").html('<iframe src="'+base_url+ad_side_3+'" width="300" height="95" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" allowtransparency="true" background-color="transparent"></iframe>');
	
});