/** jquery.fastscrollbar
  @author Christian Gabmeyer 200810

  @parameters
   -name der styleclasse
   -typ vertikal horizontal beides


  @css put in your stylefile, numbers are in sourcecode order first is 0
   scrollbar für content
   .CONTENT .scrollbar {
    position: absolute;
    right:15px;
    top:30px;
    width:8px;
    height:317px;
    z-index:2;
    background-color: #e7873b;
    background-color: red;
    background-image:url('img/scroller_bg.gif');
    background-repeat:no-repeat;
    background-color: white;
   }
   .CONTENT .scrollbar_aktiv .inner {
    padding:10px 35px 10px 10px;
   }
*/
/* override for subblocks
   .CONTENT .scrollbar_aktiv .inner * .inner {
    padding:0;
   }

*/
(function($){
 $.fn.fastscrollbar = function(settings)
 {
  var scrollhandler;
  var defaults = {
    //btn scrollspeed
    interval:200
    // how height is one line in px
    ,lineheigh:20
    //scrollbar or scrollbtn (name is uses as classname)
    ,handletyp:'scrollbar'
  };
  var settings = $.extend(defaults, settings);

  // for each and every element
  return this.each(function(i)
  {
   var obj = $(this);
   var block=this;
   /** eventhandler
   *
   */
   var evhandler={
     interval:settings.interval
    ,intervalup:null
    ,intervaldo:null
    ,status:''
    ,up:function(e)
    {
     status=e.type;
     if(status=='mousedown')
     {
      window.setTimeout(evhandler.btnup,evhandler.interval);
     }
    }
    ,down:function(e)
    {
     status=e.type;
     if(status=='mousedown')
     {
      window.setTimeout(evhandler.btndown,evhandler.interval);
     }
    }
    ,btnup:function()
    {
     if(block.scrollTop>settings.lineheigh)
      block.scrollTop-=settings.lineheigh;
     else
      block.scrollTop=0;
     if(status=='mousedown')window.setTimeout(evhandler.btnup,evhandler.interval);
    }
    ,btndown:function()
    {
     if(block.scrollTop<textheight-settings.lineheigh)
      block.scrollTop+=settings.lineheigh;
     else
      block.scrollTop=textheight-settings.lineheigh;
     if(status=='mousedown')window.setTimeout(evhandler.btndown,evhandler.interval);
    }
   }

   if(obj.css('overflow')=='auto')
   {
    var sliderlast=0;
    // set paddings here to match the scrollbar right distance (else scrollbar is above the text)
    obj.addClass('scrollbar_aktiv');
    //
    var objheight=obj.height();
    // expand to get textheight
    //obj.css(cssc('overflow:visible;height:auto'));
    obj.css(cssc('overflow:hidden;height:auto'));
    var textheight=obj.height();
    // scroll
    obj.css(cssc('overflow:auto;position:relative')).height(objheight);
    // choose uityp
    if(settings.handletyp=='scrollbar')
    {
     // the new scrollbar
     scrollhandler=$('<div class="scrollbar"><div class="ui-slider-handle"></div></div>');
     scrollhandler.css(cssc('cursor:pointer;position:absolute;right:0;top:0;z-index:10')).height(objheight);
    }
    else
    {
     scrollhandler=$('<div class="'+settings.handletyp+'"><div class="up"></div><div class="down"></div></div>');
     scrollhandler.css(cssc('cursor:pointer;position:absolute;right:0;top:0;z-index:100000')).height(objheight);
    }
    // put scrollhandler into site, position is given with css
    obj.before(scrollhandler);
    // sliderheight
    var sliderheight=scrollhandler.height();
    //
    var remainder = textheight - objheight;
    var proportion = remainder / textheight;
    var handleheight = Math.round(sliderheight - (proportion * sliderheight));
    //
    if(textheight<=objheight)
    {
     scrollhandler.hide();
    }
    else
    {
     if(settings.handletyp=='scrollbar')
     {
      // start scrollhandler
      scrollhandler.myslider({scrollbar:true,value:0,min:0,max:textheight,orientation:'vertical', slide: function(e,ui) {if(block.scrollTop>=remainder+settings.lineheigh && ui.value>=block.scrollTop+settings.lineheigh){ return false;}  block.scrollTop=ui.value;}});
      // autoslider height
      $('.ui-slider-handle',scrollhandler).height(handleheight).width(scrollhandler.width()).css(cssc('left:0;cursor:pointer;z-index:11'));
     }
     else
     {
    //deb('2textheight='+textheight+' objheight='+objheight+' remainder='+remainder);
      $(scrollhandler).find('.up')
       .bind('mousedown mouseup mouseleave',evhandler.up)
       .hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')});
      $(scrollhandler).find('.down')
       .bind('mousedown mouseup mouseleave',evhandler.down)
       .hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')});
     }
    }
    /* always add these handlers */
    // add mousehandler
    obj.mousewheel(mwscrollmehandler);
    // hide system scrollbar
    obj.css({'overflow':'hidden'});

//    deb('textheight='+textheight+' objheight='+objheight+' remainder='+remainder);
   }

	 });

  /** mwscrollmehandler
  * scrolls the element but prevents default parent scroll on scrollend
  *
  */
  function mwscrollmehandler(e,delta)
  {
   var detail=(e.detail<0)?e.detail*-1:e.detail;
   if(!detail)detail=1;
   var scrolldistance=detail * settings.lineheigh;
   if(delta <= 0)
    this.scrollTop+=scrolldistance;
   else
    if(this.scrollTop-scrolldistance>=0)
     this.scrollTop-=scrolldistance;
    else
     this.scrollTop=0;
   scrollhandler.myslider('value',this.scrollTop);
   return false;
  }

	};
})(jQuery);

