jQuery(document).ready(function(){
	initParallax();
	initReferences();
	
	function initReferences(){
		var nbReferences; //nombre total de references
		var nbLines = 3; //nombre de lignes
		var lineLength = 0; //nombre de references par ligne
		var vignetteWidth;
		var vignetteHeight;
		var lineWidth;
		var referencesWrapperHeight;
		var lastReference = 0; //derniere reference de chaque ligne
		
		vignetteWidth = jQuery(".jTscrollerContainer img").width();
		vignetteHeight = jQuery(".jTscrollerContainer img").height();
		nbReferences = jQuery(".jTscrollerContainer img").length;

		lineLength = Math.ceil(nbReferences / nbLines);
		lineWidth = jQuery(".jTscrollerContainer").css({"width":(lineLength * (vignetteWidth+9))-9});
		
		//Hauteur du wrapper references
		referencesWrapperHeight = jQuery(".jTscrollerContainer").css({"height":nbLines * (vignetteHeight+9)});
		lastReference = jQuery(".jTscroller li:nth-Child(" + lineLength + "n+" + lineLength + ")").addClass("noMargin");
		
		//Rollover des vignettes
		jQuery(".jTscroller img").hover(function(){
			jQuery(this).stop().fadeTo(200, 1);
		},
		function(){
			jQuery(this).stop().fadeTo(300, 0.3);
		});
	}
  
	function initParallax(){
		jQuery('#parallax').jparallax({xorigin:'0.5', yorigin:'0.5'},
									  {xtravel: '10px', ytravel: '30px'}, 
									  {xtravel: '30px', ytravel: '60px'}, 
									  {xtravel: '80px', ytravel: '120px'},
									  {xtravel: '250px', ytravel: '300px'});
	}
	
	jQuery("#contact_form .input").click( function(){
	       if(jQuery(this).val() == "Message" || jQuery(this).val() == "Email"){
				jQuery(this).val("");
		   }
	});
	
	jQuery(".desc").mouseenter( function(){				 
			jQuery(this).animate({ marginTop: -386	}, 200);
		});
	
	jQuery(".desc").mouseout( function(){
			jQuery(this).animate({ marginTop: -193	}, 200);
	});
	
	jQuery(".icones").mouseenter(function(e){
		jQuery(this).next(".tip").fadeIn(200);
	});
	
	jQuery(".icones").mouseleave(function(){
		jQuery(this).next(".tip").fadeOut(200);									  
	});
	
	jQuery('#flash .interieur').flash({
				src: 'flash/loader.swf',
				width: 800,
				height: 600,
				wmode: 'transparent',
				flashvars: { file : 'base'} 
	});	
	
	jQuery("#number_actif").val("0");
	ChangeCat("cat_3"); 
	ResetCarouselPosition();
	
	jQuery('#mycarousel-next').click( function(){
			restant = Number(jQuery("#number_total").val())-Number(jQuery("#number_actif").val());
			
			if(restant>=4){ nombre = 4; } else {nombre = restant; }
			
			if(Number(jQuery("#number_actif").val()) <=  Number(jQuery("#number_total").val())){
				jQuery('#mycarousel').animate({ marginLeft: Number(jQuery('#left_input').val())-(255*nombre) }, 300);
				jQuery('#left_input').val( Number(jQuery('#left_input').val())-(255*nombre));
				jQuery("#number_actif").val(Number(jQuery("#number_actif").val()) + nombre) ;
				CacheBtnNext();
				CacheBtnPrev();
				//carousel.next();
				return false;
			}
		});
		
		jQuery('#mycarousel-prev').click( function(){
			if(Number(jQuery("#number_actif").val()) >= 4){
				
				restant = Number(jQuery("#number_total").val())-Number(jQuery("#number_actif").val());
				if(Number(jQuery("#number_actif").val())>=8){ nombre = 4; } else {nombre = Number(jQuery("#number_actif").val())-4; }
				
				jQuery('#mycarousel').animate({ marginLeft: Number(jQuery('#left_input').val())+(255*nombre) }, 300);
				jQuery('#left_input').val( Number(jQuery('#left_input').val())+(255*nombre));
				jQuery("#number_actif").val(Number(jQuery("#number_actif").val()) - nombre) ;
				CacheBtnNext();
				CacheBtnPrev();
				return false;
			}
		});

// arrondis les angles des éléments
	categorie = "Site internet";
	jQuery("#mycarousel h4 span").html(categorie);
	
	jQuery('div.rounded').each(function() {
		jQuery(this).corner(); 
	});
	
// applique le menu lavalampe aux menus concernés
	jQuery('ul.menu').lavaLamp();
	jQuery('#oc_portfolio ul.menu li').corner('5px'); 

//Initialise les rubriques à "none"
	jQuery('.content').css({display: "none"});
	
// affiche et masque les rubriques du menu du header(diminue la hauteur du header)
	jQuery("#oc_header .ShowRub").click( function(){
		
		nom = jQuery(this).attr("rel");
		jQuery('.content').slideUp(500);
		jQuery('#oc_references').slideUp(500);
		
		jQuery('#oc_'+nom).slideDown(500);
		jQuery('#oc_header').animate({
			height: 339
		  }, 500, function() {
		  });
		jQuery('#oc_header .menu').animate({
			marginTop: -20
		  }, 500, function() {
		  });
		jQuery('.masque').animate({
			height: 332
		  }, 500, function() {
		  });
			jQuery('.illustration_whois').hide();
			jQuery('.illustration_portfolio').hide();
			jQuery('.illustration_contact').hide();
		  	jQuery('.illustration_'+nom).fadeIn(2000);
	});
	
// retour à la home	(rétablis la hauteur du header)
	jQuery(".HideRub").click( function(){
		jQuery('.content').slideUp();
		jQuery('#oc_references').slideUp();

		jQuery('#oc_header').animate({
			height: 589
		  }, 500, function() {
		  });
		jQuery('#oc_header .menu').animate({
			marginTop: 0
		  }, 500, function() {
		  });
		jQuery('.masque').animate({
			height: 562
		  }, 500 );
		  
		  //masque les images
		  jQuery('.illustration_whois').hide();
		  jQuery('.illustration_portfolio').hide();
		  jQuery('.illustration_contact').hide();
	});
});

