var overlayOpacity = 50;
var hideCartTimeout;
var cartDisplayTime = 5000; //milliseconds
var cartHovered = false;
var showAddProductButton = true;
var urlChooseNewPhoto;
var urlChooseNewProduct;
var urlCreateAnotherSameProduct;

var cartModalOptions = {
	modal: true,
	onShow: function(hash) {hash.w.show();},
	overlay: overlayOpacity
}
$(document).ready(function() {
	//set up the modal dialog
	$('#modal-cart').jqm(cartModalOptions);
	//set up the trigger -- this is temporary; Flash will call this
	$("#cart-tab").click(function() {
		window.showAddProductButton = false;
		showCartModal(false);
		return false;
	});
	//set up close trigger
	$("#modal-cart .cart-overlay a.btn-close").click(function() {
		cancelHideCartTimer();
		hideCart(false);
		return false;	
	});
	$("#modal-cart .upsell-overlay a.trigger-upsell-close").click(function() {
		closeUpsellModal(notifyFlashUpsellClose);
		return false;	
	});
	$("#modal-cart .cart-overlay .tab a").click(function() {
		cancelHideCartTimer();
		hideCart(false);
		return false;	
	});
	//Do not slide the cart away if the mouse is hovering over it
	$("#modal-cart .cart-overlay").hover(function() {
		cartHovered = true;
		cancelHideCartTimer();
	}, function () {
		cartHovered = false;
		setHideCartTimer();
	});
	
});
function showCartModal() {
	closeUpsellModal(notifyFlashUpsellClose);
	$('#modal-cart').jqmShow();
	loadCart();
	return false;
}
function closeCartModal() {
	cancelHideCartTimer();
	$('#modal-cart').jqmHide();
	notifyFlashCartClose();
	return false;
}
function closeUpsellModal(callbackFunction) {
	$(".jqmWindow .upsell-overlay").hide();
	$('#modal-cart').jqmHide();
	callbackFunction();
	//notifyFlashUpsellClose();
	return false;
}
//load cart contents via Ajax
function loadCart() {
	$("#modal-cart .cart-overlay").show();
	$("#modal-cart .cart-overlay .slideable").hide();
	$("#modal-cart .cart-overlay .ajax-load").html("<div class=\"ajax-load-icon\"><img src=\"images/ajax-loader.gif\" width=\"32\" height=\"32\" /></div>");
	//hash.w.show();
	$("#modal-cart .cart-overlay .slideable").slideDown("slow");
	$.ajax({
		dataType: "html",
		url: urlCartOverlay,
		success: loadCartAjaxSuccess,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("#modal-cart .cart-overlay .ajax-load").html("There was an error getting the cart");
			$(".cart-total-items").html("");
		}
	});
}
function loadCartAjaxSuccess(returnText) {
	if (! cartHovered) setHideCartTimer();
	$("#modal-cart .cart-overlay .ajax-load").html(returnText);
	if (showAddProductButton) {
		$("a.btn-addproduct").show();
	} else {
		$("a.btn-addproduct").hide();
	}
	//count number of items in cart
	var numItems = $("#modal-cart .cart-overlay .item").length;
	if (numItems > 0) {
		$(".cart-total-items").html("(" + numItems + ")");
		//		
	} else {
		$(".cart-total-items").html("");
	}
	$("#modal-cart .cart-overlay .btn-addproduct").click(addProductClick);
}
function hideCart(showUpsell) {
	if (showUpsell) {
		$("#modal-cart .cart-overlay .slideable").slideUp("slow", showUpsellModal);
	} else {
		$("#modal-cart .cart-overlay .slideable").slideUp("slow", closeCartModal);
	}
	window.showAddProductButton = true;
}
function showUpsellModal() {
	cancelHideCartTimer();
	$("textarea#debug").html($("textarea#debug").html() + "showUpsellModal ");
	$("#modal-cart .cart-overlay").hide();
	$('#modal-cart').jqmShow();
	$(".upsell-overlay").show()
	$(".upsell-overlay .ajax-load").html("<div class=\"ajax-load-icon\"><img src=\"images/ajax-loader-white.gif\" width=\"32\" height=\"32\" /></div>");
	$.ajax({
		dataType: "html",
		url: urlUpsellOverlay,
		success: loadUpsellAjaxSuccess,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$(".upsell-overlay .ajax-load").html("Error loading data");
		}
	});

}
function loadUpsellAjaxSuccess(returnText) {
	$("#modal-cart .upsell-overlay .ajax-load").html(returnText);
	$(".upsell-overlay .product").click(upsellProductClick);
	$("#modal-cart .upsell-overlay a#btn-newphoto").click(function() {
		closeUpsellModal(notifyFlashUpsellNewPhoto);
		return false;	
	});
	$("#modal-cart .upsell-overlay a#btn-newproduct").click(function() {
		closeUpsellModal(notifyFlashUpsellNewProduct);
		return false;	
	});
	$("#modal-cart .upsell-overlay a#btn-newversion").click(function() {
		closeUpsellModal(notifyFlashUpsellNewVersion);
		return false;	
	});
	
}
function cancelHideCartTimer() {
	//$("textarea#debug").html($("textarea#debug").html() + "ct ");
	window.clearTimeout(hideCartTimeout);
}
function setHideCartTimer() {
	//$("textarea#debug").html($("textarea#debug").html() + "st ");
	window.clearTimeout(hideCartTimeout);
	hideCartTimeout = window.setTimeout("hideCart(false)", cartDisplayTime);
}
function upsellProductClick() {
	$('#modal-cart').jqmHide();
	notifyFlashUpsellClick($(this).attr("id"));
	return false;
}
function checkoutClick() {
	hideCart(false);
	return false;
}
function addProductClick() {
	hideCart(false);
	return false;
}
// ******** FLASH NOTIFICATIONS TO BE FLESHED OUT **************
function notifyFlashCartClose() {
	//alert("This function will notify flash that the cart overlay has closed.");
	 if (document.getElementById("itemCnt"))
	 {
	    nItem = document.getElementById("itemCnt").value;
	 }
	 else
	 {   
	    nItem = 0;
	 }
	 CartClosed(nItem);	  
	 
}
function notifyFlashUpsellClose() {
	//alert("This function will notify flash that the upsell overlay has closed.");
}
function notifyFlashUpsellClick(productID) {
	//alert('This function will notify flash that product with ID "' + productID + '" was clicked in the upsell overlay');	
}
function notifyFlashUpsellNewPhoto() {
	//alert("This function will notify flash that the user clicked 'Choose a New Photo' in the upsell overlay");
	//location.href=urlSelectNewPhoto;
}
function notifyFlashUpsellNewProduct() {
	//alert("This function will notify flash that the user clicked 'Choose a New Product' in the upsell overlay");
	//alert(urlSelectNewProduct);
	//location.href=urlSelectNewProduct;
}
function notifyFlashUpsellNewVersion() {
	//alert("This function will notify flash that the user clicked 'Create another version of the same product' in the upsell overlay");
	//location.href=urlCreateAnotherSameProduct;
}

