jQuery.fn.centerAll = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.outerHeight(true) ) / 2 + "px");
    this.css("left", ( $(window).width() - this.outerWidth(true) ) / 2 + "px");
    return this;
}

jQuery.fn.centerParent = function () {
	var parent = this.parent();
	var parentHeight = parent.height();
	var parentWidth = parent.width();
	this.css("position","absolute");
    this.css("top", ( parentHeight - this.outerHeight(true) ) / 2 + "px");
    this.css("left", ( parentWidth - this.outerWidth(true) ) / 2 + "px");
    return this;
} 

jQuery.fn.childrenWidth = function () {
	var $parent = $(this);
	var _width = 0;
	var $children = $parent.children();
    $children.each(function() {
         _width += $(this).outerWidth(true);
    });
    var newWidth = _width + 1;      
    $parent.css({ 'width': newWidth });    
    return this;	
}

jQuery.fn.childrenHeight = function () {
	var $parent = $(this);
	var _height = 0;
	var $children = $parent.children();
    $children.each(function() {
         _height += $(this).outerHeight(true);
    });
    var newHeight = _height + 1;      
    $parent.css({ 'height': newHeight });    
    return this;	
}


$(window).load(function () {
	
 	if ($.browser.msie) {
    $('#wrapper, #wrapper *').delay(200).css({'visibility': 'visible' });
    } else {
	$('#wrapper').delay(200).animate({opacity: "1"});
	$('#illustration').delay(200).animate({opacity: "1"});
    }
    
    var lgNews = $('#imgNews').width();	
	$('#news').css({'width' : lgNews });


});


$(document).ready(function () {

	Cufon.replace('#nav a, #footer a, .voirPlus, .to-univers, .list li a, a.voirTout', {fontFamily:'Zag Regular',hover:true,hoverables:{a:true}});
	Cufon.replace('#enter a, h2, a.univ span, .retour-univ, .bold, .list li a.bold, .imgGen span, .caption a, a.apiCat, a.apiPress', {fontFamily:'Zag Bold',hover:true,hoverables:{a:true}});
	Cufon.now();

	
	$('#nav').childrenWidth();
	$('#content').childrenWidth();
	$('#news').childrenHeight();
	$('#footer span').childrenWidth();
	
    if ( $('#wrapper').height() < $(window).height()){
    	var enfantsHeight = $('#header').outerHeight() + $('#content').outerHeight() + $('#footer').outerHeight();
    	var _marginTop = ($(window).height() - enfantsHeight ) /5;
		$('#header').css({'margin-top' : _marginTop });
		$('#header').css({'margin-bottom' : 2 +"%" });
		$('#content').css({'margin-bottom' : 5 +"%" });
    }
	
	
// Univers > popup info
	$('#slideshow ul li:first-child img, #info').hover(function(){
		$('#info').css({'display' : 'block'});
	}, function() { 
		$('#info').css({'display' : 'none'});
	});

	
// Aventure > fonctions toggle affiche plus
	$('a.plus').each(function() {
		$(this).click(function() {
			var $block 	= $(this).parent();
			var	$date 	= $block.find('.date');
			var	$cornerLeft 	= $block.find('img.cornerLeft');
			var	$img 	= $block.find('.imgAventure');
			var $h1 	= $block.find('h1');
			var	$texte 	= $block.find('.texte');
			var $dest 	= $('.extendAventure');
			var $chapo = ('.chapo');
			var $txtSup = ('.textSup');
			
			var id = $(this).attr("id");
			   $.post("sources/getTextePartenaire.php",{id:id}, function(data) { 
			   	$($txtSup).html(data);
			   });
			if ($.browser.msie) {
			$('#slideshow').css({'visibility': 'hidden' });
			$('#buttons').css({'visibility': 'hidden' });
			} else {
			$('#slideshow').animate({'opacity': 0 });
			$('#buttons').animate({'opacity': 0 });
			}
			$cornerLeft.clone().appendTo($dest);
			$date.clone().appendTo('.extendImg');
			$img.clone().appendTo('.extendImg');
			
			$h1.clone().appendTo($chapo);
			$texte.clone().appendTo($chapo);
			$dest.show();
		});
	});
	
	$('a.moins').click(function() {
		$('.extendAventure').hide();
		$('.extendImg').empty();
		$('.chapo').empty();
		if ($.browser.msie) {
			$('#slideshow').css({'visibility': 'visible' });
			$('#buttons').css({'visibility': 'visible' });
		} else {
		$('#slideshow').animate({'opacity': 1 });
		$('#buttons').animate({'opacity': 1 });
		}
	});


// jeux / Naissance > Rollover Box
	$('.jeux .box, .naissance .box').hover(function(){
		$(".caption", this).stop().css({left: 0 },{queue:false,duration:160});
	}, function() {
		$(".caption", this).stop().css({left:'-175px'},{queue:false,duration:160});
	});
	
	
// Ou nous trouver > Form
	$('#Form').submit(function() {
		 if (!saveFormulaireDoudou()) return false;
	});

	$('#FormDistributeur').submit(function() {
		 if (!saveFormulaireDistributeur()) return false;

	});

    $('select#id_pays').change(function() {
		var id_pays = $("#id_pays option:selected").val()
		//if (id_pays != 0) {
			
			$.getJSON("sources/select.php",{id_type:1,id_pays:id_pays}, function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
				$("#id_ville").html(options);
				$('#id_ville option:first').attr('selected', 'selected');				
				$('#ListeBoutique').html('<li>&nbsp;</li>'); 
				if (id_pays != 0) {
					$.post("sources/getLibellePays.php",{id:id_pays}, function(data) { 
						codeAddress(data,3);
					});
				}
				
			})	
		//}
	} ) ;

	$('select#id_ville').change(function() {
		var id_ville = $("#id_ville option:selected").val()
		if (id_ville != 0) {
			$.post("sources/getVille.php",{id_ville:id_ville}, function(data) { 
				$('#ListeBoutique').html(data); 
				$.post("sources/getLibelleVille.php",{id:id_ville}, function(data) {
					codeAddress(data,8);
				});
				$('.scroll-pane').jScrollPane({
			verticalDragMinHeight: 40,
			verticalDragMaxHeight: 40});
			});
			
			
			
		}
	} ) ;
	
// Vos questions
	$('.Question').click(function () { 
		var id = $(this).attr("id");
		
		$.post("sources/getReponse.php",{id:id}, function(data) { 
			$('.reponse').html(data); 
		});
	});
	
});
    
		



function checkEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)) return true;
	else return false;
}



function saveFormulaireDoudou() {
	var nom = $('#nom').val();
	var prenom = $('#prenom').val();
	var adresse = $('#adresse').val();
	var ville = $('#ville').val();
	var cp = $('#cp').val();
	var pays = $('#pays').val();
	var tel = $('#tel').val();
	var email = $('#email').val();
	var description = $('#description').val();
	var commentaire = $('#commentaire').val();
	
	var erreur = 0;
	$("#Lnom").css("color","black");
	$("#Lprenom").css("color","black");
	$("#Ladresse").css("color","black");
	$("#Lville").css("color","black");
	$("#Lcp").css("color","black");
	$("#Lpays").css("color","black");
	$("#Lemail").css("color","black");
	$("#Ldescription").css("color","black");

	
	if(nom == ""){
		erreur = 1;
		$("#Lnom").css("color","red");
	}
	
	if(prenom == ""){
		erreur = 1;
		$("#Lprenom").css("color","red");
	}
	if(adresse == ""){
		erreur = 1;
		$("#Ladresse").css("color","red");
	}
	if(ville == ""){
		erreur = 1;
		$("#Lville").css("color","red");
	}
	if(cp == ""){
		erreur = 1;
		$("#Lcp").css("color","red");
	}
	if(pays == ""){
		erreur = 1;
		$("#Lpays").css("color","red");
	}
	if(!checkEmail(email)){
		erreur = 1;
		$("#Lemail").css("color","red");
	}
	if(description == ""){
		erreur = 1;
		$("#Ldescription").css("color","red");
	}


	if(erreur != ""){
		$('#form-erreur').html($('#message').val());
		return false;
	}else{
		return true;
	}
}

function saveFormulaireDistributeur() {
	var magasin = $('#magasin').val();
	var nom = $('#nom').val();
	var prenom = $('#prenom').val();
	var adresse = $('#adresse').val();
	var ville = $('#ville').val();
	var cp = $('#cp').val();
	var pays = $('#pays').val();
	var tel = $('#tel').val();
	var email = $('#email').val();
	var description = $('#description').val();
	
	var erreur = 0;
	$("#Lmagasin").css("color","black");
	$("#Lnom").css("color","black");
	$("#Lprenom").css("color","black");
	$("#Ladresse").css("color","black");
	$("#Lville").css("color","black");
	$("#Lcp").css("color","black");
	$("#Lpays").css("color","black");
	$("#Lemail").css("color","black");
	$("#Ldescription").css("color","black");

	if(magasin == ""){
		erreur = 1;
		$("#Lmagasin").css("color","red");
	}
	
	if(nom == ""){
		erreur = 1;
		$("#Lnom").css("color","red");
	}
	
	if(prenom == ""){
		erreur = 1;
		$("#Lprenom").css("color","red");
	}
	if(adresse == ""){
		erreur = 1;
		$("#Ladresse").css("color","red");
	}
	if(ville == ""){
		erreur = 1;
		$("#Lville").css("color","red");
	}
	if(cp == ""){
		erreur = 1;
		$("#Lcp").css("color","red");
	}
	if(pays == ""){
		erreur = 1;
		$("#Lpays").css("color","red");
	}
	if(!checkEmail(email)){
		erreur = 1;
		$("#Lemail").css("color","red");
	}
	if(description == ""){
		erreur = 1;
		$("#Ldescription").css("color","red");
	}


	if(erreur != ""){
		$('#form-erreur').html($('#message').val());
		return false;
	}else{
		return true;
	}
}
