Gallery = {

	wrappers : {},
	actualWrapper : null,	
	
	init: function(items) {

		Gallery.imageWrapperWidth = 610;
		
		items.each( function(index, item)
		{
		 	id = $(item).attr('id');

			Gallery.wrappers[id] = {
				id           : id,
				imageHolder  : $('.image-holder', item),
				currentImage : 0,
				maxCount     : $(".image-holder > img", item).length,
				nextButton   : $(".scroll-right", item),
				prevButoon   : $(".scroll-left", item)
			};
			
			$(".scroll-right", item).click(function() {
			
				var id = $(this).parents('.post_gallery').attr('id');
				Gallery.actualWrapper = Gallery.wrappers[id];
			
				Gallery.next();
				this.blur();
				return false;
			});
			
			$(".image-holder > img", item).click(function() {
			
				var id = $(this).parents('.post_gallery').attr('id');
				Gallery.actualWrapper = Gallery.wrappers[id];
			
				Gallery.next();
				this.blur();
				return false;
			});
			
			$(".scroll-left", item).click(function() {

				var id = $(this).parents('.post_gallery').attr('id');		
				Gallery.actualWrapper = Gallery.wrappers[id];
	
				Gallery.previous();
				this.blur();
				return false;
			});
		});
	},
	
	next: function() {
	
		this.gotoImage(Gallery.actualWrapper.currentImage + 1);
	},

	previous: function() {

		this.gotoImage(Gallery.actualWrapper.currentImage - 1);
	},

	gotoImage: function(num) {
	
		if(num >= Gallery.actualWrapper.maxCount) {
			num = 0;
		} else if(num < 0) {
			num = Gallery.actualWrapper.maxCount - 1;
		}

		this.animateContainers(num);

		Gallery.actualWrapper.currentImage = num;		
		
		Gallery.wrappers[ Gallery.actualWrapper.id] = Gallery.actualWrapper;
	},
	
	animateContainers: function(num) {

		$(Gallery.actualWrapper.imageHolder).animate({
			marginLeft: (num * this.imageWrapperWidth) * -1 + "px"
		}, { duration: 600, queue: false });		
	}
}
