/* ************************************************************************************************
	SITE PRELOADER
************************************************************************************************ */
/*
 * QueryLoader		Preload your site before displaying it!
 * Author:			Gaya Kessler
 * Date:			23-09-09
 * URL:				http://www.gayadesign.com
 * Version:			1.0
 * 
 * A simple jQuery powered preloader to load every image on the page and in the CSS
 * before displaying the page to the user.
 */
 
overlay = "";
loadBar = "";
preloader = "";
items = new Array();
doneStatus = 0;
doneNow = 0;
selectorPreload = "body";
ieLoadFixTime = 2000;
ieTimeout = "";
loc = window.location;
gorul = loc.hash;
	
function init() {
	if (navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/) == "MSIE 6.0,6.0") {
		//break if IE6			
		return false;
	}
	if (selectorPreload == "body") {
		spawnLoader();
		getImages(selectorPreload);
		createPreloading();
	} else {
		$(document).ready(function() {
			spawnLoader();
			getImages(selectorPreload);
			createPreloading();
		});
	}
	
	//help IE drown if it is trying to die :)
	ieTimeout = setTimeout("ieLoadFix()", ieLoadFixTime);
}

function ieLoadFix() {
	var ie = navigator.userAgent.match(/MSIE (\d+(?:\.\d+)+(?:b\d*)?)/);
	if (ie != null && ie[0].match("MSIE")) {
		while ((100 / doneStatus) * doneNow < 100) {
			imgCallback();
		}
	}
}

function imgCallback() {
	doneNow ++;
	animateLoader();
}

function getImages(selector) {
	var everything = $(selector).find("*:not(script)").each(function() {
		var url = "";
		
		if ($(this).css("background-image") != "none") {
			var url = $(this).css("background-image");
		} else if (typeof($(this).attr("src")) != "undefined" && $(this).attr("tagName").toLowerCase() == "img") {
			var url = $(this).attr("src");
		}
		
		url = url.replace("url(\"", "");
		url = url.replace("url(", "");
		url = url.replace("\")", "");
		url = url.replace(")", "");
		
		if (url.length > 0) {
			items.push(url);
		}
	});
}

function createPreloading() {
	preloader = $("<div></div>").appendTo(selectorPreload);
	$(preloader).css({
		height: 	"0px",
		width:		"0px",
		overflow:	"hidden"
	});
	
	var length = items.length; 
	doneStatus = length;
	
	for (var i = 0; i < length; i++) {
		var imgLoad = $("<img></img>");
		$(imgLoad).attr("src", items[i]);
		$(imgLoad).unbind("load");
		$(imgLoad).bind("load", function() {
			imgCallback();
		});
		$(imgLoad).appendTo($(preloader));
	}
}

function spawnLoader() {
	if (selectorPreload == "body") {
		var height = $(window).height();
		var width = $(window).width();
		var position = "fixed";
	} else {
		var height = $(selectorPreload).outerHeight();
		var width = $(selectorPreload).outerWidth();
		var position = "absolute";
	}
	var left = $(selectorPreload).offset()['left'];
	var top = $(selectorPreload).offset()['top'];
	
	overlay = $("<div></div>").appendTo($(selectorPreload));
	$(overlay).addClass("QOverlay");
	$(overlay).css({
		position: position,
		top: top,
		left: left,
		width: width + "px",
		height: height + "px"
	});
	
	loadBar = $("<div></div>").appendTo($(overlay));
	$(loadBar).addClass("QLoader");
	
	$(loadBar).css({
		position: "relative",
		top: "50%",
		width: "0%"
	});
	
	loadAmt = $("<div>0%</div>").appendTo($(overlay));
	$(loadAmt).addClass("QAmt");
	
	$(loadAmt).css({
		position: "relative",
		top: "50%",
		left: "50%"
	});
}

function animateLoader() {
	var perc = (100 / doneStatus) * doneNow;
	if (perc > 99) {
		$(loadAmt).html("100%");
		$(loadBar).stop().animate({
			width: perc + "%"
		}, 900, "linear", function() { 
			doneLoad();
		});
	} else {
		$(loadBar).stop().animate({
			width: perc + "%"
		}, 900, "linear", function() { });
		$(loadAmt).html(Math.floor(perc)+"%");
	}
}

