var Gallery = {
	ThumbnailPerPage: 9,
	ThumbnailWidth: 70,
	previous : null,
	next: null,
	thumbContainer : null,
	thumbnails : null,
	images : null,
	caption: null,
	currentIndex: 0,
	currentPage: 0,
	TotalPage: 0,
	FadeDelay: 1000,
	pause : false,
	
	init : function() {
        $(document).ready(Gallery._init);
    },
    
    _init : function () {
		Gallery.previous = $('#displayImage #thumbsBox .prev');
		Gallery.next = $('#displayImage #thumbsBox .next');
		Gallery.thumbContainer = $('#imgThumbs');
		Gallery.thumbnails = $('#imgThumbs a');
		Gallery.images = $('#displayImage .images .image');
		Gallery.caption = $('#displayImage .caption');
		
		if (!Gallery.thumbnails || !Gallery.images) return;
		
		if (!Gallery.caption[0]) {
			$('#displayImage #thumbsBox').animate({bottom: -98},{duration: 1000, queue: false});
			
			$('#displayImage .container').mouseenter(function() {
				$('#displayImage #thumbsBox').animate({bottom: -16},{duration: 1000, queue: false});
			}).mouseleave(function() {
				Gallery.pause = false;
				
				$('#displayImage #thumbsBox').animate({bottom: -98},{duration: 1000, queue: false});
			});
		} else {
			$('#displayImage #thumbsBox').children().css({visibility: 'hidden'});
			$('#displayImage #thumbsBox').animate({bottom: -68},{duration: 1000, queue: false});
			
			$('#displayImage .container').mouseenter(function() {
				$('#displayImage #thumbsBox').children().css({visibility: 'visible'});
				$('#displayImage #thumbsBox').animate({bottom: -16},{duration: 1000, queue: false});
			}).mouseleave(function() {
				Gallery.pause = false;
				
				$('#displayImage #thumbsBox').children().css({visibility: 'hidden'});
				$('#displayImage #thumbsBox').animate({bottom: -68},{duration: 1000, queue: false});
			});
		}
		
		Gallery.TotalPage = Math.ceil(Gallery.thumbnails.length/Gallery.ThumbnailPerPage);
		
		if (Gallery.thumbnails.length <= Gallery.ThumbnailPerPage) {
			Gallery.previous.css({display: 'none'});
			Gallery.next.css({display: 'none'});
			
			$('#imgThumbsCover').css({width: Gallery.ThumbnailWidth * Gallery.thumbnails.length, 'float' : 'none'});
		}
		
		Gallery.images.each(function(index) {
			if (index > 0)
				$(this).fadeOut(0);
			else
				$(this).fadeIn(1000);
		});
		
		Gallery.thumbnails.each(function(index) {
			if (index == 0)
				$(this).addClass('active');
				
			$(this).click(function() {
				Gallery.pause = true;
			
				Gallery.GotoImage(index);
				
				return false;
			});
		});
		
		Gallery.next.click(Gallery.nextPage);
		Gallery.previous.click(Gallery.previousPage);
		
		if (Gallery.images.length > 1)	
			$("#displayImage").everyTime(3000,Gallery.auto);
	},
	
	auto: function() {
		if (Gallery.pause)
			return;
			
		var index = Gallery.currentIndex + 1;
		
		if (index + 1 > Gallery.images.length) 
			index = 0;
			
		Gallery.GotoImage(index);
	},
	
	GotoImage: function(index) {
		$(Gallery.images[Gallery.currentIndex]).fadeOut(Gallery.FadeDelay);
		$(Gallery.thumbnails[Gallery.currentIndex]).removeClass('active');
		Gallery.currentIndex = index;
		
		if (Gallery.caption) {
			Gallery.caption.html($(Gallery.images[Gallery.currentIndex]).children('img').attr('alt'));
		}
		
		$(Gallery.images[Gallery.currentIndex]).fadeIn(Gallery.FadeDelay);
		$(Gallery.thumbnails[Gallery.currentIndex]).addClass('active');
	},
	
	nextPage: function() {
		if (Gallery.currentPage + 1 < Gallery.TotalPage) {
			Gallery.currentPage++;
			
			var tagetMargin = -1*Gallery.currentPage*Gallery.ThumbnailPerPage*Gallery.ThumbnailWidth;
			
			$(Gallery.thumbContainer).animate({marginLeft: tagetMargin},1000);
		}
		
		return false;
	},
	
	previousPage: function() {
		if (Gallery.currentPage > 0) {
			Gallery.currentPage--;
			
			var tagetMargin = -1*Gallery.currentPage*Gallery.ThumbnailPerPage*Gallery.ThumbnailWidth;
			
			$(Gallery.thumbContainer).animate({marginLeft: tagetMargin},1000);
		}
		
		return false;
	}
}

Gallery.init();
