var Gallery = {
	ThumbnailPerPage: 8,
	ThumbnailWidth: 68,
	previous : null,
	next: null,
	thumbContainer : null,
	thumbnails : null,
	images : null,
	currentIndex: 0,
	currentPage: 0,
	TotalPage: 0,
	FadeDelay: 1000,
	pause : false,
	
	init : function() {
        $(document).ready(Gallery._init);
    },
    
    _init : function () {
		Gallery.previous = $('#header #thumbsBox .prev');
		Gallery.next = $('#header #thumbsBox .next');
		Gallery.thumbContainer = $('#imgThumbs');
		Gallery.thumbnails = $('#imgThumbs a');
		Gallery.images = $('#imgDisplay .image');
		
		if (!Gallery.thumbnails || !Gallery.images) return;
		
		$('#header #thumbsBox').animate({marginTop: 0},{duration: 1000, queue: false});
		
		$('#imageContainer').mouseenter(function() {
			$('#header #thumbsBox').animate({marginTop: -70},{duration: 1000, queue: false});
		});
		
		$('#imageContainer').mouseleave(function() {
			Gallery.pause = false;
			
			$('#header #thumbsBox').animate({marginTop: 0},{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)	
			$("#imageContainer").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;
		
		$(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);
		}
	}
}

Gallery.init();
