$(document).ready(function() {
	gallery = {
		gallery: false,
		galleryPrev: false,
		galleryNext: false,
		galleryImages: false,
		
		workOpener: false,
		workDesc: false,
		workText: false,
		
		imagesCount: false,
		
		currentImageIndex: 0,		
		
		init: function() {
			this.currentImageIndex = 0;
			
			this.gallery = $(".gallery");
			this.galleryPrev = $(".gallery .left");
			this.galleryNext = $(".gallery .right");
			this.galleryImages = $(".gallery .images");
			this.imagesCount = $(".gallery .images li").size() - 1;
			
			this.workOpener = $(".work-opener");
			this.workText = $(".work-text");
			this.workDesc = $(".work-desc");
			
			var _self = this;
			
			this.galleryImages.find("LI IMG").click(function() {
				_self.galleryNext.click();
			});
			
			this.workDesc.hover(
				function() {
					_self.workText.show(); 
					_self.workText.stop(true, false).animate({
						opacity: 1
					}, 500);
				},
				function() {
					_self.workText.stop(true, false).animate({
						opacity: 0
					}, 300, function() { 
						_self.workText.hide(); 
					});
				}
			);

			this.galleryNext.click(function() {
				var width = $("#page").width();

				_self.galleryImages.clearQueue();
				
				if(_self.currentImageIndex+1 > _self.imagesCount) {
					/*
					_self.galleryImages.animate({
						marginLeft: (-1) * width * _self.imagesCount -48 + "px"
					}, 100, function() {
						$(".gallery .images").animate({
							marginLeft: (-1) * width * _self.imagesCount + "px"
						}, 500);
					});
					*/
					
					return false;
				}					
				
				var indexToHide = _self.currentImageIndex;
				_self.galleryImages.find("li").eq(_self.currentImageIndex).animate({
					opacity: 0
				}, function() {
					_self.galleryImages.find("li").eq(indexToHide).hide();
				});
				
				_self.currentImageIndex++;
				
				_self.galleryImages.find("li").eq(_self.currentImageIndex).show();
				_self.galleryImages.find("li").eq(_self.currentImageIndex).animate({
					opacity: 1
				});
				
				/*
				$('.gallery .images').animate({
					marginLeft: (-1) * width * (_self.currentImageIndex) + "px"
				});
				*/
			});
			
			this.galleryPrev.click(function() {
				var width = $("#page").width();
				
				_self.galleryImages.clearQueue();
				
				if(_self.currentImageIndex-1 < 0) {
				/*
					_self.galleryImages.animate({
						marginLeft: "48px"
					}, 100, function() {
						$(".gallery .images").animate({
							marginLeft: 0
						}, 500);
					});*/
					
					return false;
				}					
				
				var indexToHide = _self.currentImageIndex;
				_self.galleryImages.find("li").eq(_self.currentImageIndex).animate({
					opacity: 0
				}, function() {
					_self.galleryImages.find("li").eq(indexToHide).hide();
				});
				
				_self.currentImageIndex--;
				
				_self.galleryImages.find("li").eq(_self.currentImageIndex).show();
				_self.galleryImages.find("li").eq(_self.currentImageIndex).animate({
					opacity: 1
				});
				
				/*
				_self.galleryImages.animate({
					marginLeft: (-1) * width * (_self.currentImageIndex) + "px"
				});
				*/
			});
			
			$(window).resize(function() {
				var width = $("#page").width();
				
				/*
				_self.galleryImages.css({
					marginLeft: (-1) * (_self.currentImageIndex) * width + "px"
				});
				*/
			});
			$(window).resize();
		}
	}
	//gallery.init();
});

