var Gallery = {

	ignoreSameImage : true,
	
	showingProcess : false,
	slideShowRepeat : true,
	slideShowForward : true,
	slideShowStopped : null,
	slideShowFreq : 10,
	
	defaultImageId : 'gallery-image',
	curImageIdIndex : 0,
	newImageElement : null,
	imageUrlArray : null,
	imageStateURLSelected : null,
	imageStateURLUnSelected : null,
	
	hideEffect : function () {
		Effect.Fade(Gallery.defaultImageId, {queue: 'front'});
	}
	
	,
	
	showEffect : function () {
		Site.sortOutHeight(false);
		//alert('b' + $('gallery-image-container').getHeight())
		Effect.Appear(Gallery.defaultImageId, {duration:3, queue: 'end', afterFinish: Gallery.showEffectCB});
	}
	
	,
	
	showEffectCB : function() {
		//alert('a' + $('gallery-image-container').getHeight())
		Site.sortOutHeight(false);
	}
	
	,
	
	/**
	* Helper method that shows a pulsating effect to an element.
	*/
	showPulsateEffect : function(elementId) {
		Effect.Pulsate(elementId, {pulses:1, duration:2});
	}
	
	,
	
	showImage : function(url) {
		Gallery.hideEffect();
		newImageElement = new Image();
		newImageElement.onLoad = Gallery.imageLoaded();
		newImageElement.src = url;
	}
	
	,
	
	/**
	* Callback method for the when the Image is loaded.
	*/
	imageLoaded : function() {
		window.setTimeout("Gallery.updateImage()", 1000);
	}
	
	,
	
	updateImage : function() {
		var imgElement = Gallery.getImageElement();
		var imageContainer = imgElement.parentNode;
		
		imgElement.id = null;
		newImageElement.id = Gallery.defaultImageId;
		newImageElement.hspace = imgElement.hspace;
		newImageElement.style.display = 'none';
		
		imageContainer.replaceChild(newImageElement, imgElement);
		
		Gallery.showEffect();
		
		Gallery.showingProcess = false;
	}
	
	,
	
	/**
	* Returns the image element.
	*/
	getImageElement : function() {
		return $('gallery-image');
	}
	
	,
	
	/**
	* Returns the index id of the previous image in the current sequence.
	*/
	getPreviousImageId : function() {
		return Gallery.curImageIdIndex - 1;
	}
	
	,
	
	/**
	* Returns the index id of the next image in the current sequence of images.
	*/
	getNextImageId : function() {
		return Gallery.curImageIdIndex + 1;
	}
	
	,
	
	/**
	* Shows the image previous in line in the current sequence of images,
	* if the first image in the sequence is already shown then a new image is not shown.
	*/
	showPreviousImage : function() {
		var imageId = Gallery.getPreviousImageId();
		
		if (!Gallery.checkImageIndexBounds(imageId)) {
			return;
		}
		
		Gallery.updateImageWithImageIndex(imageId);
	}
	
	,
	
	/**
	* Shows the image next in line in the current sequence of images,
	* if the last image in the sequence is already shown then a new image is not shown.
	*/
	showNextImage : function() {
		var imageId = Gallery.getNextImageId();
		
		if (!Gallery.checkImageIndexBounds(imageId)) {
			return;
		}
		
		Gallery.updateImageWithImageIndex(imageId);
	}
	
	,
	
	/**
	* Checks whether the given image index id is within the boudary of the list of images.
	*/
	checkImageIndexBounds : function(imageIndexId) {
		if ((imageIndexId >= Gallery.getImageUrlLength()) || (imageIndexId < 0)) {
			return false; //invalid index number
		}
		
		return true; //valid index number
	}
	
	,
	
	updateImageWithImageIndex : function(imageIndexId) {
		Gallery.showThumbImageEffect(imageIndexId);
		Gallery.updateIndexImageState(imageIndexId);
		
		if (Gallery.showingProcess) {
			return false;
		}
		else {
			Gallery.showingProcess = true;
		}
		
		if (Gallery.isExistingImage(imageIndexId)) {
			
			// do something if the current image is the same image as the given index
			Gallery.showPulsateEffect(Gallery.defaultImageId);
		
			if (Gallery.showingProcess) {
				Gallery.showingProcess = false;
				//return false;
			}
			
			if (Gallery.ignoreSameImage) {
				//return false;
			}
		}
		
		var imageUrl = Gallery.getImageUrl(imageIndexId);
		Gallery.showImage(imageUrl);
		Gallery.curImageIdIndex = imageIndexId;
		Gallery.updateIndexImageState(imageIndexId);
	}
	
	,
	
	updateIndexImageState : function(imageIndexId) {
		for (i = 0; i < Gallery.imageUrlArray.length; i++) {
			var control = $('control-' + i);
			
			if (i == imageIndexId) {
				control.src = Gallery.imageStateURLSelected;
			}
			else {
				control.src = Gallery.imageStateURLUnSelected;
			}
		}
	}
	
	,
	
	showThumbImageEffect : function(imageIndexId) {
		var thumbImage = $('image-preview-' + imageIndexId);
		
		// if the thumb image don't exists then we won't the an effect
		if (thumbImage == null) {
			return;
		}
		
		Gallery.showPulsateEffect(thumbImage.id);
	}
	
	,
	
	/**
	* Returns an image url for the index 
	*/
	getImageUrl : function(imageIndexId) {
		return Gallery.imageUrlArray[imageIndexId];
	}
	
	,
	
	hasPreviousImage : function() {
		return Gallery.curImageIdIndex > 0;
	}
	
	,
	
	hasNextImage : function() {
		return Gallery.curImageIdIndex < Gallery.getImageUrlLength() - 1;
	}
	
	,
	
	getImageUrlLength : function() {
		return Gallery.imageUrlArray.length;
	}
	
	,
	
	getImageUrlFirstIndex : function() {
		return 0;
	}
	
	,
	
	getImageUrlLastIndex : function() {
		return Gallery.getImageUrlLength() - 1;
	}
	
	,
	
	updateControls : function() {
	}
	
	,
	
	isExistingImage : function(imageIndexId) {
		if (Gallery.getImageElement().src.endsWith(Gallery.getImageUrl(imageIndexId))) {
			return true;
		}
		else {
			return false;
		}
	}
	
	, 
	
	repeatSlideShow : function(b) {
		Gallery.slideShowRepeat = b;
	}
	
	, 
	
	forwardSlideShow : function(b) {
		Gallery.slideShowForward = b;
	}
	
	, 
	
	startSlideShow : function() {
		Gallery.slideShowStopped = null;
		var periodicalExecuter = new PeriodicalExecuter();
		periodicalExecuter.initialize(Gallery.periodicalSlideShowCB, Gallery.slideShowFreq);
	}
	
	, 
	
	startSlideShowRepeat : function() {
		Gallery.repeatSlideShow(true);
		Gallery.startSlideShow();
	}
	
	, 
	
	stopSlideShow : function() {
		Gallery.slideShowStopped = true;
	}
	
	, 
	
	resetSlideShow : function(toBeginning) {
		if (toBeginning) {
			Gallery.curImageIdIndex = -1;
		}
		else {
			Gallery.curImageIdIndex = (Gallery.getImageUrlLastIndex() - 1);
		}
	}
	
	, 
	
	periodicalSlideShowCB : function(periodicalExecuter) {
	
		if (Gallery.slideShowStopped) {
			periodicalExecuter.stop();
		}
	
		if (Gallery.slideShowForward) {
			if (Gallery.hasNextImage()) {
				Gallery.showNextImage();
			}
			else if (Gallery.slideShowRepeat) {
				Gallery.updateImageWithImageIndex(Gallery.getImageUrlFirstIndex());
			}
			else {
				Gallery.stopSlideShow();
			}
		}
		else if (!Gallery.slideShowForward) {
			if (Gallery.hasPreviousImage()) {
				Gallery.showPreviousImage();
			}
			else if (Gallery.slideShowRepeat) {
				Gallery.updateImageWithImageIndex(Gallery.getImageUrlLastIndex());
			}
			else {
				Gallery.stopSlideShow();
			}
		}
		else {
			periodicalExecuter.stop();
		}
	}
}