/*****************************************
	SCHNEIDER Product Quick View Script
	Author: James Van Arsdale III
	Created: 10/14/08
*****************************************/

/**********************************
	BUILD QUICK VIEW
**********************************/
function buildQuickView(parentElm)
{
	// remove old z-index's
	jQuery(".product-row").removeAttr("style");
	
	// create main quickview container, append to body, and position
	var windowWidth = $(window).width();
	var documentHeight = $(document).height();
	var productrowOffset = $(parentElm).offset();
	var quickviewPositionTop = productrowOffset.top - 20;
	var quickviewPositionLeft = Math.ceil((windowWidth - 940) / 2);
	// var quickviewDiv = '<div class="quick-view" style="position: absolute; left: ' + quickviewPositionLeft + 'px; top: ' + quickviewPositionTop + 'px;"></div>';
	jQuery(parentElm).parents("body").append('<div class="quick-view" style="position: absolute; left: ' + quickviewPositionLeft + 'px; top: ' + quickviewPositionTop + 'px;"></div>');
	jQuery(parentElm).parents("body").append("<div class=\"overlay\" style=\"width: " + windowWidth + "; height: " + documentHeight + ";\"></div>");
	$("div.overlay").fadeTo("fast", 0.8);
	
	// hide selects
	jQuery("select").css("visibility","hidden");
	
	// add top close button
	jQuery(".quick-view").append("<div class=\"controls controls-top\"><a href=\"#\" class=\"close close-top\">Close Quick View</a></div>");
	// add inner container
	jQuery(".quick-view").append("<div class=\"inner-content\"><h2 class=\"loading\">Loading...</h2></div>");
	
	// add bottom close button
	jQuery(".quick-view").append("<div class=\"controls controls-bottom\"><a href=\"#\" class=\"close close-bottom\">Close Quick View</a></div>");
	
	jQuery(".quick-view a.close").click(function() {
		removeQuickView();
		$("div.overlay").fadeOut("fast");
		return false;
	});
}

function removeQuickView()
{
	jQuery(".quick-view").remove();
	jQuery("select").css("visibility","visible");
	jQuery(".product-row").removeAttr("style");
}

