	// /////////////////////////////////////////////////////
	// Mountain Equipment
	// Common functions
	// Version 1.0	
	
	// ------------------------------------------------------------------
	// Ajax / overlays
	// ------------------------------------------------------------------

	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

	
	
	
	me_keywordSearch = function(formId,fieldId) {
		var searchString = new String(jQuery("#"+fieldId).attr('value'));
		var pattern = new RegExp("\\w+","g");		
		if (!pattern.test(searchString)){
			return false;
		}
		
		//var formDataString = jQuery("#"+formId).serialize();
		var formDataString = jQuery('#'+fieldId).attr('name')+'='+jQuery('#'+fieldId).attr('value');		
		var formAction = jQuery("#"+formId).attr("action");

		jQuery("#defaultModalWindow > .modalContent").html("");		
		me_openModalWindow("defaultModalWindow");
		jQuery("#defaultModalWindow .modalAjaxLoader").show();
				
	 	jQuery.ajax({
			type: "POST",
			url: formAction,
			data: formDataString,
			dataType: "html",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("ERROR: "+textStatus + "\n error thrown: " + errorThrown);	
			},
			success: function(data, textStatus){
  				//alert("SUCCESS: "+data);
  				jQuery("#defaultModalWindow > .modalContent").html(data);
  				jQuery("#defaultModalWindow .modalAjaxLoader").hide();
			}		
		 });
		
	}

	me_help = function(url) {
		jQuery("#defaultModalWindow > .modalContent").html("");	
		me_openModalWindow("defaultModalWindow");
		jQuery("#defaultModalWindow .modalAjaxLoader").show();
		jQuery.ajax({
			type: "POST",
			url: url,
			dataType: "html",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("ERROR: "+textStatus + "\n error thrown: " + errorThrown);	
			},
			success: function(data, textStatus){  				
  				jQuery("#defaultModalWindow > .modalContent").html(data);
  				jQuery("#defaultModalWindow .modalAjaxLoader").hide();
			}		
		 });	

	}

	// ------------------------------------------------------------------
	// Product flash image gallery
	// ------------------------------------------------------------------
	me_productGallery = function(sku) {
		//var params = {};
		//params.sku = sku;
		//sku = 022426;
		//alert(sku);
		jQuery("#defaultModalWindow > .modalContent").html("");		
		me_openModalWindow("productGalleryModalWindow");		
		
		var flashvars = {};
		flashvars.xmlurl = "/me_extra/productGalleryXML.php?sku="+sku;
		var params = {};
		params.scale = "noscale";
		params.salign = "tl";
		params.bgcolor = "#2f373c";
		var attributes = {};
		attributes.align = "center";			
		swfobject.embedSWF("/swf/video.swf", "me-flashContainerProductGallery", "526px", "400px", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
		/*
	 	jQuery.ajax({
			type: "GET",
			url: '/me_extra/productGallery.php',
			data: params,
			dataType: "html",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				alert("ERROR: "+textStatus + "\n error thrown: " + errorThrown);	
			},
			success: function(data, textStatus){
  				jQuery("#productGalleryModalWindow > .modalContent").html(data);
  				jQuery("#productGalleryModalWindow .modalAjaxLoader").hide();
			}		
		 });
		 */
	}
	
	// ------------------------------------------------------------------
	// Community flash image gallery
	// ------------------------------------------------------------------
	me_communityGallery = function(cat_id) {
		
		jQuery("#defaultModalWindow > .modalContent").html("");		
		me_openModalWindow("communityGalleryModalWindow");
		
		var flashvars = {};
		flashvars.xmlurl = "/community/index.php/gallery/xml/"+cat_id;
		var params = {};
		params.scale = "noscale";
		params.salign = "tl";
		params.bgcolor = "#2f373c";
		var attributes = {};
		attributes.align = "center";			
		swfobject.embedSWF("/swf/video.swf", "me-flashContainerCommunityGallery", "526px", "400px", "9.0.0", "/swf/expressInstall.swf", flashvars, params, attributes);
		
	}
	
	
	// ------------------------------------------------------------------
	// Overlay windows
	// ------------------------------------------------------------------
	
	me_initModalWindows = function() {
		var myOpen = function(hash) { hash.o.show(); hash.w.show(); };
		var myClose = function(hash) { hash.w.hide(); hash.o.fadeOut(500); };
		
		jQuery('.modalWindow').jqm({
			overlay: 40,
			toTop: true,
			modal: true,
			onHide: myClose,
			onShow: myOpen
		});	
	}

	// ------------------------------------------------------------------
	me_openModalWindow = function(elementId) {
		jQuery('#'+elementId).jqmShow();
	}

	// ------------------------------------------------------------------
	me_closeModalWindow = function(elementId) {
		jQuery('#'+elementId).jqmHide();
	}

	me_loadHTML = function(targetElementId, sourceURL) {
		jQuery.ajax({
			type: "GET",
			url: sourceURL,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				
			},
			success: function(data, textStatus){
  				jQuery("#" + targetElementId).html(data);
			}		
		 });

	}

	
	// ------------------------------------------------------------------
	// me_ajaxLoader 
	//		targetElementId - element to load the ajax response data into
	//		sourceURL - source URL to GET the data from
	//		hideOnFail - hide the target element if ajax fails, true/false 
	// ------------------------------------------------------------------
	me_ajaxLoader = function(targetElementId, sourceURL, hideOnFail, appendBefore, appendAfter) {

		jQuery("#" + targetElementId).hide(0);				
	 	jQuery.ajax({
			type: "GET",
			url: sourceURL,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				if (hideOnFail) { 
					jQuery("#" + targetElementId).hide(0); 
				} else {
					jQuery("#" + targetElementId).show(0);								
				}
			},
			success: function(data, textStatus){
				if(data.trim() == "") {
					if(hideOnFail) {
						jQuery("#" + targetElementId).hide(0); 
					}
				} else {
					if(appendBefore) {
						data = appendBefore + data;
					}
					if(appendAfter) {
						data = data + appendAfter;
					}
					jQuery("#" + targetElementId).html(data);
					jQuery("#" + targetElementId).show(0);								
					//jQuery("#" + targetElementId).show(0);
					//jQuery("#defaultModalWindow .modalAjaxLoader").hide();
				}
			}		
		 });
		
	}

	me_openPermaLink = function(targetElementId, sourceElementId, permaLinkId) {
		jQuery("#" + sourceElementId).hide(0);
		jQuery("#" + targetElementId).show(0);
		var permaUrl = window.location.href;
		var hashIndex = permaUrl.indexOf('#');
		if(hashIndex != -1) {
			permaUrl = permaUrl.substring(0, hashIndex);
		}
		jQuery("#" + permaLinkId).attr('value', permaUrl);
		jQuery("#" + permaLinkId).focus();
		jQuery("#" + permaLinkId).select();
		return false;
	}

	me_closePermaLink = function(targetElementId, sourceElementId) {
		jQuery("#" + targetElementId).hide(0);
		jQuery("#" + sourceElementId).show(0);
		return false;
	}

	me_colourSwatch = function(sku, colours, targetElementId) {
		jQuery.ajax({
			type: "GET",
			url: "/community/index.php/colourswatch/index/cs-"+sku,			
			dataType: "text",
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				
			},
			success: function(data, textStatus){
				var colourSwatches = Array();
				var colours_values = data.split(";");
				for(i = 0; i < colours_values.length; i ++) {
					var colour_value = colours_values[i].trim();
					if(colour_value != "") {
						var colour = colour_value.substring(0,colour_value.indexOf(",")).trim();
						var value = colour_value.substring(colour_value.indexOf(",")+1).trim();							
						colourSwatches[colour] = value;
					}
				}
				jQuery.ajax({
					type: "GET",
					url: "/community/index.php/colourswatch/default",
					error: function (XMLHttpRequest1, textStatus1, errorThrown1) {
						
					},
					success: function(data1, textStatus1){
						var defaultColourSwatches = Array();
						var default_colours_values = data1.split(";");
						for(i = 0; i < default_colours_values.length; i ++) {
							var default_colour_value = default_colours_values[i].trim();
							if(default_colour_value != "") {
								var default_colour = default_colour_value.substring(0,default_colour_value.indexOf(",")).trim();
								var default_value = default_colour_value.substring(default_colour_value.indexOf(",")+1).trim();							
								defaultColourSwatches[default_colour] = default_value;
							}
						}

						var swatchHtml = "";

						for(i = 0; i < colours.length; i ++) {
							var value = "";
							if(colourSwatches[colours[i]]) {
								value = colourSwatches[colours[i]];
							} else if(defaultColourSwatches[colours[i]]) {
								value = defaultColourSwatches[colours[i]];
							}

							if(value != "") {
								var swatchLiHtml = '<span class="swatchDescription">' + colours[i] + '</span></li>';
								var hexColourPattern = /^([0-9a-f]{1,2}){3}$/i;
								if(hexColourPattern.test(value)) {
									swatchLiHtml = '<li><span class="swatchImage" style="background-color:#' + value + ';"></span>' + swatchLiHtml;
								} else {
									swatchLiHtml = '<li><span class="swatchImage" style="background-image:url(' + value + ');background-repeat:no-repeat;"></span>' + swatchLiHtml;
								}

								swatchHtml += swatchLiHtml;
							}
						}

						jQuery("#" + targetElementId).html(swatchHtml);

					}		
				 });

			}		
		 });

	}

	me_homeFeature = function() {
		jQuery.ajax({
			type: "GET",
			url: "/community/index.php/site/home_feature_product/C40",
			error: function (XMLHttpRequest1, textStatus1, errorThrown1) {
				
			},
			success: function(data1, textStatus1){		
				var feature_link = jQuery.trim(data1);			
				if(feature_link != "") {	
					jQuery.ajax({
						type: "GET",
						url: feature_link + "?feature",			
						dataType: "json",
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							
						},
						success: function(data, textStatus) {							
							if(data.success) {
								var brandLogoHtml = ((data.product_brand_logo == '')?'':'<div id="home-feature-brand-logo" class="brand-logo-list"></div>');
								var featureHtml = '<div class="me-Product"><a class="me-ProductListImage" href="' + data.product_url + '" title="' + data.product_name + '"><img src="' + data.product_image + '" alt="' + data.product_name + '" />' + brandLogoHtml + '</a><div class="info"><a href="' + data.product_add_to_cart_url + '" class="cart"><img src="/skin/frontend/default/default/images/product_list_cart.jpg" alt="Add to cart" /></a><a href="' + data.product_url + '" class="name" title="' + data.product_name + '">' + data.product_name + '</a>' + data.product_price + '</div></div>';
								jQuery("#home-feature-product").html(featureHtml);
								if(data.product_brand_logo != '') {
									me_brand_logo(data.product_brand_logo, 'home-feature-brand-logo');
								}
							} 
						}
					});

				}
				

				

			}		
		});

		jQuery.ajax({
			type: "GET",
			url: "/community/index.php/site/random_static_banner/C41",
			error: function (XMLHttpRequest1, textStatus1, errorThrown1) {
				
			},
			success: function(data1, textStatus1){
				var featureHtml = data1;
				jQuery("#home-feature-advice").html(featureHtml);				
			}		
		});

	}

	
	
	me_brand_logo = function(brand, targetElementId) {
		jQuery.ajax({
			type: "POST",
			url: "/community/index.php/brand/logo",
			data: "brand=" + brand,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				
			},
			success: function(data, textStatus){				
				jQuery("#"+targetElementId).html(data);				
			}		
		});
	}

