/**
* Title: jQuery UI Vertical Scrollbar
* Adds a vertical scrollbar to DOM object using jQuery UI
* 
* Usage:
*	$('.elemeinlineStyleForWrappernt').scrollbar();
*	$('.element').scrollbar("destroy");
*	$('.element').scrollbar("refresh");
*	$('.element').scrollbar("create",{location:"right"});
* 
* Dependencies: jquery.ui, jquery.mousewheel
* License:
* 	Copyright (c) 2011 Patrick O'Toole
*	Licensed Under the MIT/X-11 License 
* 	www.rickotoole.com
*
* Version: 0.0.2
**/
(function($){$.fn.scrollbar=function(m,n){var o={scrollbar:$.extend({},n,$.fn.scrollbar.defaults),scrollDifference:0,setContentHeight:function(a,b){var c;b=b?b:jQuery('body').children().first();jQuery(b).after(a);c=jQuery(a).outerHeight();jQuery(a).remove();this.contentHeight=c;return c},originalStyle:{}};var p={init:function(a,b){this.clicks(a,b);this.mousewheel(a,b);this.touch(b)},clicks:function(b,c){var d=(function(a){return a.scrollDifference})(o);$.fn.scrollbar.click("up",c,b,d);$.fn.scrollbar.click("down",c,b,d);$.fn.scrollbar.click("top",c,b,d);$.fn.scrollbar.click("bottom",c,b,d)},mousewheel:function(b,c){var d=(function(a){return a.scrollDifference})(o);var e=b>10?b/10:1;$.fn.scrollbar.wheel(c,e,d)},touch:function(b){var c=(function(a){return a.scrollDifference})(o);$.fn.scrollbar.touch(b,c)}};var q={create:function(){return this.each(function(a){o=$.extend(true,o,jQuery(this).data("scroll-options"));var c=jQuery(this),w=c.outerWidth(),h=c.outerHeight(),cl=c.attr("class"),id=c.attr("id")||"scrollable_content_"+cl+"_"+a;o.wrapperHeight=h;o.wrapperWidth=w;var b=WrapContent(this);if(o.scrollDifference>0){var d=CreateSlider(id);var e=jQuery("<div></div>").attr("class",cl).attr("id",id);!o.scrollbar.inlineStyleForWrapper?null:(o.originalStyle!=undefined)?e.attr("style",o.originalStyle):null;e.css({'position':'relative','overflow':'hidden','width':w,'height':h});e.append(d).append(b);c.after(e);c.remove();SliderToScrollbar(id,h);GenerateScrollControls(id);e.data("scroll-options",o);jQuery(this).trigger('scroll_drawn')}else{c.data("scroll-options",o)}})},refresh:function(){this.each(function(){RemoveScrollbar(jQuery(this))});return q.create.call(this)},increment:function(c){return this.each(function(){var a=jQuery(this);var b=a.attr("id");scrollDifference=a.children(".scroll_content_wrapper").children(".scroll_content").height()-a.height();$.fn.scrollbar.incrementSlider(c,b,scrollDifference)})},destroy:function(){return this.each(function(a){RemoveScrollbar(jQuery(this))})}};function CreateSlider(d){var e=(o.scrollbar.location=="left")?0:o.scrollbar.margin;var f=(o.scrollbar.location=="right")?0:o.scrollbar.margin;var g=(function(a){return a.scrollDifference})(o);var h=jQuery('<div></div>').css({"margin-right":f,"margin-left":e,"position":"relative","float":o.scrollbar.location}).attr('class','scrollbar_outer');var i=jQuery('<div></div>').attr('class','scrollbar').slider({orientation:'vertical',min:0,max:100,value:100,slide:function(a,b){var c=-((100-b.value)*g/100);$('#'+d+' > .scroll_content_wrapper > .scroll_content').css({top:c})}}).css({"width":o.scrollbar.width,"overflow":"visible","border":"0px"});h.append(i);return h};function WrapContent(a){var b=jQuery(a).clone(true,true);o.originalStyle=jQuery(a).attr("style")?jQuery(a).attr("style"):"";b.css({"height":"auto",'width':jQuery(a).width()-o.scrollbar.width-o.scrollbar.margin});jQuery(b).css({"position":"absolute","left":0,"top":0}).attr("class","scroll_content").removeAttr("id");o.setContentHeight(b,a);o.scrollDifference=o.contentHeight-o.wrapperHeight;b.css({"height":o.contentHeight,'overflow-y':'hidden'});var c=jQuery('<div></div>').css({"position":"relative","float":"left",'width':o.wrapperWidth-o.scrollbar.width-o.scrollbar.margin,'height':o.wrapperHeight,"overflow-y":"hidden"}).addClass('scroll_content_wrapper');(!o.scrollbar.horizontalVisible)?c.css({"overflow-x":"hidden"}):null;c.append(b);return c};function RemoveScrollbar(c){var a=c.children('.scroll_content_wrapper').children('.scroll_content')||[];if(a.length>0){var b=c.data("scroll-options");c.html(a.html()).removeAttr("style").attr("style",b.originalStyle);(c.attr("id").split("_")[0]="scrollable")?c.removeAttr("id"):null}};function SliderToScrollbar(a,h){var b=o.scrollbar.end_cap_size;var c=parseInt(((1-((o.contentHeight-h)/o.contentHeight))*h-b*2)/2)*2;c=(c>o.scrollbar.end_cap_size*2)?c:o.scrollbar.end_cap_size*2;var d=h-c-b*2;var e=(h-d)*0.5-b;e=(e>0)?e:0;var f=parseInt($(".ui-slider-handle").css('border-top-width'))+parseInt($(".ui-slider-handle").css('border-bottom-width'));var g=$("#"+a).children(".scrollbar_outer"),scroll_bar=g.children(".scrollbar");g.children(".ui-slider-vertical").css({height:d,"border-radius":0,"-moz-border-radius":0});scroll_bar.children(".ui-slider-handle").css({height:c-f+"px",left:0,width:o.scrollbar.width-2,'margin-bottom':-0.5*c});scroll_bar.children(".ui-slider-range").css({top:-e});var i=jQuery('<div style="height:'+e+'px;padding:0px;border:0px;"></div>').attr('class','ui-widget-content');g.prepend(i.clone(true).addClass('scroll_top'));g.append(i.clone(true).addClass('scroll_bottom'));if(b>0){var j=jQuery('<div style="height:'+b+'px;padding:0px;border:0px;position:relative"></div>').attr('class','ui-widget-content');var k=16;var l=jQuery('<div></div>').addClass('ui-icon').width(k).height(k);g.prepend(j.clone(true).addClass('scroll_up'));g.append(j.clone(true).addClass('scroll_down'));if(b>=5){var w=(k-g.width())/2;var h=(k-b)/2;w=(parseInt(w)==w)?[w,w+1]:[w-0.5*w/Math.abs(w),w-0.5*w/Math.abs(w)+1];h=(parseInt(h)==h)?[h,h]:[h+0.5*h/Math.abs(h),h-0.5*h/Math.abs(h)];g.children(".scroll_up").css({"cursor":"default"}).html(l.clone().css({'position':'absolute','left':-w[0],'overflow':'hidden','width':'15px','max-height':"16px"}).addClass('ui-icon-carat-1-n').css({top:-h[0]}));g.children('.scroll_down').css({"cursor":"default"});g.children(".scroll_down").html(l.clone().css({'position':'absolute','left':-w[1],'top':-h[1],'overflow':'hidden','width':'15px','max-height':"16px"}).addClass('ui-icon-carat-1-s'))}}};function GenerateScrollControls(a){var b=$("#"+a+" .scrollbar_outer > .ui-slider-vertical");o.scrollbar.incrementSize=(o.scrollbar.incrementSize)?o.scrollbar.incrementSize:parseInt(b.height()/20);p.init(o.scrollbar.incrementSize,a)};if(q[m]){return q[m].apply(this,Array.prototype.slice.call(arguments,1))}else if(typeof m==='object'||!m){o.scrollbar=$.extend({},$.fn.scrollbar.defaults,m);return q.create.apply(this,arguments)}else{$.error('Method '+m+' does not exist')}};$.fn.scrollbar.defaults={location:"left",width:9,margin:2,end_cap_size:16,incrementSize:undefined,horizontalVisible:true,inlineStyleForWrapper:true};$.fn.scrollbar.incrementSlider=function(a,b,c){var d=$('#'+b+' > .scrollbar_outer > .scrollbar');var e=d.slider("value");a+=.5*Math.abs(a)/a;e+=a;d.slider("value",e);var f=parseInt(((100-e)*c)/100);(f<0)?f=0:undefined;(Math.abs(f)>c)?f=c:undefined;$('#'+b+' > .scroll_content_wrapper > .scroll_content').css({top:-f})};$.fn.scrollbar.click=function(a,b,c,d){$.fn.scrollbar.click.types[a](b,c,d)};$.fn.scrollbar.click.types={up:function(a,b,c){$("#"+a+" > .scrollbar_outer > .scroll_up").live('click',function(){$.fn.scrollbar.incrementSlider(b,a,c)})},down:function(a,b,c){$("#"+a+" > .scrollbar_outer > .scroll_down").live('click',function(){$.fn.scrollbar.incrementSlider(-b,a,c)})},top:function(a,b,c){$("#"+a).children(".scrollbar_outer").children('.scroll_top').bind('click',function(){$.fn.scrollbar.incrementSlider(100,a,c)})},bottom:function(a,b,c){$("#"+a).children(".scrollbar_outer").children('.scroll_bottom').bind('click',function(){$.fn.scrollbar.incrementSlider(-100,a,c)})}};$.fn.scrollbar.wheel=function(e,f,g){if(jQuery.isFunction(jQuery.fn.mousewheel)){$("#"+e).mousewheel(function(a,b,c,d){return $.fn.scrollbar.wheel.increment(e,g,d)})}else{$.fn.scrollbar.wheel.handler(e,g)}};$.fn.scrollbar.wheel.handler=function(d,e){var f=['DOMMouseScroll','mousewheel'];var g=$("#"+d).first()[0];if(g.addEventListener){for(var i=f.length;i;){g.addEventListener(f[--i],mini_handler,false)}}else{g.onmousewheel=mini_handler}function mini_handler(a){var b=a||window.event,deltaY;a=$.event.fix(b);deltaY=b.wheelDeltaY!==undefined?b.wheelDeltaY/120:a.wheelDelta?a.wheelDelta/120:(b.axis!==undefined&&b.axis===b.HORIZONTAL_AXIS)?0:a.detail?-a.detail/3:0;var c=$.fn.scrollbar.wheel.increment(d,e,deltaY);if(!c){(b.stopPropagation)?b.stopPropagation():b.cancelBubble=true;if(a.preventDefault)a.preventDefault();a.returnValue=false}}};$.fn.scrollbar.wheel.increment=function(a,b,c){var d=$('#'+a).children(' .scrollbar_outer').children('.scrollbar').slider("value");if(((d==0)&&(c>0))||((d==100)&&(c<0))||((d>0)&&(d<100))){var e=parseInt(c*$.fn.scrollbar.wheel.increment.multiplier)==c*$.fn.scrollbar.wheel.increment.multiplier?c*$.fn.scrollbar.wheel.increment.multiplier:parseInt(c/0.025)==c/0.025?$.fn.scrollbar.wheel.increment.multiplier=40:parseInt(c*3)==c*3?$.fn.scrollbar.wheel.increment.multiplier=3*3:null;var f=Math.abs(e)/e;e=e/b*100;f===$.fn.scrollbar.wheel.increment.previous_direction?$.fn.scrollbar.incrementSlider(e,a,b):$.fn.scrollbar.wheel.increment.previous_direction=f;return false};return true};$.fn.scrollbar.wheel.increment.multiplier=(function(){if(jQuery.browser.msie==true)return 5;return 1})();$.fn.scrollbar.wheel.increment.previous_direction=1;$.fn.scrollbar.touch=function(j,k){$("#"+j).live('touchstart',function(d){var f=this;var e=d.originalEvent;if(e.targetTouches.length!=1)return false;var g=e.targetTouches[0].clientY;var h=g;var i=true;$("#"+j).live('touchmove',function(a){i=false;var b=d.originalEvent;g=h;h=b.targetTouches[0].clientY;var c=g-h;$.fn.scrollbar.incrementSlider(-c,j,k);a.preventDefault()});$("#"+j).live('touchend',function(a){$("#"+j).die('touchmove');$("#"+j).die('touchend');var b=a.originalEvent;var c=b.targetTouches[0]||e.targetTouches[0];mouse_click=document.createEvent('MouseEvent');if(i===true){mouse_click.initMouseEvent('click',true,true,c.view,1,c.screenX,c.screenY,c.clickX,c.clientY,false,false,false,false,0,null);c.target.dispatchEvent(mouse_click)}});d.preventDefault()})}})(jQuery);
