(function ($) {
		   
	MM_hasImage = function(imagepath) {
		if(imagepath.indexOf("/images-mm/cleardot.gif") >= 0) {
			return false;
		} else {
			return true;	
		}
	}

	MM_swapProductDetailImage = function(link) {
		/*
			the <a> around the main image needs rel="mainimage_gallery" and the image itself needs id="mainimage"
			the <a> around the thumbnail needs class="zoomThumbActive" 
		*/
		var options = new Object();
		options = $.extend({}, eval("(" + $.trim($(link).attr('rel')) + ")"));
		if (options.smallimage) {
			var smallimage = options.smallimage;
			$('#mainimage').attr('src', smallimage);
		} else {
			alert('ERROR :: Missing parameter for largeimage or smallimage.');
			throw 'ERROR :: Missing parameter for largeimage or smallimage.';
		}
		if (options.largeimage) {
			var largeimage = options.largeimage;
			$('a.[rel="mainimage_gallery"]').attr('href', largeimage);
			$('#viewlarger').attr('href',largeimage); //viewlarger
		}
		return false;
	}
	
	MM_removeEmptyImages = function(imgselector) {
		var numimages = 0;
		var numempties = 0;
		$(imgselector).each(function() {
			numimages++;					  
			if(!MM_hasImage($(this).attr('src')) ) {
				$(this).remove();
				numempties++;
			}
		});
		return numimages - numempties;
	}
	
	MM_removeEmptyProductDetailImages = function(imgselector,firstimageid) {
		var result = MM_removeEmptyImages(imgselector);
		if(result == 1) {
			$('#'+firstimageid).remove();	
		}
	}
	
	MM_swapImageOnHover = function(imgelement, image1, image2) {
		if(MM_hasImage(image2)) {
			$(imgelement).hover(
				function(){	$(this).attr('src', image2); },
				function(){	$(this).attr('src', image1); }
			);
		}	
	}
	
	jQuery.fn.MM_swapNextImageOnHover = function() {
		this.each(function() {
			if($(this).next().attr('src')) { //make sure the image exists to prevent console error
				//hide the next image
				$(this).next().hide();
				MM_swapImageOnHover($(this), $(this).attr('src'), $(this).next().attr('src'));
			}
		});
		return this;
	}
	
	MM_swapImagesOnHoverByID = function(idprefix, swapprefix) {
		$('img[id^="'+idprefix+'"]').each(function() {
			MM_swapImageOnHover($(this), $(this).attr('src'), $('#'+swapprefix+$(this).attr('id')).attr('src'));
		});
	}
	
	
	MM_effects_flash = function() {
		$(this).fadeOut(100);
		$(this).fadeIn(500);
	}
	
	//equalize the size of cells: rowclass, imageclass, cellclass, min height
	//MUST run in $(window).load() not document.ready()
	MM_equalizeCellHeight = function(rowselector,contentselector,cellselector,minheight,maxheight) {
	
		if(!minheight) {
			var minheight = 0;	
		}
	
		$(rowselector).each( function() {
								   
				var largestheight = minheight;
				
				$(this).children().each( function() {				
					var height = $(this).find(contentselector).height();
					if(height > largestheight) {
						largestheight = height;	
					}
					
				});
				
				if(maxheight && largestheight > maxheight) {
					largestheight = maxheight;	
				}
				
				//div.image
				$(this).find(cellselector).height(largestheight);
					   
		});
	
	}
	
	//clearing placeholder text from form fields. Old way:
	
	MM_clearField = function(theText) {
     if (theText.value == theText.defaultValue)
 		{
         theText.value = "";
     	}
	}
	
	//html5 way:
	
	jQuery.support.placeholder = false;
	test = document.createElement('input');
	if('placeholder' in test) jQuery.support.placeholder = true;
	if(!$.support.placeholder) { 
		var active = document.activeElement;
		$(':text').focus(function () {
			if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
				$(this).val('').removeClass('hasPlaceholder');
			}
		}).blur(function () {
			if ($(this).attr('placeholder') != '' && ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
				$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
			}
		});
		$(':text').blur();
		$(active).focus();
		$('form').submit(function () {
			$(this).find('.hasPlaceholder').each(function() { $(this).val(''); });
		});
	}
	
})(jQuery);
