$(window).load(init);
Shadowbox.init();

var navHeight = 0;
var subNavHeight = 0;
var infoHeight = 0;
var footerTop = 0;

function init(){
	initSlideShows();
	initVideos();
	initFoldBlocs();
	initHome();
	initHomeCine();
	initImgList();
	
	//initLab();
	
	$("a").each(function(){
		$(this).focusin(function(){$(this).blur()});
	});
	
	$("#nav .info .top").click(function(e){
		e.preventDefault();
		$('html, body').animate({scrollTop:0}, 'slow');
	});
	
	$(".print").click(function(){
		print();
	});
	
	
	
	var header = $("#header.event");
	if(header.length > 0){
		var offset = 0;
		offset = Math.max(header.find(".col3").height()-10, offset);
		offset = Math.max(header.find(".col2").height(), offset);
		if(offset > 200) header.height(offset+70);
	}
	else{
		header = $("#header");
		if(header.find("h2").height() > 100) header.find("h5").addClass('smalltop'); 
	}
	
	navHeight = $('#nav ul').height() + $('#nav ul').offset().top;
	infoHeight = $('#nav .info').height()+30;
	footerTop = $("#footer").offset().top;
	var sn = $('.subnav');
	if(sn.length > 0){
		subNavHeight = sn.position().top + sn.height();
		if(sn.offset().top+sn.height()+48 >= footerTop) subNavHeight = 0;
	}	
	
	$(window).resize(resizeHandler);
	$(window).scroll(resizeHandler);
	resizeHandler();
	
	$(".home .news").click(function(){
		$(this).slideUp(400);
	});
	
	m = $("#currentM");
	if(m.length > 0){
		$('html, body').animate({scrollTop:m.offset().top}, 'slow');
	}
	
	m = $(".currentM");
	if(m.length > 0){
		var h = 100000;
		m.each(function(i){
			if(h>$(this).offset().top) h = $(this).offset().top;
		});
		if(h < 100000) $('html, body').animate({scrollTop:h}, 'slow');
	}
	
	m = $(".news");
	if(m.length > 0){
		if(m.find("p").height() > 16) m.addClass('double');
	}
}


function resizeHandler(e){

	footerTop = $("#footer").offset().top;

	var scrollTop = $(window).scrollTop();
	var i = $('#nav .info');
	if(i.hasClass('fixed')){
		if(navHeight>i.position().top) i.removeClass('fixed');
	}
	else{
		if(navHeight+infoHeight-scrollTop<$(window).height()) i.addClass('fixed');
	}
	
	var sn = $('.subnav');
	if(sn.length > 0){
		if(scrollTop > 254 && subNavHeight > 0){
			if(footerTop-scrollTop < subNavHeight + 26){
				sn.addClass('bottom');
				sn.removeClass('fixed');
			}	
			else{
				sn.addClass('fixed');
				sn.removeClass('bottom');
			}
		}
		else{
			sn.removeClass('bottom');
			sn.removeClass('fixed');
		}
	}
	
	/*if(scrollTop > 254){
		$('#nav').addClass('fixed');
	}
	else{
		$('#nav').removeClass('fixed');
	}*/
}





function initHome(){
	var module = $(".homemodule");
	if(module.length > 0){
		var currentIndex = 0;
		var timer;
		var imgs = module.find(".imgs a");
		var blocs = module.find(".blocs div.item");
		var ct = module.find(".control");
		imgs.each(function(i){
			$(this).css("left", (i*module.width())+"px");
		});
		ct.children().each(function(i){
			$(this).click(function(e){
				e.preventDefault();
				currentIndex = $(this).index();
				module.find(".imgs").animate({left: -currentIndex*module.width()}, 400);
				$(this).siblings().removeClass("on");
				$(this).addClass("on");
				
				var pl = $(blocs[i]);
				pl.siblings().find(".plus span").animate({marginTop:31}, 400);
				pl.find(".plus span").animate({marginTop:0}, 400);
				
				clearTimeout(timer);
				timer = setTimeout(function(){
					if(currentIndex < imgs.length-1) currentIndex++;
					else currentIndex = 0;
					$(ct.children()[currentIndex]).click();
				}, 5000);
			});
		});
		$(ct.children()[0]).click();
	}
}


