function unique(a)
{
   var r = new Array();
   o:for(var i = 0, n = a.length; i < n; i++)
   {
	  for(var x = 0, y = r.length; x < y; x++)
	  {
		 if(r[x]==a[i]) continue o;
	  }
	  if(a[i] != '') r[r.length] = a[i];
   }
   return r;
}

function isDublicate(check) {
	var isD = false;
	var lis = $('#cart li');
	$.each(lis,function(){
			if($(this).attr('title') == check)
				isD = true;
	});
	return isD;
}

var qty = new Array();
function updateQty() {
	$.each($('ul#cart li span'), function() {
		qty.push($(this).text());
	});
}

var products = new Array();
function updateProducts() {
      products = new Array();
      $.each($('ul#cart li'), function() {
		products.push($(this).attr('title'));
      });
      products = unique(products);
      qty = new Array();
      updateQty();
}

function init() {
	if($('#cart').children('li').size() == 1) {
		$('li#noProducts').show();
		$('form#checkout').hide();
	}
        
        $('.more').live('click', function(){
            $target = $(this).parent();
            $target.children('.desc_short').hide();
            $target.children('.desc_long').show();
            $(this).text('Hide...');
            $(this).toggleClass('less');
        });
        
        $('.less').live('click', function(){
            $target = $(this).parent();
            $target.children('.desc_short').show();
            $target.children('.desc_long').hide();
            $(this).text('Read More...');
            $(this).addClass('more');
        });

	// addProduct
	$('.full_price .button').live('click', function(){
		$('li#noProducts').hide();
		var tmp = $(this).parent('div').children('span');
		var qty = $(this).parent('div').children('select');
		var qtty = $(this).parent().children('select').find('option').filter(':selected').text();
		qtty = qtty.split(': ');
                tmp[2] = qtty[1];
                if(!isDublicate(tmp[1].innerHTML)) {
			/*
			var price = tmp[1].innerHTML;
                        var currency = price.substr(price.length-4, price.length);
			price = price.substr(0,price.length - 4);
                        price = $(qty).val() * parseFloat(price);
                        price = price.toFixed(2);
			*/
                        $('#cart').append("<li title=\""+tmp[1].innerHTML+"\">"+"<span style=\"display: none\">"+$(qty).val()+"</span>"+ tmp[0].innerHTML +": "+ qtty[1] +" (<a href=\"#\">Remove</a>)"+"</li>");	
		}
		if($('#cart').children('li').size() <= 1) {
			$('li#noProducts').show();
			$('form#checkout').hide();
		}
		else {
			$('form#checkout').show();
			$('li#noProducts').hide();
		}
		updateProducts();
	});
	
	// removeProduct
	$('ul#cart li a').live('click',function(){
		$(this).parent().remove();
		if($('#cart').children('li').size() <= 1) {
			$('li#noProducts').show();
			$('form#checkout').hide();
		}
		else {
			$('form#checkout').show();
		}
		updateProducts();
	});
	
	// generateCheckout
	$('form#checkout').submit(function(){
		var args = '';
		$.each(products,function(){
				args = args + this + ',';
		});
		$('#prods').val(args.substr(0,args.length - 1));
                
		var qargs = '';
		$.each(qty,function(){
				qargs = qargs + this + ',';
		});
		$('#qtys').val(qargs.substr(0,qargs.length - 1));
	});
}

