/*
[jScrollTools ver 0.1.2.7]
Copyright (c) 2011 Eky Kwan, http://twitter.com/ekykwan
MIT License: http://www.opensource.org/licenses/mit-license.php

[Thanks]
Mouse wheel programming in JavaScript: http://www.adomas.org/javascript-mouse-wheel/
Animation | JavaScript.ru: http://javascript.info/tutorial/animation

[TODO]
	- change event hanlder to multi applicable
	- better negative scroll amount handle of event handler
*/
var jScrollTools=new function(){return{jScrollToolses:{},pluginName:"jScrollTools",getPluginName:function(){return this.pluginName},init:function(h,k){this.newTools().init(h,k)},initByClass:function(h,k){var j=document.getElementsByClassName(h),g;for(g in j){if(!j[g].id)j[g].id=jScrollTools.pluginName+"-"+h+g;this.newTools().init(j[g].id,k)}},initByTagName:function(h,k){var j=document.getElementsByTagName(h),g;for(g in j)if(j[g]&&!isNaN(g)){if(!j[g].id)j[g].id=jScrollTools.pluginName+"-"+h+g;this.newTools().init(j[g].id,
k)}},newTools:function(){var h="jScrollTools",k={intervalTime:20,direction:"prev",autoScroll:5,endless:!1,defaultDirection:"prev",backToDefaultDirection:!0,wheelScroll:!0,isVert:!1,vibrate:!1,wheelHandlers:{},prevHandler:{id:null,event:"hover",scrollAmount:20},nextHandler:{id:null,event:"hover",scrollAmount:20},stopHandler:{id:null,event:"hover",scrollAmount:0}},j={wheelHandlers:{scrollAmount:176,reverse:!1,smooth:!0,easeLevel:-1.5,duration:200,totalPow:10,grid:176}},g=!1,l=0,m=0,d=0,q="stop",p={hover:{on:"mouseover",
off:"mouseout"},click:{on:"mousedown",off:"mouseup"}},f={direction:"next",scrollAmount:0,isWheel:!0,scrolled:0,scrolling:!1},o,i,n;return{init:function(a,b){g=!!document.all;null!=a&&(h=a);if(o=document.getElementById(h)){i=document.getElementById(h).children[0];d=i.style.left;k.wheelHandlers[h]=j.wheelHandlers;this.option=null==b?k:this.initHelper(b,k);if(0>this.option.autoScroll)this.option.autoScroll=-this.option.autoScroll,this.option.defaultDirection="prev"==this.option.defaultDirection?"next":
"prev";this.option.wheelHandlers&&this.initHelper2(b,j,"wheelHandlers");this.option.isVert?(m=this.contentHeight(),l=i.offsetWidth):(l=this.contentWidth(),m=i.children[0].offsetHeight);i.style.width=l+"px";o.style.overflow="hidden";jScrollTools.jScrollToolses[h]=this;o.style.position="relative";i.style.position="relative";this.addEvent();if(this.option.endless){var c=i.cloneNode(!0);o.appendChild(c);n=document.getElementById(h).children[1];n.innerHTML=i.innerHTML;n.style.position="absolute";n.style.top=
"0px";if(this.option.isVert)n.style.position="absolute"}q=this.option.defaultDirection;this.scroll(q)}},initHelper:function(a,b){if(null==a&&null==b)return null;var c={},c=null==b?a:b,e;for(e in b)c[e]="object"==typeof a[e]?this.initHelper(a[e],b[e]):null==a[e]?b[e]:a[e];for(e in a)c[e]="object"==typeof a[e]?this.initHelper(a[e],b[e]):null==a[e]?b[e]:a[e];return c},initHelper2:function(a,b,c){if(a[c]){this.option[c]={};for(var e in a[c])this.option[c][e]=j[c];this.option[c]=this.initHelper(a[c],a[c]);
for(e in this.option[c])for(var d in j[c])null==this.option[c][e][d]&&(this.option[c][e][d]=j[c][d])}else this.option[c][h]=j[c]},contentWidth:function(){var a=0,b=i.children.length-1,c;for(c in i.children){var e=this.outerWidth(i.children[c],!0);isNaN(e)||(a+=e);if(c>=b)return a}},contentHeight:function(){var a=0,b=i.children.length-1,c;for(c in i.children){var e=this.outerHeight(i.children[c],!0);isNaN(e)||(a+=e);if(c>=b)return a}},outerWidth:function(a,b){var c=0,e=0,d=0,f=0;if(b){if(g){if(a.currentStyle)e=
parseFloat(a.currentStyle.marginLeft.replace("px","")),e=isNaN(e)?0:e,d=parseFloat(a.currentStyle.marginRight.replace("px","")),d=isNaN(d)?0:d,f=a.offsetWidth,c=f+e+d}else e=parseFloat(document.defaultView.getComputedStyle(a,null).marginLeft.replace("px","")),d=parseFloat(document.defaultView.getComputedStyle(a,null).marginRight.replace("px","")),f=a.offsetWidth,c=f+e+d;return c}return c=a.offsetWidth},outerHeight:function(a,b){var c=0,e=0,d=0,f=0;return b?(g?a.currentStyle&&(e=parseFloat(a.currentStyle.marginTop.replace("px",
"")),e=isNaN(e)?0:e,d=parseFloat(a.currentStyle.marginBottom.replace("px","")),d=isNaN(d)?0:d,c=a.offsetHeight+e+d):(e=parseFloat(document.defaultView.getComputedStyle(a,null).marginTop.replace("px","")),d=parseFloat(document.defaultView.getComputedStyle(a,null).marginBottom.replace("px","")),f=a.offsetHeight,c=f+e+d),c):c=a.offsetHeight},scroll:function(a){var b=this.option.autoScroll;this.option.vibrate&&(a="prev"==a?"next":"prev");this.direction=a;this.onHandler&&(b+=this.handlerScrollAmount);
if("stop"!=this.direction){this.scrolling(a,b);var c=this;tiemr=setTimeout(function(){c.scroll(c.direction)},this.option.intervalTime)}},addScroll:function(){for(var a in this.option.wheelHandlers){var b=this.option.wheelHandlers[a];b.easeLevel>b.maxEaseLevel||b.easeLevel<-b.maxEaseLevel?(b.totalPow=10,easeLevel=0):b.totalPow=this.totalPow(b.duration,b.easeLevel);b=document.getElementById(a);jScrollTools.addClass(b,"jScrollTools-wheel-"+h);if(!jScrollTools.hasClass(b,"jScrollTools-wheelEventAdded"))jScrollTools.addClass(b,
"jScrollTools-wheelEventAdded"),g||b.addEventListener("DOMMouseScroll",this.wheel,!1),b.onmousewheel=this.wheel}},smoothScrolling:function(a,b,c,e){f.scrolled=0;f.scrolling=!0;this.smoothScrollingHelper(a,Math.abs(b*e.scrollAmount),c,0,e.easeLevel,e.duration,0,e.grid,e.totalPow)},smoothScrollingHelper:function(a,b,c,e,h,g,i,j,k){f.direction=a;f.easeLevel=h;f.scrollAmount=b;f.isWheel=c;f.duration=g;f.grid=j;f.totalPow=k;f.progress=e+1;b=this.ease(f.progress/(g/this.option.intervalTime),b,h);f.scrolled+=
b;this.scrolling(a,b,c);if(f.progress<g/this.option.intervalTime){var l=this;setTimeout(function(){l.smoothScrollingHelper(f.direction,f.scrollAmount,f.isWheel,f.progress,f.easeLevel,f.duration,f.scrolled,f.grid,f.totalPow)},l.option.intervalTime)}else f.scrolling=!1,f.grid&&(g=d%f.grid,0!=g&&(-g<f.grid/2?this.scrolling(a,g,c):this.scrolling(a,g+f.grid,c)))},totalPow:function(a,b){for(var c=0,e=0;e<a/this.option.intervalTime;)e++,c+=Math.pow(e/(a/this.option.intervalTime),b);return c},ease:function(a,
b,c){return b*Math.pow(a,c)/f.totalPow},scrolling:function(a,b,c){if(this.option.isVert)if(this.direction=a,"prev"==this.direction)if(this.option.endless){if(d=1*d-b,i.style.top=d+"px",n.style.top=d+m+"px",Math.ceil(d)<-m)i.style.top=Math.ceil(-b)+"px",d=Math.ceil(d+m)}else d=-d+b<=m-o.offsetWidth?1*d-b:-(m-o.offsetWidth),i.style.top=d+"px";else if("next"==this.direction)if(this.option.endless){if(d=1*d+b,i.style.top=d+"px",n.style.top=d+m+"px",Math.ceil(d+m)>m){if(this.option.endless)n.style.top=
Math.ceil(-m+b)+"px";d=Math.ceil(d-m)}}else d=d+b+m<=m?1*d+b:0,i.style.left=d+"px";if(l>o.offsetWidth&&!this.option.isVert)if(this.direction=a,0>b&&(b=-b),"prev"==this.direction)if(this.option.endless){if(d=1*d+b,i.style.left=d+"px",n.style.left=d+l+"px",Math.ceil(d+l)>=l){if(this.option.endless)n.style.left=Math.ceil(-l+d)+"px";d=Math.ceil(d-l)}}else d=d+b+l<=l?1*d+b:0,i.style.left=d+"px";else if("next"==this.direction)if(this.option.endless){if(d=1*d-b,i.style.left=d+"px",n.style.left=d+l+"px",
Math.ceil(d)<=-l)i.style.left=-Math.ceil(d)+"px",d=Math.ceil(d)+l}else d=-d+b<=l-o.offsetWidth?1*d-b:-(l-o.offsetWidth),i.style.left=d+"px";if(c&&!this.onHandler)this.direction=this.option.defaultDirection},setHandlerScroll:function(a,b,c){this.onHandler=a;this.handlerScrollAmount=b;this.direction=c;if(!a&&this.option.backToDefaultDirection)this.direction=this.option.defaultDirection},addEvent:function(){this.option.wheelHandlers&&this.addScroll();if(null!=this.option.prevHandler.id){var a=document.getElementById(this.option.prevHandler.id),
b=document.getElementById(this.option.prevHandler.id);"click"==this.option.prevHandler.event&&(b=window);var c=this,e=function(){c.setHandlerScroll(!0,c.option.prevHandler.scrollAmount,"prev")},d=function(){c.setHandlerScroll(!1,c.option.prevHandler.scrollAmount,"stop")};g?(a.attachEvent("on"+p[this.option.prevHandler.event].on,e,!1),b.attachEvent("on"+p[this.option.prevHandler.event].off,d,!1)):(a.addEventListener(p[this.option.prevHandler.event].on,e,!1),b.addEventListener(p[this.option.prevHandler.event].off,
d,!1))}null!=this.option.nextHandler.id&&(a=document.getElementById(this.option.nextHandler.id),b=document.getElementById(this.option.nextHandler.id),"click"==this.option.nextHandler.event&&(b=window),c=this,e=function(){c.setHandlerScroll(!0,c.option.nextHandler.scrollAmount,"next")},d=function(){c.setHandlerScroll(!1,c.option.nextHandler.scrollAmount,"stop")},g?(a.attachEvent("on"+p[this.option.nextHandler.event].on,e,!1),b.attachEvent("on"+p[this.option.nextHandler.event].off,d,!1)):(a.addEventListener(p[this.option.nextHandler.event].on,
e,!1),b.addEventListener(p[this.option.nextHandler.event].off,d,!1)))},wheel:function(a){var b=0;if(!a)a=window.event;a.wheelDelta?(b=a.wheelDelta/120,window.opera&&(b=-b)):a.detail&&(b=-a.detail/4);a.preventDefault&&a.preventDefault();a.returnValue=!1;a=this.className.split(" ");for(className in a){var c=a[className].split(jScrollTools.pluginName+"-wheel-");if(c=c[1]){var c=jScrollTools.jScrollToolses[c],d=c.getwheelHandler(this.id),f=d.reverse?c.getNext():c.getPrev(),g=d.reverse?c.getPrev():c.getNext();
0<b?d.smooth?c.smoothScrolling(f,b,!0,d):c.scrolling(f,b*d.scrollAmount,!0):d.smooth?c.smoothScrolling(g,b,!0,d):c.scrolling(g,b*d.scrollAmount,!0)}}},getPrev:function(){return"prev"},getNext:function(){return"next"},getwheelHandler:function(a){return this.option.wheelHandlers[a]},getSmoothScroll:function(){return f}}},addClass:function(h,k){h.className=h.className?h.className+" "+k:k},hasClass:function(h,k){return 0<=h.className.indexOf(k)?!0:!1}}};
document.getElementsByClassName=function(h){for(var k=[],h=RegExp("\\b"+h+"\\b"),j=this.getElementsByTagName("*"),g=0;g<j.length;g++)h.test(j[g].className)&&k.push(j[g]);return k};
