/*
 * ShoppingCart
 * Lop the hien shoppingcart, duoc luu o cookie.
 * Thu vien can thiet : jquery-x.x.x.min.js,jquery.cookie.js, json2.js
 */

 Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

//Tao instance san cua shoppingcart
if ( !this.ShoppingCart ){
	this.ShoppingCart = {};
}
( function(){	 
ShoppingCart._CART_COOKIE_OPTION = {}
ShoppingCart._CART_NAME = 'cart'
// JavaScript Document
//Object CartLine
//Dai dien cho 1 dong san pham trong gio hang
function CartLine( id, name, qty, price ) {
	this.id	= id;		// Id
	this.name = name;	// Ten san pham
	this.qty = qty;		// So luong san pham
	this.price = price;	// Gia san pham
}//end CartLine

/**
 * Tim san pham trong gio hang
 * @param id		ma san pham
 * @param arrCart	mang chua san pham
 * @return -1 neu khong tim thay san pham co id trong gio hang, hoac so nguyen 
 * chi vi tri cua san pham trong gio hang
 */
function findCartLine( id, arrCart ) {
	for ( var i=0; i < arrCart.length; i++ ){
		if ( arrCart[ i ].id == id ){
			return i;
		}
	}
	return -1;
}//end findCartLine

/**
 * Them san pham vao trong gio hang
 * @param id	ma san pham
 * @param name	ten san pham
 * @param price	gia san pham
 * @return false neu them khong thanh cong
 */
if ( typeof ShoppingCart.add != 'function' ){
	ShoppingCart.add = function ( id, qty, name, price ){		
		var arrCart = null;
		var bAddNew = true;		
		qty = qty > 0  ? qty  : 1;
		if ( $.cookie( ShoppingCart._CART_NAME )  != null ) {
			arrCart = JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );		
			//Lay id san pham trong gio hang ( neu co )
			var currIndex = findCartLine( id, arrCart );	
			//Kiem tra xem san pham da duoc add chua 		
			if ( currIndex != -1 ) {
				arrCart[ currIndex ].qty += qty;
				bAddNew = false;
			}	
		} 	
		if ( bAddNew == true ) {
			var objCartLine = new CartLine( id, name, qty, price );
			if ( arrCart == null ) arrCart = new Array();
			arrCart.push( objCartLine );	
		}
		//Dua vao gio hang
		$.cookie( ShoppingCart._CART_NAME, JSON.stringify( arrCart ), ShoppingCart._CART_COOKIE_OPTION );	
		return true;
	}//end addToCart
}

/**
 * Xoa san pham khoi gio hang
 * @param id	ma san pham
 * @return true neu xoa thanh cong, flase neu gio hang trong hoac khong tim thay san pham
 */
if ( typeof ShoppingCart.remove != 'function' ){
	ShoppingCart.remove = function ( id ) {
		if ( $.cookie( ShoppingCart._CART_NAME ) != null ){
			var arrCart = JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );
			var currIndex = findCartLine( id, arrCart );
			
			if ( currIndex != -1 ) {
				arrCart.splice( currIndex, 1 );
				//Dua vao gio hang
				$.cookie( ShoppingCart._CART_NAME, JSON.stringify( arrCart ), ShoppingCart._CART_COOKIE_OPTION );	
				return true;
			}		
		}				  
		return false;
	}//end removeFromCart
}
/**
 * Cap nhap san pham trong gio hang
 * @param id	ma san pham
 * @param qty	so luong can cap nhap
 * @return true neu cap nhap thanh cong, flase neu gio hang trong hoac khong co san pham
 */
if ( typeof ShoppingCart.update != 'function' ){
	ShoppingCart.update = function ( id, qty ) {
		if ( $.cookie( ShoppingCart._CART_NAME )  != null ) {		
			qty = parseInt( qty ) ? parseInt( qty ) : 0;
			if ( qty > 0 ) {
				arrCart = JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );		
				var currIndex = findCartLine( id, arrCart );	
				if ( currIndex != -1 ){ 	
					if ( arrCart[ currIndex ].qty != qty ) { //Giong so luong khong nen cap nhap				
						arrCart[ currIndex ].qty = qty;
						$.cookie( ShoppingCart._CART_NAME, JSON.stringify( arrCart ) , ShoppingCart._CART_COOKIE_OPTION );	
					}
				}
			} else if ( qty == 0 ) { //So luong = 0 dong nghia voi xoa san pham
				ShoppingCart.remove( id );
			}
		}
		return false;
	}
}
/**
 * Lay toan bo san pham trong gio hang
 * @return false neu gio hang trong, nguoc lai tra ve mang cac san pham
 */
if ( typeof ShoppingCart.getItems != 'function' ){
	ShoppingCart.getItems = function () {
		return $.cookie( ShoppingCart._CART_NAME ) == null ? false : JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );
	}//end getItemsFromCart
}
/**
 * Tinh tong so luong san pham trong gio hang
 * @return tong so luong san pham
 */
if ( typeof ShoppingCart.getTotalQty != 'function' ){
	ShoppingCart.getTotalQty = function() {
		if ( $.cookie( ShoppingCart._CART_NAME ) != null ) {
			var arrCart = JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );
			var totalQty = 0;
			for ( var i=0; i < arrCart.length; i++ ) {
				totalQty += arrCart[ i ].qty;
			}
			return totalQty;
		}
		return 0;
	}
}
/**
 * Tinh tong so tien cua gio hang
 * @return tong so tien cua gio hang
 */
if ( typeof ShoppingCart.getTotalPrice != 'function' ){
	ShoppingCart.getTotalPrice = function() {
		if ( $.cookie( ShoppingCart._CART_NAME ) != null ) {
			var arrCart = JSON.parse( $.cookie( ShoppingCart._CART_NAME ) );
			var totalPrice = 0;
			for ( var i=0; i < arrCart.length; i++ ) {
				totalPrice += ( arrCart[ i ].qty * arrCart[ i ].price );
			}
			return totalPrice.formatMoney(0, '.', ',');
		}
		return 0;
	}
}
/** 
 * Xoa gio hang 
 * @return true neu xoa thanh cong
 */
if ( typeof ShoppingCart.empty != 'function' ) {
	ShoppingCart.empty = function() {
		if ( $.cookie( ShoppingCart._CART_NAME ) != null ) {
			$.cookie( ShoppingCart._CART_NAME, null );
			return true;
		}
		return false;
	}
}
}());
