(function($){	

	$.imbslide = {
		node: function(id,duration,easing,visibleitem,itemwidth,maxitem,maxpos){
			this.id = id; // the id of the slide element
			this.status = false; // if sliding is in motion
			this.duration = duration; // how long the animation lasts
			this.easing = easing; // the characteristics of the animation
			this.visibleitem = visibleitem; // how many thumbs are visible
			this.itemwidth = itemwidth; // the width of the thumb
			this.maxitem = maxitem; // total number of thumbs
			this.currentpos = 1; // start at one
			this.maxpos = maxpos; // max number of times it can slide
		},
		slide: function(id,direction){ 		
			var n = $.imbslide[id];
			var newpos = n.currentpos + direction;
			if(!n.status & newpos > 0 & newpos <= n.maxpos){
				n.status = true;
				n.currentpos = newpos; // save the new position to memory
				var obj = $("#" + id + " ul");
				var currentx = parseFloat(obj.css("left")); // get the current position
				var newx = currentx - (direction * n.visibleitem * n.itemwidth); // calculate the new position
				obj.animate({left:newx},n.duration,n.easing,function(){ // move it
					n.status = false;
					// fire of the completed function
					$.imbslide.onchange();
				});
			}				
					
		},
		onchange: function(){
			// do something	after the animation stops
		}
	}

	$.fn.imbslide = function(options){
		var settings = $.extend({
		duration: 400,
		easing: "easeOutQuad"
		},options||{});	
		
		return this.each(function() {
			
			var obj = $(this);
			var id = obj.attr('id');
			var n = $.imbslide[id];	
			var maxitem = obj.find(".imbslide-slide li").length;
			var itemwidth = obj.find(".imbslide-slide li").outerWidth();
			var visibleitem = Math.floor(obj.find(".imbslide-slide").outerWidth() / itemwidth);
			var maxpos = Math.ceil(maxitem / visibleitem);
			
			// add this instance to the imbslide object
			$.imbslide[id] = new $.imbslide.node(id,settings.duration,settings.easing,visibleitem,itemwidth,maxitem,maxpos);
			
			obj.find(".imbslide-prev").bind("click", function() {
				$.imbslide.slide(id,-1);				      
			});
			obj.find(".imbslide-next").bind("click", function() {
				$.imbslide.slide(id,1);				      
			});
			
		});		
	}
	
	

})(jQuery);