function doneLoad() {
	//prevent IE from calling the fix
	clearTimeout(ieTimeout);
	
	//determine the height of the preloader for the effect
	if (selectorPreload == "body") {
		var height = $(window).height();
	} else {
		var height = $(selectorPreload).outerHeight();
	}
	
	//The end animation, adjust to your likings
	$(loadAmt).hide();
	$(loadBar).animate({
		height: height + "px",
		top: 0
	}, 800, "linear", function() {
		$(overlay).fadeOut(800);
		$(preloader).remove();
	});
	
	animation();
}


/* ************************************************************************************************
	FIN DU CHARGEMENT -> ANIMATION
************************************************************************************************ */
function animation() {
	// Image Homepage	
	$('#header_home').css({'display':'none'}).css({width:'1px'});
	
	// 3 blocs Homepage	
	$('#bloc1,#bloc2,#bloc3').css({'display':'none'}).css({height:'1px'});

	// Logo	
	$('#logo').css({'display':'none'}).css({height:'1px'});	
	$('#logo').fadeIn(1000).animate({height:'520px'},1000,"swing", function() {});
	
	// Menu
	$('#menu').css({'display':'none'});	
	
	// Bottom
	$('#bottom').css({'display':'none'}).css({height:'1px'});		
	$('#bottom').fadeIn(1000).animate({height:'273px'},1000,"swing", function() {
		$('#inscription').fadeIn(800);
		// Menu
		$('#menu').fadeIn(1000);

		// Image Homepage
		$('#header_home').fadeIn(150).animate({width:'510px'},150,"easeOutCirc", function() {	

			// Bloc 1 Homepage
			$('#bloc1').fadeIn(250).animate({height:'236px'},250,"swing", function() {				
				$('#bloc1 h1').fadeIn(500, function() {
					$('#bloc1 p').fadeIn(500);
					// Bloc 2 Homepage
					$('#bloc2').fadeIn(250).animate({height:'236px'},250,"swing", function() {
						$('#bloc2 h1').fadeIn(500, function() {
							$('#bloc2 p').fadeIn(500);
							// Bloc 3 Homepage
							$('#bloc3').fadeIn(250).animate({height:'236px'},250,"swing", function() {
								$('#bloc3 h1').fadeIn(500, function() {														   
									$('#bloc3 p').fadeIn(500);
									
									// Reaffichage de la barre de scroll si nécessaire
									$('body').css('overflow','auto');
									
									// Affichage page en fonction url
									if(gorul == "#wallonia") {
										switchPage(2);
										$('#onglets').animate({'width': '+=30px'}, 300);
									} else if(gorul == "#partners") {
										switchPage(3);
									}else if(gorul == "#video") {
										switchPage(2)
										slideToPageY(3);					
										
									} else if(gorul == "#programme") {
										switchPage(4);
									} else if(gorul == "#presse") {
										switchPage(5);
									} else if(gorul == "#contact") {
										switchPage(6);
									} else if(gorul == "#cocktail") {
										switchPage(7);
									}
			
								});
			
							});
			
						});
					});
				});
			});
		});
	
	});
}


