$(document).ready(function(){

	$(function() {
		$('#lightboxGallery a').lightBox();
	});

	var ProductImageGallery = new (function(){
		//var select = document.getElementById("attrValue1");
		var primaryImage = document.getElementById("mainimage");
		var largeImageLink = document.getElementById("pdlargerimage");	
		var secondaryImagesContainer = $(".imagedisplay dd.alternativeview")[0] || null;
		

		if(!primaryImage || !secondaryImagesContainer || !largeImageLink) return;
		
		var imageInformation = productImageSetMap;
		
		var state = { currentProductImageSet: null, currentProductImageIndex: 0 };
		
		/*
		if (select) {
			$(select).bind("change", swapColour);
		}
		
		function swapColour() {
			var productImageSet = imageInformation.get(select.value) || null;
			if(!productImageSet) return;
			
			setCurrentProductImageSet(select.value);
			setCurrentProductImageIndex(0);
			var mainProduct = productImageSet[0];
			primaryImage.src = mainProduct.mediumImageURL;
			primaryImage.alt = mainProduct.altText;
			primaryImage.jqimg = mainProduct.largeImageURL;
			
			largeImageLink.href = mainProduct.largeImageURL;
			thumbHandlers = [];
			var thumbsHTML = '<h2>Alternative Images</h2>';
			
			secondaryImagesContainer.innerHTML = thumbsHTML;
			
			for(var i = 1; i < productImageSet.length; i++) {
				var thumb = new ThumbHandler(productImageSet, i);
				thumbHandlers.push(thumb);
				secondaryImagesContainer.innerHTML += thumb.html;	
			}
			
			for(var i = 0; i < thumbHandlers.length; i++) {
				thumbHandlers[i].addEvent();
			}
			// Addition for Zoom: swap the jqimg attribute of the main image for the zoom
            $('#mainimage').attr("jqimg", primaryImage.jqimg);
			
		}
		*/
		
		function setCurrentProductImageSet(newValue) {
			state.currentProductImageSet = newValue;
		}
		
		function setCurrentProductImageIndex(newValue) {
			state.currentProductImageIndex = newValue;
		}
		
		function getCurrentProductImageIndex() {
			return state.currentProductImageIndex;
		}		
		
		var thumbHandlers = [];
		
		function ThumbHandler(productImageSet, index) {
			var productImageSet = productImageSet;
			var index = index;
			
			var thumbLink = null;
			var thumbImage = null;
			
			var thumbsHTML = '';
			thumbsHTML += 	'<span class="image">';
			thumbsHTML += 		'<a class="alternativeviewlink" href="'+productImageSet[index].mediumImageURL+'">';
			thumbsHTML +=			'<img alt="'+productImageSet[index].altText+'" src="'+productImageSet[index].smallImageURL+'"/>';
			thumbsHTML += 		'</a>';
			thumbsHTML += 	'</span>';
			
			this.html = thumbsHTML;
			
			this.addEvent = function() {
				thumbLink = $("a.alternativeviewlink")[index-1] || null;
				if(!thumbLink) return;
				thumbImage = $(thumbLink).find("img")[0] || null;
				if(!thumbImage) return;
				$(thumbLink).bind("click", thumb_onClick);
			}
			
			function thumb_onClick() {
				var currentIndex = getCurrentProductImageIndex();
				var thumbIndex = getThumbIndex();
				primaryImage.src = productImageSet[thumbIndex].mediumImageURL;
				primaryImage.alt = productImageSet[thumbIndex].altText;
				primaryImage.jqimg = productImageSet[thumbIndex].largeImageURL;
				//console.log('Changing jqimg to:' + primaryImage.jqimg);
				largeImageLink.href = productImageSet[thumbIndex].largeImageURL;
				thumbLink.href = productImageSet[currentIndex].mediumImageURL;
				thumbImage.src = productImageSet[currentIndex].smallImageURL;
				thumbImage.alt = productImageSet[currentIndex].altText;
				setCurrentProductImageIndex(thumbIndex);
				setThumbIndex(currentIndex);
				// BEN/01/000036: Set the jqimg custom attribute.
				$("#mainimage").attr({jqimg : primaryImage.jqimg});
				return false;
			}
			
			function getThumbIndex() {
				return index;
			}
			
			function setThumbIndex(value) {
				index = value;
			}
			
		}
		
		(function(){
			var currentProductImageSet = defaultProductImageSet;
			var thumbs = $("dd.alternativeview a.alternativeviewlink");
			var thumbHandler = null;
			for(var i = 0; i < thumbs.length; i++) {
				thumbHandler = new ThumbHandler(currentProductImageSet, i+1);
				thumbHandler.addEvent();
				thumbHandlers.push(thumbHandler);				
			}
		}());		
	
	});
	
});