mtLibAddEvent(function(){
	oMTLib.gallery.init("gallery", "http://bwinteractive.dev.prec/cms/plugins/butlins_gallery/gallery.xml");
}, "load");

oMTLib.gallery = {
	init : function(sID, sURL) {
		oMTLib.gallery.oGallery = document.getElementById(sID);
		if(this.oGallery) {
			var oLis = oMTLib.gallery.setLis();			
			oMTLib.gallery.setImages(oLis);
		};
	},
	preSelect : function(oLi) {
		oLi.className += " chosen";
		oLi.relatedNode.className = "preview";
	},
	setLis : function() {
		var oLis = oMTLib.gallery.oGallery.getElementsByTagName("li");			
		for (var i = 0; i < oLis.length; i++) {	
			var sID = oLis[i].id.replace("small","large");			
			oLis[i].relatedNode = document.getElementById(sID);
			oLis[i].onclick = function() {
				this.relatedNode.className = "preview";	
				for (var x = 0; x < oLis.length; x++) {							
					if(oLis[x].id != this.id) {
						oLis[x].relatedNode.className = "hide";										
					};
					if ((oLis[x].className.indexOf("chosen")) != -1)	{
						oLis[x].className = oLis[x].className.replace("chosen", "");
					};
				};
				this.className +=" chosen";					
			};
		};	
		
		if(oLis.length) {
			oMTLib.gallery.preSelect(oLis[0]);
		};			
		
		return oLis;
	},
	setImages : function(oLis) {
		var oImages = oMTLib.gallery.oGallery.getElementsByTagName("img");
		for (var i = 0; i < oImages.length; i++) {
			oImages[i].onclick = function() {
				if ((this.className.indexOf("left")) != -1) {
					for (var x = oLis.length - 1; x > 0; x--) {
						if ((oLis[x].className.indexOf("chosen")) != -1) {							
							oLis[x].relatedNode.className = "hide";							
							oLis[x].className = oLis[x].className.replace("chosen", "");
							x--;
							if (x > -1) {
								oLis[x].relatedNode.className = "preview";																										
								oLis[x].className += " chosen";
								if (x > -1 && x < oLis.length - 4){
									oLis[x].className = oLis[x].className.replace("hide", "");
									if ((oLis[x + 4].className.indexOf("hide")) == -1) {
										oLis[x + 4].className += " hide";
									};
								};
							};
						};
					};
				};
				if ((this.className.indexOf("right")) != -1) {
					for (var x = 0; x < oLis.length - 1; x++) {
						if ((oLis[x].className.indexOf("chosen")) != -1)	{
							oLis[x].relatedNode.className = "hide";	
							oLis[x].className = oLis[x].className.replace("chosen", "");
							x++;
							if (x < oLis.length) {
								oLis[x].relatedNode.className = "preview";									
								oLis[x].className += " chosen";
								if (x > 3 && x < oLis.length) {
									oLis[x].className = oLis[x].className.replace("hide", "");
									if ((oLis[x - 4].className.indexOf("hide")) == -1) {
										oLis[x - 4].className += " hide";
									};
								};
							};
						};
					};
				};
			};
		};
	}
};