/* jpcImageScroller - jQuery IPC Image Scroller */
/* Copyright (c) by IPC Webdesign www.ipc.at */

(function($) {
	$.fn.jpcImageScroller = function(options) {
    var $this = $(this);
    var speed = '-=2';
    var direction = '-';
    
    var opts = $.extend({},$.fn.jpcImageScroller.defaults,options);
    
    $this.css({width:'2000px'});
    $this.children().css({float:'left'});
    $this.wrap('<div id="jpcImageScrollerWrap" style="position:relative;overflow:hidden;width:'+opts.width+'px;height:'+opts.height+'px;"></div>');
    
    $('#jpcImageScrollerWrap').append('<div id="jpcImageScrollerControl" style=""></div>');
    
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerDown4" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerDown3" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerDown2" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerDown1" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerNone" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerUp1" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerUp2" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerUp3" style="height:'+opts.height+'px;"></div>');
    $('#jpcImageScrollerControl').append('<div id="jpcImageScrollerUp4" style="height:'+opts.height+'px;"></div>');
    
    $('#jpcImageScrollerDown4').mouseenter(function() { speed='+=8'; direction='+'; });
    $('#jpcImageScrollerDown3').mouseenter(function() { speed='+=4'; direction='+'; });
    $('#jpcImageScrollerDown2').mouseenter(function() { speed='+=2'; direction='+'; });
    $('#jpcImageScrollerDown1').mouseenter(function() { speed='+=1'; direction='+'; });
    $('#jpcImageScrollerNone').mouseenter(function() { speed='-=0'; direction=''; });
    $('#jpcImageScrollerUp1').mouseenter(function() { speed='-=1'; direction='-'; });
    $('#jpcImageScrollerUp2').mouseenter(function() { speed='-=2'; direction='-'; });
    $('#jpcImageScrollerUp3').mouseenter(function() { speed='-=4'; direction='-'; });
    $('#jpcImageScrollerUp4').mouseenter(function() { speed='-=8'; direction='-'; });
    
    $('#jpcImageScrollerWrap').mouseleave(function() { speed='-=2'; direction='-'; });
    
    startScroll();
    function startScroll() {
      scrollPosition = parseInt($this.css('margin-left'));
      firstWidth = $this.find('img:first-child').width()+1;
      lastWidth = $this.find('img:last-child').width()+1;
      if(direction == '-') {
        if(Math.abs(scrollPosition) > firstWidth) {
          $this.css('margin-left',(scrollPosition+firstWidth)+'px');
          $this.find('img:first-child').appendTo($this);
        }
      } else if(direction == '+') {
        if(scrollPosition > 0) {
          $this.css('margin-left',(scrollPosition-lastWidth)+'px');
          $this.find('img:last-child').prependTo($this);
        }
      }
      $this.animate({'margin-left':speed},40,'linear',function(){startScroll();});
    };
  }
  
  $.fn.jpcImageScroller.defaults = {width:450,height:50};
})(jQuery);