if (!SHOPPINGCART) {
	var SHOPPINGCART = new Object();	
}

/**
 * Method show cart message
 * 
 * @param string message
 */
SHOPPINGCART.showMessage = function(message) {
	if ($('#catMessage').length > 0) {
		$('#cartMessage').html(message);
	} else {
		$('#msgBlnContainer').html(message);
	}
};

/**
 * Method remove all cart messages
 */
SHOPPINGCART.clearMessages = function() {
	if ($('#catMessage').length > 0) {
		$('#cartMessage').html('');
	} else {
		$('#msgBlnContainer').html('');
	}
};

/**
 * Method display shopping cart products if exists
 */
SHOPPINGCART.showCart = function() {
	if ($('#itemsWrapper').is(':hidden')) {
		$('#emptyWrapper').hide();
		if ($('#selectAllItems').length > 0) {
			$('#selectAllItems').removeAttr('disabled'); 
		}
		$('#buttonCartRemove').sbutton('enable', 'click', function(){
			SHOPPINGCART.removeItems();
		});
		$('#buttonCartCheckout').sbutton('enable', 'click', function(){
			SHOPPINGCART.checkout();
		});
		$('#itemsWrapper').show();	
	}	
};

/**
 * Method hides shopping cart if no products exists
 */
SHOPPINGCART.hideCart = function() {	
	if ($('#itemsWrapper').is(':visible')) {
		$('#itemsWrapper').hide();
		if ($('#selectAllItems').length > 0) {
			$('#selectAllItems').removeAttr('checked');
			$('#selectAllItems').attr('disabled', 'disabled'); 
		}
		$('#buttonCartRemove').sbutton('disable');
		$('#buttonCartCheckout').sbutton('disable');
		$('#emptyWrapper').show();
	}
};

/**
 * Method display mini cart
 * 
 * @param string mode
 */
SHOPPINGCART.loadMiniCart = function(mode) {
	if (!mode) {
		var mode = 'normal';
	}
	$('#miniShoppingCart').Loader('show');
	$.ajax({
		url: '/cart/mini',
		data:  'mode=' + mode,
		success: function(response) {
			$('#miniShoppingCart').html(response);
			$('#miniShoppingCart').Loader('hide');
		}
	});
};

/**
 * Method build product item 
 * 
 * @param  string  desc
 * @param  string  type
 * @param  string  action
 * @param  integer quantity
 * @return object
 */
SHOPPINGCART.getItem = function(desc, type, action, quantity) {
	if (!type) {
		return false;
	}
	if (!action) {
		action = 'None';		
	}
	if (!quantity || quantity < 1) {
		quantity = 1;
	}
	var product = new Object();	
	product.description = desc;
	product.type = type;
	product.action = action;
	product.quantity = quantity;
	return product;
};

/**
 * Method add item to shopping cart
 * 
 * @param object products
 * @param bool   redirect
 */
SHOPPINGCART.add = function(products, redirect) {
	if (typeof products != 'object') {
		return false;
	}
	if (!redirect) {
		redirect = false;
	}
	var obj = new Object;
	obj.products = products;
	obj.request = request;
	var strJson = JSON.stringify(obj);
	$.ajax({			
		url: '/cart/add',
		data:  'data=' + strJson,
		success: function(JsonResponse) {
			if (isJSON(JsonResponse)) {		
				var response = JSON.parse(JsonResponse);
				if (response.status) {	
					if (response.items) {
						$('#cartItems').append(response.items);
					}
					if ($('.cartDiscount').length > 0) {
						$('.cartDiscount').show();
						$('#cartDiscount').html(response.total['discount']);
						$('#cartSubtotal').html(response.total['subtotal']);
					}
					if ($('#cartNumItems').length > 0) {
						if (response.numofitems > 1) {
							var numofitems = response.numofitems + ' items';
						} else {
							var numofitems = response.numofitems + ' item';
						}
						$('#cartNumItems').html(numofitems);
					}
					$('#cartTotal').html(response.total['total']);
					if (redirect) {
						window.location = '/cart';
					} else {
						SHOPPINGCART.showCart();
						$(window).Loader('hide');
					}
				} else {
					SHOPPINGCART.showMessage(response.message);
					$(window).Loader('hide');
				}				
			} else {
				SHOPPINGCART.showMessage(JsonResponse);
				$(window).Loader('hide');
			}					
		}
	});	
};

/**
 * Method remove item from shopping cart
 * 
 * @param integer id
 */
