jQuery(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
	jQuery('input[name=btnAddtoCart]').click(function(){
		var id = jQuery(this).attr('id');
		var product = jQuery('input#product_' + id).val();
		var qty = jQuery('input#quantity').val();
		qty = (qty == null) ? 1 : qty;
		jQuery.ajax({
			url: '/dat-mua',
			type: 'post',
			data: {'action' : 'addtoCart', 'id' : product, 'qty' : qty},
			dataType: 'json',
			success: function(j){
				if(jQuery('div#miniCart').hasClass('hideMiniCart')) {
					jQuery('div#miniCart').show("slow");
					jQuery('div#miniCart').removeClass('hideMiniCart').addClass('showMiniCart');
					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.");
				}
				jQuery('#totalItems').html(j.totalitem);
				jQuery('#totalPrice').html(j.totalprice);
			}
		});		
		return false;
	});
	
	// for remove all cart
	jQuery('[rel=removeCart]').click(function(){
		var _seft = jQuery(this);
		var url = _seft.attr('href');
		jQuery.ajax({
			url: url,
			type: 'get',
			data: {'action' : 'removeCart'},
			dataType: 'html',
			success: function(j) {
				jQuery('#totalItems').html('0');
				jQuery('#totalPrice').html('0');
			}
		});
		jQuery("div#miniCart").hide("slow").removeClass('showMiniCart').addClass('hideMiniCart');
		alert("Giỏ hàng được xóa thành công.");
		return false;
	});
	
	// for remove item in cart
	jQuery('input[name=btnRemoveItem]').click(function(){
		var id = jQuery(this).val();
		jQuery.ajax({
			url: '/xoa-san-pham',
			type: 'post',
			data: {'action' : 'removeItem', 'id' : id},
			dataType: 'json',
			success: function(j) {
				jQuery('#item_' + j.id).remove();
				j.totalprice=='0 đ' ? jQuery('#pns-cart').html(emptyCart) : jQuery('#totalPrice').html(j.totalprice);				
			}
		});
		return false;
	});
	
	// for remove item in cart
	jQuery('input[name=btnUpdateItem]').click(function(){
		var id = jQuery(this).val();
		var qty = jQuery('input#qty_' + id).val();
		jQuery.ajax({
			url: '/cap-nhat-gio-hang',
			type: 'post',
			data: {'action' : 'updateCart', 'id' : id, 'qty' : qty},
			dataType: 'json',
			success: function(j){
				j.price=='0 đ' ? jQuery('#item_' + id).remove() : jQuery('span#price_' + id).html(j.price);
				j.totalprice=='0 đ' ? jQuery('#pns-cart').html(emptyCart) : jQuery('#totalPrice').html(j.totalprice);
			}
		});		
		return false;
	});	
	
});
