jQuery(document).ready(function() {
	rollNav();
	homeSlideshow();
	slideShow();
	slideShowCat();
	navTabs();
	accAccordion();
	bulleCata();
	searchFields();
	jQuery('#rep-cont .rep-bg').css({ opacity : 0.6 });
	
	traiterChampFormulaire();
	miniCart();
});

function rollNav() {
	var menuItem = jQuery('#navbar li');
	menuItem.mouseenter( function(event) { showNav(jQuery(this)); } );
}

function showNav( menu ) {
	menu.children('.roll-nav').fadeTo( 300, 0.92 );
	menu.mouseleave( function(event) { hideNav(jQuery(this)); } );
}

function hideNav( menu ) {
	menu.children('.roll-nav').hide( 100 );
}

function miniCart() {
	jQuery('#cart-view').click( function(event){
		event.preventDefault();
		jQuery('#mini-cart').slideDown(200);
		jQuery('#close-mini-cart').unbind().click( function(event){
			event.preventDefault();
			jQuery('#mini-cart').slideUp(200);
		});
	});
}

/*--SLIDESHOW SINGLE--*/
function slideShow() {
	var firstEl = jQuery('#slideshow img');
	firstEl.css({ opacity : 0.0 });
	jQuery('#slideshow img:first-child').css({ opacity : 1.0 }).addClass('show');
	if ( firstEl.next().length )setInterval( 'gallery()' , 6000 );
}

function gallery() {
	var current = (jQuery('#slideshow img.show')?  jQuery('#slideshow img.show') : jQuery('#slideshow img:first-child'));
	var next = ((current.next().length) ? current.next() : jQuery('#slideshow img:first-child'));
	
	next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);
	
	current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');
}

/*--SLIDESHOW CAT--*/
function slideShowCat() {
	var firstEl = jQuery('#slideshow-cat img');
	firstEl.css({ opacity : 0.0 });
	jQuery('#slideshow-cat img:first-child').css({ opacity : 1.0 }).addClass('show');
	if ( firstEl.next().length )setInterval( 'galleryCat()' , 6000 );
}

function galleryCat() {
	var current = (jQuery('#slideshow-cat img.show')?  jQuery('#slideshow-cat img.show') : jQuery('#slideshow-cat img:first-child'));
	var next = ((current.next().length) ? current.next() : jQuery('#slideshow-cat img:first-child'));
	
	next.css({ opacity: 0.0 })
	.addClass('show')
	.animate({ opacity: 1.0 }, 1000);
	
	current.animate({ opacity: 0.0 }, 1000)
	.removeClass('show');
}

/*--HOME SLIDESHOW--*/
var imgBlock;
var tabs;

function homeSlideshow() {
	imgBlock = jQuery('#home-slideshow .slide-content');
	tabs = jQuery('#slideshow-tabs a');
	sContent = jQuery('#slideshow-cont');
	imgBlock.css({ 'display' : 'none' });
	jQuery('#home-slideshow .slide-content:first-child').css({ 'display' : 'block' }).addClass('show');
	jQuery('#slideshow-tabs a:first-child').addClass('selected');
	tabs.click( function(event) { event.preventDefault(); showContent(jQuery(this)); } );
}

function showContent( tab ) {
	tabs.removeClass('selected');
	tab.addClass('selected');
	jQuery('#home-slideshow .show').removeClass('show').css({ 'display' : 'none' });
	jQuery('#' + tab.attr('href')).addClass('show').css({ 'display' : 'block' });
}


/*--ONGLETS--*/
function navTabs() {
	var tabs = jQuery('#onglets a');
	tabs.click( function(event) { event.preventDefault(); showBlock(jQuery(this)); } );
}

function showBlock( tab ) {
	var blocks = jQuery('#cadres .cadre');
	blocks.hide();
	jQuery('#' + tab.attr('rel')).show();
}