SHOPPINGCART.remove = function(id) {
	if ($('#ShoppingCart').length > 0) {
		$('#ShoppingCart').Loader('show');
	}
	if (!(id instanceof Array)) {
		var tmp = id;
		id = new Array();
		id.push(tmp);		
	}
	$.ajax({
		url: '/cart/remove',
		data:  'id=' + id.toString(),
		success: function(JsonResponse) {
			if(isJSON(JsonResponse)) {
				var response = JSON.parse(JsonResponse);
				if (response.status) {					
					if (response.numofitems == 0) {
						SHOPPINGCART.hideCart();
					}
					if ($('#CartItemsCount').length > 0) {						
						var count = response.numofitems;
						if (count > 999) {
							count = Math.floor(count / 1000);
							count += 'k';
						}						
						$('#CartItemsCount').text(count);
					}					
					$.each(id, function(i, val) {
						$('tr#item' + val).empty().remove();
					});
					if ($('.cartDiscount').length > 0) {
						$('.cartDiscount').show();
						$('#cartDiscount').html(response.total['discount']);
						$('#cartSubtotal').html(response.total['subtotal']);
					}
					if ($('#cartNumItems').length > 0) {
						if (response.numofitems > 1 || response.numofitems == 0) {
							var numofitems = response.numofitems + ' items';
						} else  {
							var numofitems = response.numofitems + ' item';
						}
						$('#cartNumItems').html(numofitems);
					}
					$('#cartTotal').html(response.total['total']);					
				}
				SHOPPINGCART.showMessage(response.message);
			} else {
				SHOPPINGCART.showMessage(JsonResponse);
			}
			$('#ShoppingCart').Loader('hide');
		}
	});
};

/**
 * Method update item quantity
 * 
 * @param integer id
 * @param integer quantity
 */
SHOPPINGCART.updateQty = function(id, quantity) {
	var obj = new Object;	
	obj.id = id;
	obj.quantity = quantity;
	var oldPrice = $('#cartPrice' + id).html();
	$('#itemPrice' + id).html('<img src="/img/loaders/loading_small.gif" style="margin: 0 auto;" />');
	$('#itemQuantity' + id).attr('disabled', 'disabled');	
	$('#buttonCartCheckout').sbutton('disable');
	$.ajax({
		url: '/cart/update',
		data:  'data=' + JSON.stringify(obj),		
		success: function(JsonResponse) {
			if(isJSON(JsonResponse)) {
				var response = JSON.parse(JsonResponse);
				if (response.status) {
					$('#itemQuantity' + id + ' option:selected').removeAttr('selected');
					$('#itemQuantity' + id + " option[value='" + quantity + "']").attr('selected', 'selected');
					$('#itemQuantity' + id).removeAttr('disabled');
					$('#itemPrice' + id).html(response.price);
					if ($('.cartDiscount').length > 0) {
						$('.cartDiscount').show();
						$('#cartDiscount').html(response.total['discount']);
						$('#cartSubtotal').html(response.total['subtotal']);
					}
					$('#cartTotal').html(response.total['total']);
				} else {
					$('#itemPrice' + id).html(oldPrice);
					$('#itemQuantity' + id).removeAttr('disabled');
					SHOPPINGCART.showMessage(response.message);					
				}				
			} else {
				SHOPPINGCART.showMessage(JsonResponse);
			}
			$('#buttonCartCheckout').sbutton('enable', 'click', function(){
				SHOPPINGCART.checkout();
			});
		}
	});
};

/**
 * Method implement checkout button click event
 */
SHOPPINGCART.checkout = function() {
	if ($('#buttonCartCheckout a').hasClass('disabled')) {
		return false;
	}
	location.href = '/checkout';
};

/**
 * Method check all items in shopping cart
 */
SHOPPINGCART.checkAll = function() {
	$('input.removeItem').attr('checked', $('#selectAllItems').is(':checked'));
};

/**
 * Method remove multiple checked items from shopping cart 
 */
SHOPPINGCART.removeItems = function() {
	if ($('#buttonCartRemove a').hasClass('disabled')) {
		return false;
	}
	if (!$('input.removeItem:checked').length) {
		showModalMessage('You must select items to remove!', 'warning');	
		return false;
	}
	var ids = new Array();
	$('input.removeItem:checked').each(function(){
		ids.push(this.value);	
	});	
	SHOPPINGCART.remove(ids);
};








