var blockTotal;
var Gallery = {
	popupURL: '',
	page: 1,
	perPage: 3,
	
	init: function() {
		$('#gallery_left_arrow').bind('click', function() {
			Gallery.prevPage();
		});

		$('#gallery_right_arrow').bind('click', function() {
			Gallery.nextPage();
		});
	
		$('#gallery_left_arrow').css({display: 'none'});
		
		if (gallery_images === undefined || gallery_images.length <= this.perPage)
			$('#gallery_right_arrow').css({display: 'none'});
			
		Gallery.show();
	},

	show: function() {
		var thumb_el = $('#gallery_images');
	
		if (gallery_images === undefined || thumb_el === undefined)
			return;
		
		thumb_el.empty();
		
		var self = this;
		var images = [];
		var i = 0;
		var max_height = 0;
		
		jQuery.each(gallery_images, function(index) {
			if (index >= ((self.page - 1) * self.perPage) && index < (self.page * self.perPage))
				images[i++] = gallery_images[index];
		});
		
		for (index in images) {
			if (images[index].height > max_height)
				max_height = images[index].height;
			
			thumb_el.append('<a href="'+ images[index].full +'" class="lightbox" rel="lightbox[gallery]" title="'+ images[index].title +'" target="_blank"><img src="'+ images[index].thumb +'" class="gallery" /></a>');
		}
		
		if (max_height == 0)
			max_height = 100;
		
		$('#gallery_left_arrow').css({ 'line-height': max_height +'px' });
		$('#gallery_right_arrow').css({ 'line-height': max_height +'px' });
		$(".lightbox").lightbox({fitToScreen: true});
		blockTotal = this.perPage;
	},
	
	prevPage: function() {
		if (this.page <= 1)
			return;
		
		this.page--;
		
		if (this.page <= 1)
			$('#gallery_left_arrow').css({display: 'none'});
		
		if ((this.page + 1 * this.perPage) <= gallery_images.length)
			$('#gallery_right_arrow').css({display: ''});
		
		this.show();
	},
	
	nextPage: function() {
		if ((this.page + 1 * this.perPage) > gallery_images.length)
			return;
	
		this.page++;
		
		if (this.page > 1)
			$('#gallery_left_arrow').css({display: ''});
		
		if ((this.page * this.perPage) >= gallery_images.length)
			$('#gallery_right_arrow').css({display: 'none'});
		
		this.show();
	}
};
