$(document).ready(function() {
	
	//////////////////// INITIALISATION ////////////////////
	
	// Test screen size
	var height = screen.height;
	var width = screen.width;
	var body_width = (document.body.clientWidth);
	var body_height = (document.body.clientHeight);
	if(height < 800 || width < 1050 || body_height < 700 || body_width < 1000) {
		$("#background").css({"top": "0px", "position": "relative", "left": "0px", "margin": "auto"});
	}
	
	
	// Display message if browser = IE
	  if ($('#browser_msg').length != 0) {
	  	$('#browser_msg').delay(2000).slideDown("slow").delay(10000).slideUp("slow");
	  }
	
	// Block link with overdiv on load
	$("#blocker").css("display", "block");
	
	// Show menu on load
	$("#footer, #shadow_center").fadeIn(1500);
	
	// Hide content on load
	$("#content").add("#header").add("#contact").css("visibility", "hidden");
	$("#contact").css("display", "block");
	
	// Loading
	$("#bubble_content").bind("ajaxSend", function(){
   		$("#loading").show();
 	}).bind("ajaxComplete", function(){
   		$("#loading").hide();
 	});
	
	// Rounded corner
	$("#content").corner("8px");

	

	//////////////////// ANIMATE AND DISPLAY CONTENT ////////////////////
	
	// Wait a moment
	var wait=setTimeout(ready,500);
	function ready(){
	
		$("#content").add("#header").css("visibility", "visible");
		
		$(function() { $("#menu").lavaLamp({ fx: "backout", speed: 700 })});
		
		$("#content, li.back, #header").effect("bounce", { times:2, direction:"down", distance:8, mode:"show" }, 600, function(){ 
			
			 // Show web content
 			$("#bubble_content").load("inc/web.php", function() { 
	
	 			$("#contact").css("visibility", "visible");
	 			
	 			web_carousel();
	 			
	 			$(".link_phone").fancybox({
	 				padding: '10',
	 				hideOnContentClick: false,
	 				frameWidth: 600,
	 				frameHeight: 380,
	 				overlayShow: true,
	 				overlayOpacity: '0.3',
	 				overlayColor: '#222',
	 				enableEscapeButton: true,
	 				showCloseButton: true,
	 				hideOnOverlayClick: true,
	 				centerOnScroll: true,
	 				zoomOpacity: true
	 			});
	 			
		 		$(".service_overlay").fancybox({
	 				padding: '10',
	 				hideOnContentClick: false,
	 				frameWidth: 600,
	 				frameHeight: 500,
	 				overlayShow: true,
	 				overlayOpacity: '0.3',
	 				overlayColor: '#222',
	 				enableEscapeButton: true,
	 				showCloseButton: true,
	 				hideOnOverlayClick: true,
	 				centerOnScroll: true,
	 				zoomOpacity: true
	 			});
	 			
	 			$("div#fancy_bg").corner("8px");
	 			
	 			// Unblock link with overdiv on load
				$("#blocker").css("display", "none");
	 			
	 		}).show();
			});
		
		// Stop waiting
		clearTimeout(wait);
	}

	
	//////////////////// CAROUSEL ////////////////////
	
	// Construct overlay
	var carousel_overlay = function() {
		// Description
		$(".carousel_mode .overlay_content").html("<h1>" + $(".carousel_mode .pagination .active a").attr("title") + "</h1>" + $(".pagination .active a").html());
		// Transform carousel links
		$(".carousel_mode .overlay_content .realisation_link").each(function(){
			var link = $(this).find("span").eq(0).text();
			var text = $(this).find("span").eq(1).text();
			$(this).replaceWith('<a href="' +link+ '" class="rea_link" target="_blank">' +text+ '</a>');
		});
	}


	// Display carousel
	var web_carousel = function () {
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion<=7) {
  			
  		$('.carousel_mode #carousel').loopedSlider({ containerClick: false });
		$(".carousel_mode #overlay div").css("display", "none");
		$(".container").css("cursor", "help");
		
		
		// Rollover
 		var hide = false;
		$(".carousel_mode .container, .carousel_mode  #overlay div, .carousel_mode .info_icon").hover(function(){
			if (hide) clearTimeout(hide);
			$(".carousel_mode .overlay_content").html("<h1>" + $(".carousel_mode .pagination .active a").attr("title") + "</h1>" + $(".pagination .active a").html());
			
			carousel_overlay();
			
			$(".carousel_mode #overlay div").css("display", "block");
			
		}, function() {
			hide = setTimeout(function() {
 			$(".carousel_mode #overlay div").css("display", "none");
			}, 500);
		});
		
		$(".carousel_mode .bg_overlay").corner("5px");
		$(".carousel_mode #bt_list").corner("2px");
		$(".carousel_mode #bt_carousel").corner("2px");
		
		// Click on previous or next
		$(".previous, .next").click( function(){
			
			carousel_overlay();
			
			$(".carousel_mode #overlay div").css("display", "block");
					
				// Wait a moment
				var wait=setTimeout(ready,1000);
				function ready(){
		
					$(".carousel_mode #overlay div").css("display", "none");
				
					// Stop waiting
					clearTimeout(wait);
				}
		});
		
  			
  		}
	}
	else {
	
		$('.carousel_mode #carousel').loopedSlider({ containerClick: false });
		$(".carousel_mode .bg_overlay, .carousel_mode #overlay div").css({"opacity": "0"});
		$(".container").css("cursor", "help");
		
		
		// Rollover
 		var hide = false;
		$(".carousel_mode .container, .carousel_mode  #overlay div, .carousel_mode .info_icon").hover(function(){
			if (hide) clearTimeout(hide);
			$(".carousel_mode .overlay_content").html("<h1>" + $(".carousel_mode .pagination .active a").attr("title") + "</h1>" + $(".pagination .active a").html());
			
			carousel_overlay();
			
			$(".carousel_mode .bg_overlay").stop().animate({"opacity": "0.8"}, "normal");
			$(".carousel_mode #overlay div:not(.carousel_mode .bg_overlay)").stop().animate({"opacity": "1"}, "normal");
			
		}, function() {
			hide = setTimeout(function() {
 			$(".carousel_mode #overlay div").animate({"opacity": "0"}, "slow");
			}, 500);
		}).mousemove(function(){ 
			clearTimeout(hide);
			$(".carousel_mode .bg_overlay").stop().animate({"opacity": "0.8"}, "normal");
			$(".carousel_mode #overlay div:not(.carousel_mode .bg_overlay)").stop().animate({"opacity": "1"}, "normal");
		});
		
		$(".carousel_mode .bg_overlay").corner("5px");
		$(".carousel_mode #bt_list").corner("2px");
		$(".carousel_mode #bt_carousel").corner("2px");
		
		// Click on previous or next
		$(".previous, .next").click( function(){
			
			carousel_overlay();
			$(".carousel_mode .bg_overlay").stop().animate({"opacity": "0.8"}, "normal");
			$(".carousel_mode #overlay div:not(.carousel_mode .bg_overlay)").stop().animate({"opacity": "1"}, "normal", function() {
				
				// Wait a moment
				var wait=setTimeout(ready,1000);
				function ready(){
		
					$(".carousel_mode #overlay div").animate({"opacity": "0"}, "slow");
				
					// Stop waiting
					clearTimeout(wait);
				}
			});
		});
	
	}		


		// List Mode
		$("#bt_list a").bind("click", function() {
			$("#bt_list").addClass("active");
			$("#bt_carousel").removeClass("active");
			$("#display_mode").removeClass("carousel_mode").addClass("list_mode");
			
			$("#list_items").remove();
			$(".list_mode .container").prepend('<div id="list_items"></div>');
			
			$(".container").css("cursor", "default");
			
			var i = 0;
			$(".list_mode .pagination li a").each( function() {
				var image = $(".list_mode .container .slides div").eq(i).html();
				var title = $(this).attr("title");
				var description = $(this).html();

/*
				// Thumbs
				if ($(this).attr('href') == '#web') { 
					var type = 'web'; 
				} else if ($(this).attr('href') == '#print') {  
					var type = 'print';
				} else if ($(this).attr('href') == '#both') {
					var type = 'both';
				} else {
					var type = '';
				} 
				// A mettre apres   <div class="list_title">   =>   <div class="'+type+'"></div>
*/
				
				
				$("#list_items").append('<div class="list_item">'+image+'<div class="list_title">'+title+'</div><div class="list_description">'+description+'</div></div>');
				
				// Transform links
				$("#list_items .list_item .realisation_link").each(function(){
					var link = $(this).find("span").eq(0).text();
					var text = $(this).find("span").eq(1).text();
					$(this).replaceWith('<a href="' +link+ '" class="rea_link" target="_blank">' +text+ '</a>');
				});
				
				// Set active
				if (($("#carousel .pagination li").eq(i).attr("class")) == "active") {
					$(".list_item").eq(i).addClass("active");
				}
				i++;
			});
			
			$(".list_item:odd").css({"background-color": "#ddd"});
			$(".list_item:even").css({"background-color": "#fff"});
			
			$(".list_item").corner("5px").css("margin-right", "16px");
			
			$('.list_mode .container').jScrollPane({
				showArrows:false,
				scrollbarWidth: 10,
				animateTo: false
			});
			
			if (!($(".list_mode").hasClass("active"))) {
				$('.list_mode .container')[0].scrollTo(".list_item.active");
			}
			
			$(".list_mode").addClass("active");
			
			$(".list_item.active").stop().animate({"backgroundColor": "#f8f199"}, 400, function() { 
				if ($(".list_item").index(this)%2 == 0) {
					$(".list_item.active").animate({"backgroundColor": "#fff"}, 1000);
				}
				else {
					$(".list_item.active").animate({"backgroundColor": "#ddd"}, 1000);
				}
			});			
			return false;
		});
		
		

		// Carousel
		$("#bt_carousel a").bind("click", function() {
			$("#bt_carousel").addClass("active");
			$("#bt_list").removeClass("active");
			$("#list_items").remove();
			
			$(".container").css("cursor", "help");
			
			//$('.list_mode .container')[0].scrollTo(0);
			$('.list_mode .container').jScrollPaneRemove();

			$("#display_mode").removeClass("list_mode active").addClass("carousel_mode");
		
			return false;
		});
	}
	

	
	//////////////////// MENU AND NAVIGATION ////////////////////
	
	// Navigation Menu
	var bubble_services = "330";
	var bubble_normal = "350";
	var bubble_contact = "370";
	var bubble_team = "390";
	
	// Shadow variables
	var shadow_services = "1100";
	var shadow_normal = "1070";
	var shadow_contact = "1040";
	var shadow_team = "1010";
	
	// INIT shadow
	$("#shadow_menu, #shadow_menu img").css({"opacity": "0.8"});
	
	
	// REALISATION LINK
	$("#realisation_link").bind("click", function() {
		if ($("#content").height() != bubble_normal) {
			$("#content").stop().animate({"height": bubble_normal + "px"}, 800, "easeinout");
		}
		if ($("#shadow_menu").height() != shadow_normal) {
			$("#shadow_menu, #shadow_menu img").stop().animate({"width": shadow_normal + "px", "opacity": "0.8"}, 800, "easeinout");
		}
		$("#bubble_content").fadeOut("slow", function () { $("#bubble_content").load("inc/web.php", function() { 
	 			web_carousel();
	 			
	 			$(".link_phone").fancybox({
	 				padding: '10',
	 				hideOnContentClick: false,
	 				frameWidth: 600,
	 				frameHeight: 380,
	 				overlayShow: true,
	 				overlayOpacity: '0.3',
	 				overlayColor: '#222',
	 				enableEscapeButton: true,
	 				showCloseButton: true,
	 				hideOnOverlayClick: true,
	 				centerOnScroll: true,
	 				zoomOpacity: true
	 			});
	 			
	 			$("div#fancy_bg").corner("8px");
	 	}).add("#contact").fadeIn("slow"); });
	});
	
	// SERVICES LINK
	$("#services_link").bind("click", function() {
		if ($("#content").height() != bubble_services) {
			$("#content").stop().animate({"height": bubble_services + "px"}, 800, "easeinout");
		}
		if ($("#shadow_menu").height() != shadow_services) {
			$("#shadow_menu, #shadow_menu img").stop().animate({"width": shadow_services + "px", "opacity": "0.7"}, 800, "easeinout");
		}
		$("#bubble_content").fadeOut("slow", function() { $("#bubble_content").load("inc/services.php").fadeIn("slow", function(){
		
			$(".service_overlay").fancybox({
 				padding: '10',
 				hideOnContentClick: false,
 				frameWidth: 600,
 				frameHeight: 500,
 				overlayShow: true,
 				overlayOpacity: '0.3',
 				overlayColor: '#222',
 				enableEscapeButton: true,
 				showCloseButton: true,
 				hideOnOverlayClick: true,
 				centerOnScroll: true,
 				zoomOpacity: true
 			});
 			
 			$("div#fancy_bg").corner("8px");
		
		}); });
	});
	
	// TEAM LINK
	$("#team_link").bind("click", function() {
		if ($("#content").height() != bubble_team) {
			$("#content").stop().animate({"height": bubble_team + "px"}, 800, "easeinout");
		}
		if ($("#shadow_menu").height() != shadow_team) {
			$("#shadow_menu, #shadow_menu img").stop().animate({"width": shadow_team + "px", "opacity": "1"}, 800, "easeinout");
		}
		$("#bubble_content").fadeOut("slow", function() { $("#bubble_content").load("inc/team.php").fadeIn("slow", function(){
			
			$("#julien a, #gregoire a, #yann a, /* #ulysse a , */ #clochette a").fancybox({
 				padding: '10',
 				hideOnContentClick: false,
 				frameWidth: 500,
 				frameHeight: 310,
 				overlayShow: true,
 				overlayOpacity: '0.3',
 				overlayColor: '#222',
 				enableEscapeButton: true,
 				showCloseButton: true,
 				hideOnOverlayClick: true,
 				centerOnScroll: true,
 				zoomOpacity: true
 			});
 			$("div#fancy_bg").corner("8px");

			
			// TEAM ANIMATION	
			$("#julien, #ulysse, #gregoire, #yann, #clochette").hover(function(){
				$(this).find(".animation").stop().fadeTo("slow", 0);
				$(this).find(".hover").stop().fadeTo("slow", 1);
			}, function() {
				$(this).find(".animation").stop().fadeTo("slow", 1);
				$(this).find(".hover").stop().fadeTo("slow", 0);
			});
			
	
		}); });
	});
	
	// CONTACT LINK
	$("#contact_link, .link_where").bind("click", function() {
		if ($("#content").height() != bubble_contact) {
			$("#content").stop().animate({"height": bubble_contact + "px"}, 800, "easeinout");
		}
		
		if ($("#shadow_menu").height() != shadow_contact) {
			$("#shadow_menu, #shadow_menu img").stop().animate({"width": shadow_contact + "px", "opacity": "0.9"}, 800, "easeinout");
		}
		
		$("#bubble_content").fadeOut("slow", function() { $("#bubble_content").load("inc/contact.php").fadeIn("slow", function(){
				
			if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 				var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
				if (ieversion<=7) {
					$("#map_overlay").css("display", "block");
				}
			}
			else {
				
					$("#map_overlay").fadeIn("normal", function() {
					
						// Wait a moment
						var wait=setTimeout(ready,2000);
						function ready(){
				
							$("#map_overlay").fadeOut("slow");
						
							// Stop waiting
							clearTimeout(wait);
						}
						
						// Rollover
				 		var hide = false;
						$("#map, #map_overlay, #map_link, #map_overlay_bg, .left").hover(function(){
							if (hide) clearTimeout(hide);
							$("#map_overlay").fadeIn("normal");
							$("#map, .left").css("cursor", "help");
						}, function() {
							hide = setTimeout(function() {
				 			$("#map_overlay").fadeOut("slow");
							}, 500);
						}).mousemove(function(){ clearTimeout(wait); $("#map_overlay").fadeIn("normal"); });
					});
			}

			
			$("#map_overlay_bg").corner("5px");
			
			$("#map_link").fancybox({
 				padding: '10',
 				hideOnContentClick: false,
 				frameWidth: 500,
 				frameHeight: 400,
 				overlayShow: true,
 				overlayOpacity: '0.3',
 				overlayColor: '#222',
 				enableEscapeButton: true,
 				showCloseButton: true,
 				hideOnOverlayClick: true,
 				centerOnScroll: true,
 				zoomOpacity: true
 			});
 			$("div#fancy_bg").corner("8px");

		}); });
	});


	$("#menu ul li a:not(#realisation_link)").add(".link_where").click( function () {
		$("#contact").hide();
	});
	
	// Rollover animation menu
	$("#menu ul li a").mouseover( function() {
		$(this).stop().animate({"color": "#3a3e45"}, 400);
	}).mouseout( function() {
		$(this).stop().animate({"color": "#fff"}, 400);
	}).click( function() { $("#bubble_content").empty(); });
	
	
	
	////////////// Newsletter form ////////////////
	$("#newsletter_content, #newsletter_content_unsubscribe").corner("10px");
	$('.rounded_input').corner("4px");
	
	// Submit button
	$('#newsletter_submit, #newsletter_submit_unsubscribe').mousedown(function(){
		$(this).css('background-position', '0px bottom');
	}).mousemove(function(){
		$(this).css('background-position', '0px center');
	}).mouseup(function(){
		$(this).css('background-position', '0px top');
	}).mouseleave(function(){
		$(this).css('background-position', '0px top');
	});
	
	// Display form
	$('#newsletter_link').click(function(){
		$('#newsletter_box').fadeIn();
		return false;
	});
	
	// Close form
	$('#newsletter_close_link, #newsletter_overlay, #newsletter_container').click(function(){
		$('#newsletter_box').fadeOut("slow");
		return false;
	});
	$('#newsletter_close_link_unsubscribe, #newsletter_overlay_unsubscribe, #newsletter_container_unsubscribe').click(function(){
		$('#newsletter_box_unsubscribe').fadeOut("slow");
		return false;
	});
	
	$('#newsletter_content, #newsletter_content_unsubscribe').click(function(){ return false; });
	
	// Newsletter activation
	$('#newsletter_activation').stop(true, false).css('background-color', '#F8F199').delay(1000).slideDown("slow").delay(8000).slideUp("slow");
	
	// Newsletter subscription
	$('#newsletter_submit').click(function(){
		if($('.newsletter_input.name').val() == '') {
			$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Veuillez renseigner un prénom et un nom !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
			$('.newsletter_input.name').focus();
		}
		else if($('.newsletter_input.email').val() == '') {
			$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Veuillez renseigner une adresse e-mail !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
			$('.newsletter_input.email').focus();
		}
		else if(( $('.newsletter_input.email').val().search("@") == -1) || ( $('.newsletter_input.email').val().search("[.*]" ) == -1 )) {
			$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Adresse e-mail invalide !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
			$('.newsletter_input.email').focus();
		}
		else {
		
			$.ajax({
				type: "POST", 
				url: "newsletter/subscribe_ajax.php",
				data: "email="+$('.newsletter_input.email').val()+"&name="+$('.newsletter_input.name').val(),
				success: function(data){
					if(data!='ko') {
						$('#newsletter_form_container').fadeOut("normal", function(){
							$(this).html('<div style="height:50px;"></div><div class="alert"><h2 style="font-size: 11px;">'+data+'</h2></span>').fadeIn("normal").delay(400).fadeTo("normal", 0.6).delay(400).fadeTo("normal", 1).delay(400).fadeTo("normal", 0.7).delay(400).fadeTo("normal", 1).delay(400).fadeTo("normal", 0.8).delay(400).fadeTo("normal", 1);
						});
					}
					else {
						$('.newsletter_input.email').focus();
					}
				}
			});			
			
			return false;
		}
	});
	
	// Newsletter unsubscription
	$('#newsletter_submit_unsubscribe').click(function(){
		if($('.newsletter_input_unsubscribe.email').val() == '') {
			$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Veuillez renseigner une adresse e-mail !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
			$('.newsletter_input_unsubscribe.email').focus();
		}
		else if(( $('.newsletter_input_unsubscribe.email').val().search("@") == -1) || ( $('.newsletter_input_unsubscribe.email').val().search("[.*]" ) == -1 )) {
			$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Adresse e-mail invalide !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
			$('.newsletter_input_unsubscribe.email').focus();
		}
		else {
		
			$.ajax({
				type: "POST", 
				url: "newsletter/unsubscribe_ajax.php",
				data: "email="+$('.newsletter_input_unsubscribe.email').val(),
				success: function(data){
					if(data!='ko') {
						$('#newsletter_form_container_unsubscribe').fadeOut("normal", function(){
							$(this).html('<div style="height:20px;"></div><div class="alert"><h2 style="font-size: 11px;">'+data+'</h2></span>').fadeIn("normal").delay(400).fadeTo("normal", 0.6).delay(400).fadeTo("normal", 1).delay(400).fadeTo("normal", 0.7).delay(400).fadeTo("normal", 1).delay(400).fadeTo("normal", 0.8).delay(400).fadeTo("normal", 1);
						});
					}
					else {
						$('#messenger').stop(true, false).css('background-color', '#F8F199').html('Cette adresse e-mail n\'est pas abonnée à la newsletter !').delay(500).slideDown("slow").delay(4000).slideUp("slow");
						$('.newsletter_input_unsubscribe.email').focus();
					}
				}
			});			
			
			return false;
		}
	});
	
	// Check email registered
	$('.newsletter_input.email').bind('keyup focusout', function(){
		$.ajax({
			type: "POST", 
			url: "newsletter/check_email_ajax.php",
			data: "email="+$('.newsletter_input.email').val(),
			success: function(data){
				if(data=='ok') {
					$('.check_email').text('e-mail').css('color', '#4A4E56');
					$('.newsletter_input.email').css('background-color', '#fff');
					$('.newsletter_input.email').parent('.rounded_input').css('background-color', '#fff');
				}
				else {
					$('.check_email').text('e-mail déjà enregistré').css('color', '#ff0000');
					$('.newsletter_input.email').css('background-color', '#ff7e7e');
					$('.newsletter_input.email').parent('.rounded_input').css('background-color', '#ff7e7e');
				}
			}
		});
			

	});
		
});


//tooltip
$(function(){
   $(".someClass").tipTip();
});
