function _MW_string_trim(thestring) {
	return thestring.replace(/^\s+|\s+$/g, '');
}

function MW_enhance_SideCategoryList() {
	if (($("#SideCategoryList li a").length > 0) && ($(".Breadcrumb li a").length > 0)) {
		$("#SideCategoryList ul ul").addClass("subcategory");
		
		var link_list = new Array();
		var breadcrumblinks = "|";
		var breadcrumbtext = "|";
		$(".Breadcrumb li").each( function() {
			if ($(this).find("a").length > 0) {
				link_list.push(_MW_string_trim($(this).find("a").attr("href")).toLowerCase());
			}
		});

		link_list.push = location.href.toLowerCase();
				
		$("#SideCategoryList li a").each( function() {
			var curlink = $(this).attr("href").toLowerCase();
			for (link_index in link_list) {
				if (curlink == link_list[link_index]) {
					$(this).addClass("current");
					$(this).parents("li").addClass("current");
				}
			}
		});
		
	}
}

function MW_fix_SubCategoryListGrid() {
	if ($(".SubCategoryListGrid li").length > 0) {
		$(".SubCategoryListGrid li").each( function() {
			if ($(this).css("clear") == "both") {
				$(this).addClass("clearer");
			}
		});
	}
}

function MW_enhance_NewsletterSubscription() {
	if ($("#nl_email").length > 0) {
		if ($("#nl_email").val().length == 0) {
			$("#nl_email").addClass("fieldbg");
		}
		$("#nl_email").focus( function() {
			$("#nl_email").removeClass("fieldbg");
		});
		$("#nl_email").blur( function() {
			if ($("#nl_email").val().length == 0) {
				$("#nl_email").addClass("fieldbg");
			} else {
				$("#nl_email").removeClass("fieldbg");
			}
		});
	}
}

function MW_add_variation_price() { // crazy madness //
	if ($("em.ProductPrice").length > 0) {
		var CurrentPrice = parseFloat($(".ProductPrice").text().replace("$","").replace(",",""));
		CurrentPrice = CurrentPrice.toFixed(2);
		
		if ($(".ProductOptionList input").length > 0) {
			var CurrentCombination = "";
			$(".ProductOptionList input").each( function() {
				CurrentCombination = $(this).val();
				for (comboindex in VariationList) {
					if (VariationList[comboindex].combination == CurrentCombination) { // found it
						VariationPrice = parseFloat(VariationList[comboindex].price.replace("$","").replace(",",""));
						VariationPrice = VariationPrice.toFixed(2);
						
						PriceDifference = VariationPrice - CurrentPrice;
						PriceDifference = PriceDifference.toFixed(2);
						if (PriceDifference > 0) {
							$(this).parent().append(" <span class=\"VariationPrice\">($" + PriceDifference + ")</span> ");
						}
						break;
					}
				}
			});
		}

		if ($(".ProductOptionList select").length > 0) {
			$(".ProductOptionList select").change( function() {
				var PrefixCombination = "";
				var PrefixCombinationArray = new Array();
				$(".ProductOptionList select").each( function() {
					if ($(this).find("option:selected").val().length > 0) {
						PrefixCombinationArray.push($(this).find("option:selected").val());
					}
				});
				PrefixCombination = PrefixCombinationArray.join(",");
				
				//alert($(".ProductOptionList select:last option").length);
				$(".ProductOptionList select:last option").each( function() {
					var CurrentCombination = PrefixCombination + "," + $(this).val();
					for (comboindex in VariationList) {
						if (VariationList[comboindex].combination == CurrentCombination) { // found it
							VariationPrice = parseFloat(VariationList[comboindex].price.replace("$","").replace(",",""));
							VariationPrice = VariationPrice.toFixed(2);
							
							PriceDifference = VariationPrice - CurrentPrice;
							PriceDifference = PriceDifference.toFixed(2);
							if (PriceDifference > 0) {
								$(this).append(" ($" + PriceDifference + ")");
							}
							break;
						}
					}
				});
			});
		}
	}
}

function MW_hide_colorshopUD_button() {
	if ($("#udesign").length > 0) {
		$("#udesign").hide(); // if they have JS, hide it show we can show it later in the appropriate places
		
		var display_sections = new Array();
		display_sections[0] = "http://upperroomhome.ca/categories/Dining/";
		
		// gather the links from the breadcrumb list
		if ($(".Breadcrumb li a").length > 0) {
			var link_list = new Array();
			$(".Breadcrumb li").each( function() {
				if ($(this).find("a").length > 0) {
					link_list.push($(this).find("a").attr("href").toLowerCase());
				}
			});
		}
		
		link_list.push(location.href.toLowerCase());
		
		for (link_index in link_list) {
			for (section_index in display_sections) {
				if (link_list[link_index].toLowerCase() == display_sections[section_index].toLowerCase()) {
					$("#udesign").show();
					return;
				}
			}
		}
	}
}

$(document).ready(function() {
	MW_enhance_SideCategoryList();
	MW_fix_SubCategoryListGrid();
	MW_enhance_NewsletterSubscription();
	MW_add_variation_price();
	MW_hide_colorshopUD_button();
	//MW_fix_flash();
});
