var fadeDuration = 150; //time in milliseconds

// apply inline-box only for mozilla
if( jQuery.browser.mozilla ) {
	// do when DOM is ready
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		$( 'form.cmxform' ).hide().find( 'p>label:not(.nocmx):not(.error)' ).each( function() {
			var $this = $(this);
			var labelContent = $this.html();
			var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
			// create block element with width of label
			var labelSpan = $("<span>")
				.css("display", "block")
				.width(labelWidth)
				.html(labelContent);
			// change display to mozilla specific inline-box
			$this.css("display", "-moz-inline-box")
				// remove children
				.empty()
				// add span element
				.append(labelSpan);
		// show form again
		}).end().show();
	});
};

function GetDomaine() { return 'http://'+window.location.hostname+'/'; }

ListeFavoris();

function captcharefresh()
{
  var ran = Math.floor(Math.random()*10);
  var path=document.getElementById("imgsrc").src;
  document.getElementById('imgsrc').src=path+"?rand="+ran; 
}

function ListeFavoris()
{
	$("#bloclistefavorisloader").show();
	$.ajax({
		type: "POST",
		url: GetDomaine(),
		data: 'favorisliste=OK',
		success: function(res)
		{
			$("#bloclistefavoris").html(res)
			if(res == '') setInterval('location.reload()', 1000);
		}
	});
	
	setInterval('$("#bloclistefavorisloader").hide()', 1000);
	return false;
}

function AddProduitFavoris(Id)
{
	var IdSpan = '#' + Id;
	$.ajax({
		type: "POST",
		url: GetDomaine(),
		data: 'favorisajouter=' + Id,
		success: function(r)
		{
			if(r)
			{
				$("#bloclistefavorisInfo").html('Favoris ajouté !');
				$('#bloclistefavorisInfo').addClass("validationfavoris");
				setInterval('location.reload()', 1000);
			} else {
				
				$('#favoriserreur').addClass("validationErrsp");
				setInterval('$("#favoriserreur").removeClass("validationErrsp");', 5000);
			}
		}
	});
	return false;
}

function DelProduitFavoris(Id)
{
	var IdSpan = '#' + Id;
	$.ajax({
		type: "POST",
		url: GetDomaine(),
		data: 'favorissupprimer=' + Id,
		success: function(r)
		{
			if(r) 
			{
				$("#bloclistefavorisInfo").html('Favoris supprimé !');
				$('#bloclistefavorisInfo').addClass("validationErr");
				setInterval('location.reload()', 1000);
			} else {
				$('#bloclistefavorisErr').addClass("validationErr");
				$('#bloclistefavorisErr').html('Vous devez être identifié pour utiliser les favoris ')
			}
		}
	});
	return false;
}

function disableAutocomplete(elementId)
{
    var e = document.getElementById(elementId);
    if(e != null)
    {
        e.setAttribute("autocomplete", "off");
    }
}

/*
 * JQUERY evenements
 */
