/* ************************************************************************************************
	ANIMATION HOME
************************************************************************************************ */
function animation() {	
	// Header
	$('#header_titre').css({'display':'none'}).css({height:'1px'});		
	$('#header_100').css({'display':'none'}).css({width:'1px'});		
	$('#header_picots').css({'display':'none'});		
	$('#header_infos').css({'display':'none'});	
	$('#header_etoile1').css({'display':'none'});	
	$('#header_etoile2').css({'display':'none'});	
	$('#header_etoile3').css({'display':'none'});	
	
	$('#header_titre').fadeIn(1000).animate({height:'245px'},500,"easeOutBack");
	$('#header_100').fadeIn(1100).animate({width:'980px'},700,"easeOutBack", function() {
		$('#header_etoile1').show(150, function() {
			$('#header_etoile2').show(150, function() {
				$('#header_etoile3').show(150);
			});
		});
		$('#header_picots').fadeIn(500, function() {
			$('#header_infos').fadeIn(500);
		});
	});	
}


//fonction gérant l'annimation
function anim() {
	// $('#header_etoile1').animate({opacity:1.0},200,"linear", function() {
		// $('#header_etoile2').animate({opacity:1.0},200,"linear", function() {
			// $('#header_etoile3').animate({opacity:1.0},200,"linear", function() {
				// $('#header_etoile1').animate({opacity:0.5},500,"linear", function() {
					// $('#header_etoile2').animate({opacity:0.5},200,"linear", function() {
						// $('#header_etoile3').animate({opacity:0.5},200,"linear");
					// });
				// });
			// });
		// });
	// });
}

function anim2() {
	$('#header_100').fadeIn(1100).animate({width:'990px'},700,"easeOutBack", function() {
		$('#header_100').fadeIn(1100).animate({width:'980px'},700,"easeOutBack");
	});	
}

$(document).ready(function(){
	//setInterval('anim();',4000);
	setInterval('anim2();',9000);
	
	// Boutons Home
	$('#bouton_grattez img').mouseover(function() {
		$(this).animate({height:'65px'},250,"easeOutBack");
	});
	$('#bouton_grattez img').mouseout(function() {
		$(this).animate({height:'62px'},500,"easeOutBack");
	});
	
	$('#bouton_verifier img').mouseover(function() {
		$(this).animate({height:'65px'},250,"easeOutBack");
	});
	$('#bouton_verifier img').mouseout(function() {
		$(this).animate({height:'62px'},500,"easeOutBack");
	});
	
	// Lightbox
	$("a[rel^='prettyPhoto']").prettyPhoto({
		social_tools: '<div class="pp_social"><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-lang="fr" data-count="none">Tweeter</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="http://www.facebook.com/plugins/like.php?locale=fr_FR&href='+location.href+'&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div></div>'
	});
	
});


/* ************************************************************************************************
	HOME BANNERS
************************************************************************************************ */ 
var theImages = new Array();
var theURL = new Array();

theImages[0] = 'avenir.gif';
theImages[1] = 'decathlon.gif';
theImages[2] = 'ebike.gif';
theImages[3] = 'heelys.gif';
theImages[4] = 'mc.gif';
theImages[5] = 'medipedia.gif';
theImages[6] = 'waw.gif';

theURL[0] = 'www.lavenir.net';
theURL[1] = 'portal.decathlon.be/FR';
theURL[2] = 'www.bikers.be';
theURL[3] = 'www.heelys.com';
theURL[4] = 'www.mc.be/fr/100/index.jsp';
theURL[5] = 'fr.medipedia.be';
theURL[6] = 'www.walloniemagazine.be/fr';

var j = 0;
var p = theImages.length;
var preBuffer = new Array();
for (i = 0; i < p; i++){
   preBuffer[i] = new Image();
   preBuffer[i].src = theImages[i];
}
var whichImage = Math.round(Math.random()*(p-1));

function showImage(){
	document.write('<a href="http://'+theURL[whichImage]+'" target="_blank"><img src="images/pub/'+theImages[whichImage]+'" alt="" /></a>');
}


/* ************************************************************************************************
	CAROUSEL / TABS
************************************************************************************************ */ 
var tabsrandom = (Math.floor(Math.random()*5)) + 1; /* 0 - 6 */
$(function() {
	// Carousel
    $('#carousel').cycle({
        fx:     'fade',
        speed:  'slow',
        timeout: 3000,
		pause: 1,
		pauseOnPagerHover: 1,
        pager:  '#nav',
		slideExpr: 'img'
    });
	
	// Tabs
	$("ul.tabs").tabs("> .pane", {effect: 'slide', initialIndex: tabsrandom});
	$(".accordion").tabs(".pane_inside", {tabs: 'h2', effect: 'slide'});
});

