/****************************************************
	BarriePace Product Detail functions
	Created: 11-25-2008
	By: James Van Arsdale III
****************************************************/

//populate summary data
updateSummary = function(productId)
{
	//console.log(productId);
	var sizeGroup = jQuery('#info-container-' + productId + ' .current-sizegroup span').text();
	var size = jQuery('#info-container-' + productId + ' .current-size option:selected').text();
	//var color = jQuery('#info-container-' + productId + ' .color .swatch-name').text();
	
	jQuery('#info-container-' + productId + ' .summary .summary-sizegroup').html(sizeGroup);
	jQuery('#info-container-' + productId + ' .summary .summary-size').html(size);
	//jQuery('#info-container-' + productId + ' .summary .summary-color').html(color);
}


function addToWishList(formID)
{
	jQuery("#" + formID).unbind("submit");
	jQuery("#" + formID).children("input.input-fuseaction").val('wishList.addItem');
	jQuery("#" + formID).submit();
}


// set single size/color options by default
/* not needed, since we're now selecting the first swatch by default
selectSingleOptions = function()
{
	// for each info-container check the color options count and select if there is only 1
	jQuery(".vProduct-detailPageTemplate-container .info-container").each(function() {
		var kids = jQuery(this).children();
		// which type is it?
		var single = jQuery(".vProduct-productForm-container").length;
		var package = jQuery(".vProduct-packageProductsForm-container").length;
		
		var swatchCollectionCount = 0;
		var swatchCount = 0;
		
		var productIdSafe = jQuery(this).attr("id");
		productIdSafe = productIdSafe.replace("info-container-","");
		var productId = productIdSafe.replace(/_/g,"-");
		
		if( single )
		{
			// get the collection count.
			swatchCollectionCount = jQuery(kids).filter(".vProduct-productForm-container").find(".color > .component_swatches_"+productIdSafe).length;
			
			// if collection count is EQ to 1, then continue.
			if( swatchCollectionCount == 1 )
			{
				// get the swatch count
				swatchCount = jQuery(kids).filter(".vProduct-productForm-container").find(".color > .component_swatches_"+productIdSafe+" .swatch-list ul li").length;
				
				// if count is EQ 1, then there's only 1 swatch, trigger the onclick event for the child anchor
				if( swatchCount == 1 )
				{
					jQuery(kids).filter(".vProduct-productForm-container").find(".color > .component_swatches_"+productIdSafe+" .swatch-list ul li:eq(0) a").trigger("click");
				}
			}
		}
		else if( package )
		{
			// get the collection count.
			swatchCollectionCount = jQuery(kids).filter(".size").find(".component_swatches_"+productIdSafe).length;
			
			// if collection count is EQ to 1, then continue.
			if( swatchCollectionCount == 1 )
			{
				// get the swatch count
				swatchCount = jQuery(kids).filter(".productFormContainer").find(".color > .swatches > .swatch-list ul li").length;
				
				// if count is EQ 1, then there's only 1 swatch, trigger the onclick event for the child anchor
				if( swatchCount == 1 )
				{
					jQuery(kids).filter(".productFormContainer").find(".color > .swatches > .swatch-list ul li:eq(0) a").trigger("click");
				}
			}
		}
	});
}
*/


/* READY SCRIPTS
jQuery(document).ready(function() {
	
	// update the summary info for each product
	jQuery(".vProduct-detailPageTemplate-container .info-container").each(function() {
		var productID = jQuery(this).attr("id");
		productID = productID.replace("info-container-","");
		updateSummary(productID);
	});
	
	// set single size/color options on load
	selectSingleOptions(); //not needed, since we're now selecting the first swatch by default
	
});
*/



/* AJAX add to cart */
var frmOptions = {
	url: "/index.cfm",
	type: "post",
	dataType: "html",
	resetForm: false,
	timeout: 90000,
	beforeSubmit: validateOrder,
	success: showResponse,
	error: function(XMLHttpRequest, textStatus, errorThrown)
	{
		//alert(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(document).ready(function() {
	jQuery(".vProduct-detailPageTemplate-container form.add-to-cart-form").submit(function() { 
		jQuery(this).ajaxSubmit(frmOptions); 
		return false;
	});
	/* moved to detail page, due to IE7 bug
	alert(jQuery(".vProduct-detailPageTemplate-container .addcart-action").parents("form").attr("id"));
	
	jQuery(".vProduct-detailPageTemplate-container form.add-to-cart-form input.addcart-action").click(function() {
		jQuery(this).parents("form.add-to-cart-form").submit();
		return false;
	 });
	*/
});

// pre-submit callback 
function validateOrder(formData, jqForm, options) { 
	// formData is an array; here we use jQuery.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); 
	
	// get the form values for validation
	var valuePairs = queryString.split("&");
	var thisQty = 0;
	
	//alert(valuePairs);
	
	// loop through each set, and search for quantity
	jQuery.each(valuePairs, function(i,n) {
		valPair = n.split("=");
		// if quantity is found, set the value to thisQty
		if( jQuery.inArray("quantity", valPair) != -1 )
		{
			thisQty = valPair[jQuery.inArray("quantity", valPair) + 1];
		}
    });
	
	// reset array for continued use.
	valuePairs = "";
		
	if( thisQty > 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 enter a Quantity to add this item to your Shopping Bag.");
		return false;
	}
} 
 
// post-submit callback 
function showResponse(responseTxt, statusText)  { 
	//jQuery('#content-container').html('status: ' + statusText + '\n\nresponseText:' + responseTxt);	
	
	// get cart details.
	jQuery.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' )
			{
				// add newly returned items
				jQuery("#header .bag").append(XMLHttpRequest.responseText);
				
				// update the item count in the util nav
				jQuery("#header .bag h3 a span.quantity span").html(jQuery("#header .bag .cart-drop ul li.cart-message input.cart-count").val());
				
				// assign close button function
				jQuery("#header .bag .cart-drop a.close").click(function() {
					jQuery("#header .bag .cart-drop").remove();
					toggleSelects(1);
					return false;
				});
			
				hideLoading();
				toggleSelects(0);
				
				var temp = setTimeout('jQuery("#header .bag .cart-drop").remove()', 7000);
				var temp2 = setTimeout('toggleSelects(1)', 7000);
			}
			else
			{
				hideLoading();
				alert("Error\nWe were unable to update the Shopping Bag summary, visit your Shopping Bag to see your items added.");
				return false;
			}
		},
		error: function()
		{
			alert("Error\nWe were unable to update the Shopping Bag summary, visit your Shopping Bag to see your items added.");
		}
	});
} 

function showLoading()
{
	toggleSelects(0);
	jQuery("body").append("<div id=\"ordersave-loading\" class=\"overlay\" style=\"display: none;\"><div class=\"loading\"><h2>{ Adding Items to your Shopping Bag }</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("#header",500);
}

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

function toggleSelects(toggle)
{
	if( jQuery.browser.msie	 && jQuery.browser.version <= 6 )
	{
		if(toggle)
		{
			jQuery("select").show();
		}
		else
		{
			jQuery("select").hide();
		}
	}
}
