// JavaScript Document
var offsetStart = {};

function conf(msg){
	var message =  ( msg ) ? msg : 'Etes-vous certain ?';
	return confirm( message )	;
}

function getPage(element, url){	
	//element.hide();	
	element.load(url); 	
	//element.fadeIn('fast');	
}


function bindLinks( src, element ){
	
	$('a', src).click(function(){
		
		getPage( element, $(this).attr('href'));
		return false;								   
	});		
}


function refreshPanier(){
	
	$("#bloc_panier").load('/index.php?cmd=refreshPanier');

}

function addPanier(  container, pid ){
	
	var produit = $(container); // $("#produit_"+ pid );
	
	var  option_ids = $(".option" , produit);	
	var option_url = '';	
	option_ids.each(function(){
		option_url += '&option_value_ids[]='+ $(this).val();				 
	});

	//var  quantite = $('#id_quantite_'+ pid );
	var quantite = $('input[@type=text].quantite', produit );	
	$.ajax({
		url: '/index.php',
		data: 'cmd=addPanier&produit_id=' + pid + '&quantite='+ quantite.val() + option_url,
		type: 'post',
		 
		success: function(data , produit){				
			
			refreshPanier();
			$("#bloc_panier").fadeIn("slow");				
			$('input[@type=text].quantite', $(container) ).val(1);				
			updatePrix(pid);	
	
		}		
	});
	
}

/* Comme avec addPanier, mais avec un pas incremental différent */
function addPanier_pas(  container, pid, pas ){
	
	var produit = $(container); // $("#produit_"+ pid );
	
	var  option_ids = $(".option" , produit);	
	var option_url = '';	
	option_ids.each(function(){
		option_url += '&option_value_ids[]='+ $(this).val();				 
	});

	//var  quantite = $('#id_quantite_'+ pid );
	var quantite = $('input[@type=text].quantite', produit );	
	$.ajax({
		url: '/index.php',
		data: 'cmd=addPanier&produit_id=' + pid + '&quantite='+ quantite.val() + option_url,
		type: 'post',
		 
		success: function(data , produit){				
			
			refreshPanier();
			$("#bloc_panier").fadeIn("slow");				
			$('input[@type=text].quantite', $(container) ).val(pas);				
			updatePrix(pid);	
	
		}		
	});
	
}
/*
function updatePanier( container, pid  ){

	var produit = $("#produit_"+ pid );
	
	var  option_ids = $(".option", produit);	
	var option_url = '';	
	option_ids.each(function(){
		option_url += '&option_value_ids[]='+ $(this).val();				 
	});

	var quantite = $('input[@type=text].quantite', produit );	
//	var  quantite = $('#id_quantite_'+ pid );
	$.ajax({
		url: '/index.php', 
		data: 'cmd=updatePanier&produit_id='+ pid +'&quantite='+ quantite.val()  + option_url, 
		type : 'post',
		
		success : function(data){ 
		
			refreshPanier();
			$("#bloc_panier").fadeIn();
			$('#id_quantite_'+ pid ).val(1);
		} 
	});		
}*/

function deletePanier( pid  ){

	$.ajax({
		url: '/index.php', 
		data: 'cmd=deletePanier&produit_id='+ pid , 
		
		success : function(data){ 
		
			refreshPanier();
			$("#bloc_panier").fadeIn("slow");
		} 
	});	
}

function update_qte( container, pid , operator ){

	var produit = $(container); // $("#produit_"+ pid );

	//var quantite =  $('#id_quantite_'+ pid );		
	var quantite = $('input[@type=text].quantite', produit );	
	var quantite_total = $('#quantite_total').val();
	var current_value =  parseInt( quantite.val());		
	
	switch ( operator ){
		
		case '+' :
			new_value = current_value + 1;
			if (quantite_total-new_value == -1 )
			{
				alert("plus de stock");
			}
			else { 
			 if (new_value > 0) {
				quantite.val( new_value );
				}
			}	
			break;
			
		case '-' :		
			new_value = current_value - 1;
			if (new_value > 0)
				quantite.val( new_value );
				
			break;
	}

	updatePrix( container , pid);
}

function update_qte_pata( container, pid , operator , var_focus){

	var produit = $(container); // $("#produit_"+ pid );

	//var quantite =  $('#id_quantite_'+ pid );		
	var quantite = $('input[@type=text].quantite', produit );	
	var quantite_total = $('#quantite_total').val();
	var current_value =  parseInt( quantite.val());		

	switch ( operator ){
		
		case '+' :
			if (var_focus != null)
			{
			new_value = var_focus;
			}
			else
			{
			new_value = current_value + 1;
			}
			if (quantite_total-new_value < 0  )
			{
				alert("plus de stock");
				quantite.val( quantite_total );
			}
			else { 
			 if (new_value > 0) {
				quantite.val( new_value );
				}
			}	
			break;
			
		case '-' :		
			new_value = current_value - 1;
			if (new_value > 0)
				quantite.val( new_value );
				
			break;
	}

	updatePrix( container , pid);
}


