// JavaScript Document
function initcarrello(){
	var limit = 16;
	if($.cookie('chart')){
		if($('#carrello').size() > 0){
			var b = $('#carrello .products');
		} else{
			var b = $('body #sidebar');
			$('.languages').after('<li id="carrello"></li>');
			b = $('#carrello');
			b.append('<h4>Shopping cart</h4>');
			b.append('<div class="products">empty</div>');
			b.append('<h5 class="checkout"><a href="?p=331">Checkout &raquo;</a></h5>');
		}
		if($.cookie('prodotti')){
			var b = $('#carrello .products');
			$('#carrello .products').empty();
			data = $.cookie('prodotti').split(",");
			prices = $.cookie('prezzi').split(",");
			id = $.cookie('id').split(",");
			for(var i=0; i<data.length; i++){
				if(data[i].length > limit){ data[i] = data[i].substring(0,limit)+'...'}
				b.append("<p> <a href=\"?p="+id[i]+"\">"+data[i]+"</a><span> "+prices[i]+"&euro;</span><a href=\"#\" class=\"remove\" rel=\""+i+"\">[x]</a></p>");
			}
			$('#carrello .remove').click(function(){
				removeproduct(parseInt($(this).attr('rel')));
				return false;
			});
		}
	}
}
function pushproduct(prodotto,prezzo,id,ship){
	if(!$.cookie('chart')){ $.cookie('chart','true'); };
	if($.cookie('prodotti')){
		$.cookie('prodotti',$.cookie('prodotti')+','+prodotto);
		$.cookie('prezzi',$.cookie('prezzi')+','+prezzo);
		$.cookie('id',$.cookie('id')+','+id);
		$.cookie('spedizione',$.cookie('spedizione')+','+ship);
	}else{
		$.cookie('prodotti',prodotto);
		$.cookie('prezzi',prezzo);
		$.cookie('id',id);
		$.cookie('spedizione',ship);
	}
	initcarrello();
}
function removeproduct(n){
	data = $.cookie('prodotti').split(",");
	prices = $.cookie('prezzi').split(",");
	id = $.cookie('id').split(",");
	spedizione = $.cookie('spedizione').split(",");
	datan = new Array ();
	pricesn = new Array ();
	idn = new Array ();
	spedizionen = new Array ();
	for(var i=0; i<data.length; i++){
		if(i!==n){
			datan.push(data[i]);
			pricesn.push(prices[i]);
			idn.push(id[i]);
			spedizionen.push(spedizione[i]);
		}
	}
	if(datan.length == 0){
		$('#carrello').remove();
		clearecookie();
	}else{
		d = datan.join(",");
		p = pricesn.join(",");
		ids = idn.join(",");
		s = spedizionen.join(",");
		$.cookie('prodotti',d);
		$.cookie('prezzi',p);
		$.cookie('id',ids);
		$.cookie('spedizione',s);
		$('#carrello .products').empty();
		initcarrello();
	}
}
function clearecookie(){
	$.cookie('prodotti',null);
	$.cookie('prezzi',null);
	$.cookie('id',null);
	$.cookie('spedizione',null);
	$.cookie('chart',null)
}
$(document).ready(function(){
	if(!(jQuery.browser.msie && jQuery.browser.version == "6.0")){
	
	
	
	
	$('form.store').submit(function(){
		title = '';
		size = '';
		option = '';
		ship = '';
		id = '';
		price = 0;
		
		if($(this).children('.abbonamenti').length > 0){
			var value = $(this).children('.abbonamenti').children('option:selected').val();
			value = value.split("-");
			title = value[0];
			ship = $(this).children('.ship').val();
			id = $(this).children('.ids').val();
			price = value[1];

		}else{
			if($(this).children('.taglia').length > 0){
				size = " - " + $(this).children('.taglia').children('option:selected').val();
			}
			if($(this).children('.opzione').length > 0){
				option = " - " + $(this).children('.opzione').children('option:selected').val();
			}
			price = $(this).children('.price').val();	
			
			title = $(this).children('.title').val();
			title = title + size + option;
			
			ship = $(this).children('.ship').val();
			
			id = $(this).children('.ids').val();
			
		};	
		
		
		
		pushproduct(title,price,id,ship);
		return false;
	});
	if($('form.store').length >0){initcarrello()};
	};
	if($('#chart').children('form').length >0){
		$('#chart .remove').click(function(){
			var product = parseInt($(this).attr('rel'));
			$('#chart .submit').remove();
			$(this).parent().parent().fadeOut('slow',function(){
				removeproduct(product);
				location.reload(); 
				$(this).remove();
			});
		});
	}
});
