// CR11
if (typeof Salmon != "object") {
		var Salmon = {};
	}
	
	Salmon.ImageSwapper = {
		mainImageID: "mainimage",
		altImageClass: "alternativeviewlink",
		mainImage: null,
		isZoomEnabled: false,
		productZoomImagePath: "",
		itemZoomImagePath: "",
		
		debug: function(pFunction, pMessage) {
			//alert(pFunction + ": " + pMessage);
		},
		
		init: function() {
			if (!document.getElementById) return;
			
			Salmon.ImageSwapper.mainImage = document.getElementById(Salmon.ImageSwapper.mainImageID);
			if(Salmon.ImageSwapper.mainImage == null) {
				return;
			}
			Salmon.ImageSwapper.prepareAltImageMenu();
		},
		
		prepareAltImageMenu: function() {
			var selector = "a."+Salmon.ImageSwapper.altImageClass;
			var activators = $(selector);
			for(var i=0;i<activators.length;i++) {
				activators[i].onclick = Salmon.ImageSwapper.altImageActivator;
			}
		},
		
		altImageActivator: function() {
			var method = "altImageActivator";
			Salmon.ImageSwapper.debug(method, "In mainImage.src=" + Salmon.ImageSwapper.mainImage.src);
					
			var thumbImage = this.getElementsByTagName('img')[0];
			var mainImageSrc = Salmon.ImageSwapper.mainImage.src;
			var originalImageName = $("#mainimage")[0].src;
			
			// get original image name only
			originalImageName = originalImageName.substring(originalImageName.lastIndexOf("/")+1, originalImageName.length);

			// swap the thumbnails			
			Salmon.ImageSwapper.mainImage.src = this.href; // set the main image src to be the href of the a.alternativeviewlink
			this.href = mainImageSrc; // set a.alternativeviewlink to link to swapped in image
			var thumbImageName = thumbImage.src.substring(thumbImage.src.lastIndexOf("/")+1, thumbImage.src.length);
			
			//switch the URL's use for thumbs / main image between product / items as necessary
			var newThumbSrc = thumbImage.src;
			var itemtoproductchange = false;
			var producttoitemchange = false;
			if (mainImageSrc.indexOf("products") > 0 && thumbImage.src.indexOf("items") > 0){
				newThumbSrc = newThumbSrc.replace("items", "products");
				producttoitemchange = true;
			}
			else if (mainImageSrc.indexOf("items") > 0 && thumbImage.src.indexOf("products") > 0){
				newThumbSrc = newThumbSrc.replace("products", "items");
				itemtoproductchange = true;
			}
		
			// swap thumbnail image names, keeping url before image name
			thumbImage.src = newThumbSrc.replace(thumbImageName, originalImageName);
		
			// create new view larger image link using current image ref
			var swappedImage = Salmon.ImageSwapper.mainImage.src.substring(Salmon.ImageSwapper.mainImage.src.lastIndexOf("/")+1, Salmon.ImageSwapper.mainImage.src.length);
			
			// for each a.pdlargerimage replace in new image name (and convert the resultant url per product/item as necessary)
			$(".pdlargerimage").each(
				function(i){
  					$(this).attr("href", this.href.replace(originalImageName, swappedImage));
  					if (producttoitemchange){
	  					$(this).attr("href", this.href.replace("products", "items"));
					}
					else if (itemtoproductchange){
  						$(this).attr("href", this.href.replace("items","products"));
					}
				}
			);
			
			Salmon.ImageSwapper.setZoomImage(Salmon.ImageSwapper.mainImage.src, swappedImage);

			Salmon.ImageSwapper.debug(method, "Out mainImage.src=" + Salmon.ImageSwapper.mainImage.src);
			return false;
		},
		
		setZoom: function(isZoomEnabled, imagePath, productZoomImagePath, itemZoomImagePath) {
			Salmon.ImageSwapper.isZoomEnabled = isZoomEnabled;
			Salmon.ImageSwapper.productZoomImagePath = imagePath + productZoomImagePath;
			Salmon.ImageSwapper.itemZoomImagePath = imagePath + itemZoomImagePath;
		},
		
		setZoomImage: function(mainImageSrc, swappedImage) {
			var method = "setZoomImage";
			Salmon.ImageSwapper.debug(method, "mainImageSrc=" + mainImageSrc + ", swappedImage=" + swappedImage);
			
			if (Salmon.ImageSwapper.isZoomEnabled) {
				var zoomImagePath = Salmon.ImageSwapper.productZoomImagePath;
				if (mainImageSrc.indexOf("items") > 0) {
					zoomImagePath = Salmon.ImageSwapper.itemZoomImagePath;
				}
				Salmon.ImageSwapper.debug(method, "zoomImage=" + zoomImagePath + swappedImage);
				$('#mainimage').attr("jqimg", zoomImagePath + swappedImage);
			}
		}
	}
	
	$(document).ready(function(){ Salmon.ImageSwapper.init(); });
	