/* AJAX Cart Functions */


var READY_STATE = "ready";
var WORKING_STATE = "working";
var STARTED_STATE = "started";

var CURRENT_CART_STATE = STARTED_STATE;

function Cart() {
	this.clearCartUI = clearCartUI;
	this.showEmptyCartUI = showEmptyCartUI;
	this.showCartItemsUI = showCartItemsUI;
	this.loadCart = loadCart;
	this.processUserCart = processUserCart;
	this.end = end;
	this.processFailure = processFailure;
	this.processComplete = processComplete;
	this.processException = processException;
	this.returnHash = returnHash;
	this.showCartSizeUI = showCartSizeUI;
	this.cartSize = 0;
	this.itemHash = {};
}//END: Cart()

function cartIsReady() {
	logToWindow("State of cart: " + CURRENT_CART_STATE);
	if (CURRENT_CART_STATE==READY_STATE) {
		return true;
	}
	else {
		return false;
	}
} //END: isReady

function end() {		
	logToWindow("end" );
}//END: end 

function processFailure(XMLHttpRequest, textStatus, errorThrown) {
	CURRENT_CART_STATE = READY_STATE;
	logToWindow("processFailure - failed. " + textStatus);
	if (errorThrown != null)
	{
		processException(XMLHttpRequest, errorThrown);
	}
}//END: processFailure

function processComplete(XMLHttpRequest, textStatus) {
	logToWindow("processComplete - complete.");
}//END: processComplete

function processException(Req, Ex1) {
	CURRENT_CART_STATE = READY_STATE;
	logToWindow("processException - execption " + Ex1);
}//END: processException

function loadCart(force) {
	logToWindow("loadCart - about to create new Ajax request: force ="+force );
	
	showLoaderUI();
	CURRENT_CART_STATE = WORKING_STATE;
	$.ajax(
	{	url: "/bzJApp/GetCartItems.action?cb=" + Math.random()+"&force="+force, 
		context: this,
		success: this.processUserCart, 
		error : this.processFailure, 
		complete : this.processComplete
	});	
		
}//END: loadCart

function processUserCart (data, textStatus, XMLHttpRequest) {
	this.itemHash = {};
	
	//init cartsize
	this.cartSize = 0;
	
	logToWindow("processUserCart - status = " + textStatus);
 	logToWindow("processUserCart - responseText = " + data); 	
 	
	var userCartDoc = parseXMLString(data);
	
	//check for errors
	if(userCartDoc.documentElement == null || userCartDoc.documentElement.tagName == "parsererror") {  
		//if errors occured while parsing the doc, get cart size (should be 0)
		//and end function
		logToWindow("processUserCart - error parsing doc");
		this.showCartSizeUI();
	} else {  
		logToWindow("processUserCart - userCartDoc : " + userCartDoc);
		
		logToWindow("processUserCart - userCart " + userCartDoc.getElementsByTagName("UserCart"));
		var usercart = userCartDoc.getElementsByTagName("UserCart")[0];		
		logToWindow("processUserCart - usercart :" + usercart);
		logToWindow("processUserCart - cart id : " + usercart.getAttribute("CID"));
		
		var itemNodes = userCartDoc.getElementsByTagName("Item");	
		var itemsToDisp='';
		//iterate through all item elements
		for (i = 0; i < itemNodes.length; i++) {
			var sku = itemNodes[i].getAttribute("sku");
			
			logToWindow("processUserCart - item#" + i + "'s sku: " + sku );
		
			var item = new cartItemObject(sku);		
			
			// get image
			item.image = itemNodes[i].getElementsByTagName("Thumbnail")[0].firstChild.nodeValue;
			item.image = unescape(item.image);
			logToWindow("processUserCart - item#" + i + "'s thumbNail location : " + item.image);
			
			//get title
			item.title = itemNodes[i].getElementsByTagName("Title")[0].firstChild.nodeValue.substring(0, 100) + '...';
			logToWindow("processUserCart - item#" + i + "'s title: " + item.title);
			
			//get quantity
			item.qty = itemNodes[i].getElementsByTagName("qty")[0].firstChild.nodeValue;
			logToWindow("processUserCart - item#" + i + "'s qty: " + item.qty);		
			
			//get item unit price
			item.price = itemNodes[i].getElementsByTagName("price")[0].firstChild.nodeValue;
			logToWindow("processUserCart - item#" + i + "'s price: " + item.price);
			
			logToWindow("processUserCart - item#" + i + " to hash." + this.itemHash);
			this.itemHash[""+sku] = item;
			
			//add qty to total cart size
			this.cartSize = parseInt(item.qty) + this.cartSize;	
			
			// form text to display items
			itemsToDisp = itemsToDisp + '<li>' + '<img src="' + item.image  +'" />';
			itemsToDisp = itemsToDisp + '<div class="cart_title">' + item.title + '</div>';
			itemsToDisp = itemsToDisp +'<div class="cart_qty">Qty: ' + item.qty + '</div>';
			//itemsToDisp = itemsToDisp + '<div class="cart_price">Price: $' + priceToDisplay.toFixed(2) + '</div>';
			itemsToDisp = itemsToDisp + '<div class="cart_price">Price: $' + item.price.substring(0, item.price.length-2) + '</div>';
			itemsToDisp = itemsToDisp + '<div style="clear: left;"></li>';
		
		}//END: for (i=0; i<itemNodes.length; i++)
		
		// Do not insert checkout button if cart is empty
		if (this.cartSize != 0) {
			itemsToDisp = itemsToDisp + '<a class="checkout_btn" href="'+ jsPaymentLink + '"><span>Checkout Now</span></a>';
			itemsToDisp = itemsToDisp + '<span class="hidecart_btn" style="color: #0077d4; position: relative; left: 0.5em; bottom: 1.5em; cursor: pointer;" href="" onclick="ajaxcart.close();">Hide Cart</span>';
			showCartItemsUI(itemsToDisp);
		} else {
			showEmptyCartUI();
		}
		
		this.showCartSizeUI();
		CURRENT_CART_STATE = READY_STATE;
		logToWindow("Completed work. State of cart: " + CURRENT_CART_STATE);
		
	}//END: if (userCartDoc.documentElement.tagName == "parsererror")
	return true;
}//END: processUserCart
 
function returnHash(){
	return this.itemHash;
}//END: returnHash

function cartItemObject(sku) {
 	this.sku = sku;
	this.qty = "";
	this.price = "";
	this.title = "";	
	this.image= "";
}//END: cartItemObject constructor

// JJ's functions

function showLoaderUI() {
	$('#cart_tab').html('<span style="right: 40px;"><img src="/bzJApp/views/store/images/ajax_loader.gif" alt="" /></span>');
}

function showCartSizeUI() {
  	logToWindow("showCartSizeUI - cart size : " + this.cartSize );
	if(this.cartSize == 1){
		$('#cart_tab').html('<span>' + this.cartSize +' item </span>');
	} else {
		$('#cart_tab').html('<span>' + this.cartSize +' items</span>');
	}
}

function clearCartUI() {
	$('#cart_itemlist').html('');
}

function showEmptyCartUI() {
	$('#cart_itemlist').html('<li><div class="cart_title">Your cart is empty</div></li>');
}

function showCartItemsUI(items){
 	$('#cart_itemlist').html(items);
	//ajaxcart.open();
}
