$(document).ready(function(){
	var emptyCart='<div style="margin: 5px 15px; padding: 10px; background-color: rgb(255, 245, 0); text-align: center; position: relative; -moz-border-radius-topleft: 5px; -moz-border-radius-topright: 5px; -moz-border-radius-bottomright: 5px; -moz-border-radius-bottomleft: 5px;"><strong>Giỏ hàng của quý khách thì trống</strong>.<br/><br/>Nếu quý khách không thể thêm bất cứ sản phẩm nào vào giỏ hàng, xin quý khách vui lòng kiểm tra rằng trình duyệt Interne của quý khách đã bật tính năng cookies và các phần mềm an ninh khác thì không ngăn chặn việc mua hàng của quý khách.<div id="left-top"/><div id="left-bottom"/><div id="right-top"/><div id="right-bottom"/></div>';						   
	// for add item to cart
	$('[rel=addToCart]').click(function(){
		var _seft = $(this);
		var url = _seft.attr('href');
		$.ajax({
			url: url,
			type: 'get',
			dataType: 'json',
			beforeSend: function (){
				_seft.parent().parent().find('.busy').show();
			},
			success: function(j) {
				_seft.parent().parent().find('.busy').hide();
				if(j.totalitem == 1) {
					$("div.clsMiniCart").show("slow");
					alert("Giỏ hàng đã được tạo.\nHiện đang có 1 sản phẩm trong giỏ hàng.\nMời Quý khách tiếp tục mua hàng.");
				}
				$('#totalItems').html(j.totalitem);
				$('#totalPrice').html(j.totalprice);
			}
		});
		return false;
	});
	// for remove all cart
	$('[rel=removeAllCart]').click(function(){
		var _seft = $(this);
		var url = _seft.attr('href');
		$.ajax({
			url: url,
			type: 'get',
			dataType: 'html',
			beforeSend: function (){
				_seft.parent().parent().parent().find('.busy').show();
			},
			success: function(j) {
				_seft.parent().parent().parent().find('.busy').hide();
				$('#totalItems').html('0');
				$('#totalPrice').html('0');
			}
		});
		$("div.clsMiniCart").hide("slow");
		alert("Giỏ hàng được xóa thành công.");
		return false;
	});
	
	// for remove item in cart
	$('[rel=remove]').click(function(){
		var _seft = $(this);
		var url = _seft.attr('href');
		$.ajax({
			url: url,
			type: 'get',
			dataType: 'json',
			beforeSend: function (){
				_seft.parent().parent().parent().find('.busy').show();
			},
			success: function(j) {
				_seft.parent().parent().parent().find('.busy').hide();
				$('#product_' + j.id).hide("slow");
				j.totalprice==0 ? $('.contentMyCart').html(emptyCart) : $('#totalPrice').html(j.totalprice);
				
			}
		});
		return false;
	});
	
	// for remove item in cart
	$('input[name=btnUpdate]').click(function(){
		var id = $(this).attr("id");
		var qty = $('input#qty_' + id).val();
		$.ajax({
			url: 'cart.php',
			type: 'post',
			data: {'act' : 'update', 'id' : id, 'qty' : qty},
			dataType: 'json',
			success: function(j){
				j.price==0 ? $('#product_' + id).hide("slow") : $('#price_' + id).html(j.price);
				j.totalprice==0 ? $('.contentMyCart').html(emptyCart) : $('#totalPrice').html(j.totalprice);
			}
		});		
		return false;
	});
	
});
