var Gallery = {
	images : null,
	thumbnailContainer : null,
	thumbnails : null,
	explanation : null,
	imagePrev : null,
	imageNext : null,
	currentObject : null,
	currentThumbnail : null,
	btnPlay : null,
	btnStop : null,
	
	init : function(iImage,iThumbContainer,iThumbnail,iPlay,iStop,iCaption) {
        $(function () {
			Gallery._init(iImage,iThumbContainer,iThumbnail,iPlay,iStop,iCaption);
		});
    },
    
    _init : function (iImage,iThumbContainer,iThumbnail,iPlay,iStop,iCaption) {
		Gallery.images = $(iImage);
		Gallery.thumbnailContainer = $(iThumbContainer);
		Gallery.thumbnails = $(iThumbnail);
		
		if ($(iCaption) != null) {
			Gallery.explanation = $(iCaption);
		}
			
		if ($('.imgBigCover .prev') != null) {
			Gallery.imagePrev = $('.imgBigCover .prev');
			Gallery.imagePrev.fadeOut(0);
		}
		
		if ($('.imgBigCover .next') != null) {
			Gallery.imageNext = $('.imgBigCover .next');
			if(Gallery.images.size() <= 1) {
				Gallery.imageNext.fadeOut(0);
				$('#controlCover').fadeOut(0);
			}
		}
		
		if ($(iPlay) != null) {
			Gallery.btnPlay = $(iPlay);
		}
		
		if ($(iStop) != null) {
			Gallery.btnStop = $(iStop);
		}
		
		if (!Gallery.images || !Gallery.thumbnailContainer || !Gallery.thumbnails || !Gallery.explanation || !Gallery.btnPlay || !Gallery.btnStop) return;
		
		Gallery.images.each(function(index) {
			if (index != 0)
				$(this).fadeOut(0);
				
			this.index = index;
		});
		
		Gallery.currentObject = Gallery.images[0];
		Gallery.thumbnails.each(function(index) {
			this.DisplayObject = Gallery.images[index];
			this.index = index;
			$(this).bind('click',Gallery.onThumbnailClick);
			$(this).bind('display',Gallery.onDisplayImage);
			
			if (index == 0) {
				Gallery.currentThumbnail = $(this);
				$(this).addClass('active');
			}
		});
		
		Gallery.btnPlay.bind('click',Gallery.play);
		Gallery.btnStop.bind('click',Gallery.stop);
		
		Gallery.imagePrev.attr('rel','prev');
		Gallery.imagePrev.bind('click',Gallery.onImageMoveClick);
		Gallery.imageNext.attr('rel','next');
		Gallery.imageNext.bind('click',Gallery.onImageMoveClick);
		
		$("#gallery").everyTime(5000,Gallery.autoPlay);
	},
	
	onImageMoveClick : function(event) {
		event.preventDefault();

		$("#gallery").stopTime();
		
		var nextIndex;
		if ($(this).attr('rel') == 'next') 
			nextIndex = Gallery.currentObject.index + 1;			
		else
			nextIndex = Gallery.currentObject.index - 1;
			
		$(Gallery.currentObject).fadeOut(500);			
		$(Gallery.images[nextIndex]).fadeIn(500);
		Gallery.currentObject = Gallery.images[nextIndex];
		Gallery.explanation.html($(Gallery.currentObject).children('img').attr('alt'));
		
		
		if (Gallery.currentThumbnail)
			Gallery.currentThumbnail.removeClass('active');
		
		Gallery.currentThumbnail = $(Gallery.thumbnails[nextIndex]);
		Gallery.currentThumbnail.addClass('active');
		
		if ($(this).attr('rel') == 'next') {
			if (nextIndex == (Gallery.images.length - 1))
				$(this).fadeOut(500);
				
			Gallery.imagePrev.fadeIn(500);

			if (Math.floor(nextIndex/4) != (Gallery.currentThumbPages - 1))	 {
				Gallery.gotoPage(Math.floor(nextIndex/4));
			}				
		}else {
			if (nextIndex == 0)
				$(this).fadeOut(500);
				
			Gallery.imageNext.fadeIn(500);
				
			if (Math.floor(nextIndex/4) != (Gallery.currentThumbPages - 1))	 {
				Gallery.gotoPage(Math.floor(nextIndex/4));
			}
		}
		
		return false;
	},
	
	onThumbnailClick : function(event) {
		event.preventDefault();

		$("#gallery").stopTime();
		
		$(this).trigger('display');
		
		return false;
	},
	
	onDisplayImage : function() {
		if (Gallery.currentThumbnail)
			Gallery.currentThumbnail.removeClass('active');
			
		Gallery.currentThumbnail = $(this);
		
		$(this).addClass('active');
		
		$(Gallery.currentObject).fadeOut(1000);
		$(this.DisplayObject).fadeIn(1500);
		Gallery.currentObject = this.DisplayObject;
		
		Gallery.explanation.html($(Gallery.currentObject).children('img').attr('alt'));
		
		if (Gallery.currentObject.index == (Gallery.images.length - 1)) {
			Gallery.imageNext.fadeOut(500);
		} else {
			Gallery.imageNext.fadeIn(500);	
		}
		
		if (Gallery.currentObject.index > 0) {
			Gallery.imagePrev.fadeIn(500);		
		} else {
			Gallery.imagePrev.fadeOut(500);
		}
	},
	
	autoPlay : function() {
		var currentIndex = (Gallery.currentObject.index + 1)%Gallery.thumbnails.length;

		$(Gallery.thumbnails[currentIndex]).trigger('display');
	},
	
	play : function() {
		$("#gallery").everyTime(5000,Gallery.autoPlay);
		return false;
	},
	
	stop : function() {
		$("#gallery").stopTime();
		return false;
	}
}