//=======Remove Item from cart========
function RemoveItemInCart(index)
{
	$.ajax({
		dataType: "html",
		url: urlRemoveCartItem + index,
		success: removeCartItemClickAjaxSuccess,
		error: removeCartItemClickError
	});
}
//Success
function removeCartItemClickAjaxSuccess(responseText) {
	if (trim(responseText)=="OK") {
		ReloadCart();
	} else {
		throwError("Could not remove cart item; " + responseText);
	}
}
//Failure
function removeCartItemClickError() {
	throwError("Could not remove cart item; Ajax request failed");
}

//============Change quantity in cart============
function ChangeQuantity(index)
{
	//Get new qty
	qty =$('#qty'+index).attr("value");

	//Call server to change qty
	$.ajax({
		dataType: "html",
		url: urlChangeCartItemQuantity + "index=" + index + "&qty=" + qty,
		success: changeCartItemClickAjaxSuccess,
		error: changeCartItemClickError
	});
}
//Success
function changeCartItemClickAjaxSuccess(responseText) {
	if (trim(responseText)=="OK") {
		ReloadCart();
	} else {
		throwError("Could not chage cart item quantity; " + responseText);
	}
}
//Failure
function changeCartItemClickError() {
	throwError("Could not change cart item quantity; Ajax request failed");
}

//==============Clear Cart==================
function Clearcart(responseText) {
	$.ajax({
		dataType: "html",
		url: urlEmptyCart,
		success: clearCartClickAjaxSuccess,
		error: clearCartClickError
	});
	closeCartModal();
}
//Success
function clearCartClickAjaxSuccess(responseText) {
	if (trim(responseText)=="OK") {
		ReloadCart();
	} else {
		throwError("Could not clear cart; " + responseText);
	}
}
//Failure
function clearCartClickError() {
	throwError("Could not cleart; Ajax request failed");
}

//===============Reload cart======================
function ReloadCart()
{
	$.ajax({
		dataType: "html",
		url: urlCartOverlay,
		success: loadCartAjaxSuccess,
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			$("#modal-cart .cart-overlay .ajax-load").html("There was an error getting the cart");
			$(".cart-total-items").html("");
		}
		});
}