/* ************************************************************************************************
	VARIABLES PAGES ET DETECTION TOUCHES CLAVIER
************************************************************************************************ */
$(document).ready(function(){
	// Empecher drag des images et le focus du formuaire
	$(document).bind("dragstart", function() { return false; });
	$("input,textarea").mousedown(function (e) {$(this).trigger('focus');});

	// Slide X
	_pageX = 1;
    _pagesX = $('div.panel').length;

	// Slide Y
	_pageY = 1;
    _pagesY = $('div.panelInside').length;
	
	// Affichage / Masquage fleches haut / bas au chargement
	if(_pageY == 1) { $('#fleche_top').css('display','none'); }
	
	// Touches haut bas, gaiche et droite pour naviguer
	$(document).keydown(function(e){
		if(e.keyCode == 40){
			if(_pageY != _pagesY) { slideToPageY(_pageY+1); }
		}else if(e.keyCode == 38){
			if(_pageY != 1) { slideToPageY(_pageY-1); }
		}else if(e.keyCode == 39){
			if(_pageX != _pagesX) { slideToPageX(_pageX+1); }
		}else if(e.keyCode == 37){
			if(_pageX != 1) { slideToPageX(_pageX-1); }
		}
	});
	
	// Resize hauteur >< taille ecran
	$('#site').css({'height':(($(window).height())-20)+'px'});
	$('#drag').css({'height':(($(window).height())-90)+'px'});
	$('#scroll,.panelContainer,.panel').css({'height':(($(window).height())-115)+'px'});
	
	var onglets = ($(window).height()-115);
	var onglet = ($(window).height()-170);
	$('#onglets').css({'height': onglets+'px'});
	$('.onglet').css({'height': (onglet/6)+'px'}).css({'line-height':(onglet/6)+'px'});

	$(window).bind('resize', function() {
		$('#site').css({'height':(($(window).height())-20)+'px'});
		$('#drag').css({'height':(($(window).height())-90)+'px'});
		$('#scroll,.panelContainer,.panel').css({'height':(($(window).height())-115)+'px'});
		
		onglets = ($(window).height()-115);
		onglet = ($(window).height()-170);
		$('#onglets').css({'height': onglets+'px'});
		$('.onglet').css({'height': (onglet/6)+'px'}).css({'line-height':(onglet/6)+'px'});
	});
});	


/* ************************************************************************************************
	SCROLL PAGES
************************************************************************************************ */ 
// ScrollTo X
function slideToPageX(page){
	// Affichage onglets
	if(page == (2)) {
		$('#onglets').animate({'width':'+=30px'}, 300);
	} else {
		$('#onglets').animate({'width':'0px'}, 300);
	}

	if(page >= 1 && page <= _pagesX){
		if(page != (_pagesX)) {
			// Classe active menu
			$('#cross-links li').removeClass('active').eq(page-1).addClass('active');
		
			// Remise en haut du scroll Y
			$('.scrollInside').scrollTo('#inside1',500,{axis:'y', easing:'easeOutQuint', onAfter : function(){ _pageY = 1; }});
		
			// Scroll X
			$('#scroll').scrollTo('#page'+page,500,{axis:'x', easing:'easeOutQuint', onAfter : function(){ _pageX = page; }});
			
			// Changement background
			if(tf_bg_img.is(':animated'))
				return false;
				scroll('tb');
		}
	}
};

// ScrollTo Y
function slideToPageY(page){
	// Classe active sous-menu
	if ( (page == 9) || (page == 8) || (page == 7) ) {
		$('#onglets a').removeClass('onglet_active').eq(5).addClass('onglet_active');
	} else if ( (page == 6) || (page == 5) ) {
		$('#onglets a').removeClass('onglet_active').eq(4).addClass('onglet_active');
	} else {
		$('#onglets a').removeClass('onglet_active').eq(page-1).addClass('onglet_active');
	}
	
	// Scroll Y
	if(page >= 1 && page <= _pagesY){
		$('.scrollInside').scrollTo('#inside'+page,500,{axis:'y', easing:'easeOutQuint', onAfter : function(){
			_pageY = page;
			if(_pageY == _pagesY) {
				$('#fleche_bottom').css('display','none');
			} else if(_pageY == 1) {
				$('#fleche_top').css('display','none');
			}			
		}});
		$('#fleche_bottom,#fleche_top').css('display','block');
	}
};

/* ************************************************************************************************
	DETECTION MOUVEMENT SOURIS
************************************************************************************************ */ 
$(function(){
	$('*').gestures({
		callback:function(gesture) {
			if(gesture.string == 'left') { slideToPageX(_pageX + 1); }
			if(gesture.string == 'right') { slideToPageX(_pageX - 1); }
			if(gesture.string == 'up') { slideToPageY(_pageY + 1); }
			if(gesture.string == 'down') { slideToPageY(_pageY - 1); }
		}
	});	
});