//--ACCORDEON COMPTE--//
function accAccordion() {
	var table = jQuery('.acc-block .acc-content:not(.open)');
	var openTable = jQuery('.acc-block .acc-content.open');
	var toggler = jQuery('.acc-block .acc-title');
	
	openTable.prev('.acc-title').addClass('current-tab');
	table.css({'display' : 'none'});
	
	toggler.click(function(){
		if ( !jQuery(this).hasClass('current-tab') ) {
			toggler.removeClass('current-tab');
			jQuery('.acc-block .acc-content').slideUp(300);
			jQuery(this).addClass('current-tab').next('.acc-content').slideDown(300);
		} else {
			toggler.removeClass('current-tab');
			jQuery(this).next('.acc-content').slideUp(300);
		}
	});

}

/*--BULLE CATALOGUES SIDEBAR--*/
function bulleCata() {
	var links = jQuery('#side-cata .cata-content a');
	var buble = jQuery('#side-cata #bulle-cata');
	links.mouseenter( function() {
		buble.empty().show().append( jQuery(this).attr('rel') );
	}).mouseleave( function() {
		buble.empty().hide();
	});
}


/*--Valeur par d�faut champs de recherche--*/
function searchFields() {
	jQuery('#h-search input[name="cs-all-0"]').attr( 'value' , 'Ex : P\350re No\353l, chalet...').focus( function() {  jQuery(this).attr( 'value' , '') ;  if(jQuery('#h-search input[name="cs-localisation-1"]').attr('value')=='Ex : Paris, Ile de France, Loire...')  jQuery('#h-search input[name="cs-localisation-1"]').attr( 'value' , '') ; } );
	jQuery('#h-search input[name="cs-localisation-1"]').attr( 'value' , 'Ex : Paris, Ile de France, Loire...').focus( function() { jQuery(this).attr( 'value' , '') ; if(jQuery('#h-search input[name="cs-all-0"]').attr('value')=='Ex : P\350re No\353l, chalet...')  jQuery('#h-search input[name="cs-all-0"]').attr( 'value' , '') ; } );
}

// valider les champs obligatoires des formulaires
function valider(idForm) {
	var champ = document.getElementById(idForm);
	
	if(idForm=="formCreateAccount"){
		if(champ.societe.value!="" && champ.nom.value!="" && champ.tel.value!="" && champ.mail.value!="" && champ.adresse.value!="" && champ.cp.value!="" && champ.ville.value!="" && champ.pass.value!="" && champ.c-pass.value!=""){
		// on retourne 'true' pour que le formulaire soit envoy�
		  return true;
		} else {
			alert("tous les champs avec asterisques* sont obligatoires.");
			return false;
		}
	}else if(idForm=="formConnectAccount"){
		if(champ.log.value!="" && champ.pwd.value!=""){
		// on retourne 'true' pour que le formulaire soit envoy�
		return true;
		} else {
			alert("tous les champs avec asterisques* sont obligatoires.");
			return false;
		}
	} else if(idForm=="form-sur-mesure"){
	  if(champ.societe.value!="" && champ.nom.value!="" && champ.prenom.value!="" && champ.email.value!="" && champ.phone.value!="" && champ.postal.value!="" && champ.ville.value!="" && champ.projet.value!=""){
	    return true;
	  } else {
			alert("tous les champs avec asterisques* sont obligatoires.");
	    return false;
	  }
	} else if(idForm=="form-artistes"){
	  if(champ.societe.value!="" && champ.nom.value!="" && champ.prenom.value!="" && champ.email.value!="" && champ.phone.value!="" && champ.postal.value!="" && champ.ville.value!="" && champ.projet.value!=""){
	    return true;
	  } else {
			alert("tous les champs avec asterisques* sont obligatoires.");
	    return false;
	  }
	} 
}

