function limitChars(textid, limit, infodiv)
{
	var text = $('#'+textid).val();	
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}
$.fx.speeds._default = 1000;

//for the genaral incompatible width of select in safari, Mac, 
//Editor: Ouissam Bousalhi wwww.neptis.info
function selectBoxMacWidth()
{	
	if( $.browser.safari() || $.browser.mac() ) { 
		$('.dd, .ddChild').each(function(){
		var widthSelect = $(this).width()+23; //alert(widthSelect);
		$(this).css('width', widthSelect+'px'); //alert('ok'); 
		
		});
		
	}
}
//////////////
	
$(document).ready(function(){
	//$('.w_home_connecte_redacteur').show("blind", "", 500);						   
	$('.checkbox_radiobox').customInput();					   
	//$(".selectbox").msDropDown().data("dd");
	$("select.selectbox").sb({ fixedWidth: false, maxHeight:215  });

	//J'ai ajout� ce code pour enlever la proprit� opacity:0.3 apr�s qu'on met le disabled sur le select
	//var width = $('#w_search .dd').css("width"); //alert(width);
	//$('#w_search .dd').attr("style", "opacity:1");
	//$('#w_search .dd').attr("style", "width: "+width);
	//
	$(".input_file").filestyle({ 
     image: "/images/input_file.png",
     imageheight : 23,
     imagewidth : 102,
     width : 190
 	});	
	$(".input_file_2").filestyle({ 
     image: "/images/input_file_2.png",
     imageheight : 19,
     imagewidth : 102,
     width : 190
 	});
	
	if($(".tabs").length > 0) $(".tabs").tabs();
	
	$( "a.supprimerPopup" ).click(function(){ 
		$(this).colorbox({width:"290px", height:"93px", inline:true, href:"#supprimerPopup"});	
	});

	$( "a.modifierPopup" ).click(function(){ 
		$(this).colorbox({width:"340px", height:"140px", inline:true, href:"#modifierPopup"});	
	});
	$( "a.plusdephotos, a.plusdeurl" ).click(function(){	
		$(this).colorbox({width:"505px", height:"257px", inline:true, href:"#plusdephotos"});	
	});
	$( "a.plusdeurl" ).click(function(){	
		$(this).colorbox({width:"505px", height:"257px", inline:true, href:"#plusdeurl"});	
	});
	$( "a.demandeModification" ).click(function(){	
		$(this).colorbox({width:"410px", height:"265px", inline:true, href:"#demandeModification"});	
	});
	
	$( "a.refuserPopup" ).click(function(){ 
		$(this).colorbox({width:"410px", height:"280px", inline:true, href:"#refuserPopup"});	
	});
	$( "a.accepterPopup" ).click(function(){ 
		$(this).colorbox({width:"290px", height:"93px", inline:true, href:"#accepterPopup"});	
	});
	$( "a.bloquerPopup" ).click(function(){ 
		$(this).colorbox({width:"410px", height:"120px", inline:true, href:"#bloquerPopup"});	
	});
	// page commande
	$( "a.abusPopup" ).click(function(){ 
		$(this).colorbox({width:"360px", height:"100px", inline:true, href:"#abusPopup"});	
	});
	/*$( "a.refuserCmdPopup" ).click(function(){ 
		$(this).colorbox({width:"310px", height:"100px", inline:true, href:"#refuserCmdPopup"});	
	});*/
	
	$( "a.modifierCmdPopup" ).click(function(){ 
		$(this).colorbox({width:"310px", height:"100px", inline:true, href:"#modifierCmdPopup"});	
	});
	$( "a.annulerCmdPopup" ).click(function(){ 
		$(this).colorbox({width:"310px", height:"100px", inline:true, href:"#annulerCmdPopup"});	
	});
       $( "a.accepterCmdPopup" ).click(function(){ 
		$(this).colorbox({width:"310px", height:"100px", inline:true, href:"#accepterCmdPopup"});	
	});

	//page redacteur public
	$( "a.wishlistPopup" ).click(function(){ 
		$(this).colorbox({width:"410px", height:"120px", inline:true, href:"#wishlistPopup"});	
	});
	$( "a.blacklistPopup" ).click(function(){ 
		$(this).colorbox({width:"410px", height:"120px", inline:true, href:"#blacklistPopup"});	
	});
	
	// left menu	
	$('.w_leftmenu ul li:nth-child(1), .w_toggle_1 ul li:nth-child(1), .w_toggle_2 ul li:nth-child(1), .w_toggle_3 ul li:nth-child(1), .w_toggle_4 ul li:nth-child(1)').addClass('firstLi');
	$('.w_leftmenu ul ul li:nth-child(1), .w_toggle_1 ul ul li:nth-child(1), .w_toggle_2 ul ul li:nth-child(1), .w_toggle_3 ul ul li:nth-child(1), .w_toggle_4 ul ul li:nth-child(1)').removeClass('firstLi');
	
	$('.w_leftmenu ul li:nth-child(2)').addClass('secondLi');
	$('.w_leftmenu ul ul li:nth-child(2)').removeClass('secondLi');
	$('.w_leftmenu ul ul li:last-child').addClass('last');
	
	$('.w_leftmenu ul li.firstLi h2').click(function(){ 					   
		$('.w_leftmenu ul li.firstLi ul').toggle('blind','',200);
		$('.w_leftmenu ul li.firstLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	
	$('.w_leftmenu ul li.secondLi h2').click(function(){ 					   
		$('.w_leftmenu ul li.secondLi ul').toggle('blind','',200);
		$('.w_leftmenu ul li.secondLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	// left menu search
	$('.w_toggle_1 ul li.firstLi h2').click(function(){ 					   
		$('.w_toggle_1 ul li.firstLi div').toggle('blind','',200);
		$('.w_toggle_1 ul li.firstLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	$('.w_toggle_2 ul li.firstLi h2').click(function(){ 					   
		$('.w_toggle_2 ul li.firstLi div').toggle('blind','',200);
		$('.w_toggle_2 ul li.firstLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	$('.w_toggle_3 ul li.firstLi h2').click(function(){ 					   
		$('.w_toggle_3 ul li.firstLi div').toggle('blind','',200);
		$('.w_toggle_3 ul li.firstLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	$('.w_toggle_4 ul li.firstLi h2').click(function(){ 					   
		$('.w_toggle_4 ul li.firstLi div').toggle('blind','',200);
		$('.w_toggle_4 ul li.firstLi').toggleClass('w_leftmenu_up').next().slideDown();	
	});	
	//
	$('#textarea_maxlength').keyup(function(){
 		limitChars('textarea_maxlength', 2000, 'charlimitinfo');
 	});
	//$('.wysiwyg').wysiwyg();
	if($(".wysiwyg").length > 0) $(".wysiwyg").cleditor({width:"500", height:"250", useCSS:true})[0].focus();
	
	$(".toTop").click(function(event){ 
	//prevent the default action for the click event
	event.preventDefault();
	
	//get the full url - like mysitecom/index.htm#home
	var full_url = this.href;
	
	//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
	var parts = full_url.split("#");
	var trgt = parts[1];
	
	//get the top offset of the target anchor
	var target_offset = $("#"+trgt).offset();
	var target_top = target_offset.top;
	
	//goto that anchor by setting the body scroll top to anchor top
	$('html, body').animate({scrollTop:target_top}, 500);
	}); 

	$("#formID").validationEngine();
       $("#formID1").validationEngine();
	//
	$(".w_list_search_articles ul li:first").addClass("first");
	
	 

	/**** Fil input in focus ***/
   $('.clearMeFocus').each(function() {

      var default_value = this.value;

      $(this).focus(function() {

      if(this.value == default_value) {

      this.value = '';

      }

      });

      $(this).blur(function() {

      if(this.value == '') {

      this.value = default_value;

      }

      });
	  
	});
   /*********************************/
   
   /******Pseudonyme disabled*/
   $('label[for=commande_type_commande_0], label[for=model_commande_type_commande_0]').click(function() {		
		$('.Pseudonyme').attr("disabled", "disabled");
		$('.Pseudonyme').val("");
		$('.Pseudonyme').addClass("disabled");
		$('#afficheNiveau').attr("disabled", "disabled");
		$('#afficheNiveau').addClass("disabled");
		$('#commande_idniveau_redacteur').removeAttr("disabled");
		$('#commande_idniveau_redacteur').removeClass("disabled");
		$('#afficheRedact').attr("style", "display:none");
		$('#idniveauR').attr("style", "display:block");
	});  
   $('label[for=commande_type_commande_1], label[for=model_commande_type_commande_1]').click(function() {				
		$('.Pseudonyme').removeAttr("disabled");
		$('.Pseudonyme').removeClass("disabled");
		$('#commande_idniveau_redacteur').attr("disabled", "disabled");
		$('#commande_idniveau_redacteur').addClass("disabled");
		$('#afficheRedact').attr("style", "display:none");
		$('#afficheNiveau').removeAttr("disabled");
		$('#afficheNiveau').removeClass("disabled");
		$('#idniveauR').attr("style", "display:none");
	});
   if($('input:radio[name=commande[type_commande]]:checked').val()==0 || $('input:radio[name=model_commande[type_commande]]:checked').val()==0){
	   $('.Pseudonyme').attr("disabled", "disabled");
		$('.Pseudonyme').val("");
		$('.Pseudonyme').addClass("disabled");
		$('#afficheNiveau').attr("disabled", "disabled");
		$('#afficheNiveau').addClass("disabled");
		$('#commande_idniveau_redacteur').removeAttr("disabled");
		$('#commande_idniveau_redacteur').removeClass("disabled");
		$('#afficheRedact').attr("style", "display:none");
		$('#idniveauR').attr("style", "display:block");
   }
   if($('input:radio[name=commande[type_commande]]:checked').val()==1 || $('input:radio[name=model_commande[type_commande]]:checked').val()==1){
	   $('.Pseudonyme').removeAttr("disabled");
		$('.Pseudonyme').removeClass("disabled");
		$('#afficheNiveau').removeAttr("disabled");
		$('#afficheNiveau').removeClass("disabled");
		$('#commande_idniveau_redacteur').attr("disabled", "disabled");
		$('#commande_idniveau_redacteur').addClass("disabled");
		$('#idniveauR').attr("style", "display:none");
   }
   
   $('#afficheNiveau').click(function() {
	   $('#afficheRedact').attr("style", "display:none");
	   $('#afficheRedact').html("");
	   afficherNiveauRedacteur($('.Pseudonyme').val());
	   $('#afficheRedact').attr("style", "display:block");
   });
   /******************************/
   /*textarea */
   $('textarea.disabled').click(function() {				
		$(this).removeClass("disabled");
		$(this).val("");
	});
   /**/
   
  /*Ajouter une select / Supprimer une select*/
  function addSup_select(actionSelector, selectorName, lenght, textAdd, textSup, selectWidth){
			
	var numAdd = 1; 
	var numSup = 1; 

	$(actionSelector).click(function() {
									 
		if(numAdd <= lenght){ 
		
				numAdd = numAdd+1;
				$(selectorName+'_'+numAdd).attr("style", "position:inherit; top: inherit;");
				
				/*if(selectWidth) {
					$(selectorName+'_'+numAdd+' .dd').attr("style", "width:"+selectWidth);
					$(selectorName+'_'+numAdd+' .ddChild').attr("style", "z-index: 0; display: none; width:"+selectWidth);
				}*/
				if(numAdd == lenght) {
					numSup = numAdd;
					numAdd = lenght+1;
					$(this).text(textSup);
				}
			}else{ 
				
				$(selectorName+'_'+numSup).attr("style", "position:absolute; top: -1000px;");
				/*$(selectorName+'_'+numSup+' select option:first').attr("selected", "selected");
				$(selectorName+'_'+numSup+' .ddTitleText .ddTitleText').text($(selectorName+'_'+numSup+' select option:first').text());*/
				
				numSup = numSup-1;
				if(numSup == 1) {
					numAdd = numSup;
					$(this).text(textAdd);
				}
			}
	});
  }
  /*addSup_select('.ajouter_une_categorie', '.w_selectbox_', 3, 'Ajouter une cat\351gorie', 'Supprimer une cat\351gorie', '194px');*/
  addSup_select('.ajouter_une_categorie_filtre', '.w_selectbox', 3, 'Ajouter une cat\351gorie', 'Supprimer une cat\351gorie', '160px');
  /*addSup_select('.ajouter_un_diplome', '.Diplome_', 3, '+ Ajouter un diplome', 'Supprimer un diplome', '156px');*/
	/*****************************************/
	
	/*Ajouter une inputfile / Supprimer une inputfile*/
  function addSup_inputfile(actionSelector, selectorName, lenght, textAdd, textSup, selectWidth){
			
	var numAdd = 1; 
	var numSup = 1; 

	$(actionSelector).click(function() {
									 
		if(numAdd <= lenght){ 
		
				numAdd = numAdd+1;
				$(selectorName+numAdd).show();
				
				if(numAdd == lenght) {
					numSup = numAdd;
					numAdd = lenght+1;
					$(this).text(textSup);
				}
			}else{ 
				
				$(selectorName+numSup).hide(); 
				$(selectorName+numSup+' input').val("");
				numSup = numSup-1;
				if(numSup == 1) {
					numAdd = numSup;
					$(this).text(textAdd);
				}
			}
	});
  }
  addSup_inputfile('.ajouter_une_image', '.w_inputfile_', 3, 'Ajouter une autre image', 'Supprimer une image');
  
	/*****************************************/
	
	/*Ajouter une expression / Supprimer une expression*/
  /*function addSup_expression(actionSelector, selectorName, lenght, textAdd, textSup, selectWidth){
			
	var numAdd = 1; 
	var numSup = 1; 

	$(actionSelector).click(function() {
									 
		if(numAdd <= lenght){ 
				
				numAdd = numAdd+1;

				$(selectorName+numAdd+' .dd').attr("style", "width:200px");
				$(selectorName+numAdd+' .ddChild').attr("style", "z-index: 0; display: none; width:200px");
				$(selectorName+numAdd).show();
				
				if(numAdd == lenght) {
					numSup = numAdd;
					numAdd = lenght+1;
					$(this).text(textSup);
				}
			}else{ 
				
				$(selectorName+numSup).hide(); 
				$(selectorName+numSup+' input[type=text]').val("");
				$(selectorName+numSup+' label.checked').removeClass("checked");
				$(selectorName+numSup+' input[type=checkbox]').attr("checked","");
				$(selectorName+numSup+' select option:first').attr("selected", "selected");
				$(selectorName+numSup+' .ddTitleText .ddTitleText').text($(selectorName+numSup+' select option:first').text());
				
				numSup = numSup-1;
				if(numSup == 1) {
					numAdd = numSup;
					$(this).text(textAdd);
				}
			}
	});
  }
  addSup_expression('.ajouter_expression', '#expression_', 3, 'Ajouter une nouvelle expression', 'Supprimer une expression');*/
  
	/*****************************************/						  
	$( ".datepicker" ).datepicker({
		showOn: "button",
		buttonImage: "/images/calendar_icon.jpg",
		buttonImageOnly: true
	}, $.datepicker.regional[ "fr" ] );

	var heightBody = document.getElementById('w_leftcolumn').offsetHeight - 113 ; //alert(heightBody);
	var heightBody2 = document.getElementById('w_leftcolumn').offsetHeight - 225 ; //alert(heightBody2);
	$('.w_home_connecte_client, .w_home_connecte_redacteur').css("height", heightBody+"px");
	$('#w_body_content').css("min-height", heightBody2+"px");
	
	//selectBoxMacWidth();
	
	//Tooltip

	$('.w_list_search_articles li').hover(

	$('.w_list_search_articles li a.w_name').tooltip({
        contents: 'popup_search',
        extraClass: 'popup_search',
		offset: [14, 2],
		effect: 'slide'
	})
	
	);
	
	$('a.en_savoir_plus_enreg').tooltip({
		contents: 'cmd_directe',
		extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
	
	$('a.en_savoir_plus_enreg2').tooltip({
		contents: 'cmd_directe',
		extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
		
	$('a.en_savoir_plus_1').tooltip({
		contents: 'optionOptimisation',
		extraClass: 'popup_search',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
	
	$('a.en_savoir_plus_2').tooltip({
		contents: 'optionRecherche',
		extraClass: 'popup_search',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
	
		$('#id_delai_36').tooltip({
		contents: 'delai_36',
		extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
	
	$('#id_delai_72').tooltip({
		contents: 'delai_72',
		extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } }); 
	
	$('.panier1').tooltip({
        contents: 'peut_faire',
        extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } });
	
	$('.panier2').tooltip({
        contents: 'ne_peut_faire',
        extraClass: 'popup_content',
		offset: [7, 2],
		effect: 'slide'
	}).dynamic({ bottom: { direction: 'down', bounce: true } });
	
});