/* ************************************************************************************************
	MENU CHANGEMENT DE PAGE
************************************************************************************************ */ 
function switchPage(page) { 
	// Affichage onglets
	if(page == (2)) {
		$('#onglets').animate({'width':'+=30px'}, 300);
	} else {
		$('#onglets').animate({'width':'0px'}, 300);
	}

	// Classe active menu
	$('#cross-links li').removeClass('active').eq(page-1).addClass('active');
	
	// Remise en haut du scroll Y
	$('.scrollInside').scrollTo('#inside1',800,{axis:'y', easing:'easeOutQuint', onAfter : function(){ _pageY = 1; }});
	
	// Scroll X
	$('#scroll').scrollTo('#page'+page,800,{axis:'x', easing:'easeOutQuint', onAfter : function(){ _pageX = page; }});
	
	// Changement background
	if(tf_bg_img.is(':animated'))
		return false;
		scroll('tb');
}

			
/* ************************************************************************************************
	LIGHTBOX
************************************************************************************************ */ 	
$(document).ready(function() {
	$(".lightbox").fancybox({
		'width'				: 900,
		'height'			: 500,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});
});


/* ************************************************************************************************
	SCROLL PARTENAIRES
************************************************************************************************ */ 
$(document).ready(function(){
	currentPosition = 0;
	slideWidth = 542;
	slides = $('.slide');
	numberOfSlides = slides.length;

	// Masque les tooltips
	$('div.caption').css('opacity','0').css('display','none');

	slides.wrapAll('<div id="slideInner"></div>').css({'float':'left', 'width':slideWidth});
	$('#slideInner').css('width', slideWidth * numberOfSlides);

	// Cache la fleche gauche au chargement
	manageControls(currentPosition);
});

// Clic sur le carré du partenaire
function tooltipPartner(id){
	$('div.caption').fadeTo("fast", 0).css('display','none');
	if(id != 1) { $('div#caption'+id).css('display','block').fadeTo("fast", 1); }
	
	currentPosition = id-2;
	
	// Afficher / Masquer controles
	manageControls(currentPosition);
	
	// Deplacement avec la margin
	$('#slideInner').animate({'marginLeft':slideWidth*(-currentPosition)});	
}

// Clique sur les fleches	
function slidepart(direction){
	currentPosition = currentPosition+direction

	// Afficher / Masquer controles
	manageControls(currentPosition);
	
	// Deplacement avec la margin
	$('#slideInner').animate({'marginLeft':slideWidth*(-currentPosition)});
	
	// Affichage tooltip
	tooltipDisplay = currentPosition+2;
	$('div.caption').fadeTo("fast", 0).css('display','none');
	if(tooltipDisplay != 1) { $('div#caption'+tooltipDisplay).css('display','block').fadeTo("fast", 1); }
}

function manageControls(position){
	// Masque la fleche gauche si premier
	if(position==0){ $('#leftControl').hide() } else { $('#leftControl').show() }
	// Masque la fleche droite si au bout
	if(position==numberOfSlides-1){ $('#rightControl').hide(); } else { $('#rightControl').show(); }
}


/* ************************************************************************************************
	IMAGES FOND
************************************************************************************************ */ 
$(document).ready(function(){
	tf_bg_images	= $('#tf_bg').find('img');
	tf_bg_img		= tf_bg_images.eq(0);
	total			= tf_bg_images.length;
	current			= 0;

	var dim	= getImageDim(tf_bg_img);

	tf_bg_img.css({
		width	: dim.width,
		height	: dim.height,
		left	: dim.left,
		top		: dim.top
	}).fadeIn();

	// Resize de l'image au resize de la fenetre
	$(window).bind('resize',function(){
		var dim	= getImageDim(tf_bg_img);
		tf_bg_img.css({
			width	: dim.width,
			height	: dim.height,
			left	: dim.left,
			top		: dim.top
		});
	});
});

