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