function initHomeCine(){
	var cinemodule = $(".homecinemodule");
	if(cinemodule.length > 0){
		var cols = cinemodule.find(".cols");
		var max = cols.find(".col").length;
		var currentIndex = 0;
		cols.find(".col").each(function(i){
			$(this).css("left", (i*(cinemodule.width()+16))+"px");
			$(this).removeClass('init');
		});
		function slide(dir){
			if(dir){
				currentIndex = currentIndex < max-1 ? currentIndex+1 : 0;
			}
			else{
				currentIndex = currentIndex > 0 ? currentIndex-1 : max-1;
			}
			cols.animate({left: -currentIndex*(cinemodule.width()+16)}, 400);
		}
		$(".pnnav .p").click(function(e){slide(false); e.preventDefault()});
		$(".pnnav .n").click(function(e){slide(true); e.preventDefault()});
	}
}



function initSlideShows(){
	$(".slideshow").each(function(){
		var currentIndex = 0;
		var sl = $(this);
		var imgs = sl.find("a");
		var ct = sl.find(".control");
		var ov = sl.find(".overlay");
		//var zoomurl;
		var timer;
		imgs.each(function(i){
			$(this).css("left", (i*sl.width())+"px");
			ct.append('<a href="javascript:void(0);">');
		});
		ct.children().each(function(i){
			$(this).click(function(){
				currentIndex = $(this).index();
				sl.find(".imgs").animate({left: -currentIndex*sl.width()}, 400);
				//zoomurl = $(imgs[currentIndex]).attr("href");
				$(this).siblings().removeClass("on");
				$(this).addClass("on");
				clearTimeout(timer);
				timer = setTimeout(function(){
					if(currentIndex < imgs.length-1) currentIndex++;
					else currentIndex = 0;
					$(ct.children()[currentIndex]).click();
				}, 5000);
			});
		});
		$(ct.children()[0]).click();
		ov.css("visibility", "visible");
		ov.css("opacity", 0);
		ov.mouseover(function(){
			ov.animate({opacity:1}, 400);
		});
		ov.mouseout(function(){
			ov.animate({opacity:0}, 400);
		});
		ov.click(function(){
			$(imgs[currentIndex]).click();
		})
	});
}

function initVideos(){
	$(".video").each(function(){
		var sl = $(this);
		var ov = sl.find(".overlay");
		ov.css("visibility", "visible");
		ov.css("opacity", 0);
		ov.mouseover(function(){
			ov.animate({opacity:1}, 400);
		});
		ov.mouseout(function(){
			ov.animate({opacity:0}, 400);
		});
		ov.click(function(){
			sl.find("a").click();
		})
	});
}

function initFoldBlocs(){
	$(".foldbloc").each(function(){
		var fb = $(this);
		var bt = fb.find("h6");
		var state = fb.hasClass('open');
		if(state) fb.removeClass('open');
		
		var closeHeight = fb.height();
		fb.css("height", "auto");
		var openHeight = fb.height();
		
		if(!state){
			fb.css("height", closeHeight+"px");
		}
		else{
			bt.addClass("on");
		}
		

		bt.click(function(){
			state = !state;
			if(state){
				fb.animate({"height": openHeight}, 400);
				bt.addClass("on");
			}
			else{
				fb.animate({"height": closeHeight}, 400);
				bt.removeClass("on");
			}
		})
	});
}


function initImgList(){
	$(".imglist .imgoverlay").each(function(){
		var l = $(this);
		var o = l.find('.lay');
		o.css("visibility", "visible");
		o.css("opacity", 0);
		l.mouseover(function(){
			o.animate({opacity:1}, 400);
		});
		l.mouseout(function(){
			o.animate({opacity:0}, 400);
		});
	});
}


function initLab(){
	var site = $("#footer .sub .right");
	site.t = 0;
	site.mouseenter(function(){
		this.t = setTimeout(function(o){
			$(o).find("span").animate({marginTop:-16}, 400);
		}, 10000, this);
	});
	site.mouseleave(function(){
		clearTimeout(this.t);
		$(this).find("span").animate({marginTop:0}, 400);
	});
}