function ChangeCat(cat){
		//jQuery("#mycarousel	li").hide();
		ResetCarouselPosition();
		categorie = cat.split("_");
		LoadCat(categorie[1]);
		//jQuery("#mycarousel	li").fadeIn(500);
}

function LoadCat(cat){
		jQuery("#mycarousel").load("projets.php", {categorie: cat},function(response, status, xhr) {
			i = response.split("---");
			largeur = i[1]*255;
			jQuery("#number_total").val(i[1]);
			jQuery("#mycarousel").css("width",largeur+"px");
			if(Number(i[1]) < 5){
				jQuery("#mycarousel_div").animate({
					width: largeur
				}, 200);
			} else {
				if(jQuery("#mycarousel_div").css("width") != "1022px"){
					jQuery("#mycarousel_div").css("width","1022px");
				}
			}
			jQuery("#mycarousel").html(i[0]);
			
			CacheBtnNext();
			jQuery(".loading").css("display","none");
		});			
}

function GoToContact(objet){
		if(objet == 0){jQuery(".objet").val("");}
		if(objet == 2){jQuery(".objet").val("Demande de devis");}
	
		jQuery('.content').slideUp(500);
		jQuery('#oc_contact').slideDown(500);
		jQuery(".backLava").animate({
		});
		jQuery('#oc_header').animate({
			height: 339
		  }, 500, function() {
		  });
		jQuery('.masque').animate({
			height: 332
		  }, 500, function() {
		  });
		jQuery('.illustration_whois').hide();
		jQuery('.illustration_contact').fadeIn(1000);
}
	