$(function() {
	$("#abouttabs").tabs(".aboutpanes > div");
});


/* ************************************************************************************************
	CONCOURS
************************************************************************************************ */ 
function concoursShow() {
	$('#videosconcours').css({'display':'none'});	
	$('#jeuconcours').css({'display':'block'});	
}

function concoursMail() {
	// Envoi de l'e-mail
	$.ajax({ // fonction permettant de faire de l'ajax
	   type: "POST", // methode de transmission des données au fichier php
	   url: "envoi_concours_mail.php", // url du fichier php	   
	   data: "nom="+$("#nom").val()
			+"&prenom="+$("#prenom").val() 
			+"&telephone="+$("#telephone").val() 
			+"&adresse="+$("#adresse").val() 
			+"&codepostal="+$("#codepostal").val() 
			+"&ville="+$("#ville").val() 
			+"&email="+$("#email").val() 
			+"&datenaissance="+$("#datenaissance").val() 
			+"&lot="+$("#cadeaupicto").html() // données à transmettre
	});
	return false;
}

function concoursWin() {
	$('#cadeau').css({'display':'block'});		
}

// Formattage champs
jQuery(function($){
   $("#telephone").mask("9999/99.99.99");
   $("#codepostal").mask("9999");
   $("#datenaissance").mask("99/99/9999");
   $("#lot").mask("99999999");
});

// Validation formulaire
jQuery.validator.addMethod("ageverif", function(value, element) { 
	var date = value;
	var dateArray = date.split('/');
	var day = dateArray[0];
	var month = dateArray[1];
	var year = dateArray[2];
	var age = 14;
	var anneemin = 1920;
	var anneemax = 2011;

	var mydate = new Date();
	mydate.setFullYear(year, month-1, day);

	var currdate = new Date();
	currdate.setFullYear(currdate.getFullYear() - age);

	if ( ((currdate - mydate) < 0 ) || (year < anneemin) || (year > anneemax) ) {
		return false;
	}
	return true;
});

$(document).ready(function() {
	$("#form_concours").validate({
		rules: {
			nom: "required",
			prenom: "required",
			adresse: "required",
			codepostal: "required",
			ville: "required",
			email: {
				required: true,
				email: true
			},
			datenaissance: {
				required: true,
				ageverif: true
			},
			reglement: "required"
		},
		messages: {
			nom: 'Champ requis',
			prenom: 'Champ requis',
			adresse: 'Champ requis',
			codepostal: 'Champ requis',
			ville: 'Champ requis',
			email: 'Adresse e-mail incorrecte',
			datenaissance: 'Vous devez avoir 14 ans pour participer',
			reglement: 'Vous devez accepter le règlement'
		}
	});
});

/* ************************************************************************************************
	VALIDATION BILLET
************************************************************************************************ */ 
// Validation formulaire
jQuery.validator.addMethod("ageverif", function(value, element) { 
	var date = value;
	var dateArray = date.split('/');
	var day = dateArray[0];
	var month = dateArray[1];
	var year = dateArray[2];
	var age = 14;
	var anneemin = 1920;
	var anneemax = 2011;

	var mydate = new Date();
	mydate.setFullYear(year, month-1, day);

	var currdate = new Date();
	currdate.setFullYear(currdate.getFullYear() - age);
	
	if ( ((currdate - mydate) < 0 ) || (year < anneemin) || (year > anneemax) ) {
		return false;
	}
	return true;
});

$(document).ready(function() {
	$("#form_obtenir").validate({
		rules: {
			nom: "required",
			prenom: "required",
			adresse: "required",
			codepostal: "required",
			ville: "required",
			email: {
				required: true,
				email: true
			},
			datenaissance: {
				required: true,
				ageverif: true
			},
			reglement: "required",
			lot: "required"
		},
		messages: {
			nom: 'Champ requis',
			prenom: 'Champ requis',
			adresse: 'Champ requis',
			codepostal: 'Champ requis',
			ville: 'Champ requis',
			email: 'Adresse e-mail incorrecte',
			datenaissance: 'Vous devez avoir 14 ans pour participer',
			reglement: 'Vous devez accepter le règlement',
			lot: 'Veuillez entrer le numéro de votre billet'
		}
	});
});


/* ************************************************************************************************
	SIMULATEUR / PARTENAIRES
************************************************************************************************ */ 
$(function() {
	// Tabs
	$("ul.tabssimulateur").tabs("> .pane", {effect: 'fade'});
	$("ul.tabspartenaires").tabs("> .pane", {effect: 'fade'});
});


/* ************************************************************************************************
	IMAGE 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
		});
	});
});

// 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'
	};
}
