/**
 * @author florent@ezoulou.be 20092010 - feel free to use
 */
// globals variables
var hl = 825; 							// height limits : after that, we display 3 rows!!
var hl2 = 1025; 						// height limits : after that, we display 3 rows!!
var ns = null;							// new size : resized images if window allow us to do so
/*
 * main function that displays each page bits by bits
 */
$(document).ready(function(){ 
	// we declare the javascript activated!! party on! :-)
 	$("body").addClass("jsOn");
	//we addapt the size of lightbox image so it is as big as possible. 
	lightboxAdaptLargeImageSize();	
	// case of the intro page
	if ($('#introduction-page').length > 0) activatePageIntroduction();
	else {
		// homepage, accueil with 6 blocks
		if ($('#accueil-page').length > 0) activatePageHome();
		else {
			// case of artists and editions first level pages : the list of artists
			if ($('body .slider').length > 0) activateSlider(); 
			else {
				// case of the gallery : we don't hide/show content because it would cause problems with javascript height detection
				if ($('.ezoulouGallery').length > 0) activateEzoulouGallery();
				else {
					// default page animations
					// hide everything	
					$("#utils, #title, #footer, #content").hide();
					// show everything bits by bits
//					$("#header").fadeIn(function(){
						$("#utils").show();
						$("#title").slideDown(function(){
							$("#content").fadeIn(function(){
								$("#footer").fadeIn();
							});
						});
//					});
				}
			}
		}
	}
});

function lightboxAdaptLargeImageSize() {
	// 	if window is tall enough, we change src of lightbox images so it adapts to large screens
	ns = 620; // default height of the large dynimage produced by the system
	if ($(window).height() >= hl) {
		ns = 800;
		if ($(window).height() >= hl2) {
			ns = 1000;
		}
	}
	// change src of lightbox image
	for (var i = 0; i < $('a.lightbox:not(.video)').length; i++) {
		// lightboxImageSouce 	: /dynimage/620/files/images/photo.jpg : for normal fckeditor text lightbox link  
		// 					or  : /artistes/residents/patrick-chappert-gaujal/tableaux/chappert-rouge.jpg : lightbox link from gallery 
		var lightboxImageSouce = $('a.lightbox:not(.video):eq(' + i + ')').attr('href');
		if (lightboxImageSouce.indexOf('.com')>0) lightboxImageSouce = lightboxImageSouce.substring(lightboxImageSouce.indexOf('.com')+4);



//	console.log('lightboxImageSouce : '+lightboxImageSouce);



	    if ($('a.lightbox:not(.video):eq(' + i + ')').parent().hasClass('video')) {
	      lightboxImageSouce = '/files/_galleries'+lightboxImageSouce; 
	      $('a.lightbox:not(.video):eq(' + i + ')').attr('href', lightboxImageSouce);
	    } else {
			if (lightboxImageSouce.indexOf('/dynimage/') == 0) {
				lightboxImageSouce = lightboxImageSouce.substring(lightboxImageSouce.indexOf('/dynimage/')+11);
				lightboxImageSouce = lightboxImageSouce.substring(lightboxImageSouce.indexOf('/'));
			} else {
				lightboxImageSouce = '/files/_galleries'+lightboxImageSouce; 
			}
			// place new source for current lightbox link
			$('a.lightbox:not(.video):eq(' + i + ')').attr('href', '/dynimage/'+ns+lightboxImageSouce);
		}
	}
}

function clearColumnsAndSlider() {
	$('body .columns5 li').removeClass('active');
	$('body .slider a').removeClass("active");	
}

function activatePageIntroduction() {
	// hide everything
	$("#content img").css({
		marginTop: "-620px"
	});
	$("#header, #content address, #content .block, #content .block p").hide();
	// show everything bits by bits
	$("#content img").animate({
		marginTop: "0px"
	}, 600, function(){
		$("#header").fadeIn(function(){
			$("#content address").fadeIn(function(){
				$("#content .block").fadeIn(function(){
					$("#content .block p").fadeIn();
				});
			});
		});
	});
	// events
	$("#content .block:has(a)").mouseenter(function(){
		$(this).addClass("active");
	});
	$("#content .block:has(a)").mouseleave(function(){
		$(this).removeClass("active");
	});
	$("#content .block:has(a)").css({
		cursor: "pointer"
	});
	$("#content .block:has(a)").click(function(){
		document.location = $(this).find("a").attr("href");
	});
}

