
roar = null;
smooth = null;

window.addEvent('domready', function() {
	roar = new Roar({position: 'upperLeft'});
	smooth = new Fx.Scroll(document.body);
});

function string_subtract(str1, str2) { 
	var pos = str1.indexOf(str2); 
	if( pos == -1 )
		return str1; 
	else { 
		var result = str1.substr(0, pos) + str1.substr(pos + str2.length); 
		return result; 
	}
}


var panSupProd = function(id_prod) {
	sid = $('phpsessid').get('value');
	new Request({
		url: '/panier_ajax.php',
		method: 'post',
		data: 'supProd='+id_prod+'&sid='+sid,
		onSuccess: function (rt, rx) {
			if($('t_panier').getElements('tr').length == 3) {
				$('t_panier').dispose();
				$('contenuGauche').getFirst().dispose();
			} else {
				$('pan_'+id_prod).dispose();
				$('pan_cout_total').set('html',rt+'&euro;');
			}
		}
	}).send();

	return false;
}


var panChgQte = function(id_prod, el) {
	sid = $('phpsessid').get('value');
	qte = el.get('value');
	if (qte == '')
		return false;
	if (qte == 0)
		panSupProd(id_prod);
	else
		new Request({
			url: '/panier_ajax.php',
			method: 'post',
			data: 'chgQte='+id_prod+'&qte='+qte+'&sid='+sid,
			onSuccess: function (rt, rx) {
				$('pan_cout_total').set('html',rt+'&euro;');
			}
		}).send();
}

var panAjtProd = function(id_prod) {
	sid = $('phpsessid').get('value');
	
	new Request({
		url: '/panier_ajax.php',
		method: 'post',
		data: 'ajtProd='+id_prod+'&sid='+sid,
		evalScripts: true,
		onSuccess: function (rt, rx) {
			if(rt.indexOf('Prod') == 0) {
				return true;
			}
			if(!$('t_pan_tbody'))
				create_t_pan();
			$('t_pan_tbody').set('html',rt);
			$$(".pan_qte").each(function(item){
				item.addEvent('keyup',function() {
					panChgQte(this.getProperty('alt'),this);
					event.returnValue = false;
					return false;
				});
			});
		}
	}).send();

	smooth.toElement('contenuGauche');
	setTimeout('roar.alert("Ajout&eacute; !", "Ce produit a bien &eacute;t&eacute; ajout&eacute; &agrave; votre panier.")',700)

	return false;
}

var create_t_pan = function() {
	t_panier = new Element('div').set('id','t_panier').set('html','<table id="prod_list"><thead><tr><th colspan="2">Produit</th><th>Prix</th><th>Qte</th><th></th></tr></thead><tbody id="t_pan_tbody"> </tbody></table><a href="/panier/" id="pan_detail"></a>').inject('contenuGauche','top');
	pan_span = new Element('span').set('class','ongletMenuGauche').set('html','Mon panier').inject('contenuGauche','top');
}

var panGetTotal = function() {
	sid = $('phpsessid').get('value');
	
	new Request({
		url: '/panier_ajax.php',
		method: 'post',
		data: 'getTotal='+id_prod+'&sid='+sid,
		onSuccess: function (rt, rx) {
			$('pan_cout_total').set('html',rt+'&euro;');
		}
	}).send();

	return false;
}

