var jq_togglePrice_id = null;
var jq_togglePrice_itemPrices = null;
var jq_togglePrice_origPrice = null;


(function($) {

	 $.fn.togglePrice = function(productId, form, JSONItemPricingURL) {
		 if (jq_togglePrice_id == null || jq_togglePrice_id != productId) {
			 $.ajax({
				url: JSONItemPricingURL,
				dataType: 'json',
				success: function(json) {
				 	jq_togglePrice_id = productId;
				 	jq_togglePrice_itemPrices = json.itemPrices;
				 	jq_togglePrice_origPrice = form.find("span.price").html();
				 	$.fn.togglePrice.processSelection(form);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					//alert(textStatus + errorThrown);
					//alert(XMLHttpRequest.responseText);
					return;
				}
			});
		 }
		 else {
			 $.fn.togglePrice.processSelection(form);
		 }
	};
	
	
	$.fn.togglePrice.processSelection = function(form) {
		var selectedAttributes = form.find('.attribute .selected');
		
		var colorAttr = null;
		$.each(selectedAttributes, function(i) {
			if ($(this).hasClass('color-swatch')) {
				colorAttr = $(this);
				return false;
			}
		 });
		
		var colorAttrName = $.fn.togglePrice.getAttrName(colorAttr, form);
		var colorAttrValue = $.fn.togglePrice.getAttrValue(colorAttrName, form);
		$.fn.togglePrice.showPrice(colorAttrName, colorAttrValue, form);
	}
	
	
	$.fn.togglePrice.getAttrName = function(button, form) {
		var attrName = /attribute-(\w+)/.exec(button.parents('li[class^=attribute-]').attr('class'))[1];
		return attrName;
	};
	
	
	$.fn.togglePrice.getAttrValue = function(attrName, form) {
		var attrTag = form.find('.attribute-' + attrName + ' .selected')[0];
		if(attrTag) {
			return $(attrTag).val().toLowerCase();
		}
		else {
			return "";
		}
	};
	

	$.fn.togglePrice.showPrice = function(colorAttrName, colorAttrValue, form) {
		var colorSelected = false;
		
		$.each(jq_togglePrice_itemPrices, function(i, item) {
			var match = true;
			
			if (item[colorAttrName] != colorAttrValue) {
				match = false;
			}
									
			if (match == true) {
				colorSelected = true;
				var priceRegex = new RegExp(".(\\d+\\.\\d+)");
				var listPrice = priceRegex.exec(item["listPrice"]);
				var offerPrice = priceRegex.exec(item["offerPrice"]);
				
				if (offerPrice != null) {
					if (listPrice != null && (new Number(listPrice[1])) > (new Number(offerPrice[1]))) {
						form.find("span.price").html("<del> " + item["listPrice"] + " </del><ins> " + item["offerPrice"] + " </ins>");
					}
					else {
						form.find('span.price').html(item['offerPrice']);
					}
				}
				else {
					form.find("span.price").html(jq_togglePrice_origPrice);
				}
				
				return false;
			}
		});
		
		if (colorSelected == false) {
			form.find("span.price").html(jq_togglePrice_origPrice);
		}
	};
	
	 $.fn.toggleSearchPrice = function(productId, form, JSONItemPricingURL, colorCode) {
		 
		 if (jq_togglePrice_id == null || jq_togglePrice_id != productId) {
			 $.ajax({
				url: JSONItemPricingURL,
				dataType: 'json',
				success: function(json) {
				 	jq_togglePrice_id = productId;
				 	jq_togglePrice_itemPrices = json.itemPrices;
				 	jq_togglePrice_origPrice = form.find("span.price").text();
				 	$.fn.togglePrice.showPrice("color", colorCode, form);
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					//alert(textStatus + errorThrown);
					//alert(XMLHttpRequest.responseText);
					return;
				}
			});
		 }
		 else {
			 $.fn.togglePrice.showPrice("color", colorCode, form);
		 }
	};
	

})(jQuery)