$(document).ready(function()
{
	/*
	 * DESC : Action sur le input recherche
	 * NOM  : Autocomplete
	 url: GetDomaine() + "arecherche/" + inputString + "/",
	 */
	$("#idrecherche").keyup(function()
	{
		var inputString = this.value;

		if(inputString.length <= 1) {
			$('#suggestionsrechercher').hide();
		} else {
			
			$.ajax({
				type: "POST",
				url: GetDomaine() + "arecherche/",
				data: "motsclefs=" + this.value,
				success: function(msg)
				{
					if(msg == "") { $('#suggestionsrechercher').hide(); } else
					{
						$('#suggestionsrechercher').show();
						$('#suggestionsrechercher').html(msg);
					}
				}
			});
		}
	});

	
	/*
	 * DESC : Action sur le input identification
	 * NOM  : identification
	 */
	$("#rechercheok").click(function()
	{
		var inputString = $("#idrecherche").attr('value');
		inputString = inputString.replace(' ', '_');
		
		if(inputString.length <= 1) return false;
		if ( $("#idrecherche").attr('value') == $("#idrecherche").attr('title')) { return false; }
		if ( $('#id_rechercher_code').attr('value') != false) { window.location = GetDomaine() + 'recherche/'+ inputString + '/'; }
		else { window.location = GetDomaine() + 'recherche/' + inputString + '/'; }
	});
	
	/*
	 * DESC : Formulaire newsletter
	 * NOM  : newsletterform
	 */
	$('#form-newsletter').submit( function() {
		var _state = true;
		var _clientemail = $("#email_newsletter").val();
		if(_clientemail.length <= 1) { $("#newsletterclientid").addClass("focusred"); _state = false; }
		else { $("#email_newsletter").removeClass("focusred"); $("#email_newsletter").addClass("idleField");}

		var partner = $('#email_newsletter_partner').attr('checked')?1:0;
		
		if (_state == false ) { return false; }

		$.ajax({
			type: "post",
			url: GetDomaine(),
			data: "newsletterclientadd="+$("#email_newsletter").val()+"&email_newsletter_partner=" + partner,
			success: function(r)
			{
				if (r)
				{
					$("#retournewslleter").html('Inscription réalisée !');
	   			$("#retournewslleter").addClass("validation");
	   			$("#retournewslleter").fadeOut("slow");
	   			$("#retournewslleter").fadeIn("slow");
				} else { return false; } 
			}
		});
		return false;
	});

	/*
	 * DESC : Imprimer la page
	 * NOM  : Imprimer
	 */
	$("#imprimer").click(function()
	{
		window.print();
	});
	
	/*
	 * DESC : Envoyer cette page dans les favoris
	 * NOM  : AjouterFavo
	 */
	$("#ajouterfavo").click(function()
	{
		if ( navigator.appName != 'Microsoft Internet Explorer' )
		{ window.sidebar.addPanel(document.title,GetDomaine(),document.title); }
		else { window.external.AddFavorite(GetDomaine(),document.title); } 	
	});
	
	/*
	 * DESC : Description des inputs
	 * NOM  : Input Focus
	 */
	$('#colonneDroite input[type="text"]').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';  
		} 
    if(this.value != this.defaultValue){
			this.select();  
		}
	});  
	
	/*
	 * DESC : Description des inputs
	 * NOM  : Input blur
	 */
	$('input[type="text"]').blur(function() {
		$(this).removeClass("focusField").addClass("idleField");
		if (this.value == '')
		{
			this.value = (this.defaultValue ? this.defaultValue : '');  
		}
	});
	
	$('#listePanel li a').hover(function() {
		$(this).animate({ paddingLeft: '30px' }, fadeDuration);
		$(this).children('span').show().animate({ left: -5 }, fadeDuration);
	}, function() {
		$(this).animate({ paddingLeft: '15px' }, fadeDuration);
		$(this).children('span').animate({ left: -35 }, fadeDuration).fadeOut(fadeDuration);          
	});
	
	$("#headermenu").lavaLamp({
		fx: "backout", 
		speed: 700,
		click: function(event, menuItem) {
			//return false;
		}
	});

	$('#listefavoris a').hover(function() {
		$(this).animate({ paddingLeft: '30px' }, fadeDuration);
		$(this).children('span').show().animate({ left: -5 }, fadeDuration);
		$(this).btOn({trigger: 'none',fill: '#FFFFFF' });
	}, function() {
		$(this).btOff();
		$(this).animate({ paddingLeft: '15px' }, fadeDuration);
		$(this).children('span').animate({ left: -35 }, fadeDuration).fadeOut(fadeDuration);          
	});
	
	$('#listefavoris li a').bt({
			trigger: ['focus', 'blur'],
			padding : 2,
			width : '200px',
			positions: ['left']
	});
});

function fill(thisValue,codeproduit,motsclefs) {
	$('#idrecherche').val(codeproduit);
	$('#id_rechercher_code').val(codeproduit); 
	$('#id_rechercher_clefs').val(motsclefs); 
}

function fillGo(codeproduit) {
	window.location = GetDomaine() + 'produit/'+ codeproduit + '/';
}

function verifemail(thisValue)
{
	if (thisValue != '')
	{
		$.ajax({
			type: "get",
			url: GetDomaine() +"index.php",
			data: "clientemail="+thisValue,
			success: function(res)
			{
				return res;
			}
		});
	}
}