
function show_decrease( div, id )
{
	new Ajax.Updater( div , '/ajax.php?action=get_decrease&id=' + id, { 
		method: 'get', 
		onSuccess: function() { new Effect.toggle(div, 'Slide', {duration: 0.5}) }
		});
}

function toggleSlide( div )
{
	div.toggle(250);
}
function toggle( div )
{
	$(div).toggle(250);
}


function track( id_prod, id_cat, item_type )
{
	// jQuery Method
	$.fn.colorbox({href:'/ajax.php?action=track&item=' + id_prod + '&cat=' + id_cat + '&type=' + item_type, open:true});
}



/* Nouvelles fonctions jQuery */
$(document).ready(function(){
	
	$(".add_input .plus").click(function(){
		var target = $(this).parent().parent().find(".qte_inp");
		var value = parseInt(target.val());
		var multiple = $(this).attr("rel");
		value += 1 * multiple;
		target.val(value);
		return false;
	});
	$(".add_input .minus").click(function(){
		var target = $(this).parent().parent().find(".qte_inp");
		var value = parseInt(target.val());
		var multiple = $(this).attr("rel");
		if (value > 1) { 
			value -= 1 * multiple;
			target.val(value);
		}
		return false;
	});
	
	// Autocompleter
	function formatItem(row) {
		return row[0] + "<br /><span>" + row[1] + "</span>";
	}
	function formatResultSearch(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	function formatResultClient(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}
	function log(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	// fonction autoscroll
	{{{

	(function($){
	$.fn.autoscroll = function() {
	jQuery('html,body').animate(
	{
	scrollLeft: this.offset().left,
	scrollTop: this.offset().top
	},
	500
	);
	return this;
	};
	})(jQuery);
	
	}}}
	
	// fonction d'ajout panier
	$('a.add2cart').click(function(){
		var id = $(this).closest(".product").attr("rel");
		var qte = $('#qte_'+id).val();
		var type = '';
		if ($(this).closest("#purchase_options").attr("rel") == "fo") {
			qte = 1;
			type = "fo";
		}
		var url = '/ajax.php?action=add2cart&item=' + id + '&qte=' + qte + '&type=' + type;
		
		$.getJSON(url, function(json){
			var html = '<div><h2>Ajout&eacute; au panier</h2>';
			if (json.response == true) {
				html += '<p><img src="http://images.beauty-tech.fr/catalog/products/small/' + json.id + '.jpg" />';
				html += '<strong>' + json.libelle + '</strong><br />';
				//html += 'a &eacute;t&eacute; ajout&eacute; &agrave; votre panier.<br />';
				html += 'Quantit&eacute : <strong>' + json.qte + '</strong><br />';
				html += 'Prix : <strong>' + json.prix + ' &euro;</strong><br />';
				html += '<br />';
				html += 'Total commande : <strong>' + json.subtotal + ' &euro;</strong><br />';
				html += '<p><hr />';
			}
			else {
				html += "<p>Erreur d'enregistrement dans le panier.</p>";
			}
			html+= '<p><a href="#" class="button close">Continuer vos achats</a>';
			html+= '<a href="/cart" class="button cart">Voir votre panier</a></p><hr /></div>';
			
			$("#ajax_data").html(html); // remplissage de la div cachée avec le résultat formaté de la requête Ajax JSON
		});
		
		$("#ajax_data").addClass("ajax_response");
		
		$("#ajax_data").overlay({ 
		    expose: { 
		        color: '#fff', 
		        loadSpeed: 200, 
		        opacity: 0.5 
		    }, 
		    closeOnClick: false, 
		    api: true 
		}).load();

		$('#num_items').load('/ajax.php?action=get_num_items');
	});
	
	$('#ajax_data .close').live("click", function(){
		$("#ajax_data").overlay().close();
	});
	
	
	$('#item').autocomplete('/ajax.php?action=search_autocompletion', {
		width : 200,
		cacheLenght : 1,
		formatItem: formatItem,
		formatResult: formatResultSearch
	});
	
	$('#item2').autocomplete('/ajax.php?action=search_autocompletion', {
		width : 200,
		cacheLenght : 1,
		formatItem: formatItem,
		formatResult: formatResultSearch
	});
	
	$('#client_selector').autocomplete('/ajax.php?action=client_autocompletion', {
		width : 200,
		cacheLenght : 1,
		formatItem: formatItem,
		formatResult: formatResultClient
	});
	$("#client_data").result(log).next().click(function() {
		$(this).prev().search();
	});
	
	//prise de commande
	$('#type_liv .envoi').click(function(){
		$('#retrait').hide();
		$('#envoi').show(500);
	});
	$('#type_liv .retrait').click(function(){
		$('#retrait').show(500);
		$('#envoi').hide();
	});

});