// Suivante / Précédante image
function scroll(dir){
	//if dir is "tb" (top -> bottom) increment current, 
	//else if "bt" decrement it
	current	= (dir == 'tb')?current + 1:current - 1;
	
	//we want a circular slideshow, 
	//so we need to check the limits of current
	if(current == total) current = 0;
	else if(current < 0) current = total - 1;

	//we get the next image
	var tf_bg_img_next = tf_bg_images.eq(current),
	//its dimentions
	dim	= getImageDim(tf_bg_img_next),
	//the top should be one that makes the image out of the viewport
	//the image should be positioned up or down depending on the direction
	top	= (dir == 'tb')?$(window).height() + 'px':-parseFloat(dim.height,10) + 'px';
			
	//set the returned values and show the next image	
	tf_bg_img_next.css({
		width	: dim.width,
		height	: dim.height,
		left	: dim.left,
		top		: top
	}).show();
		
	//now slide it to the viewport
	tf_bg_img_next.stop().animate({
		top : dim.top
	},1000);
		
	//we want the old image to slide in the same direction, out of the viewport
	var slideTo	= (dir == 'tb')?-tf_bg_img.height() + 'px':$(window).height() + 'px';
	tf_bg_img.stop().animate({
		top : slideTo
	},1000,function(){
		//hide it
		$(this).hide();
		//the tf_bg_img is now the shown image
		tf_bg_img = tf_bg_img_next;
	});
}

// Resize de l'image par rapport au navigateur
function resize($img){
	var w_w	= $(window).width(),
		w_h	= $(window).height(),
		i_w	= $img.width(),
		i_h	= $img.height(),
		r_i	= i_h / i_w,
		new_w,new_h;
	
	if(i_w > i_h){
		new_w	= w_w;
		new_h	= w_w * r_i;
		
		if(new_h > w_h){
			new_h	= w_h;
			new_w	= w_h / r_i;
		}
	} else {
		new_h	= w_w * r_i;
		new_w	= w_w;
	}
	
	$img.animate({
		width	: new_w + 'px',
		height	: new_h + 'px',
		top		: '0px',
		left	: '0px'
	},350);
}

// Récupération de la taille de l'image
function getImageDim($img){
	var w_w	= $(window).width(),
		w_h	= $(window).height(),
		r_w	= w_h / w_w,
		i_w	= $img.width(),
		i_h	= $img.height(),
		r_i	= i_h / i_w,
		new_w,new_h,
		new_left,new_top;
	
	if(r_w > r_i){
		new_h	= w_h;
		new_w	= w_h / r_i;
	} else {
		new_h	= w_w * r_i;
		new_w	= w_w;
	}

	return {
		width	: new_w + 'px',
		height	: new_h + 'px',
		left	: (w_w - new_w) / 2 + 'px',
		top		: (w_h - new_h) / 2 + 'px'
	};
}


/* ************************************************************************************************
	VALIDATION FORMULAIRE
************************************************************************************************ */ 
// Si validation OK
SubmittingForm=function() {
	var last_name = $("input#last_name").val();
	var first_name = $("input#first_name").val();
	var company = $("input#company").val();
	var fonction = $("input#fonction").val();
	var email = $("input#email").val();
	var street = $("input#street").val();
	var num = $("input#num").val();
	var post_code = $("input#post_code").val();
	var town = $("input#town").val();
	var country = $("input#country").val();
	var phone = $("input#phone").val();
	var party = $('#party:checkbox:checked').val(); 
	var accompanied = $('#accompanied:checkbox:checked').val(); 	
	var accompanied_last_name = $("input#accompanied_last_name").val();
	var accompanied_first_name = $("input#accompanied_first_name").val();
	var accompanied_company = $("input#accompanied_company").val();
	var accompanied_function = $("input#accompanied_function").val();
		
	$.ajax({
		type: "POST",
		url: "envoi.php",
		data: 	"last_name="+ last_name + 
				"&first_name="+ first_name +
				"&company="+ company +
				"&fonction="+ fonction +
				"&email="+ email +
				"&street="+ street +
				"&num="+ num +
				"&post_code="+ post_code +
				"&town="+ town +
				"&country="+ country +
				"&phone="+ phone +
				"&party="+ party +
				"&accompanied="+ accompanied +
				"&accompanied_last_name="+ accompanied_last_name +
				"&accompanied_first_name="+ accompanied_first_name +
				"&accompanied_company="+ accompanied_company +
				"&accompanied_function="+ accompanied_function,
		success: function(){
			$('#cocktail').hide();
			$('div.success').fadeIn();
		}
	});
	return false;
}

// Validation formulaire
$(document).ready(function() {
	$("#cocktail").validate({
		submitHandler:function(form) {
			SubmittingForm();
		},
		rules: {
			last_name: "required",
			first_name: "required",
			company: "required",
			email: {
				required: true,
				email: true
			}
		}
	});
});

