(function(a){a.fn.hasScrollBar=function(){return this.get(0).scrollHeight>this.height()};a.fn.lionbars=function(b){function bg(b,c){var d=a(b).get(0);F=typeof c!="undefined"?c.height:d.scrollHeight;G=typeof c!="undefined"?c.width:d.scrollWidth;K=d.clientHeight;J=d.clientWidth;I=d.offsetHeight;H=d.offsetWidth;ba(a(b));bb(a(b))}function bf(b){var c=a(b);B=parseInt(c.css("border-top-width").replace("px",""));C=parseInt(c.css("border-right-width").replace("px",""));D=parseInt(c.css("border-bottom-width").replace("px",""));E=parseInt(c.css("border-left-width").replace("px",""))}function be(b){var c=a(b);x=parseInt(c.css("padding-top").replace("px",""));y=parseInt(c.css("padding-left").replace("px",""));z=parseInt(c.css("padding-bottom").replace("px",""));A=parseInt(c.css("padding-right").replace("px",""))}function bd(b){var c=a(b);w=false;v=false;be(c);bf(c);c.css({overflow:"hidden"});if(c.get(0).scrollHeight>c.get(0).clientHeight){w=true}if(c.get(0).scrollWidth>c.get(0).clientWidth){v=true}c.css({overflow:"auto"});if(w||v){return true}}function bc(b,c,d){var e=a(b);var f=e.attr("id");var g=0;if(f!==undefined){e.wrapInner('<div class="lb-wrap" id="lb-wrap-'+s+"-"+f+'"></div>');g=a("#lb-wrap-"+s+"-"+f)}else{e.wrapInner('<div class="lb-wrap" id="lb-wrap-'+s+'"></div>');g=a("#lb-wrap-"+s)}g.wrapInner('<div class="lb-content"></div>');if(c){e.prepend('<div class="lb-v-scrollbar"></div>');e.find(".lb-v-scrollbar").append('<div class="lb-v-scrollbar-slider"></div>')}if(d){e.prepend('<div class="lb-h-scrollbar"></div>');e.find(".lb-h-scrollbar").append('<div class="lb-h-scrollbar-slider"></div>')}s=s+1}function bb(b){var c=a(b);c.css({overflow:"auto"});u=I-K-B-D;c.css({overflow:"hidden"})}function ba(b){var c=a(b);c.css({overflow:"auto"});t=H-J-E-C;c.css({overflow:"hidden"})}function _(b,c){var d=a(b);var e=a(c);d.css({position:"relative"});e.css({width:d.width()+t-y-A,height:d.height()+u-x-z})}function Z(b,c){var d=a(b);var e=a(c);d.css({padding:0});e.css({"padding-top":x+"px","padding-left":y+"px","padding-bottom":z+"px","padding-right":A+"px"})}function Y(b){var c=a(b);c.css({width:c.width()+y+A,height:c.height()+x+z})}function X(b,c,d){var e=a(b);if(c||d){e.css({overflow:"hidden"});Z(e,e.find(".lb-wrap"));Y(e);_(e,e.find(".lb-wrap"))}}function W(b){var c=a(b);if(w&&v){P=c.height()-12;Q=c.width()-12;c.find(".lb-v-scrollbar").css({height:P});c.find(".lb-h-scrollbar").css({width:Q})}else{P=c.height()-4;Q=c.width()-4;c.find(".lb-v-scrollbar").css({height:P});c.find(".lb-h-scrollbar").css({width:Q})}}function V(){t=0;u=0;v=false;w=false;x=0;y=0;z=0;A=0;B=0;E=0;D=0;C=0;F=0;G=0;H=0;I=0;J=0;K=0;N=0;O=0;P=0;Q=0}function U(b){var c=a(b);var d,e,f;if(c.find(".lb-v-scrollbar").length!=0){d=20;f=I-c.find(".lb-v-scrollbar").height();e=I-f-d;N=Math.round(I*e/F);N=N<d?d:N}if(c.find(".lb-h-scrollbar").length!=0){d=20;f=H-c.find(".lb-h-scrollbar").width();e=H-f-d;O=Math.round(H*e/G);O=O<d?d:O}c.find(".lb-v-scrollbar-slider").css({height:N});c.find(".lb-h-scrollbar-slider").css({width:O})}function T(b){L=(I-a(b).find(".lb-wrap").get(0).scrollHeight-B-D)/(P-N);M=(H-a(b).find(".lb-wrap").get(0).scrollWidth-E-C)/(Q-O);var c=a(b);c.attr("vratio",L);c.attr("hratio",M)}function S(j){var k=a(j);if(w||v){k.find(".lb-wrap").scroll(function(d){k.find(".lb-v-scrollbar-slider").css({top:-a(this).scrollTop()/k.attr("vratio")});k.find(".lb-h-scrollbar-slider").css({left:-a(this).scrollLeft()/k.attr("hratio")});if(k.find(".lb-v-scrollbar").height()==parseInt(k.find(".lb-v-scrollbar-slider").css("top"))+k.find(".lb-v-scrollbar-slider").height()&&typeof b.reachedBottom=="function"&&!p){p=true;var e=a(this);b.reachedBottom.apply(a(this).children(".lb-content"),[function(){bg(a(e).parent(),{height:a(e).children(".lb-content").get(0).scrollHeight,width:a(e).children(".lb-content").get(0).scrollWidth});W(a(e).parent());U(a(e).parent());T(a(e).parent());V();p=false}])}if(k.find(".lb-h-scrollbar").width()==parseInt(k.find(".lb-h-scrollbar-slider").css("left"))+k.find(".lb-h-scrollbar-slider").width()&&typeof b.reachedRight=="function"&&!q){q=true;var e=a(this);b.reachedRight.apply(a(this).children(".lb-content"),[function(){bg(a(e).parent(),{height:a(e).children(".lb-content").get(0).scrollHeight,width:a(e).children(".lb-content").get(0).scrollWidth});W(a(e).parent());U(a(e).parent());T(a(e).parent());V();q=false}])}if(autohide){k.find(".lb-v-scrollbar, .lb-h-scrollbar").fadeIn(150);clearTimeout(c);c=setTimeout(function(){k.find(".lb-v-scrollbar, .lb-h-scrollbar").fadeOut(150)},2e3)}})}if(w){k.find(".lb-v-scrollbar-slider").mousedown(function(b){i=b.pageY;e=true;f=a(this);g=k.find(".lb-wrap");l=g.parent().attr("vratio");m=f.position().top;return false});k.find(".lb-v-scrollbar").mousedown(function(b){if(!a(b.target).hasClass("lb-v-scrollbar-slider")){k.find(".lb-wrap").scrollTop((b.pageY-a(this).offset().top)*Math.abs(k.attr("vratio"))-a(this).find(".lb-v-scrollbar-slider").height()/2)}return false})}if(v){k.find(".lb-h-scrollbar-slider").mousedown(function(b){h=b.pageX;d=true;f=a(this);g=k.find(".lb-wrap");l=g.parent().attr("hratio");m=f.position().left;return false});k.find(".lb-h-scrollbar").mousedown(function(b){if(!a(b.target).hasClass("lb-h-scrollbar-slider")){k.find(".lb-wrap").scrollLeft((b.pageX-a(this).offset().left)*Math.abs(k.attr("hratio"))-a(this).find(".lb-h-scrollbar-slider").width()/2)}return false})}if((w||v)&&autohide){k.find(".lb-v-scrollbar, .lb-h-scrollbar").hide();k.hover(function(){},function(){k.find(".lb-v-scrollbar, .lb-h-scrollbar").fadeOut(150)})}}function R(){for(var b=0;r[b]!==undefined;b++){if(bd(r[b])&&!a(r[b]).hasClass("nolionbars")){target=r[b];bg(target);bc(target,w,v);X(target,w,v);W(target);U(target);T(target);S(target);V()}}}b=b||{};autohide=b.autohide;var c,d=false,e=false,f=0,g=0,h,i,j,k,l,m,n,o=false,p=false,q=false;var r=a(this),s=0,t=0,u=0,v=false,w=false,x=0,y=0,z=0,A=0,B=0,C=0,D=0,E=0,F=0,G=0,H=0,I=0,J=0,K=0,L=0,M=0,N=0,O=0,P=0,Q=0;R();a(document).mousemove(function(a){if(e){k=a.pageY;g.scrollTop((m+k-i)*Math.abs(l))}if(d){j=a.pageX;g.scrollLeft((m+j-h)*Math.abs(l))}});a(document).mouseup(function(a){if(e){e=false}if(d){d=false}});return this.each(function(){})}})(jQuery)

// Chama scroll
function setScroll( alvo, W, H ){
	// Alvo
	var alvo = $( alvo );
	// Largura
	if( W ) alvo.css('width',W);
	// Altura
	if( H ) alvo.css('height',H);
	// Ini scroll
	alvo.lionbars(); 
}
