//	Couleurs evenements cart

var colorCartTransit = 'E487B4';
var colorCartOver = 'fceff6';
var colorCartOut = 'FFFFF';

/*
var colorCartTransit = 'background-color: #E487B4';
var colorCartOver = 'background-color: #fceff6';
var colorCartOver = 'background: #E487B4';
*/
/*
var colorCartOut = 'width:710px; background:url(images/fond/topletters.gif) left top no-repeat';
*/
//var total = 0;
//var totallettres = 0;
var itemCard = null;

window.addEvent('domready', function(){
	initTabCalculVolume();	
});

function initTabCalculVolume( ){					
	var drop = $('catalogproducts-word2');
	drop.removeEvents();
	var dropFx = drop.effect('background-color', {wait: false});


	
	$$('.catalogproducts-letters .item').each(function(item){
		
item.addEvent('dblclick', function(e) {
			
			var clone = this.clone()
			
			productkey = parseInt(productkey) + 1;
			new_product_name = $('productname-'+clone.id).value;
			new_product_id = $('productid-'+clone.id).value;
			new_product_price = $('productprice-'+clone.id).value;
			
			function setTextTotal( total, totallettres ){
				$('totalprice').innerHTML='<strong>Total :</strong> ' + total + ' € TTC';
			}	
			
			var itemcard = clone.inject(drop).addEvent('click', function(){
						
						total = Math.round( (parseFloat( total ) - parseFloat( new_product_price ) ) * 100 ) / 100;						
						totallettres = parseInt( totallettres ) - 1;
						
						setTextTotal( total, totallettres );
						
						//dropFx.start( colorCartTransit ).chain(dropFx.start.pass( colorCartOut, dropFx ));
						
						this.remove();
						
					});
								
			var new_product_input_text = new Element( 'span' ).injectInside(itemcard) ;
								
			new Ajax('addproductinfos.php', {
				method: 'get',
				data : {name: new_product_name, id: new_product_id, productkey: productkey},
				update: new_product_input_text
				// onComplete: function() {
				// }
			}).request();					

			total = Math.round( (parseFloat( new_product_price ) + parseFloat( total ) ) * 100 ) / 100;
			totallettres = parseInt( totallettres ) + 1;
			setTextTotal( total, totallettres );

			//suppression de l'ancien Hr et ajout du nouveau hr
			
			drop.removeChild(document.getElementById('catalogproducts-hr'));

			nouveauhr = document.createElement('hr') ;
			nouveauhr.setAttribute('id','catalogproducts-hr') ;
		    nouveauhr.setAttribute('class','clear') ;
			nouveauhr.injectInside(drop) ;
			drop.setAttribute('style','width:710px; background:url(images/fond/topletters.gif) left top no-repeat');

		});
		
		
		item.addEvent('mousedown', function(e) {
			document.body.style.cursor='not-allowed';
			
			
			//alert(e.detail);
			//var x = drop.getLeft() + document.body.scrollLeft;
			//var y = drop.getTop() + document.body.scrollTop;		
			
			//alert(y);
			
			//drop.setStyles({'position': 'absolute', 'left': x+'px', 'top': y+'px'});
			
			e = new Event(e).stop();
					
			var clone = this.clone()
				.setStyles(this.getCoordinates())
				.setStyles({'opacity': 0.7, 'position': 'absolute'})
				.addEvent('emptydrop', function() {
					this.remove();
					drop.removeEvents();
					document.body.style.cursor='auto';
					
				}).inject(document.body);

			
			drop.addEvents({
				'drop': function() {	

					document.body.style.cursor='auto';
					
					productkey = parseInt(productkey) + 1;
					new_product_name = $('productname-'+clone.id).value;
					new_product_id = $('productid-'+clone.id).value;
					new_product_price = $('productprice-'+clone.id).value;
													
					
					function setTextTotal( total, totallettres ){
						//$('totalprice').setText( "<strong>Total :</strong> " + total + ' € TTC' );
						//$('totalqte').setText( "<strong>Nombre de lettres :</strong> " + totallettres );
						$('totalprice').innerHTML='<strong>Total :</strong> ' + total + ' € TTC';
						//$('totalqte').innerHTML='<strong>Nombre de lettres :</strong> ' + totallettres;
						
					}				
					
					clone.remove();
					drop.removeEvents();
					
					itemCard = item.clone().inject(drop).addEvent('click', function(){
						
						total = Math.round( (parseFloat( total ) - parseFloat( new_product_price ) ) * 100 ) / 100;						
						totallettres = parseInt( totallettres ) - 1;
						
						setTextTotal( total, totallettres );
						
						//dropFx.start( colorCartTransit ).chain(dropFx.start.pass( colorCartOut, dropFx ));
						
						this.remove();
						
					});
					
					var new_product_input_text = new Element( 'span' ).injectInside(itemCard) ;
										
					new Ajax('addproductinfos.php', {
						method: 'get',
						data : {name: new_product_name, id: new_product_id, productkey: productkey},
						update: new_product_input_text
						// onComplete: function() {
						// }
					}).request();					
					
					//dropFx.start( colorCartTransit ).chain(dropFx.start.pass( colorCartOut, dropFx ));
					
					//total = Math.round( (parseFloat( $(clone).id ) + parseFloat( total ) ) * 100 ) / 100;
					total = Math.round( (parseFloat( new_product_price ) + parseFloat( total ) ) * 100 ) / 100;
					totallettres = parseInt( totallettres ) + 1;
					setTextTotal( total, totallettres );

					//suppression de l'ancien Hr et ajout du nouveau hr
					
					drop.removeChild(document.getElementById('catalogproducts-hr'));
		
					nouveauhr = document.createElement('hr') ;
					nouveauhr.setAttribute('id','catalogproducts-hr') ;
				    nouveauhr.setAttribute('class','clear') ;
					nouveauhr.injectInside(drop) ;
				    //document.getElementById("lesp").insertBefore(nouveaup,document.getElementById("explik"));
					//this.setAttribute('style','background : url(../images/fond/topletters.gif)') ;
					this.setAttribute('style','width:710px; background:url(images/fond/topletters.gif) left 0px top 0px no-repeat');

					//itemCard.
	
					
				},
				'over': function() {
				
					document.body.style.cursor='crosshair';
					//dropFx.start( colorCartOver );
					
				},
				'leave': function() {
					document.body.style.cursor='not-allowed';
					//dropFx.start( colorCartOut );
					//this.setAttribute('style','width:710px; background:url(images/fond/topletters.gif) left top no-repeat');
					
				}
			});
	 
			var drag = clone.makeDraggable({
				droppables: [drop]
			});
			
			drag.start(e);
			
		});
		
		
	
		
	});

}

function removeLastItem(){
	
	itemCard.remove();
	return false;
	
}

function removePreSelectedItem( itemToRemove, price ) {
	
	// mise à jour du prix et du nombre de produits
	total = Math.round( ( parseFloat( total ) - parseFloat( price ) ) * 100 ) / 100;
	//totallettres = parseInt( totallettres ) - 1;
	
	$('totalprice').setText( 'Total : ' + total + ' € TTC' );
	//$('totalqte').setText( 'Nombre de lettres : ' + totallettres );	
	
	// suppression de la lettre
	itemToRemove.parentNode.removeChild(itemToRemove);
}


function getCart(){
	
	var text = '';
	$$('.letterscart .item').each(function(item){
		text += item.id + ' ' + item.getFirst().getText() + "\n";
	});
	
	alert( text );
}