$(document).ready(function(){
	var ejcs = {
		zoomSize : "900",		
		defaultSize : "350",
		flipPrefix : "back_",
		origPath : "",
		
		imageChangeSize : function(size1,size2) {
			var pattern = new RegExp('(-)' + size1 + '(\.jpg)$');
			if (this.origPath.match(pattern)){
				var replacement = '$1' + size2 + '$2';
				return "prodimg/" + this.origPath.replace(pattern,replacement);
			}
			else 
				return "prodimg/imgnotfound";
		},
		setup : function() { //This function detects if the large image for the main image exists.  If not, we don't call the hook so we get no zoom.
			if(!$(".itemPicture").length > 0) return; // we're on a page without an item picture.  run!
			var checkHook = new Image();
			var ejcs = this;			
			ejcs.origPath = $(".itemPicture").attr('src').split('prodimg/')[1];
			checkHook.onload = function() { // The only way I know to check an images existence from clientside. blech. currently does double loading.
					ejcs.lightboxHook();	
			}
			checkHook.src = ejcs.imageChangeSize(ejcs.defaultSize,ejcs.zoomSize);
		},
		imagePath : function() {
			return $(".itemPicture").attr('src').split('prodimg/')[1];
		},
		lightboxHook : function() {
			// Change to manually calling an event of our choosing which then triggers the lightbox event
			var ejcs = this;
			$('.itemPicture').wrap('<a href="#"></a>').click(function(){
				ejcs.origPath = $(".itemPicture").attr('src').split('prodimg/')[1]; // Reset the base path in case they've changed colors
				$(this).lightbox.start($('<a href="' + ejcs.imageChangeSize(ejcs.defaultSize,ejcs.zoomSize) + '"></a>')[0]); //The actual call into the modified lightbox library
				return false; // don't redirect page to #
			});
			$('a[@rel*=lightbox]').lightbox({customEvents: true}); //hack of lightbox to not auto-call on click but let me intercept
			$('.bodyTextSmall > .itemNum').before('<span style="font-size: x-small">Click Image to Enlarge</span>'); //add some text below as instructions
		}
	}
	ejcs.setup(); //Self calling so all we have to do is include this script.
});