var GalleryList = {
	initHovers : function() {
		$('.gallery-thumb a').hover(function(){
			var d_img = $(this).closest('div.gallery-thumb').find('img.default');
			d_img.stop(true,true).animate({opacity:0}, 300);
		}, function(){
			var d_img = $(this).closest('div.gallery-thumb').find('img.default');
			d_img.stop(true,true).animate({opacity:1}, 300);
		});
	},
	load:function(hovers) {
		var $this = this;
		this.items = hovers;	
		
		this.itemcount = this.items.length;
		this.itemindex = 0;
		
		for (var i=0; i<this.items.length;i++) {
			var item = this.items[i];
			
			if (typeof(item) != 'undefined') {
				var hvr = new Image();
				hvr.id = item.id;
				hvr.onload = function() {
					var img = $('<img />',{'class':'hover'});
					$('#gallery-item-' + this.id + ' .gallery-thumb a:first').append(img);
					img.attr('src', this.src);
				}
				hvr.src = item.hover;
			}
		}
	},
	open : function(type, page) {
		GalleryUrlData.display = type;
		GalleryUrlData.page = page;
		
		$.ajax({
			url:'?',
			data:GalleryUrlData,
			method:'GET',
			dataType : 'html',
			cache : false,
			beforeSend : function() {
				var w = $('#list-'+type);
				w.append($('<div/>',{'class':'loading',height:w.outerHeight()}));			
			},
			success : function(html) {
				$('#list-'+type).html(html);
				GalleryList.initHovers();
			}
		});
	}
}

var MiniGallery = {
     inPage : 4,
     itemIndex : 0,
     load : function() {
    	 var $this = this;
    	 this.itemCount = $('#gallery-items li').length;
    	 $('#gallery-items a.prev').css('display', 'none');
		 $('#gallery-items a.next').css('display', this.itemCount > this.inPage ? 'block':'none');
		
		$('#gallery-items a').click(function(){
			var dir = $(this).hasClass('next') ? 'next':'prev';
			var idx_c = dir == 'next' ? 1 : -1;
			
			var can_prev = $this.itemIndex > 0;
			var can_next = $this.itemIndex + $this.inPage < $this.itemCount;
			
			if ((dir == 'prev' && can_prev) || (dir == 'next' && can_next)) {
				var a_dir = dir == 'next' ? '-=233px':'+=233px';
				$('#gallery-thumbnails ul').stop(true,true).animate({left:a_dir}, 200);
				$this.itemIndex += idx_c;
				
				var can_prev = $this.itemIndex > 0;
				var can_next = $this.itemIndex + $this.inPage < $this.itemCount;
			
				$('#gallery-items a.prev').css('display', can_prev ? 'block':'none');
				$('#gallery-items a.next').css('display', can_next ? 'block':'none');
			}
		});
	 } 
 };
