$(document).ready(function() {
	$(".show-bio").click(function(){
		var who = $(this).attr("rel");
		$("div.bio."+who).fadeIn();
		return false;
	});
	$(".bio a.close").click(function(){
		$(this).parent().fadeOut();
		return false;
	});

	$("div#entry .slider").each(function(){
		var itemCount = $(this).find("ul li").length;
		var $items = $(this).find("ul");
		var step = 195;
		var maxLeft = 0 - (itemCount - 3) * step;
		var pos = 0;

		var $back = $("div#entry p.slider-controls").find("a.back");
		var $forward = $("div#entry p.slider-controls").find("a.forward");

		$back.click(function(){
			if(pos < 0) pos += step;
			else pos = maxLeft;
			$items.stop(true, true).animate({left: pos}, 500);
			return false;
		});
		$forward.click(function(){
			if(pos > maxLeft) pos -= step;
			else pos = 0;
			$items.stop(true, true).animate({left: pos}, 500);
			return false;
		});

	});

	$("#PrestationsCoachingdeco").css({'display' : 'none'});
	$(".linkCoaching").click(function(){
		$("#PrestationsHomestaging").css({'display' : 'none'});
		$("#PrestationsCoachingdeco").fadeIn("slow");
		return false;
	});
	$(".linkDeco").click(function(){
		$("#PrestationsHomestaging").fadeIn("slow");
		$("#PrestationsCoachingdeco").css({'display' : 'none'});
		return false;
	});

	$("button").click(function(){
		var bad = false;
		$("input.required").each(function(){
			if($(this).val()==""){
				bad = true;
			}
		});
		if(bad){
			$("div#alerte").fadeIn("slow");
			return false;
		}
		else{
			$("form#contact").submit();
		}
	});


	//loading stuff to the project showcase
	$(".secret").each(function(){
		var id = $(this).find("span.id").html();
		var img = new Image;
		img.src = "photos/" + id + ".jpg";
	});
	var photoCount;
	var showcasePos = 0;
	$(".show-project").click(function(){
		var $stuff = $("div."+$(this).attr("rel"));
		var projectTitle =  $stuff.find("h4").html();
		$(".showcase").find("h2").html(projectTitle);

		photoCount = $stuff.find("p").length;
		showcasePos = 0;
		for(i = 0; i < photoCount; i++){
			var $data = $stuff.find("p").eq(i);
			var item = '<p class="img"><img src="photos/'+ $data.find(".id").html() +'.jpg" alt="" /></p>';
			item += '<p><strong>'+$data.find(".title").html()+'</strong></p><p>'+$data.find(".descr").html()+'</p>';
			$(".showcase .slider").find("ul").append('<li>'+item+'</li>');
		}
		if(photoCount == 1) $(".showcase p.slider-controls").hide();
		else $(".showcase p.slider-controls").show();
		$(".showcase").fadeIn();
		return false;
	});


	$(".showcase .slider").each(function(){
		var $items = $(this).find("ul");
		var step = 800;

		var $back = $(".showcase p.slider-controls").find("a.back");
		var $forward = $(".showcase p.slider-controls").find("a.forward");

		$back.click(function(){
			var maxLeft = 0 - (photoCount - 1) * step;
			if(showcasePos < 0) showcasePos += step;
			else showcasePos = maxLeft;
			$items.stop(true, true).animate({left: showcasePos}, 200);
			return false;
		});
		$forward.click(function(){
			var maxLeft = 0 - (photoCount - 1) * step;
			if(showcasePos > maxLeft) showcasePos -= step;
			else showcasePos = 0;
			$items.stop(true, true).animate({left: showcasePos}, 200);
			return false;
		});

	});

	$(".showcase a.close").click(function(){
		$(".showcase").fadeOut(500);
		$(".showcase ul, .showcase h2").html("");
		return false;
	});


	$("a.photo-th").click(function(){
		var path = $(this).attr("href");
		$(".showcase").html("");
		$(".showcase").append('<a href="#" class="close">X</a>').append('<img src="'+path+'" alt="">');
		$(".showcase").fadeIn();
		$(".showcase a.close").click(function(){
			$(".showcase").fadeOut(500);
		});
		return false;
	});

});