function traiterChampFormulaire(){
  $("#societe").blur(function(){ /* projet sur mesure */
    	if($("#societe").val() == "") $("#societe").attr( 'value' , 'Raison sociale*');
	valid = true;
	if($("#societe").val() == "" || $("#societe").val() == "Raison sociale*"){
	  $("#societe-error-message").fadeIn().text("Veuillez saisir le nom de votre organisme.");
	  valid = false;
	}
	else {
	  $("#societe-error-message").fadeOut();
	}
	return valid;
  });
  
  $("#nom").blur(function(){ /* projet sur mesure */
	    	if($("#nom").val() == "") $("#nom").attr( 'value' , 'Nom*');
		valid = true;
		if($("#nom").val() == "" || $("#nom").val() == "Nom*" ){
		  $("#nom-error-message").fadeIn().text("Veuillez saisir votre nom.");
		  valid = false;
		} else {
			if(!$("#nom").val().match(/^[^1-9]+$/i)){
				$("#nom-error-message").fadeIn().text("Veuillez saisir un nom valide.");
				valid = false;
			} else {
				$("#nom-error-message").fadeOut();
			}
		}
		return valid;
	  });
  
  $("#prenom").blur(function(){ /* projet sur mesure */
	    	if($("#prenom").val() == "") $("#prenom").attr( 'value' , 'Pr\351nom*');
		valid = true;
		if($("#prenom").val() == "" || $("#prenom").val() == "Pr\351nom*"){
			$("#prenom-error-message").fadeIn().text("Veuillez saisir votre pr\351nom.");
		  valid = false;
		} else if(!$("#prenom").val().match(/^[^1-9]+$/i)){
			$("#prenom-error-message").fadeIn().text("Veuillez saisir un pr\351nom valide.");
			valid = false;
		} else {
			$("#prenom-error-message").fadeOut();
		}
		return valid;
	  });
  $("#mail").blur(function(){ /* projet sur mesure */
		if($("#mail").val() == "") $("#mail").attr( 'value' , 'E-mail*');
	    	valid = true;
		if($("#mail").val() == "" || $("#mail").val() == "E-mail*"){
		  $("#mail-error-message").fadeIn().text("Veuillez saisir votre email.");
		  valid = false;
		} else if( !$("#mail").val().match(/^(\w|-|\.)+@((\w|-)+\.)+[a-z]{2,6}$/i) ){
			$("#mail-error-message").fadeIn().text("Veuillez saisir un email valide.");
			valid = false;
		} else {
		  $("#mail-error-message").fadeOut();
		}
		return valid;
	  });
  $("#email").blur(function(){ /* projet sur mesure */
	    	valid = true;
		if($("#email").val() == ""){
		  $("#email").next(".error-message").fadeIn().text("Veuillez saisir votre email.");
		  valid = false;
		} else if( !$("#email").val().match(/^(\w|-|\.)+@((\w|-)+\.)+[a-z]{2,6}$/i) ){
			$("#email").next(".error-message").fadeIn().text("Veuillez saisir un email valide.");
			valid = false;
		} else {
		  $("#email").next(".error-message").fadeOut();
		}
		return valid;
	  });
  $("#tel").blur(function(){ /* creation de compte */
	    	valid = true;
		if($("#tel").val() == "") $("#tel").attr( 'value' , 'T\351l\351phone*');
		if($("#tel").val() == "" || $("#tel").val() == "T\351l\351phone*"){
		  $("#tel-error-message").fadeIn().text("Veuillez saisir votre num\351ro de t\351l\351phone");
		  valid = false;
		} else if( !$("#tel").val().match(/^\+{0,1}[0-9\s]*$/) ){
			$("#tel-error-message").fadeIn().text("Veuillez saisir un num\351ro de t\351l\351phone valide.");
			valid = false;
		} else {
			$("#tel-error-message").fadeOut();
		}
		return valid;
	  });
  $("#phone").blur(function(){ /* projet sur mesure */
	    	valid = true;
		if($("#phone").val() == ""){
		  $("#phone-error-message").fadeIn().text("Veuillez saisir votre num\351ro de t\351l\351phone");
		  valid = false;
		} else if( !$("#phone").val().match(/^\+{0,1}[0-9\s]*$/) ){
			$("#phone-error-message").fadeIn().text("Veuillez saisir un num\351ro de t\351l\351phone valide.");
			valid = false;
		} else {
			$("#phone-error-message").fadeOut();
		}
		return valid;
	  });
  $("#adresse").blur(function(){ /* creation de compte */
		if($("#adresse").val() == "") $("#adresse").attr( 'value' , 'Adresse*');
	    	valid = true;
		if($("#adresse").val() == "" || $("#adresse").val() == "Adresse*"){
		  $("#adresse-error-message").fadeIn().text("Veuillez saisir votre adresse.");
		  valid = false;
		} else {
			$("#adresse-error-message").fadeOut();
		}
		return valid;
	  });
  $("#cp").blur(function(){ /* creation de compte */
		if($("#cp").val() == "") $("#cp").attr( 'value' , 'Code postal*');
	    	valid = true;
		if($("#cp").val() == "" || $("#cp").val() == "Code postal*"){
		  $("#cp-error-message").fadeIn().text("Veuillez saisir votre code postal.");
		  valid = false;
		} else if( !$("#cp").val().match(/^[0-9\s]*(a|b|A|B)*$/) ){
			$("#cp-error-message").fadeIn().text("Veuillez saisir un code postal valide.");
			valid = false;
		} else {
			$("#cp-error-message").fadeOut();
		}
		return valid;
	  });
  $("#postal").blur(function(){ /* projet sur mesure */
	    	valid = true;
		if($("#postal").val() == ""){
			$("#postal-error-message").fadeIn().text("Veuillez saisir votre code postal.");
		  valid = false;
		} else if( !$("#postal").val().match(/^[0-9\s]*$/) ){
			$("#postal-error-message").fadeIn().text("Veuillez saisir un code postal valide.");
			valid = false;
		} else {
			$("#postal-error-message").fadeOut();
		}
		return valid;
	  });
  $("#ville").blur(function(){ /* projet sur mesure */
		if($("#ville").val() == "") $("#ville").attr( 'value' , 'Ville*');
	   	valid = true;
		if($("#ville").val() == "" || $("#ville").val() == "Ville*"){
		  $("#ville-error-message").fadeIn().text("Veuillez saisir votre ville.");
		  valid = false;
		} else if( !$("#ville").val().match(/^[^1-9]+$/i) ){
			$("#ville-error-message").fadeIn().text("Veuillez saisir une ville valide.");
			valid = false;
		} else {
		  $("#ville-error-message").fadeOut();
		}
		return valid;
	  });
  $("#pass").blur(function(){ /* creation de compte */
		if($("#pass").val() == "") $("#pass").attr( 'value' , 'Mot de passe*');
	    	valid = true;
		if($("#pass").val() == "" || $("#pass").val() == "Mot de passe*"){
		  $("#pass-error-message").fadeIn().text("Champ mot de passe obligatoire");
		  valid = false;
		} else {
			$("#pass-error-message").fadeOut();
		}
		return valid;
	  });
  $("#c-pass").blur(function(){ /* creation de compte */
		if($("#c-pass").val() == "") $("#c-pass").attr( 'value' , 'Confirmer le mot de passe*');
		valid = true;
		if($("#c-pass").val() == "" || $("#c-pass").val() == "Confirmer le mot de passe*" ){
		  $("#c-pass-error-message").fadeIn().text("Champ confirmer votre mot de passe obligatoire");
		  valid = false;
		} else {
			$("#c-pass-error-message").fadeOut();
		}
		return valid;
	  });
  $("#budget").blur(function(){ /* projet sur mesure */
		
	    	valid = true;
		if($("#budget").val() == ""){
		  $("#budget").next(".error-message").fadeIn().text("Veuillez saisir votre budget.");

		  valid = false;
		} else if( !$("#budget").val().match(/^[0-9]*$/) ){
			$("#budget").next(".error-message").fadeIn().text("Le budget ne doit comporter que des chiffres.");
			valid = false;
		} else {
		  $("#budget").next(".error-message").fadeOut();
		}
		return valid;
	  });
  $("#projet").blur(function(){ /* projet sur mesure */
	    valid = true;
		if($("#projet").val() == ""){
		  $("#projet").next(".error-message").fadeIn().text("Veuillez saisir votre projet.");
		  valid = false;
		}
		else {
		  $("#projet").next(".error-message").fadeOut();
		}
		return valid;
	  });
}