function update_qte_pas( container, pid , operator, pas ){

	var produit = $(container); // $("#produit_"+ pid );

	pas = parseInt(pas);

	//var quantite =  $('#id_quantite_'+ pid );		
	var quantite = $('input[@type=text].quantite', produit );	
	
	var current_value =  parseInt( quantite.val());		
	
	switch ( operator ){
		
		case '+' :
			new_value = current_value + pas;
			if (new_value > 0)
				quantite.val( new_value );
				
			break;
			
		case '-' :		
			new_value = current_value - pas;
			if (new_value > 0)
				quantite.val( new_value );
				
			break;
	}

	updatePrix( container , pid);
}

function updatePrix( container,  pid ){	
	
	var produit = $(container); //$("#produit_"+ pid );

	var prix_ttc=  $('.prix_ttc' , produit );
	var prix_ht =  $('.prix_ht' , produit );
	
	var quantite = $('input[@name=quantite]', produit );
	
	var current_value =  parseInt( quantite.val() );		
	
	$.getJSON("/index.php", 
		{ cmd: 'getPrixRemise' , produit_id : pid , quantite : quantite.val()   }, 		
		function(data ){ 
						
			if ( parseInt(data['prix_ht']) > 0 &&  parseInt(data['prix_ttc']) > 0 ) {				
			
				//alert(parseInt(prix_ht.val()) +' '+  parseInt(data['prix_ht']));			
			
				if ( parseInt(prix_ht.val()) !=  parseInt(data['prix_ht']) && parseInt(prix_ttc.val()) !=  parseInt(data['prix_ttc']))	{
					prix_ht.highlightFade({attr:'color',color:'white'});
					prix_ttc.highlightFade({attr:'color',color:'white'});
				}
				
				prix_ht.html(data['prix_ht']);
				prix_ttc.html(data['prix_ttc']);
				
			}
		} 
	);
	
}

function setMenu(element){
	
		element.hover(
		function(){ 
			var uls = $('> ul', this);
			uls.show();
			$(this).addClass('hover');
		}, 
		function(){ 
			var uls = $('> ul', this);
			uls.hide();
			$(this).removeClass('hover');
		}
	);
}


/* jquery / dimension */

function setPosition( element ){
	var innerHeight = $(window).innerHeight() ;
	var scrollTop = $(window).scrollTop() ;
	

	
	var offset = {};
	element.offset({scroll:true}, offset);
	var positionTop = offset['top'] + scrollTop;
//	element.offset({scroll:false}, offset);
	
	//var positionTop = (scrollTop) ? parseInt( scrollTop /2)-25  : parseInt( innerHeight /2)-25 ;
	
	//alert(element.css('padding-top') + ' ' + offset['top'] +' '+scrollTop);
	if (  offsetStart['top']   < scrollTop)
		element.css('padding-top', (scrollTop -  offsetStart['top']) +'px'  );
	else
		element.css('padding-top', '0px'  );
}



/* jquery / autocomplete */

function findValueCallback(event, data, formatted) {
	//$("<li>").text( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	$("#frmRecherche").submit();
}

function formatItem(row) {
	var row = row.split("|");
	return row[0] + " (id: " + row[1] + ")";
}

function formatResult(row) {
	return row[0];
}





// Chargement du document
function ready(){
	
	setMenu($('#menu li'));
	bindLinks($('#menu .sitadom') , $('#content') );
	
	// Cacher sous menu
	var uls = $('> ul', '#menu li');
	uls.hide();
	
	$(".focus").focus();		

	// Positionner le panier
	$("#bloc_panier").offset({scroll:true}, offsetStart);
	
	// Event manager on : scroll
	$(window).scroll( function(){setPosition($("#bloc_panier"))});	
	
	
	/* NEWS - BETA
	function(){ 
		$('#news').innerfade({ 
			animationtype: 'slide', 
			speed: 750, 
			timeout: 2000, 
			type: 'random', 
			containerheight: '1em' 
		});
		
		$('#portfolio').innerfade({ 
			speed: 'slow', 
			timeout: 4000, 
			type: 'sequence', 
			containerheight: '220px' 
		}); 
		
		$('.fade').innerfade({ 
			speed: 'slow', 
			timeout: 1000, 
			type: 'sequence', 
			containerheight: '1.5em'
		}); 
	};
	*/
	
	/* AUTOCOMPLETE - BETA */
	$("#input_recherche").autocomplete(
		
		"index.html?cmd=search" , 
		{
			delay: 200,
			width:200,
			minChars: 2,
			max :15,
			selectFirst: false
		}
	);

	
	//bindImageThumb();
	//$(window).scroll( function(){setPosition($("#menu"))});	
	
	
	//$('a, input, img').Tooltip();// exempl
	/*
	$('#menu a').Tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " - ",
		extraClass: "fancy",
		fixPNG: true
	});
	*/
	
	// Update Quantite
		
	
	//refreshPanier();
	
	/*
	$(".recherche .recherche_input").click(function(){
		$(this).select();
		$(this).focus();
	});
	*/
	
	//$(document).pngFix(); 
}
