var Gallery = {
	
	galleryId: false, 

	prevId: false, 

	nextId: false,
		
	count: 0,
	
	current: 1,
	
	previous: 0,
	
	imgs: false,
	
	animates: false,
	
	width: 0,
	
	shown: 1,
	
	create: function(galleryId, prevId, nextId, width, shown, current)
	{
		this.galleryId=galleryId; 
		this.prevId=prevId; 
		this.nextId=nextId;
		this.width=width;
		this.shown=shown;
		
		this.imgs=$("#"+this.galleryId+" .thumb");
		
		this.count=this.imgs.length;
		
	    if (typeof current != 'undefined') 
	    {
	    	this.current=current-1;
	    	if (this.current>this.count-3)
	    		this.current=1;
	    	else if (this.current<1)
	    		this.current=1;
	    }
	    
	    $("#"+this.prevId).click(function(){
	    	Gallery.prev();
	    });
	    
	    $("#"+this.nextId).click(function(){
	    	Gallery.next();
	    });
	    
	    this.setup();
	    
	    this.show();
	},

	setup: function()
	{
		h=$("#"+this.galleryId).html();
		$("#"+this.galleryId).html('<div id="gallery-container">'+h+'</div>').css('overflow', 'hidden');
		$("#gallery-container").css('width', this.count * this.width );
		$("#"+this.galleryId+" .thumb").css('float', 'left');
		$("#gallery-container").css({'margin-left': (-(this.current * this.width))});

		if (this.current==(this.count-this.shown))
			$("#"+this.nextId).css('cursor', 'default').addClass('disabled');
		if (this.current==1)
			$("#"+this.prevId).css('cursor', 'default').addClass('disabled');

		w=$("#"+this.galleryId).width();
		$("#"+this.galleryId).width(this.width*this.shown);
		$("#content").width(this.width*this.shown);
	},

	next: function()
	{
		if (!this.animates)
		{
			if ((this.current+1)<(this.count-this.shown+1))
			{
				this.animates=true;
				this.previous=this.current;
				this.current++;
				this.show();
				$("#"+this.nextId).css('cursor', 'pointer').removeClass('disabled');
			}
		}

		if (this.current==(this.count-this.shown))
			$("#"+this.nextId).css('cursor', 'default').addClass('disabled');
	},

	prev: function()
	{
		if (!this.animates)
		{
			if ((this.current-1)>=1)
			{
				this.animates=true;
				this.previous=this.current;
				this.current--;
				this.show();
				$("#"+this.prevId).css('cursor', 'pointer').removeClass('disabled');
			}
		}
		
		if (this.current==1)
			$("#"+this.prevId).css('cursor', 'default').addClass('disabled');
	},
	
	show: function()
	{
		$("#gallery-container").animate({'marginLeft': (-(this.current * this.width))}, "def", "swing", function(){
    		Gallery.animates=false;
    	});
	}
		
};

$(window).ready(function(){
	Gallery.create('gallery', 'prev', 'next', 400, 3, 3);	
});
