  ;(function($){

	$.widget("ui.leloSlider", {
		init: function() {
			
			var self = this;
			
			this.items = $(this.options.items, this.element);			
			this.itemWidth = this.items.outerWidth(true);
			this.itemHeight = this.items.outerHeight(true);
			this.current = this.options.currentItem; //item to show at start
			this.left = this.options.currentLeft;
			
			this.currentElement = $(this.items).eq(this.current);
			
			this.glideTo(this.current,this.left);
		},
		moveTo: function(item) {
			
			this.previous = this.current;
			this.current = !isNaN(parseInt(item)) ? parseInt(item) : this.items.index(item);
			var self = this, to = Math.abs(self.previous-self.current) <=1 ? self.previous : self.current+(self.previous < self.current ? -1 : 1);
			
			if(this.previous == this.current) {
				return false;
			}
			
			var left = (-this.current * this.itemWidth);
			this.element.animate({left: (left)}, {duration: 600});
		},
		glideTo: function(item,left) {
			this.previous = this.current;
			this.current = !isNaN(parseInt(item)) ? parseInt(item) : this.items.index(item);			
			
			this.element.css({left: (left)});
			$(this.items).eq(this.current).css('opacity',0.5);
			
		},
		clickTo: function(direction) {
		
				switch (direction) {
					case 'left':
					    if(this.current >0) {
							this.moveTo(this.current -1);
							this.currentElement = $(this.items).eq(this.current);
                        }
						break;
			 		case 'right':
						if(this.current < (this.items.length -1)) {
							this.moveTo(this.current + 1);
							this.currentElement = $(this.items).eq(this.current);
                        }
						break;
				}
			
		}
	});
	
	$.extend($.ui.leloSlider, {
		defaults: {
			items: "> *",
			currentItem:0,
			currentLeft:"0px"
		}
	});
	
})(jQuery); 
