(function($){  
	  
    $.fn.extend({   
          
        //pass the options variable to the function  
        slider: function(options) {  
  
            //Set the default values, use comma to separate the settings, example:  
            var defaults = {  
                leftbutton: '#move_left',
                rightbutton: '#move_right',
                autoanimate: false,
                autoanimateinterval: 10, //seconds
                //start: 'left', //left, centre, right -- NOT IMPLEMENTED YET.
                easing: 'swing'
            }; 
                  
            var options =  $.extend(defaults, options);  
  
            return this.each(function() {  
                var o = options;  

                var slider = $(this);
                var scrollarea = slider.find("> .scrollarea");
                var sliderlist = scrollarea.find('> ul');
                var items = sliderlist.find('> li');
                var singleWidth = items.filter(':first').outerWidth();
                var currentItem = 1;
                
                sliderlist.width(singleWidth*items.length);

				// hide buttons if not needed
				if(singleWidth*items.length <= scrollarea.width()) {
					$(o.rightbutton).hide(); 
					$(o.leftbutton).hide(); 
				}

				$(o.rightbutton).click(function() {
	                return next();
                });
                $(o.rightbutton).css('cursor', 'pointer');

                $(o.leftbutton).click(function() {
                	return previous();
                });
                $(o.leftbutton).css('cursor', 'pointer');

                scrollarea.css('overflow', 'hidden');
                scrollarea.css('position', 'relative'); //needed for IE

                if(o.autoanimate) {
    				scrollarea.everyTime('2s', 'sliderautoanimate', function(){
    					if(scrollarea.data('animates') == 'true') return;
    					scrollarea.data('autoanimatetimer', scrollarea.data('autoanimatetimer')+2)
    					if(scrollarea.data('autoanimatetimer') >= 10) {
    						scrollarea.data('autoanimatetimer', 0)
    						if(scrollarea.data('animates') != 'true') {
    							if(scrollarea.scrollLeft() < (singleWidth*(items.length/2)-scrollarea.width()/2)) {
    								scrollarea.animate({scrollLeft: singleWidth*(items.length)-scrollarea.width()}, items.length*5000, 'linear', function(){scrollarea.data('animates', 'false');});
    								scrollarea.data('animates', 'true');
    							} else {
    								scrollarea.animate({scrollLeft: 1}, items.length*5000, 'linear', function(){scrollarea.data('animates', 'false');});
    								scrollarea.data('animates', 'true');
    							}
    						}
    					}
    				});
    				scrollarea.data('animates', 'true');
    				scrollarea.animate({scrollLeft: singleWidth*(items.length)-scrollarea.width()}, items.length*5000, 'linear', function(){
    					scrollarea.data('animates', 'false');
    				});
    				scrollarea.click(function(){
    					scrollarea.stop();
    					scrollarea.data('autoanimatetimer', 0);
    					scrollarea.data('animates', 'false');
    				});
                }
                
                function next() {
                	if(o.autoanimate) {
	                	scrollarea.stop();
	                	scrollarea.animate({scrollLeft : '+=' + singleWidth}, 500, o.easing, function(){scrollarea.data('animates', 'false');});
	                	scrollarea.data('autoanimatetimer', 0);
	                	scrollarea.data('animates', 'true');
                	} else {
                		currentItem = items.length <= currentItem+1 ? items.length-1 : currentItem+1 ;
						return slideTo(currentItem);
					}
                }
                
                function previous() {
                	if(o.autoanimate) {
	                	scrollarea.stop();
	                	scrollarea.animate({scrollLeft : '-=' + singleWidth}, 500, o.easing, function(){scrollarea.data('animates', 'false');});
	                	scrollarea.data('autoanimatetimer', 0);
	                	scrollarea.data('animates', 'true');
                	} else {
                		currentItem = currentItem-1 < 0 ? 0 : currentItem-1;
						return slideTo(currentItem);
                	}
                }
                
                //private function to slide to specific element
                // elno is 0 based
                function slideTo(elno) {
                	elno = elno == -1 ? currentItem : elno;
                	elno = elno < 0 ? 0 : elno;
                	elno = items.length <= elno ? items.length-1 : elno;
                	
                	var remainder = elno - parseInt(elno);
                	elno = parseInt(elno);
                	
                	var itempos = $(items[elno]).position().left+scrollarea.scrollLeft();
                	scrollarea.stop();
                	scrollarea.animate({scrollLeft : itempos-scrollarea.width()/2+$(items[elno]).outerWidth()/2+$(items[elno]).outerWidth()*remainder}, 500, o.easing, function(){scrollarea.data('animates', 'false');});
                	scrollarea.data('autoanimatetimer', 0);
                	scrollarea.data('animates', 'true');
                	currentItem = elno;
                }
                
                // create a public interface to slidee to a specific element
                // elno is 0 based.
                $(this).bind('slideTo', function (event, elno) {
                    slideTo(elno);
                });
                $(this).bind('next', function (event, elno) {
                    next();
                });
                $(this).bind('previous', function (event, elno) {
                    previous();
                });
                
            }); 
            
        }
    });  
      
})(jQuery);
