
var jscart = [];

var jcartTotal;

function jSCart( article, price, id, popt, special ) {
	
	range = ( special > 9999 ) ? special : id;
	
	this.cart = {"article":article, "price":price, "id":id, "popt":popt, "special":special};
	
	jscart.push(this.cart);
	
	// alert('length : ' + jscart.length);
}

function deleteUnique (current, newart) {
	var breaking = false;
	jscart.each(function( obj, key ) {
		if ( breaking == true ) return breaking;
		if ( current == obj.special ) {
			// delete jscart[key];
			// alert('replace by' + newart.define + ' on key : ' + key);
			newobj = {"article":newart.define, "price":newart.price, "id":newart.key, "popt":newart.popt, "special":newart.special};
			jscart.splice(key, 1, newobj);
			breaking = true;
		}
	});
	return breaking;
}
function deleteSingle (current) {
	jscart.each(function( obj, key ) {
		if ( current == obj.id ) {
			delete jscart[key];
		}
	});
}

var cartRequest = new Request.JSON({
	method: 'get',
	url: '/pack-order.php',
	link: 'cancel',
	noCache: true,
	autoCancel: true, 
	evalScripts: true,
	onRequest: function(html) {
		// $('companyloader').setStyle('background-image', 'url(/images/admin/ajax-loader.gif)');
		// $('companyloader').setStyle('display', 'block');
	},
	onSuccess: function(html) {
		
	},
	onFailure: function() {
		alert('The request failed.');
	}
});

function transmit () {
	//cartRequest.get({'json':JSON.encode(jscart).toBase64()});
	$('jsoncart').value = JSON.encode(jscart).toBase64();
	// return MOOdalBox.click(null, $('theform'));

}


displayJsCart = function() {
	
	var jcartTotal = 0;
	
	jcartTotal.toFloat();
	$('mjscart').erase('html');
	$('mjscartspecs').erase('html');
	$('mjscartsolution').erase('html');
	
		
	jscart.each(function( obj, key ) {
		/*
		alert('key : ' + key + ' object : ' + obj + "\n\n"
			+ 'special : ' + obj.special + "\n"
			+ 'article : ' + obj.article + "\n"
			+ 'price : ' + obj.price + "\n"
			+ 'id : ' + obj.id
		);
		*/
		line  = new Element('p');
		article = new Element( 'span', {'class':'define'+(obj.special===1000000?' special':''),'html':'&raquo; '+obj.article} );
		breaker  = new Element('span', {'class':'breaker','html':'&nbsp;'});
		
		article.inject(line);
		breaker.inject(line);
		
		if (obj.special===1000000){
			line.inject($('mjscartsolution'));
		}
		else {
			line.inject($('mjscartspecs'));
		}
		
		
		totalArt = obj.price.toFloat();
		jcartTotal = jcartTotal + totalArt;
	});
	
	if ( jscart.length > 0 ) {
		buttonAdd		= new Element('input', {'id':'buttontocart','type':'submit','name':'gotocart','value':'Demande de devis','class':'fright'});
		buttonAdd.addEvent( 'click',transmit);

		buttonDevis		= new Element('p', {'id':'buttontodevis','html':'GRATUIT !! Effectuer une demande de devis ou réservez immédiatement votre pack e-commerce, vitrine','class':'fleft'});
		breaker		= new Element('span', {'class':'breaker','html':'&nbsp;'});
		
		total = new Element( 'strong', { 'html': '' + jcartTotal.numberFormat(2, '.', ' ') + ' &euro; HT' } );
		total.inject($('mjscart'));
		buttonAdd.inject($('mjscart'));
		buttonDevis.inject(buttonAdd, 'before');
		breaker.inject($('mjscart'));
		
	}
	
}

addToCart = function(articleObj) {
	articleObj.each(function(art) {
		$(art.key).addEvent('click', function(e){
			if ( this.type == 'radio' ) {
				if ( this.checked == true ) {
					isDeleted = deleteUnique(art.special, art);
					if ( isDeleted == false ) {
						new jSCart(art.define, art.price, art.key, art.popt, art.special);
					}
					
					displayJsCart();
				}
			}
			else if ( this.checked == true && this.type == 'checkbox' ) {
				new jSCart(art.define, art.price, art.key, art.popt);
				displayJsCart();
				
			}
			else if ( this.checked == false && this.type == 'checkbox' ) {
				deleteSingle(art.key);
				displayJsCart();
			}
			else {
			
			}
			
		});
	});
};
