(function($) {
	$.fn.JScroller = function(options) {
		
		var opts = $.extend({}, $.fn.JScroller.defaults, options);
		
		var ImageCount = 0;
		var LoadedCount = 0;
		
		var ScrollerObj = $(this);
		
		var ScrollingObj = null;
		
		var ContainerWidth = 0;
		
		var bRunning = false;
		
	  this.each(function() {
	  	$(this).find('img').each(function(){
	  		ImageCount++;
	  		if($(this)[0].clientWidth > 15) {
	  			LoadedCount++;
	  		} else {
	  			$(this).load(ImageLoaded);
	  		}
	    });
	  });
	 	if(LoadedCount >= ImageCount) {
     	InitScroll();
    }
	  
	  function ImageLoaded() {
      LoadedCount++;
      if(LoadedCount >= ImageCount) {
      	InitScroll();
      }
    }
    
    function InitScroll()
    {
    	if(bRunning) {
    		return;
    	}
    	bRunning = true;
    	
    	var UlWidth = 0;
   		$('li', ScrollerObj).each(function(){
      	UlWidth += parseInt($(this).width());
        UlWidth += parseInt($(this).css('margin-right'));
    	});
    	$('ul:first', ScrollerObj).css('width',UlWidth + 'px');
    	    	
    	ContainerWidth = UlWidth;
      
      if(UlWidth < ScrollerObj.width()) {
      	$(opts.LeftButton).remove();
      	$(opts.RightButton).remove();
      	return;
      }
    	
    	var PriObj = $("ul:first", ScrollerObj);
	    PriObj.css("float", "left"); 
	    PriObj.css("height", ScrollerObj.height()); 
	    var ComWidth = $(PriObj).width();
	    var SecObj = $(PriObj).clone();
	    
	    var SubContainer = document.createElement('div');
	    PriObj.appendTo(SubContainer);
	    SecObj.appendTo(SubContainer);
	    ScrollerObj.append(SubContainer);
	    ScrollerObj.css('position','relative');
	    $(SubContainer).css('width',(UlWidth*2) + 'px');
	    $(SubContainer).css('position','absolute');
	    $(SubContainer).addClass('ul_container');
	    
	    ScrollingObj = $(SubContainer);
	    ScrollingObj.hover(Pauze, Resume);
	    $(opts.LeftButton).hover(BtnLeft, Resume);
	    $(opts.RightButton).hover(BtnRight, Resume);
	    StartScrollingToLeft();
    }
    
    function StartScrollingToLeft()
    {
    	ScrollingObj.css('left', '0px');
    	ScrollingObj.animate({left : (ContainerWidth * -1) + "px"},
    										   ContainerWidth * opts.ScrollSpeed,
    										   'linear',
    										   function() {
    										   		StartScrollingToLeft();
    										  });	
    }
    
    function BtnLeft()
    {
    	ScrollingObj.stop();
      var Remaining = ContainerWidth - Math.abs(parseInt(ScrollingObj.css('left'))); 
      ScrollingObj.animate({left : (ContainerWidth * -1) + "px"},
    										   Remaining * opts.ButtonSpeed,
    										   'linear',
    										   function() {
    										   		BtnDoLeft();
    										  });		
    }
    
    function BtnDoLeft()
    {
    	ScrollingObj.css('left', '0px');
    	ScrollingObj.animate({left : (ContainerWidth * -1) + "px"},
									   ContainerWidth * opts.ButtonSpeed,
									   'linear',
									   function() {
									   		BtnDoLeft();
									  });		

    }
     
    function BtnRight()
    {
    	ScrollingObj.stop();
    	var Remaining = ContainerWidth - (ContainerWidth - Math.abs(parseInt(ScrollingObj.css('left')))); 
      ScrollingObj.animate({left : 0 + "px"},
    										   Remaining * opts.ButtonSpeed,
    										   'linear',
    										   function() {
    										   		BtnDoRight();
    										  });		
    }
    
    function BtnDoRight()
    {
    	ScrollingObj.css('left', (ContainerWidth * -1) + 'px');
    	ScrollingObj.animate({left : 0 + "px"},
									   ContainerWidth * opts.ButtonSpeed,
									   'linear',
									   function() {
									   		BtnDoRight();
									  });		
    }
    
    function Resume()
    {
    	ScrollingObj.stop();	
    	var Remaining = ContainerWidth - Math.abs(parseInt(ScrollingObj.css('left'))); 
    	ScrollingObj.animate({left : (ContainerWidth * -1) + "px"},
    										   Remaining * opts.ScrollSpeed,
    										   'linear',
    										   function() {
    										   		StartScrollingToLeft();
    										  });		
    }    
    function Pauze()
    {
    	ScrollingObj.stop();	
    }

	}
	$.fn.JScroller.defaults = {
	  ScrollSpeed: 15,
	  ButtonSpeed: 5,
	  LeftButton: '#left_scroller_button',
	  RightButton: '#right_scroller_button'
	}
})(jQuery);	
