$(window).load(function(){
	
	/* video js */
	VideoJS.setupAllWhenReady();
	
	
	//ipad detection
	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	

	//////////////////////////////////////////////////////
	/* FOOTER AND GOTOTOP */
	$('#footer').hide();
	
	//back to top
	$(".gotop").click(function(){
		$( 'html, body' ).animate( { scrollTop: 0 }, 'normal', 'easeOutExpo' );
	});
	
	// show / hide footer if scrolling
	$(window).scroll(function() {		
		if($(window).height() >= $(document).height()) $('#footer').hide();
		else $('#footer').show();	
	});
	/**/
	
	
	//	
	$("#index").animate({opacity:1},1000);
	$("#thumbs").animate({opacity:1},1000);
	$("#project").animate({opacity:1},1000);
	$("#contact").animate({opacity:1},1000);
	
	//////////////////////////////////////////////////////
	/* INDEX */
	$("a.index").click(function(event){
		event.preventDefault();
		linkLocation = this.href;
		$("#index").fadeOut(500, redirectToPage);		
	});
	/* INDEX END */
	
	//////////////////////////////////////////////////////
	/* COOKIE LANGUAGE */
	var language = window.navigator.userLanguage || window.navigator.language;
	var lang = language.substr(0,2);
	//check if language cookie exist
	if($.cookie("langue") == null){
		//if not, set it depend on browser language
		$.cookie("langue", lang, {path: '/', expires:365});
	}else{
		//load it
		var langue = $.cookie("langue");
	}
	//setter
	$(".setfr").click(function(){
		$.cookie("langue", "fr", {path: '/', expires:365});
		location.reload();
	});
	$(".seten").click(function(){
		$.cookie("langue", "en", {path: '/', expires:365});
		location.reload();
	});
	//getter
	$("#getlang").click(function(){
		alert($.cookie("langue"));
	});
	/* COOKIE LANGUAGE END */
	
	
	//////////////////////////////////////////////////////
	//add background where its needed!
	if ((window.location.pathname.split("/")[2] == "contact") ||
	(((window.location.pathname.split("/")[1] == "jocelyn") || (window.location.pathname.split("/")[1] == "guillaume")) && (window.location.pathname.split("/")[2] == "")))
	{
		$("body").css({'background': 'url(/img/bg'+ langue +'.png) center top repeat-y'});
	}


	//////////////////////////////////////////////////////
	/* THUMBS */
		
	//thumbnails total width calculation
	$("ul#projets li ul").each(function() {
		var width = 0;
		$(this).children("li").each(function() {
		 width += $(this).outerWidth();
		 //alert(width);
		});
		$(this).css("width", width+"px");
		if(!isiPad){
			//display none les UL
			$(this).css("display", "none");
		}
		
	});
	//thumbnails fadein 
	$("ul#projets li").hover(function(){ 
		$(this).children("ul").stop().fadeTo(800, 1);
	}, function() { 
		$(this).children("ul").stop().fadeTo(300, 0, function(){
			$(this).hide();
		}); 
	});
	
	// Thumbs Rollover
	$("ul#projets a").hover(function(){
		$(this).fadeTo(300, 0.8);
	},function(){
		$(this).fadeTo(300, 1);
	});	
	
	//fade content on click
	$("ul#projets a").click(function(event){
		event.preventDefault();
		linkLocation = this.href;
		$("body").css({'background':'none'});
		$("#footer").fadeOut(500);
		$("#thumbs").fadeOut(500, redirectToPage);		
	});
	
	/* THUMBS END */
	
	
	
	/* PROJECT */	
	var index = 0, hash = window.location.hash;
	if (hash) {
		index = /\d+/.exec(hash)[0];
		index = (parseInt(index) || 1) - 1; // slides are zero-based
	}


	$("#images").cycle({
			startingSlide: index, //requested image
			fx: 'scrollHorz',
			speed: 800, 
			timeout:  0,
			easing: 'easeOutExpo' ,
			prev: '#nav a.prev',
			next: '#nav a.next',
			sync:1,
			after: function(curr,next,opts) {
				window.location.hash = opts.currSlide + 1;
			}
		});
	
	//fade content on close
	$("#nav a.close").click(function(event){
		event.preventDefault();
		linkLocation = this.href;
		$("#project").fadeOut(500, redirectToPage);		
	});
	
	/* PROJECT END */

	
	
});

function redirectToPage() {
	window.location = linkLocation;
}

