$(document).ready(function() {
	var scrollingDiv;
	var elemTop = 0;
	var isIPAD = false;
	if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) isIPAD = true;
	
	$(window).resize(function(){
		$("#content .slideshow.gallery .scroller ul").attr("rel", 1);
		SlideshowChangePager($("#content .slideshow.gallery .scroller ul"));
		Resize();
	});

	/*if (isIPAD) {
		scrollingDiv = $("#menu");
		elemTop = scrollingDiv.offset().top;
		$(window).scroll(function(){
		  iPadMenuAnimate();
		});
	}*/
	
	function Resize() {
		var w = $(window).width() - 129 - 45 < 837 ? 837 : $(window).width() - 129 - 45;
		
		if ($("#content .slideshow.gallery").length > 0) {
			var h = $(window).height() - 78 - 78 - 45;
			if (h < 468) h = 468;
			var ratio = h / w;
			var ratio_img = $("#content .slideshow.gallery .big img").height() / $("#content .slideshow.gallery .big img").width();
			if (ratio_img > ratio) {
				$("#content .slideshow.gallery .big img").height(h+"px");
				$("#content .slideshow.gallery .big img").width("auto");
				//$("#content .slideshow.gallery .big img").css({"left":Math.round((w-$("#content .slideshow.gallery .big img").width())/2)+"px"});
				$("#content .slideshow.gallery .big img").css({"top":"0px"});
			} else if (ratio_img <= ratio) {
				$("#content .slideshow.gallery .big img").width(w+"px");
				$("#content .slideshow.gallery .big img").height("auto");
				//$("#content .slideshow.gallery .big img").css({"top":Math.round((h-$("#content .slideshow.gallery .big img").height())/2)+"px"});
				$("#content .slideshow.gallery .big img").css({"left":"0px"});
			}
			
			$("#content .slideshow.gallery .big .loader").width(w+"px");
			$("#content .slideshow.gallery .big .loader").height(h+"px");
			
			var maxW = $("#content .slideshow.gallery .scroll").width() - 90;
			$("#content .slideshow.gallery .scroller").css({"width":(maxW)+"px"});

			var maxPage = Math.ceil($("#content .slideshow.gallery .scroller ul").width()/$("#content .slideshow.gallery .scroll").width());
			$("#content .slideshow.gallery .scroller ul").attr("max", maxPage);
			
			SlideshowChangePager($("#content .slideshow.gallery .scroller ul"));
		} else if ($("#content .slideshow").length > 0) {
			//$("#content .slideshow .big .loader").height($("#content .slideshow .big .loader").height()+"px");
		} else {
			$("#content .content.photo img").width((w-256)+"px");
			$("#content .content.photo .loader").width((w-256)+"px");
		}
	}
	
	function iPadMenuAnimate() {
		scrollingDiv.stop();
		if ($(window).scrollTop() > elemTop) scrollingDiv.animate({ "marginTop": ($(window).scrollTop() - elemTop) + "px" }, "fast");
		else scrollingDiv.animate({ "marginTop": "0px" }, "fast");
	}
	
	// LOADING IMG
	$("img.load").each(function(){
		$(this).fadeOut(0, 0).attr("rel", $(this).attr("src")).attr("src", "").load(function(){
			var loader = $(this).parent();
			loader.addClass("off");		
			$(this).fadeTo("slow", 1, function(){
				if (!$(this).is(".done")) $(this).closest("li").find(".over:not(.image)").css({height:($(this).closest("li").height()-2)+"px"});
			});
		}).attr("src", $(this).attr("rel"));
	});
	
	// NEWS FIX OVER HEIGHT
	$("#nav li").each(function(){
		$(this).find(".over:not(.image)").css({height:($(this).height()-2)+"px"});
	});
	
	// NEWS FIX TEXT HEIGHT
	var bestCol = 0;
	$("#content .inner .col2:not(.contact)").each(function(){
		if ($(this).height() > bestCol) bestCol = $(this).height();
	});
	if ($("#content .inner .col1:not(.contact)").height() > bestCol) bestCol = $("#content .inner .col1:not(.contact)").height();
	$("#content .inner .col1:not(.contact), #content .inner .col2:not(.contact)").height(bestCol+"px");
	$("#content .inner.bio .col2").height((bestCol-2)+"px");

	// CONTACT
	$("#content .inner .contactline").each(function(){
		if ($(this).find(".col2").height() > $(this).find(".col1").height()) $(this).find(".col1").height(($(this).find(".col2").height() - 169)+"px");
		else $(this).find(".col2").height($(this).find(".col1").height()+"px");
	});
	
	// SLIDESHOW
	$(".scroller ul").each(function(){
		$(this).attr("rel", 1);
		$(this).attr("max", Math.ceil($(this).find("li").length/6));
		$(this).find("li:eq(0) a").addClass("current");
		var total = 0;
		$(this).find("li").each(function(){
			total += $(this).width() + 1;
			$(this).find(".over").width(($(this).width()-2) + "px");
		});
		$(this).width((total-1) + "px");
		$(this).find("a").click(function(){
			$("div.gallery-more").removeClass("more");
			$("a.more-infos").removeClass("active");
			$("a.more-infos").html("More infos");
			SlideshowChange($(this));
			return false;
		});
		
		var slideshow = $(this).closest(".slideshow");
		SlideshowChangePager($(this));
		
		if ($(this).find("li").length <= 6) {
			slideshow.find("a.prev").addClass("off");
			slideshow.find("a.next").addClass("off");
		}
		
		slideshow.find("a.next").click(function(){
			if (!$(this).is(".off")) {
				var ul = $(this).closest(".scroll").find("ul");
				var page = Number(ul.attr("rel")) + 1;
				ul.attr("rel", page);
				SlideshowChangePager(ul);
			}
			return false;
		});
		slideshow.find("a.prev").click(function(){
			if (!$(this).is(".off")) {
				var ul = $(this).closest(".scroll").find("ul");
				var page = Number(ul.attr("rel")) - 1;
				ul.attr("rel", page);
				SlideshowChangePager(ul);
			}
			return false;
		});
		
		if (!$("div.gallery-more").is(".more")) SlideshowChange($(this).find("li:first-child a"), true);
	});
	function SlideshowChange(target, force) {
		if (force || !target.is(".current")) {
			var slideshow = target.closest(".slideshow");
			slideshow.find("ul li a").removeClass("current");
			target.addClass("current");
			$("#photo-legend").html(target.parent().find("span.hide").html());
		
			slideshow.find("div.big .loader").removeClass("off");
			slideshow.find("div.big img").fadeTo(0, 0).load(function(){
				Resize();
				var loader = $(this).parent();
				loader.addClass("off");
				//$(this).closest(".big").animate({"height":$(this).height()+"px"}, {queue:false, duration:500, easing:"easeOutQuart"}, function(){
					//$(this).find("img").css({"opacity":"0.1"});
					$(this).find("img").fadeTo("slow", 1);
				//});
			});
			slideshow.find("div.big img").attr("src", target.attr("rel"));
		}
	}
	function SlideshowChangePager(target) {
		var slideshow = target.closest(".slideshow");
		target.animate({"margin-left":-((slideshow.find(".scroller").width()+1)*(Number(target.attr("rel"))-1))+"px"}, {queue:false, duration:500, easing:"easeOutQuart"});
		
		if (target.attr("rel") == "1") slideshow.find("a.prev").addClass("off");
		else slideshow.find("a.prev").removeClass("off");
		
		if (target.attr("rel") == target.attr("max")) slideshow.find("a.next").addClass("off");
		else slideshow.find("a.next").removeClass("off");
	}
	
	// BIO
	var bestImg = 0;
	var nbBioImg = 0;
	$("#content .inner.bio .col2 img").hide().load(function(){
		nbBioImg++;
		if (this.height > bestImg) bestImg = $(this).height();
		if ($("#content .inner.bio .im").length == nbBioImg) {
			$("#content .inner.bio .col2").animate({ "height": ($("#content .inner.bio .col2").height()+bestImg) + "px" }, "fast", function(){
				$("#content .inner.bio .col2 .im").height(bestImg+"px");
				$("#content .inner.bio .col2 img").fadeIn("slow");
			});
		}
	});

	var pageBio = 1;
	var nbPageBio = $(".inner.bio .col1 a").length;
	UpdatePagerBio();
	
	$(".inner.bio .bio").width(($(".inner.bio .bio .col2").length * 582) + "px");
	$(".inner.bio .col1 a").click(function(){
		if ($(this).attr("rel") != pageBio) {
			pageBio = $(this).attr("rel");
			UpdatePageBio();
		}
		return false;
	});
	$("#nav.bio .cmd a.prev").click(function(){
		if (!$(this).is(".off")) {
			pageBio--;
			UpdatePageBio();
		}
		return false;
	});
	$("#nav.bio .cmd a.next").click(function(){
		if (!$(this).is(".off")) {
			pageBio++;
			UpdatePageBio();
		}
		return false;
	});
	$(".scroller-bio .bio .col2").click(function(){
		if (!$(this).is(".active")) {
			pageBio = $(this).index() + 1;
			UpdatePageBio();
		}
		return false;
	});
	
	function UpdatePageBio() {
		$(".scroller-bio .bio").animate({"margin-left":-(582*(pageBio-1))+"px"}, {queue:false, duration:500, easing:"easeOutQuart"});
		UpdatePagerBio();
	}
	function UpdatePagerBio() {
		$(".scroller-bio .bio .col2").removeClass("active");
		$(".scroller-bio .bio .col2:eq("+(pageBio-1)+")").addClass("active");
		
		$(".inner.bio .col1 a").removeClass("active");
		$(".inner.bio .col1 a[rel="+pageBio+"]").addClass("active");
		
		if (pageBio <= 1) $("#nav.bio .cmd a.prev").addClass("off");
		else $("#nav.bio .cmd a.prev").removeClass("off");
		
		if (pageBio >= nbPageBio) $("#nav.bio .cmd a.next").addClass("off");
		else $("#nav.bio .cmd a.next").removeClass("off");
	}
	
	// PRESS
	$("#press-view").fadeTo(0, 0);
	$("ul.press a").click(function(){
		var sHtml = '<h1 class="cW">'+$(this).find(".title").html()+'</h1><h2 class="cV">'+$(this).find(".subtitle").html()+'</h2>';
		$(this).find("span.list span").each(function(){
			sHtml += '<div class="loader"><img class="load" src="" rel="'+$(this).html()+'" width="'+$(this).attr("w")+'px" height="'+$(this).attr("h")+'px" /></div>';
		});
		$("#press-view").addClass("open").fadeTo("fast", 1);
		$("#press-view .inner").html(sHtml);
		$("#press-view img.load").each(function(){
			$(this).fadeTo(0, 0).attr("src", $(this).attr("rel")).load(function(){
				var loader = $(this).parent();
				loader.addClass("off");		
				$(this).fadeTo("slow", 1);
			});
		});
		return false;
	});
	$("#press-view").click(function(){
		$(this).fadeTo("fast", 0, function(){
			$(this).find(".inner").html("");
			$(this).removeClass("open")
		});
		return false;
	});
	$("#press-view").mousemove(function(e){
		$("#press-close").animate({top:(e.pageY-30)+"px", left:(e.pageX-25)+"px"}, {queue:false, duration:80, easing:"easeOutQuart"});
	});
	
	Resize();

	// GALLERY
	$("a.more-infos").click(function(){
		if ($("div.gallery-more").is(".more")) {
			$("div.gallery-more").removeClass("more");
			//$(this).removeClass("active");
			$(this).html("More infos");
			$("div.big img").fadeIn("fast");
		} else {
			$("div.gallery-more").addClass("more");
			
			if (!$("#content .inner").is(".fixed")) {
				$("#content .inner").addClass("fixed");
				$("#content .inner .col1, #content .inner .col2").height("auto");
				var bestCol = $("#content .inner .col2").height();
				if ($("#content .inner .col1").height() > bestCol) bestCol = $("#content .inner .col1").height();
				$("#content .inner .col1, #content .inner .col2").height(bestCol+"px");
			}

			$(this).html("Close");
			$("div.big img").fadeOut("fast");
		}
		return false;
	});
	if ($("div.gallery-more").is(".more")) {
		$("a.more-infos").html("Close");
	}
});