function activatePageHome() {
	// hide everything						
	$("#header, #utils, #footer, .block").hide();
	// show everything bits by bits
			$(".block.expositions").fadeIn(function(){
	$(".block.artistes").fadeIn(function(){
		$(".block.editions").fadeIn(function(){
				$(".block.services").fadeIn(function(){
					$(".block.newsletter").fadeIn(function(){
						$(".block.contact").fadeIn(function(){
							$('#header').fadeIn(function(){
								$('#utils').show();
								$('#footer').fadeIn();
							});
						});
					});
				});
			});
		});
	});
	// events
	$("#content .block").mouseenter(function(){
		$(this).addClass("active");
	});
	$("#content .block").mouseleave(function(){
		$(this).removeClass("active");
	});
	$("#content .block:not(.expositions):has('h1 a')").css({
		cursor: "pointer"
	}).attr("title", function(arr){
		return $(this).find(".button").attr("title");
	}).click(function(){
		document.location = $(this).find("h1 a").attr("href");
	});
}

function activateSlider () {
	// hide everything	
	$("#utils, #title, #footer, body .slider, body .columns5").hide();
	// show everything bits by bits			
	$("body .slider").fadeIn(function(){
		// activate referrer
// bugs on ie...
//					activateReferer();
//		$("#header").fadeIn(function(){
			$("#utils").show();
			$("#title").slideDown(function(){
				$("body .columns5").slideDown(function(){
					$("#footer").fadeIn();
				});
			});
//		});
	});
	/* 
	 * we coordinate the 2 navigations mechanisms (slider & the five columns)
	 */
	if ($('body .columns5').length > 0) {
		$('body .slider a').mouseenter(function(){
			var referenceId = $(this).attr('id').substring(5);
			// clear navigation mechanism
			clearColumnsAndSlider();
			// activate correspondant columns link
			$('body .columns5 #link' + referenceId).addClass('active');
		});
		$('body .slider a').mouseleave(clearColumnsAndSlider);
		$("body .columns5 li").mouseenter(function(){
			var referenceId = $(this).attr('id').substring(4);
			// clear navigation mechanism
			clearColumnsAndSlider();
			// pauses slider autoscroll
			clearInterval($mom.autoScrollInterval);
			// activate the correspondant image
			$('body .slider #image' + referenceId).addClass('active');
			/*
			 * slider position
			 */
			// size of one image
			var imageSize = $('body .slider .image').width();
			// calculate the numeric position (relative to its siblings) of active image
			var imagePosition = $('body .slider .scrollableArea').children('a').index($('body .slider #image' + referenceId));
			// place slider to the center of screen
			$('body .slider .scrollWrapper').scrollLeft((imagePosition+1) * imageSize - $(window).width() / 2 + imageSize / 2);
		});
		$("body .columns5 a").mouseleave(clearColumnsAndSlider);
	}
}



function activateEzoulouGallery () {
	// hide comon stuff	
	$("#utils, #title, #footer, #gallery .filters").hide();
	// if there is a paging system, we hide its bits				
	if ($('#gallery .page').length > 0) {
		$('#gallery .page:eq(0), #gallery .pager, #gallery .navigation').hide();
	}
	else {
		$('#gallery .images').hide();
	}
	// show everything bits by bits
//	$("#header").fadeIn(function(){
		$("#utils").show();
		$("#title").slideDown(function(){
			// if we have a pagin mechanism
			if ($('#gallery .page').length > 0) {
				// show first page
				updatePage();
				// timer detecting changes in url for the anchor navigation, navigation with browser next/previous buttons
	 			setInterval(updatePage, 500);
				// then we show the rest
				$("#gallery .filters, #gallery .pager, #gallery .navigation").fadeIn(function () {
					$("#footer").fadeIn();
				});
			}
			// else, we simply show the images
			else {
				$('#gallery .images, #gallery .filters').fadeIn(function () {
					$("#footer").fadeIn();
				});
			}							
		});
//	});
}