function assignQVFunctions()
{
	// setup the ajax form submit
	jQuery(document).ready(function() {
		var frmOptions = {
			url: "/index.cfm",
			type: "post",
			dataType: "html",
			resetForm: true,
			timeout: 10000,
			beforeSubmit: validateOrder,
			success: showResponse,
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				hideLoading();
				alert("Error\nAn error occurred and we couldn't add your items, please try again or contact us for further assistance.");
			}
		};
		
		jQuery(".quick-view .vProduct-packageProductsForm-controlsContainer").remove();
							   
		jQuery(".quick-view .upper-details form").submit(function() { 
			jQuery(this).ajaxSubmit(frmOptions); 
			return false;
		});
		
		jQuery(".quick-view .recurring-available a").click(function() {
			buildModal();
			return false;
		});
	
		
		// assign wishlist functionality
		jQuery(".quick-view .vProduct-quickViewDetailLinks-container .wishlist a").click(function() {
			jQuery(".quick-view .upper-details form").unbind("submit");
			jQuery(".quick-view .upper-details input.input-fuseaction").val(jQuery(".quick-view .vProduct-quickViewDetailLinks-container input.wishlist-action").val());
			jQuery(".quick-view .upper-details form").trigger("submit");
			return false;
		});
		
		
		jQuery(".quick-view .vProduct-quickViewDetailLinks-container .cart a").click(function() {
			var cartAction = jQuery(".quick-view .vProduct-quickViewDetailLinks-container input.addtocart-action").val();
	
			jQuery(".quick-view .upper-details form input.input-fuseaction").val(cartAction);

			jQuery(".quick-view .upper-details form").trigger("submit");
			
			return false;
		 });
		
	});
	// Show/hide more info containers in product descriptions
	$("a.view-more").click(function() {
		$(this).slideToggle("fast");
		$(this).closest("div").children("div.view-more-content").slideToggle("fast");
		return false;
	});
	$("a.view-more-close").click(function() {
		$(this).closest("div.view-more").children("a.view-more").slideToggle("fast");
		$(this).closest("div.view-more-content").slideToggle("fast");
		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 = jQuery(".quick-view .upper-details form select[name=quantity]").val();
		
		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: true, // 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("#util-navigation ul.utility li.cart ul li.cart-message").remove();
					jQuery("#util-navigation ul.utility li.cart ul li.cart-prod").remove();
					
					// if there are no items in the cart, create the nested UL
					if( jQuery("#util-navigation ul.utility li.cart:has(ul)").length < 1 )
					{
						jQuery("#util-navigation ul.utility li.cart").append("<ul><li class=\"actions\"><a href=\"/index.cfm/fuseaction/cart.view\" class=\"bag\">View My Bag</a> <a href=\"/index.cfm/fuseaction/checkout.start\" class=\"checkout\"><img src=\"/resources/mightyleaf/images/buttons/btn_checkout-sm.gif\" alt=\"Checkout\" /></a></li></ul>");
						// reinit the cart drop down now that there's a nested UL
						$("#util-navigation ul.utility li.cart:has(ul)").mouseover(function() {
							$("select").css("visibility","hidden");
							$(this).addClass("over");
						});
						$("#util-navigation ul.utility li.cart:has(ul)").mouseout(function() {
							$("select").css("visibility","visible");
							$(this).removeClass("over");
						});
						
					}
					
					// add newly returned items
					jQuery("#util-navigation ul.utility li.cart ul li.actions").before(XMLHttpRequest.responseText);
					jQuery("#util-navigation ul.utility li.cart ul li.cart-message").append("<h2>Your Items have been added to your Shopping Cart</h2>");
					jQuery("#util-navigation ul.utility li.cart ul li.cart-message").append(jQuery("#util-navigation ul.utility li.cart ul li.actions").html());
					
					// update the item count in the util nav
					jQuery("#util-navigation ul.utility li.cart a .item-count").html("("+jQuery("#util-navigation ul.utility li.cart ul li.cart-message input.cart-count").val()+")");
				
					//alert(jQuery("#util-navigation ul.utility li.cart ul li.cart-message input.cart-count").val());
					removeQuickView();
					
					hideLoading();
					
					// scroll to top and trigger the over state
					//jQuery(document).scrollTo("#util-navigation",500);
					jQuery("#util-navigation ul.utility li.cart").addClass("over");
					var temp = setTimeout('jQuery("#util-navigation ul.utility li.cart").removeClass("over")', 6000);
				}
				else
				{
					hideLoading();
					alert("Error\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("Error\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.5);
		jQuery("#ordersave-loading").fadeTo("fast", 0, function() { jQuery("#ordersave-loading").css("display","block"); });
		jQuery("#ordersave-loading").fadeTo("slow", 0.9);
		jQuery(document).scrollTo("#util-navigation",500);
	}
	
	function hideLoading()
	{
		jQuery("#ordersave-loading").remove();
	}	


$.initQuickview = function() {
/*
	// hide the quickview on load 
	jQuery(".product-container a.quickview").hide();
	// IE6 needs a little help cause of supersleight.
	jQuery(".product-container a.quickview").css({
		position: "absolute",
		top: "1px",
		left: "4px"
	});
*/
	
	// show the quickview button on hover of product image
	jQuery(".product-container a.prod-link").mouseover(function() {
		jQuery(this).siblings("a.quickview").show();
	});
	jQuery(".product-container a.prod-link").mouseout(function() {
		jQuery(this).siblings("a.quickview").hide();
	});
	
	// keep the quickview open on hover of itself
	jQuery(".product-container a.quickview").mouseover(function() {
		jQuery(this).show();
	});
	jQuery(".product-container a.quickview").mouseout(function() {
		jQuery(this).hide();
	});
	
	// assign ajax click event to quickview button
	jQuery(".product-container a.quickview").click(function() {
		// remove all previous quickviews
		jQuery(".quick-view").remove();
		
		var prodID = jQuery(this).attr("id");
		prodID = prodID.replace("qv_","");
		
		buildQuickView(jQuery(this).parents(".product-container"));
		jQuery(".quick-view").show();
		
		$.ajax({
			async: true, // set to false for testing
			url: "/index.cfm/fuseaction/product.quickview/productID/"+prodID,
			dataType: "html",
			cache: true,
			complete: function(XMLHttpRequest, textStatus)
			{				
				if ( textStatus == 'success' )
				{
					jQuery(".quick-view .inner-content").html(XMLHttpRequest.responseText);
					
					assignQVFunctions();
				}
			},
			error: function()
			{
				jQuery(".quick-view .inner-content").html("<h2>An Error Occurred</h2>");
			}
		});
		
		return false;
	});

};

