// JavaScript Document

function update_total(html)
{
	$('.cart-info').html(html);
}
function show_cart()
{
	$.ajax({
		cache: false,
		url: "/index.php/cart/viewcart",
		success: function(html){
			$('#cart-products').html(html);
			$('#cart-status').css('display', 'none');
		}
	});
	$('#cart').animate({height:'400px'}, {"duration": "slow", "easing": "easeOutExpo"});
	$('#cart').addClass('open');
	$('#cart').removeClass('closed');
}
	
function close_cart()
{
	$('#cart').animate({height:'30px'}, {"duration": "slow", "easing": "easeOutExpo"});
	$('#cart').addClass('closed');
	$('#cart').removeClass('open');
}

$(".qty-box").live('focus', function(){
	var cur = $(this).val();
	$(this).val('');
});
	
$(".qty-box").live('focusout', function(){
	if($(this).val() == ''){
		$(this).val('1');
	}
	var qty = $(this).val();
	var id 	= $(this).attr('name');	
	var id	= id.replace("qty_", "");
	
	$('#cart-status').css('display', 'block');
	$.ajax({
		cache: false,
		type: "POST",
		url: "/index.php/cart/update",
		data: "qty="+qty+'&id='+id,
		success: function(html){
			show_cart();
			update_total(html);
		}
	});
	
	return false;
});

$(document).ready(function()
{
	
	$('.addtocart').click(function(){
		
		var prodid	= $(this).attr('id');		
		var product	= prodid.replace("buy_", "");
		$('#cart-status').css('display', 'block');
		$.ajax({
			cache: false,
			type: "POST",
			url: "/index.php/cart/add",
			data: "product="+product,
			success: function(html){
				show_cart();
				update_total(html);
			}
		});
	
	});
	
	$('.remove-btn').live('click', function() {

		var cid	= $(this).attr('id');		
		var cid	= cid.replace("clear_", "");
		$('#cart-status').css('display', 'block');
		$.ajax({
			cache: false,
			type: "POST",
			url: "/index.php/cart/remove",
			data: "item="+cid,
			success: function(html){
				show_cart();
				update_total(html);
			}
		});
	
	});
});
