/*****************************************
	MIGHTY LEAF Product Detail Script
	Author: James Van Arsdale III
	Created: 11/7/08
	* 
	* Modified for FreshFinds
	* JLP 3/12/09
*****************************************/
var frmOptions = {
	url: "/index.cfm",
	type: "post",
	dataType: "html",
	resetForm: false,
	timeout: 100000,
	beforeSubmit: showLoading,
	success: showResponse,
	error: function(XMLHttpRequest, textStatus, errorThrown)
	{
		hideLoading();
		alert("Error1\nAn error occurred and we couldn't add your items, please try again or contact us for further assistance.");
	}
};

jQuery(document).ready(function() {
	
	jQuery(".vProduct-thumbnails-productContainer .vProduct-thumbnails-actionContainer form.vProduct-thumbnails-productForm").submit(function() { 
		jQuery(this).ajaxSubmit(frmOptions); 
		return false;
	});
	
});

/*
// pre-submit callback 
function validateOrder(formData, jqForm, options) { 
	// formData is an array; here we use $.param to convert it to a string to display it 
	// but the form plugin does this for you automatically when it submits the data 
	var queryString = jQuery.param(formData); 
	var orderTotal = 0;
	
	if ( jQuery(".vProduct-detailPageTemplate-productContainer .vProduct-productForm-container form input[name=quantity]").length ) {
		orderTotal = jQuery(".vProduct-detailPageTemplate-productContainer .vProduct-productForm-container form input[name=quantity]").val();
	}
	else {
		var itemCount = jQuery(".vProduct-detailPageTemplate-productContainer .vProduct-packageProductsForm-container form .vProduct-packageProductsForm-productControlsQuantityContainer input").length;
		jQuery(".vProduct-detailPageTemplate-productContainer .vProduct-packageProductsForm-container form .vProduct-packageProductsForm-productControlsQuantityContainer input").each(function(){
			orderTotal = orderTotal + parseInt($(this).val(),10);
		});
	}
	
	if( orderTotal > 0 )
	{
		showLoading();
		return true;
	}
	else
	{
		// this is for IE not being able to resubmit the form after the first ajax submit, unless you reselect a product in the drop down menu
		alert("Please Select A Product\nPlease select a product and quantity to add it to your Shopping Cart.");
		return false;
	}
} 
*/

// post-submit callback 
function showResponse(responseTxt, statusText)  { 
	//alert('status: ' + statusText + '\n\nresponseText:' + responseTxt);	
	
	// get cart details.
	$.ajax({
		async: false, // set to false for testing
		url: "/index.cfm/fuseaction/cart.ajaxCartSummary",
		dataType: "html",
		cache: false,
		complete: function(XMLHttpRequest, textStatus)
		{	
			//alert('status: ' + textStatus + '\n\nresponseText:' + XMLHttpRequest.responseText);
			if ( textStatus == 'success' )
			{
				// remove old items from drop down
				jQuery("#cart-container ul li.cart-item ul li.cart-message").remove();
				jQuery("#cart-container ul li.cart-item ul li.cart-prod").remove();
				
				// if there are no items in the cart, create the nested UL
				if( jQuery("#cart-container ul li.cart-item:has(ul)").length < 1 )
				{
					jQuery("#cart-container ul li.cart-item").append("<ul><li class=\"actions\"><a href=\"/index.cfm/fuseaction/cart.view\" class=\"bag\">View My Cart</a> <a href=\"/index.cfm/fuseaction/checkout.start\" class=\"checkout\">Checkout</a></li></ul>");
					// reinit the cart drop down now that there's a nested UL
					/*$("#cart-container ul li.cart-item:has(ul)").mouseover(function() {
						$("select").css("visibility","hidden");
						$(this).addClass("modal");
					});
					$("#cart-container ul li.cart-item:has(ul)").mouseout(function() {
						$("select").css("visibility","visible");
						$(this).removeClass("modal");
						jQuery("#cart-container ul li.cart-item ul").css("top","");
						jQuery("#cart-container ul li.cart-item ul").css("left","");
						
					});*/
					
				}

				// add newly returned items
				jQuery("#cart-container ul li.cart-item ul li.actions").before(XMLHttpRequest.responseText);
				jQuery("#cart-container ul li.cart-item ul li.cart-message").append("<h2>Your Items have been added to your Shopping Cart</h2>");
				jQuery("#cart-container ul li.cart-item ul li.cart-message").append(jQuery("#cart-container ul li.cart-item ul li.actions").html());
				jQuery("#cart-container ul li.cart-item ul li.cart-message .scanalert").remove();
				
				// update the item count in the util nav
				jQuery("#cart-container ul li.cart-item a.cart-link").html("<span>Shopping Cart</span>"+jQuery("#cart-container ul li.cart-item ul li.cart-message input.cart-count").val()+" item");
			
				hideLoading();
				
				// scroll to top and trigger the over state
				//jQuery(document).scrollTo("#util-navigation",500);
				
				jQuery("body").append('<div id="modalOverlay" class="overlay"/>');
				jQuery("#modalOverlay").css("height",jQuery(document).height()+"px");
				jQuery("#modalOverlay").css("width",jQuery(document).width()+"px");
				jQuery("#modalOverlay").fadeTo("fast", 0.8);
				jQuery("#cart-container ul li.cart-item ul li.actions-row2").append('<span id="modalCartCloseBtn">Close</span>');
				jQuery("#modalCartCloseBtn").click(function(){
					jQuery("#cart-container ul li.cart-item").removeClass("modal").removeClass("over");
					jQuery("#cart-container ul li.cart-item ul").css("top","");
					jQuery("#cart-container ul li.cart-item ul").css("left","");
					jQuery("#modalCartCloseBtn").remove();
					jQuery("#modalOverlay").remove();
				});
				
				jQuery("#cart-container ul li.cart-item").addClass("modal");
				
				var modalCartHeight = jQuery("#cart-container ul li.modal ul").height();
				var modalCartWidth = jQuery("#cart-container ul li.modal ul").width();
				jQuery("#cart-container ul li.modal ul").css("top", 50);
				jQuery("#cart-container ul li.modal ul").css("left", f_scrollLeft()+(f_clientWidth()/2) - (modalCartWidth/2));
				
				
				//var temp = setTimeout('jQuery("#cart-container ul li.cart-item").removeClass("modal").removeClass("over");jQuery("#modalOverlay").remove();', 6000);
			}
			else
			{
				hideLoading();
				alert("Error2\nWe were unable to update the Shopping Cart summary, refresh the page or visit your Shopping Cart to see your items added.");
			}
		},
		error: function()
		{
			alert("Error3\nWe were unable to update the Shopping Cart summary, refresh the page or visit your Shopping Cart to see your items added.");
		}
	});
} 

function showLoading()
{
	jQuery("body").append("<div id=\"ordersave-loading\" class=\"overlay\" style=\"display: none;\"><div class=\"loading\"><h2>Adding Items to your Shopping Cart</h2></div></div>");
	jQuery("#ordersave-loading").css("height",jQuery(document).height()+"px");
	jQuery("#ordersave-loading").css("width",jQuery(document).width()+"px");
	jQuery("#ordersave-loading").fadeTo("fast", 0.8);
	jQuery("#ordersave-loading").fadeTo("fast", 0, function() { jQuery("#ordersave-loading").css("display","block"); });
	jQuery("#ordersave-loading").fadeTo("slow", 0.9);
	jQuery(document).scrollTo("#header-container",500);
}

function hideLoading()
{
	jQuery("#ordersave-loading").remove();
}	
