Gallery = {
	// Flyweight listeners attached to the body wrapper (listens for click events further down the page)
	attachFlyWeights : function () {
		var self = this;
		jQuery("#wrap").unbind().bind("click", function(e) { 
		    var targetElement = jQuery(e.originalTarget || e.srcElement);
		    
			if( targetElement.is(".slidePrev") ) {
		    	self.handleImageNav( targetElement, "prev");
		    }
		    else if( targetElement.is(".slideNext") ) {
		    	self.handleImageNav( targetElement, "next");
		    }
			else if( targetElement.parent().is(".slideMagnify") ) {
		    	self.loadPreview( targetElement );
		    }
		});				
	},
	
	// Handles the previous and next buttons on the image gallery
	handleImageNav : function( button, direction ){
		var mainImgDiv = button.parents('div.imageNavigation').siblings('div.mainImg');
		var currentImage = this.updateImageCount( mainImgDiv, direction );
		this.updateImageSource( mainImgDiv, Number(currentImage - 1) );
	},
	
	// Updates the rel tag of the gallery to contain the current image number within the selected image gallery
	updateImageCount : function ( mainImgDiv, direction ) {
		var currentImage = mainImgDiv.attr("rel").substring( 0, mainImgDiv.attr("rel").indexOf("/") );
		var lastImage = mainImgDiv.attr("rel").substring( mainImgDiv.attr("rel").indexOf("/") + 1, mainImgDiv.attr("rel").length );

		if ( direction == "prev" ) {
			if( currentImage == 1 ) currentImage = lastImage;
			else currentImage--;
			
		}
		else {
			if( currentImage == lastImage ) currentImage = 1;
			else currentImage++;
		}
		
		mainImgDiv.attr( "rel", currentImage + "/" + lastImage );
		mainImgDiv.siblings( "div.imageNavigation" ).find( "span.currentImage" ).text( currentImage );
		return currentImage;
	},
	
	// updates the thumbnail and magnify button with a new image source
	updateImageSource : function ( mainImgDiv, imagePosition ){
		var imagesMapId = this.findId( "imagesMap-", mainImgDiv.attr("class") );
		// imagesMap maps an id of a hotel or location to its associated thumbnail and preview image URLs
		var thumb = imagesMap[imagesMapId][imagePosition].thumbnail;
		var preview = imagesMap[imagesMapId][imagePosition].preview;
		mainImgDiv.find( 'img' ).attr( 'src', thumb );
		mainImgDiv.siblings( "div.imageNavigation" ).find( "div.slideMagnify > a" ).attr( "rel", preview );
	},
	
	// Get preview images (magnified versions) within the gallery and compile as an array
	getPreviewImageArray : function ( imagesMapId ) {
		var previewImageArray = new Array();
		for ( var i in imagesMap[imagesMapId] ) {
			var array = new Array();
			array[0] = imagesMap[imagesMapId][i].preview;
			previewImageArray[i] = array;
		}
		return previewImageArray;
	},
	
	// Load slimbox gallery for the current image gallery
	loadPreview : function ( button ) {
		var mainImgDiv = button.parents('div.imageNavigation').siblings('div.mainImg');
		var currentImage = mainImgDiv.attr("rel").substring( 0, mainImgDiv.attr("rel").indexOf("/") );
		var imagesMapId = this.findId( "imagesMap-", mainImgDiv.attr("class") );
		var previewImageArray = this.getPreviewImageArray( imagesMapId );
		jQuery.slimbox( previewImageArray, currentImage-1, {
				captionAnimationDuration: 1,
				resizeDuration: 1,
				loop: true,
				counterText: "<a href='javascript:void(0);' style='float: left;'><img src='/other_files/tcd/images/slimbox/prev.png' onclick='Gallery.prevImg()' /></a><span style='float: left;line-height:18px;padding:4px;'> {x} of {y} photos </span><a href='javascript:void(0)' onclick='Gallery.nextImg()' style='float: left;' ><img src='/other_files/tcd/images/slimbox/next.png' /></a>"
		});
	},
	
	// Custom slimbox navigation buttons
	prevImg : function() {
		jQuery('#prevLink').trigger('click');
	},
	
	nextImg : function() {
		jQuery('#nextLink').trigger('click');
	},
	
	// Regex checker
	regExec: function( expression, toMatch ) {
		var matchedPattern = expression.exec(toMatch);
		
		if (matchedPattern == null) return "";
		else return matchedPattern;
	}, 
	
	// Find an id
	findId : function( prefix, str ) {
		var regex = new RegExp(prefix+"[0-9a-zA-Z]+");
		return this.regExec(regex, str).toString().replace(prefix,'');
	}
}

// Attach flyweights to page on load
jQuery(document).ready(function(){
	Gallery.attachFlyWeights();
});