function Verif(){
	error = "";
	validation = "";
	
	if(jQuery(".message").val() == ""){ error = "Veuillez saisir votre message";}
	if(jQuery(".objet").val() == ""){ error = "Veuillez sélectionner un objet";}
	//si mail contient caractere speciaux
	mail =/[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(ad|aero|ae|af|ag|ai|al|am|an|ao|aq|arpa|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|biz|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|com|coop|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|eur|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|info|int|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|museum|mu|mv|mw|mx|my|mz|name|na|nc|net|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pro|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$/;
	 reponse=mail.test(jQuery(".email").val())
	 if(reponse){ }else{ error = "Veuillez saisir un e-mail correct"; } 
	   
	//validation mail
	if(jQuery(".email").val() == ""){ error = "Veuillez saisir votre e-mail";}
	
	if(error != ""){ jQuery(".error_div").html("<b>"+error+"</b>");jQuery(".error_div").css("background","none");jQuery(".error_div").fadeIn(200); }
	else { 
	
			jQuery.get("contact.php",
			{ email: jQuery(".email").val(), objet: jQuery(".objet").val(), message: jQuery(".message").val()  },
						function(data){
						validation = "Votre message à bien été envoyé";
						jQuery(".error_div").html(validation);jQuery(".error_div").css("background","#18a5ff");jQuery(".error_div").fadeIn(200);
						jQuery(".email").val("Email");
						jQuery(".objet").val("");
						jQuery(".message").val("Message");
						}
			);
	}
}

//initalise le caroussel d'images
	
function Showflash(fichier,largeur,hauteur,background){
	jQuery('#flash embed').attr("flashvars","file="+fichier);
	jQuery("#flash").css("width",largeur+"px");
	jQuery("#flash").css("height",hauteur+"px");
	
	largeur_tourblanc = Number(largeur)+60;
	hauteur_tourblanc = Number(hauteur)+55;
	//marge = 350-(hauteur-300);
	marge = (jQuery(window).height() - hauteur) / 2;
	
	jQuery('.total_flash .tour_blanc').css("margin-top",marge+"px");
	jQuery('.total_flash .tour_blanc').css("width",largeur_tourblanc+"px");
	jQuery('.total_flash .tour_blanc').css("height",hauteur_tourblanc+"px");
	
	jQuery("#flash .interieur").css("width",largeur+"px");
	jQuery("#flash .interieur").css("height",hauteur+"px");
	jQuery("#flash .interieur").css("background",background);
	jQuery(".total_flash").fadeIn(500);
	//jQuery("#fond_flash").fadeIn(500);
}

function HideFlash(){
	jQuery(".total_flash").fadeOut(500);
	//jQuery(".fond_noir").fadeOut(500);
}

function ShowImg(fichier,nom,lien){

	if(lien != ""){chaine = "<span>"+lien+"</span>"; } else { chaine = ""; }
	jQuery('#zoom .visuel').html("<img src='"+fichier+"' alt='"+nom+"'/>"+chaine);
	
	marge = 150;
	jQuery('.tour_blanc').css("margin-top",marge+"px");
	
	jQuery(".total_img").fadeIn(500);
}

function HideImg(){
	//jQuery('#zoom .visuel').html("");
	jQuery(".total_img").fadeOut(500);
}

function ResetCarouselPosition(){
	jQuery("#mycarousel").css("margin-left","0");
	jQuery("#number_actif").val("4");
	jQuery("#left_input").val("0");
	CacheBtnPrev();
}

function CacheBtnNext(){

	if(Number(jQuery("#number_total").val()) <= 4){
		jQuery('#mycarousel-next').hide();
	} 
	else if(Number(jQuery("#number_actif").val()) >= Number(jQuery("#number_total").val())){
		jQuery('#mycarousel-next').css("visibility","hidden");
	} else {
		jQuery('#mycarousel-next').css("visibility","visible");
		jQuery('#mycarousel-next').css("display","block");
	}
}

function CacheBtnPrev(){
	if(jQuery("#number_actif").val() <= 4){
		jQuery('#mycarousel-prev').css("visibility","hidden");
	} else {
		jQuery('#mycarousel-prev').css("visibility","visible");
		jQuery('#mycarousel-prev').css("display","block");
	}
}
