(function(a){a.fn.hoverscroll=function(b){if(!b)b={};b=a.extend({},a.fn.hoverscroll.params,b);this.each(function(){var f=a(this);b.debug&&a.log("[HoverScroll] Trying to create hoverscroll on element "+this.tagName+"#"+this.id);f.wrap('<div class="listcontainer"></div>');f.addClass("list");var e=f.parent();e.wrap('<div class="ui-widget-content hoverscroll"></div>');var c=e.parent();if(b.arrows)if(!b.vertical)e.append('<div class="arrowLeft"><div class="arrow left"></div></div>').append('<div class="arrowRight"><div class="arrow right"></div></div>');else e.append('<div class="arrowLeft"><div class="arrow left"></div></div>').append('<div class="arrowRight"><div class="arrow right"></div></div>');c.width(b.width).height(b.height);e.width(b.width).height(b.height);var d=0;if(!b.vertical){c.addClass("horizontal");f.children().each(function(){a(this).addClass("item");if(a(this).outerWidth)d+=a(this).outerWidth(true);else d+=a(this).width()+parseInt(a(this).css("padding-left"))+parseInt(a(this).css("padding-right"))+parseInt(a(this).css("margin-left"))+parseInt(a(this).css("margin-right"))});f.width(d);b.debug&&a.log("[HoverScroll] Computed content width : "+d+"px");if(c.outerWidth)d=c.outerWidth();else d=c.width()+parseInt(c.css("padding-left"))+parseInt(c.css("padding-right"))+parseInt(c.css("margin-left"))+parseInt(c.css("margin-right"));b.debug&&a.log("[HoverScroll] Computed container width : "+d+"px")}else{c.addClass("vertical");f.children().each(function(){a(this).addClass("item");if(a(this).outerHeight)d+=a(this).outerHeight(true);else d+=a(this).height()+parseInt(a(this).css("padding-top"))+parseInt(a(this).css("padding-bottom"))+parseInt(a(this).css("margin-bottom"))+parseInt(a(this).css("margin-bottom"))});f.height(d);b.debug&&a.log("[HoverScroll] Computed content height : "+d+"px");if(c.outerHeight)d=c.outerHeight();else d=c.height()+parseInt(c.css("padding-top"))+parseInt(c.css("padding-bottom"))+parseInt(c.css("margin-top"))+parseInt(c.css("margin-bottom"));b.debug&&a.log("[HoverScroll] Computed container height : "+d+"px")}var g={1:{action:"move",from:0,to:.02*d,direction:-1,speed:25},2:{action:"stop",from:.2*d,to:.97*d},3:{action:"move",from:.97*d,to:d,direction:1,speed:25}};c[0].isChanging=false;c[0].direction=0;c[0].speed=1;function m(d,e){d=d-c.offset().left;e=e-c.offset().top;var a;if(!b.vertical)a=d;else a=e;for(i in g)if(a>=g[i].from&&a<g[i].to)if(g[i].action=="move")l(g[i].direction,g[i].speed);else h()}function j(){if(!b.arrows)return;var g,h;if(!b.vertical){g=e[0].scrollWidth-e.width();h=e[0].scrollLeft}else{g=e[0].scrollHeight-e.height();h=e[0].scrollTop}var d=h/g,f=b.arrowsOpacity;if(isNaN(d))d=0;var i=false;if(d<=0){a("div.arrow.left, div.arrow.top",c).hide();i=true}if(d>=f||g<=0){a("div.arrow.right, div.arrow.bottom",c).hide();i=true}if(!i){a("div.arrow.left, div.arrow.top",c).show().css("opacity",d>f?f:d);a("div.arrow.right, div.arrow.bottom",c).show().css("opacity",1-d>f?f:1-d)}}function l(e,d){if(c[0].direction!=e){b.debug&&a.log("[HoverScroll] Starting to move. direction: "+e+", speed: "+d);h();c[0].direction=e;c[0].isChanging=true;k()}if(c[0].speed!=d){b.debug&&a.log("[HoverScroll] Changed speed: "+d);c[0].speed=d}}function h(){if(c[0].isChanging){b.debug&&a.log("[HoverScroll] Stoped moving");c[0].isChanging=false;c[0].direction=0;c[0].speed=1;clearTimeout(c[0].timer)}}function k(){if(c[0].isChanging==false)return;j();var a;if(!b.vertical)a="scrollLeft";else a="scrollTop";e[0][a]+=c[0].direction*c[0].speed;c[0].timer=setTimeout(function(){k()},50)}c.mousemove(function(a){m(a.pageX,a.pageY)}).bind("mouseleave",function(){h()});if(b.arrows)j();else a(".arrowleft, .arrowright, .arrowtop, .arrowbottom",c).hide()});return this};if(!a.fn.offset)a.fn.offset=function(){this.left=this.top=0;if(this[0]&&this[0].offsetParent){var a=this[0];do{this.left+=a.offsetLeft;this.top+=a.offsetTop}while(a=a.offsetParent)}return this};a.fn.hoverscroll.params={vertical:false,width:400,height:50,arrows:true,arrowsOpacity:1,debug:false};a.log=function(a){console&&console.log&&console.log(a)}})(jQuery)
