// Copyright Benoit Villière, 2009

$( function() {
	
	$.fn.alert = function (message) { alert(message); return this; };
	
	// Init
	$("#boutons_slider, #bouton_more_logos_wrap").removeClass("js_only");

	// 
	$("#bouton_realisations").click( function () { $.scrollTo("1000px", 1000); });
	
	// Contact AJAX form
	var form = $("#contact_interior").html();
	
	$("#send").click(
		function () { 
			$("#ajax_loader").removeClass("hidden");
			
			var credentials = $("input#credentials").val();
			var email = $("input#email").val();
			var message = $("textarea#message").val();
			
			$("#contact p").each( function () { $(this).remove(); });
						
			$.ajax({
				type: 'post',
				url: 'mailer.php',
				data: 'credentials=' + credentials + '&email=' + email + '&message=' + message,
	
				success: function(results) {
					$("#ajax_loader").remove();
					$("#reponse").html(results);
					$("#reponse").animate({ opacity : 1 }, 3000);
					$("#reponse").animate({ opacity : 0 }, 300);
					setTimeout( function() { $("#contact").append(form); }, 3350);
				}
			});
			
		}
	);
		
	// Logos
	$("div.logo").animate({ opacity : 0.3 }, 0);
	$("div.logo").hover( 
		function () { $(this).stop().animate({ opacity : 1 }, 500) },
		function () { $(this).stop().animate({ opacity : 0.3 }, 800) }	
	);	
	
	//$(".logo_first_nest").addClass("visible");
	//$(".logo_second_nest, .logo_third_nest").addClass("hidden");
	$(".logo_second_nest, .logo_third_nest").hide();
	//$(".logo_second_nest, .logo_third_nest").animate({ opacity : 0 }, 0);
	
	$("#bouton_more_logos").click(
		function () {
			if ( $(".logo_first_nest").is(":visible") )
			{
				$(".logo_first_nest")
					.stop()
					.animate({ opacity : 0 }, 800)
					.hide();
				$(".logo_second_nest")
					.stop()
					.show()
					.animate({ opacity : 1 }, 600);
				return;
			}
			if ( $(".logo_second_nest").is(":visible") )
			{ 
				$(".logo_second_nest")
					.stop()
					.animate({ opacity : 0 }, 800)
					.hide();
				$(".logo_third_nest")
					.stop()
					.show()
					.animate({ opacity : 1 }, 600);
				return;
			}
			if ( $(".logo_third_nest").is(":visible") )
			{ 
				$(".logo_third_nest")
					.stop()
					.animate({ opacity : 0 }, 800)
					.hide();
				$(".logo_first_nest")
					.stop()
					.show()
					.animate({ opacity : 1 }, 600);
				return;
			}
		}, 
		function () {
			
		}
	);
/*
	$("#bouton_more_logos").click(
		function () {
			if ( $(".logo_first_nest").is(":visible") )
			{
				$(".logo_first_nest")
					.stop()
					.animate({ marginLeft : "200px", opacity : 0 }, 800)
					.hide();
				$(".logo_second_nest")
					.stop()
					.show()
					.animate({ marginLeft : "0px", opacity : 1 }, 600);
				return;
			}
			if ( $(".logo_second_nest").is(":visible") )
			{ 
				$(".logo_second_nest")
					.stop()
					.animate({ marginLeft : "200px", opacity : 0 }, 800)
					.hide();
				$(".logo_third_nest")
					.stop()
					.show()
					.animate({ marginLeft : "0px", opacity : 1 }, 600);
				return;
			}
			if ( $(".logo_third_nest").is(":visible") )
			{ 
				$(".logo_third_nest")
					.stop()
					.animate({ marginLeft : "200px", opacity : 0 }, 800)
					.hide();
				$(".logo_first_nest")
					.stop()
					.show()
					.animate({ marginLeft : "0px", opacity : 1 }, 600);
				return;
			}
		}, 
		function () {
			
		}
	);
*/

// Constantes
	var low_opacity = 0.1;
	var very_slow = 800;
	
	// Fun 
	$("body").fadeOut(0).fadeIn(very_slow);
	
	// Competences
	$.fn.descend = function (vitesse) {
		return $(this).stop().animate({ opacity : 1, marginTop: "12px" }, vitesse);
	}
	
	$("#grandes_icones ul > li").animate({ opacity : low_opacity });
	$("#creative_suite, #pour_cs").hover( 
		function () 
		{ 
			$("#icon_big_ai").descend(1100);
			$("#icon_big_fl").descend(900);
			$("#icon_big_ps").descend(600);
			$("#icon_big_dw").descend(300);
		}, 
		function () 
		{
			$("#icon_big_ai").stop().animate({ 
				opacity : low_opacity,
				marginTop : "0px"
			}, 600, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_fl").stop().animate({ 
				opacity : low_opacity,
				marginTop : "0px"
			}, 500, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_ps").stop().animate({ 
				opacity : low_opacity,
				marginTop : "0px"
			}, 400, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_dw").stop().animate({ 
				opacity : low_opacity,
				marginTop : "0px"
			}, 300, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
		}
	);
	
	$("#dev, #pour_dev").hover( 
		function () 
		{ 
			$("#icon_big_php").descend(1100);
			$("#icon_big_mysql").descend(900);
			$("#icon_big_ci").descend(600);
			$("#icon_big_fb").descend(300);
		}, 
		function () 
		{
			$("#icon_big_php").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 600, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_mysql").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 500, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_ci").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 400, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_fb").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 300, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
		}
	);
	
	$("#cms, #pour_cms").hover( 
		function () 
		{ 
			$("#icon_big_wp").descend(900);
			$("#icon_big_drupal").descend(600);
			$("#icon_big_cmsms").descend(300);
		}, 
		function () 
		{
			$("#icon_big_wp").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 500, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_drupal").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 400, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_cmsms").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 300, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
		}
	);
	
	$("#html, #pour_html").hover( 
		function () 
		{ 
			$("#icon_big_jquery").descend(1100);
			$("#icon_big_ui").descend(900);
			$("#icon_big_ie").descend(600);
			$("#icon_big_ff").descend(300);
		}, 
		function () 
		{
			$("#icon_big_jquery").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 600, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_ui").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 500, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_ie").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 400, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
			$("#icon_big_ff").stop().animate({ 
				opacity : low_opacity,
				marginTop: "0px"
			}, 300, 
				function() {
					$(this).animate({opacity : low_opacity}, very_slow);
				}
			);
		}
	);
			
	// Slider
	$items = $("#items");
	$first_item = $("#items .item:eq(0)");
	$last_item = $("#items .item").filter(":last"); // $last_item.is("div#maroctechjobs.item") == true
	nbre_items = $(".item").size();
	
	$.fn.disparait = function () {
		return $(this).stop()
			.animate({ opacity : 0 }, 600)
			.removeClass("current")
			.addClass("hidden");
	}
	
	$.fn.apparait = function () {
		return $(this).stop()
			.animate({ opacity : 1 }, 600)
			.addClass("current")
			.removeClass("hidden");
	}
	
	$("#items .item").each( function () { $(this).addClass("hidden") });
	
	$first_item
		.removeClass("hidden")
		.addClass("current");
	
	$(".previous").addClass("inactive");
	
	$(".previous").hover(
		function () { $(this).addClass("hovering") },
		function () { $(this).removeClass("hovering") }
	);
	$(".next").hover(
		function () { $(this).addClass("hovering") },
		function () { $(this).removeClass("hovering") }
	);
		
	$(".next").click(
		function () { 

			$(".previous").removeClass("inactive");
			
			if( $last_item.hasClass("current") ) { $(this).stop().addClass("inactive") }

			if ( ! $(this).hasClass("inactive") )
			{
				if( $first_item.hasClass("current") )
				{		
					$first_item
						.disparait()
							.next(".item")
								.apparait();

				} else {
											
					$items
						.find(".current")
						.disparait()
							.next(".item")
								.apparait();
								
					if( $last_item.hasClass("current") ) { $(this).stop().addClass("inactive") }
				}
			}
		});
	
	
	$(".previous").click(
		function () { 	
		
			if( ! $(this).hasClass("inactive") ) // $first_item.hasClass("current") )
			{
				$items
					.find(".current")
					.disparait()
						.prev(".item")
							.apparait();
				if( $first_item.hasClass("current") ) { $(this).stop().addClass("inactive") }
			}
							
			if( ! $first_item.hasClass("hidden") || ! $first_item.hasClass("current") ){
				$(".next").removeClass("inactive");
			}
	});	

});
