(function($, undefined) {
	$.widget( "ui.imageSlider", {
		_init: function() {
            //the width of what is visible
            this.options.availWidth = this.element.children().width();
            
            //count the images
			this.options.itemCount = this.element.find(this.options.sliderItem).length;
			
            //start at position one
            this.options.current = this.element.find(this.options.sliderItem + ':first').addClass('current');
            this.options.position = this.options.current.index();
            
            //set the width of the images
            this.options.itemWidth = this.element.find(this.options.sliderItem + ':first').outerWidth(true);
            
            //determine the number of visible images
            this.options.visibleTotal = Math.floor(this.options.availWidth/this.options.itemWidth);
            
            //cancel init if all items can fit in the parent element or if the slider is already enabled
            if (this.options.itemCount <= this.options.visibleTotal ||  this.options.slider_enabled) {
                return false;
            }
                                    
            //save options and this for use inside functions
            var options = this.options;
            var that = this;
            
            //initialize next/previous links
            this.element.find(options.nextLink).click(function() {
                that.move('forward', 1);
                if (typeof options.nextCallback == 'function') {
					options.nextCallback.call();
				}
                return false;
            }).show();
            
            this.element.find(options.prevLink).click(function() {
                that.move('backward', 1);
                if (typeof options.prevCallback == 'function') {
					options.prevCallback.call();
				}
                return false;
            }).addClass('disabled').show();
            
            this.options.slider_enabled = true;
           
            if (typeof options.initCallback == 'function') {
				options.initCallback.call();
			}
			
            return true;
		},
		
		move: function(direction, steps) {
            var options = this.options;
            var that = this;
            // only move if checkMove decides we should
            if(that._checkMove(direction, steps)) {
	            direction == 'forward' ? options.position += steps : options.position -= steps;
	                
	            var moveDistance = -options.itemWidth * options.position; 
	            
	            options.current.removeClass('current');
	            options.current = this.element.find(options.sliderItem).eq(options.position).addClass('current');
	               	      
	            //animate slider                     
	            this.element.find(options.sliderStrip).animate({
	                marginLeft: moveDistance
	            }, 200, 'linear');
	            
	            //check enable or disable controls based on new position
	            that._checkControls();
	            
	          	if (typeof options.moveCallback == 'function') {
					options.moveCallback.call();
				}
	                        
            }
		},
		
		_checkMove: function(direction, steps) {
			var lastVisibleOffset = this.options.position + steps + this.options.visibleTotal

			//check for last image that will fit in visible space or first image and prevent move	
            if(direction == 'forward' && lastVisibleOffset > this.options.itemCount || direction == 'backward' && this.options.position <= 0) {
                return false;
            }
      
            return true;
		},
		
		_checkControls: function() {
			if(this.options.position + this.options.visibleTotal == this.options.itemCount) {
				this.element.find(this.options.nextLink).addClass('disabled');
			} else {
				this.element.find(this.options.nextLink).removeClass('disabled');
			}
			
			if(this.options.position <= 0) { 
				this.element.find(this.options.prevLink).addClass('disabled');
			} else {
				this.element.find(this.options.prevLink).removeClass('disabled');
			}		
		},
		
		destroy: function() {
			this.element.find(this.options.prevLink + ', ' + this.options.nextLink).removeClass('disabled').hide().unbind('click');
			$.Widget.prototype.destroy.call(this);
		}
		
        
	});
})(jQuery);


