/*
== malihu jquery custom scrollbar plugin ==
version: 3.1.5
plugin uri: http://manos.malihu.gr/jquery-custom-content-scroller
author: malihu
author uri: http://manos.malihu.gr
license: mit license (mit)
*/
/*
copyright manos malihutsakis (email: manos@malihu.gr)
permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "software"), to deal
in the software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the software, and to permit persons to whom the software is
furnished to do so, subject to the following conditions:
the above copyright notice and this permission notice shall be included in
all copies or substantial portions of the software.
the software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. in no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in
the software.
*/
/*
the code below is fairly long, fully commented and should be normally used in development.
for production, use either the minified jquery.mcustomscrollbar.min.js script or
the production-ready jquery.mcustomscrollbar.concat.min.js which contains the plugin
and dependencies (minified).
*/
(function(factory){
if(typeof define==="function" && define.amd){
define(["jquery"],factory);
}else if(typeof module!=="undefined" && module.exports){
module.exports=factory;
}else{
factory(jquery,window,document);
}
}(function($){
(function(init){
var _rjs=typeof define==="function" && define.amd, /* requirejs */
_njs=typeof module !== "undefined" && module.exports, /* nodejs */
_dlp=("https:"==document.location.protocol) ? "https:" : "http:", /* location protocol */
_url="cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js";
if(!_rjs){
if(_njs){
require("jquery-mousewheel")($);
}else{
/* load jquery-mousewheel plugin (via cdn) if it's not present or not loaded via requirejs
(works when mcustomscrollbar fn is called on window load) */
$.event.special.mousewheel || $("head").append(decodeuri("%3cscript src="+_dlp+"//"+_url+"%3e%3c/script%3e"));
}
}
init();
}(function(){
/*
----------------------------------------
plugin namespace, prefix, default selector(s)
----------------------------------------
*/
var pluginns="mcustomscrollbar",
pluginpfx="mcs",
defaultselector=".mcustomscrollbar",
/*
----------------------------------------
default options
----------------------------------------
*/
defaults={
/*
set element/content width/height programmatically
values: boolean, pixels, percentage
option default
-------------------------------------
setwidth false
setheight false
*/
/*
set the initial css top property of content
values: string (e.g. "-100px", "10%" etc.)
*/
settop:0,
/*
set the initial css left property of content
values: string (e.g. "-100px", "10%" etc.)
*/
setleft:0,
/*
scrollbar axis (vertical and/or horizontal scrollbars)
values (string): "y", "x", "yx"
*/
axis:"y",
/*
position of scrollbar relative to content
values (string): "inside", "outside" ("outside" requires elements with position:relative)
*/
scrollbarposition:"inside",
/*
scrolling inertia
values: integer (milliseconds)
*/
scrollinertia:950,
/*
auto-adjust scrollbar dragger length
values: boolean
*/
autodraggerlength:true,
/*
auto-hide scrollbar when idle
values: boolean
option default
-------------------------------------
autohidescrollbar false
*/
/*
auto-expands scrollbar on mouse-over and dragging
values: boolean
option default
-------------------------------------
autoexpandscrollbar false
*/
/*
always show scrollbar, even when there's nothing to scroll
values: integer (0=disable, 1=always show dragger rail and buttons, 2=always show dragger rail, dragger and buttons), boolean
*/
alwaysshowscrollbar:0,
/*
scrolling always snaps to a multiple of this number in pixels
values: integer, array ([y,x])
option default
-------------------------------------
snapamount null
*/
/*
when snapping, snap with this number in pixels as an offset
values: integer
*/
snapoffset:0,
/*
mouse-wheel scrolling
*/
mousewheel:{
/*
enable mouse-wheel scrolling
values: boolean
*/
enable:true,
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollamount:"auto",
/*
mouse-wheel scrolling axis
the default scrolling direction when both vertical and horizontal scrollbars are present
values (string): "y", "x"
*/
axis:"y",
/*
prevent the default behaviour which automatically scrolls the parent element(s) when end of scrolling is reached
values: boolean
option default
-------------------------------------
preventdefault null
*/
/*
the reported mouse-wheel delta value. the number of lines (translated to pixels) one wheel notch scrolls.
values: "auto", integer
"auto" uses the default os/browser value
*/
deltafactor:"auto",
/*
normalize mouse-wheel delta to -1 or 1 (disables mouse-wheel acceleration)
values: boolean
option default
-------------------------------------
normalizedelta null
*/
/*
invert mouse-wheel scrolling direction
values: boolean
option default
-------------------------------------
invert null
*/
/*
the tags that disable mouse-wheel when cursor is over them
*/
disableover:["select","option","keygen","datalist","textarea"]
},
/*
scrollbar buttons
*/
scrollbuttons:{
/*
enable scrollbar buttons
values: boolean
option default
-------------------------------------
enable null
*/
/*
scrollbar buttons scrolling type
values (string): "stepless", "stepped"
*/
scrolltype:"stepless",
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollamount:"auto"
/*
tabindex of the scrollbar buttons
values: false, integer
option default
-------------------------------------
tabindex null
*/
},
/*
keyboard scrolling
*/
keyboard:{
/*
enable scrolling via keyboard
values: boolean
*/
enable:true,
/*
keyboard scrolling type
values (string): "stepless", "stepped"
*/
scrolltype:"stepless",
/*
scrolling amount in pixels
values: "auto", integer
*/
scrollamount:"auto"
},
/*
enable content touch-swipe scrolling
values: boolean, integer, string (number)
integer values define the axis-specific minimum amount required for scrolling momentum
*/
contenttouchscroll:25,
/*
enable/disable document (default) touch-swipe scrolling
*/
documenttouchscroll:true,
/*
advanced option parameters
*/
advanced:{
/*
auto-expand content horizontally (for "x" or "yx" axis)
values: boolean, integer (the value 2 forces the non scrollheight/scrollwidth method, the value 3 forces the scrollheight/scrollwidth method)
option default
-------------------------------------
autoexpandhorizontalscroll null
*/
/*
auto-scroll to elements with focus
*/
autoscrollonfocus:"input,textarea,select,button,datalist,keygen,a[tabindex],area,object,[contenteditable='true']",
/*
auto-update scrollbars on content, element or viewport resize
should be true for fluid layouts/elements, adding/removing content dynamically, hiding/showing elements, content with images etc.
values: boolean
*/
updateoncontentresize:true,
/*
auto-update scrollbars each time each image inside the element is fully loaded
values: "auto", boolean
*/
updateonimageload:"auto",
/*
auto-update scrollbars based on the amount and size changes of specific selectors
useful when you need to update the scrollbar(s) automatically, each time a type of element is added, removed or changes its size
values: boolean, string (e.g. "ul li" will auto-update scrollbars each time list-items inside the element are changed)
a value of true (boolean) will auto-update scrollbars each time any element is changed
option default
-------------------------------------
updateonselectorchange null
*/
/*
extra selectors that'll allow scrollbar dragging upon mousemove/up, pointermove/up, touchend etc. (e.g. "selector-1, selector-2")
option default
-------------------------------------
extradraggableselectors null
*/
/*
extra selectors that'll release scrollbar dragging upon mouseup, pointerup, touchend etc. (e.g. "selector-1, selector-2")
option default
-------------------------------------
releasedraggableselectors null
*/
/*
auto-update timeout
values: integer (milliseconds)
*/
autoupdatetimeout:60
},
/*
scrollbar theme
values: string (see css/plugin uri for a list of ready-to-use themes)
*/
theme:"light",
/*
user defined callback functions
*/
callbacks:{
/*
available callbacks:
callback default
-------------------------------------
oncreate null
oninit null
onscrollstart null
onscroll null
ontotalscroll null
ontotalscrollback null
whilescrolling null
onoverflowy null
onoverflowx null
onoverflowynone null
onoverflowxnone null
onimageload null
onselectorchange null
onbeforeupdate null
onupdate null
*/
ontotalscrolloffset:0,
ontotalscrollbackoffset:0,
alwaystriggeroffsets:true
}
/*
add scrollbar(s) on all elements matching the current selector, now and in the future
values: boolean, string
string values: "on" (enable), "once" (disable after first invocation), "off" (disable)
liveselector values: string (selector)
option default
-------------------------------------
live false
liveselector null
*/
},
/*
----------------------------------------
vars, constants
----------------------------------------
*/
totalinstances=0, /* plugin instances amount */
livetimers={}, /* live option timers */
oldie=(window.attachevent && !window.addeventlistener) ? 1 : 0, /* detect ie < 9 */
touchactive=false,touchable, /* global touch vars (for touch and pointer events) */
/* general plugin classes */
classes=[
"mcsb_dragger_ondrag","mcsb_scrolltools_ondrag","mcs_img_loaded","mcs_disabled","mcs_destroyed","mcs_no_scrollbar",
"mcs-autohide","mcs-dir-rtl","mcs_no_scrollbar_y","mcs_no_scrollbar_x","mcs_y_hidden","mcs_x_hidden","mcsb_draggercontainer",
"mcsb_buttonup","mcsb_buttondown","mcsb_buttonleft","mcsb_buttonright"
],
/*
----------------------------------------
methods
----------------------------------------
*/
methods={
/*
plugin initialization method
creates the scrollbar(s), plugin data object and options
----------------------------------------
*/
init:function(options){
var options=$.extend(true,{},defaults,options),
selector=_selector.call(this); /* validate selector */
/*
if live option is enabled, monitor for elements matching the current selector and
apply scrollbar(s) when found (now and in the future)
*/
if(options.live){
var liveselector=options.liveselector || this.selector || defaultselector, /* live selector(s) */
$liveselector=$(liveselector); /* live selector(s) as jquery object */
if(options.live==="off"){
/*
disable live if requested
usage: $(selector).mcustomscrollbar({live:"off"});
*/
removelivetimers(liveselector);
return;
}
livetimers[liveselector]=settimeout(function(){
/* call mcustomscrollbar fn on live selector(s) every half-second */
$liveselector.mcustomscrollbar(options);
if(options.live==="once" && $liveselector.length){
/* disable live after first invocation */
removelivetimers(liveselector);
}
},500);
}else{
removelivetimers(liveselector);
}
/* options backward compatibility (for versions < 3.0.0) and normalization */
options.setwidth=(options.set_width) ? options.set_width : options.setwidth;
options.setheight=(options.set_height) ? options.set_height : options.setheight;
options.axis=(options.horizontalscroll) ? "x" : _findaxis(options.axis);
options.scrollinertia=options.scrollinertia>0 && options.scrollinertia<17 ? 17 : options.scrollinertia;
if(typeof options.mousewheel!=="object" && options.mousewheel==true){ /* old school mousewheel option (non-object) */
options.mousewheel={enable:true,scrollamount:"auto",axis:"y",preventdefault:false,deltafactor:"auto",normalizedelta:false,invert:false}
}
options.mousewheel.scrollamount=!options.mousewheelpixels ? options.mousewheel.scrollamount : options.mousewheelpixels;
options.mousewheel.normalizedelta=!options.advanced.normalizemousewheeldelta ? options.mousewheel.normalizedelta : options.advanced.normalizemousewheeldelta;
options.scrollbuttons.scrolltype=_findscrollbuttonstype(options.scrollbuttons.scrolltype);
_theme(options); /* theme-specific options */
/* plugin constructor */
return $(selector).each(function(){
var $this=$(this);
if(!$this.data(pluginpfx)){ /* prevent multiple instantiations */
/* store options and create objects in jquery data */
$this.data(pluginpfx,{
idx:++totalinstances, /* instance index */
opt:options, /* options */
scrollratio:{y:null,x:null}, /* scrollbar to content ratio */
overflowed:null, /* overflowed axis */
contentreset:{y:null,x:null}, /* object to check when content resets */
bindevents:false, /* object to check if events are bound */
tweenrunning:false, /* object to check if tween is running */
sequential:{}, /* sequential scrolling object */
langdir:$this.css("direction"), /* detect/store direction (ltr or rtl) */
cboffsets:null, /* object to check whether callback offsets always trigger */
/*
object to check how scrolling events where last triggered
"internal" (default - triggered by this script), "external" (triggered by other scripts, e.g. via scrollto method)
usage: object.data("mcs").trigger
*/
trigger:null,
/*
object to check for changes in elements in order to call the update method automatically
*/
poll:{size:{o:0,n:0},img:{o:0,n:0},change:{o:0,n:0}}
});
var d=$this.data(pluginpfx),o=d.opt,
/* html data attributes */
htmldataaxis=$this.data("mcs-axis"),htmldatasbpos=$this.data("mcs-scrollbar-position"),htmldatatheme=$this.data("mcs-theme");
if(htmldataaxis){o.axis=htmldataaxis;} /* usage example: data-mcs-axis="y" */
if(htmldatasbpos){o.scrollbarposition=htmldatasbpos;} /* usage example: data-mcs-scrollbar-position="outside" */
if(htmldatatheme){ /* usage example: data-mcs-theme="minimal" */
o.theme=htmldatatheme;
_theme(o); /* theme-specific options */
}
_pluginmarkup.call(this); /* add plugin markup */
if(d && o.callbacks.oncreate && typeof o.callbacks.oncreate==="function"){o.callbacks.oncreate.call(this);} /* callbacks: oncreate */
$("#mcsb_"+d.idx+"_container img:not(."+classes[2]+")").addclass(classes[2]); /* flag loaded images */
methods.update.call(null,$this); /* call the update method */
}
});
},
/* ---------------------------------------- */
/*
plugin update method
updates content and scrollbar(s) values, events and status
----------------------------------------
usage: $(selector).mcustomscrollbar("update");
*/
update:function(el,cb){
var selector=el || _selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginpfx)){ /* check if plugin has initialized */
var d=$this.data(pluginpfx),o=d.opt,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")];
if(!mcsb_container.length){return;}
if(d.tweenrunning){_stop($this);} /* stop any running tweens while updating */
if(cb && d && o.callbacks.onbeforeupdate && typeof o.callbacks.onbeforeupdate==="function"){o.callbacks.onbeforeupdate.call(this);} /* callbacks: onbeforeupdate */
/* if element was disabled or destroyed, remove class(es) */
if($this.hasclass(classes[3])){$this.removeclass(classes[3]);}
if($this.hasclass(classes[4])){$this.removeclass(classes[4]);}
/* css flexbox fix, detect/set max-height */
mcustomscrollbox.css("max-height","none");
if(mcustomscrollbox.height()!==$this.height()){mcustomscrollbox.css("max-height",$this.height());}
_expandcontenthorizontally.call(this); /* expand content horizontally */
if(o.axis!=="y" && !o.advanced.autoexpandhorizontalscroll){
mcsb_container.css("width",_contentwidth(mcsb_container));
}
d.overflowed=_overflowed.call(this); /* determine if scrolling is required */
_scrollbarvisibility.call(this); /* show/hide scrollbar(s) */
/* auto-adjust scrollbar dragger length analogous to content */
if(o.autodraggerlength){_setdraggerlength.call(this);}
_scrollratio.call(this); /* calculate and store scrollbar to content ratio */
_bindevents.call(this); /* bind scrollbar events */
/* reset scrolling position and/or events */
var to=[math.abs(mcsb_container[0].offsettop),math.abs(mcsb_container[0].offsetleft)];
if(o.axis!=="x"){ /* y/yx axis */
if(!d.overflowed[0]){ /* y scrolling is not required */
_resetcontentposition.call(this); /* reset content position */
if(o.axis==="y"){
_unbindevents.call(this);
}else if(o.axis==="yx" && d.overflowed[1]){
_scrollto($this,to[1].tostring(),{dir:"x",dur:0,overwrite:"none"});
}
}else if(mcsb_dragger[0].height()>mcsb_dragger[0].parent().height()){
_resetcontentposition.call(this); /* reset content position */
}else{ /* y scrolling is required */
_scrollto($this,to[0].tostring(),{dir:"y",dur:0,overwrite:"none"});
d.contentreset.y=null;
}
}
if(o.axis!=="y"){ /* x/yx axis */
if(!d.overflowed[1]){ /* x scrolling is not required */
_resetcontentposition.call(this); /* reset content position */
if(o.axis==="x"){
_unbindevents.call(this);
}else if(o.axis==="yx" && d.overflowed[0]){
_scrollto($this,to[0].tostring(),{dir:"y",dur:0,overwrite:"none"});
}
}else if(mcsb_dragger[1].width()>mcsb_dragger[1].parent().width()){
_resetcontentposition.call(this); /* reset content position */
}else{ /* x scrolling is required */
_scrollto($this,to[1].tostring(),{dir:"x",dur:0,overwrite:"none"});
d.contentreset.x=null;
}
}
/* callbacks: onimageload, onselectorchange, onupdate */
if(cb && d){
if(cb===2 && o.callbacks.onimageload && typeof o.callbacks.onimageload==="function"){
o.callbacks.onimageload.call(this);
}else if(cb===3 && o.callbacks.onselectorchange && typeof o.callbacks.onselectorchange==="function"){
o.callbacks.onselectorchange.call(this);
}else if(o.callbacks.onupdate && typeof o.callbacks.onupdate==="function"){
o.callbacks.onupdate.call(this);
}
}
_autoupdate.call(this); /* initialize automatic updating (for dynamic content, fluid layouts etc.) */
}
});
},
/* ---------------------------------------- */
/*
plugin scrollto method
triggers a scrolling event to a specific value
----------------------------------------
usage: $(selector).mcustomscrollbar("scrollto",value,options);
*/
scrollto:function(val,options){
/* prevent silly things like $(selector).mcustomscrollbar("scrollto",undefined); */
if(typeof val=="undefined" || val==null){return;}
var selector=_selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginpfx)){ /* check if plugin has initialized */
var d=$this.data(pluginpfx),o=d.opt,
/* method default options */
methoddefaults={
trigger:"external", /* method is by default triggered externally (e.g. from other scripts) */
scrollinertia:o.scrollinertia, /* scrolling inertia (animation duration) */
scrolleasing:"mcseaseinout", /* animation easing */
movedragger:false, /* move dragger instead of content */
timeout:60, /* scroll-to delay */
callbacks:true, /* enable/disable callbacks */
onstart:true,
onupdate:true,
oncomplete:true
},
methodoptions=$.extend(true,{},methoddefaults,options),
to=_arr.call(this,val),dur=methodoptions.scrollinertia>0 && methodoptions.scrollinertia<17 ? 17 : methodoptions.scrollinertia;
/* translate yx values to actual scroll-to positions */
to[0]=_to.call(this,to[0],"y");
to[1]=_to.call(this,to[1],"x");
/*
check if scroll-to value moves the dragger instead of content.
only pixel values apply on dragger (e.g. 100, "100px", "-=100" etc.)
*/
if(methodoptions.movedragger){
to[0]*=d.scrollratio.y;
to[1]*=d.scrollratio.x;
}
methodoptions.dur=_istabhidden() ? 0 : dur; //skip animations if browser tab is hidden
settimeout(function(){
/* do the scrolling */
if(to[0]!==null && typeof to[0]!=="undefined" && o.axis!=="x" && d.overflowed[0]){ /* scroll y */
methodoptions.dir="y";
methodoptions.overwrite="all";
_scrollto($this,to[0].tostring(),methodoptions);
}
if(to[1]!==null && typeof to[1]!=="undefined" && o.axis!=="y" && d.overflowed[1]){ /* scroll x */
methodoptions.dir="x";
methodoptions.overwrite="none";
_scrollto($this,to[1].tostring(),methodoptions);
}
},methodoptions.timeout);
}
});
},
/* ---------------------------------------- */
/*
plugin stop method
stops scrolling animation
----------------------------------------
usage: $(selector).mcustomscrollbar("stop");
*/
stop:function(){
var selector=_selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginpfx)){ /* check if plugin has initialized */
_stop($this);
}
});
},
/* ---------------------------------------- */
/*
plugin disable method
temporarily disables the scrollbar(s)
----------------------------------------
usage: $(selector).mcustomscrollbar("disable",reset);
reset (boolean): resets content position to 0
*/
disable:function(r){
var selector=_selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginpfx)){ /* check if plugin has initialized */
var d=$this.data(pluginpfx);
_autoupdate.call(this,"remove"); /* remove automatic updating */
_unbindevents.call(this); /* unbind events */
if(r){_resetcontentposition.call(this);} /* reset content position */
_scrollbarvisibility.call(this,true); /* show/hide scrollbar(s) */
$this.addclass(classes[3]); /* add disable class */
}
});
},
/* ---------------------------------------- */
/*
plugin destroy method
completely removes the scrollbar(s) and returns the element to its original state
----------------------------------------
usage: $(selector).mcustomscrollbar("destroy");
*/
destroy:function(){
var selector=_selector.call(this); /* validate selector */
return $(selector).each(function(){
var $this=$(this);
if($this.data(pluginpfx)){ /* check if plugin has initialized */
var d=$this.data(pluginpfx),o=d.opt,
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
scrollbar=$(".mcsb_"+d.idx+"_scrollbar");
if(o.live){removelivetimers(o.liveselector || $(selector).selector);} /* remove live timers */
_autoupdate.call(this,"remove"); /* remove automatic updating */
_unbindevents.call(this); /* unbind events */
_resetcontentposition.call(this); /* reset content position */
$this.removedata(pluginpfx); /* remove plugin data object */
_delete(this,"mcs"); /* delete callbacks object */
/* remove plugin markup */
scrollbar.remove(); /* remove scrollbar(s) first (those can be either inside or outside plugin's inner wrapper) */
mcsb_container.find("img."+classes[2]).removeclass(classes[2]); /* remove loaded images flag */
mcustomscrollbox.replacewith(mcsb_container.contents()); /* replace plugin's inner wrapper with the original content */
/* remove plugin classes from the element and add destroy class */
$this.removeclass(pluginns+" _"+pluginpfx+"_"+d.idx+" "+classes[6]+" "+classes[7]+" "+classes[5]+" "+classes[3]).addclass(classes[4]);
}
});
}
/* ---------------------------------------- */
},
/*
----------------------------------------
functions
----------------------------------------
*/
/* validates selector (if selector is invalid or undefined uses the default one) */
_selector=function(){
return (typeof $(this)!=="object" || $(this).length<1) ? defaultselector : this;
},
/* -------------------- */
/* changes options according to theme */
_theme=function(obj){
var fixedsizescrollbarthemes=["rounded","rounded-dark","rounded-dots","rounded-dots-dark"],
nonexpandedscrollbarthemes=["rounded-dots","rounded-dots-dark","3d","3d-dark","3d-thick","3d-thick-dark","inset","inset-dark","inset-2","inset-2-dark","inset-3","inset-3-dark"],
disabledscrollbuttonsthemes=["minimal","minimal-dark"],
enabledautohidescrollbarthemes=["minimal","minimal-dark"],
scrollbarpositionoutsidethemes=["minimal","minimal-dark"];
obj.autodraggerlength=$.inarray(obj.theme,fixedsizescrollbarthemes) > -1 ? false : obj.autodraggerlength;
obj.autoexpandscrollbar=$.inarray(obj.theme,nonexpandedscrollbarthemes) > -1 ? false : obj.autoexpandscrollbar;
obj.scrollbuttons.enable=$.inarray(obj.theme,disabledscrollbuttonsthemes) > -1 ? false : obj.scrollbuttons.enable;
obj.autohidescrollbar=$.inarray(obj.theme,enabledautohidescrollbarthemes) > -1 ? true : obj.autohidescrollbar;
obj.scrollbarposition=$.inarray(obj.theme,scrollbarpositionoutsidethemes) > -1 ? "outside" : obj.scrollbarposition;
},
/* -------------------- */
/* live option timers removal */
removelivetimers=function(selector){
if(livetimers[selector]){
cleartimeout(livetimers[selector]);
_delete(livetimers,selector);
}
},
/* -------------------- */
/* normalizes axis option to valid values: "y", "x", "yx" */
_findaxis=function(val){
return (val==="yx" || val==="xy" || val==="auto") ? "yx" : (val==="x" || val==="horizontal") ? "x" : "y";
},
/* -------------------- */
/* normalizes scrollbuttons.scrolltype option to valid values: "stepless", "stepped" */
_findscrollbuttonstype=function(val){
return (val==="stepped" || val==="pixels" || val==="step" || val==="click") ? "stepped" : "stepless";
},
/* -------------------- */
/* generates plugin markup */
_pluginmarkup=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
expandclass=o.autoexpandscrollbar ? " "+classes[1]+"_expand" : "",
scrollbar=["
",""],
wrapperclass=o.axis==="yx" ? "mcsb_vertical_horizontal" : o.axis==="x" ? "mcsb_horizontal" : "mcsb_vertical",
scrollbars=o.axis==="yx" ? scrollbar[0]+scrollbar[1] : o.axis==="x" ? scrollbar[1] : scrollbar[0],
contentwrapper=o.axis==="yx" ? "" : "",
autohideclass=o.autohidescrollbar ? " "+classes[6] : "",
scrollbardirclass=(o.axis!=="x" && d.langdir==="rtl") ? " "+classes[7] : "";
if(o.setwidth){$this.css("width",o.setwidth);} /* set element width */
if(o.setheight){$this.css("height",o.setheight);} /* set element height */
o.setleft=(o.axis!=="y" && d.langdir==="rtl") ? "989999px" : o.setleft; /* adjust left position for rtl direction */
$this.addclass(pluginns+" _"+pluginpfx+"_"+d.idx+autohideclass+scrollbardirclass).wrapinner("");
var mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container");
if(o.axis!=="y" && !o.advanced.autoexpandhorizontalscroll){
mcsb_container.css("width",_contentwidth(mcsb_container));
}
if(o.scrollbarposition==="outside"){
if($this.css("position")==="static"){ /* requires elements with non-static position */
$this.css("position","relative");
}
$this.css("overflow","visible");
mcustomscrollbox.addclass("mcsb_outside").after(scrollbars);
}else{
mcustomscrollbox.addclass("mcsb_inside").append(scrollbars);
mcsb_container.wrap(contentwrapper);
}
_scrollbuttons.call(this); /* add scrollbar buttons */
/* minimum dragger length */
var mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")];
mcsb_dragger[0].css("min-height",mcsb_dragger[0].height());
mcsb_dragger[1].css("min-width",mcsb_dragger[1].width());
},
/* -------------------- */
/* calculates content width */
_contentwidth=function(el){
var val=[el[0].scrollwidth,math.max.apply(math,el.children().map(function(){return $(this).outerwidth(true);}).get())],w=el.parent().width();
return val[0]>w ? val[0] : val[1]>w ? val[1] : "100%";
},
/* -------------------- */
/* expands content horizontally */
_expandcontenthorizontally=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
mcsb_container=$("#mcsb_"+d.idx+"_container");
if(o.advanced.autoexpandhorizontalscroll && o.axis!=="y"){
/* calculate scrollwidth */
mcsb_container.css({"width":"auto","min-width":0,"overflow-x":"scroll"});
var w=math.ceil(mcsb_container[0].scrollwidth);
if(o.advanced.autoexpandhorizontalscroll===3 || (o.advanced.autoexpandhorizontalscroll!==2 && w>mcsb_container.parent().width())){
mcsb_container.css({"width":w,"min-width":"100%","overflow-x":"inherit"});
}else{
/*
wrap content with an infinite width div and set its position to absolute and width to auto.
setting width to auto before calculating the actual width is important!
we must let the browser set the width as browser zoom values are impossible to calculate.
*/
mcsb_container.css({"overflow-x":"inherit","position":"absolute"})
.wrap("")
.css({ /* set actual width, original position and un-wrap */
/*
get the exact width (with decimals) and then round-up.
using jquery outerwidth() will round the width value which will mess up with inner elements that have non-integer width
*/
"width":(math.ceil(mcsb_container[0].getboundingclientrect().right+0.4)-math.floor(mcsb_container[0].getboundingclientrect().left)),
"min-width":"100%",
"position":"relative"
}).unwrap();
}
}
},
/* -------------------- */
/* adds scrollbar buttons */
_scrollbuttons=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
mcsb_scrolltools=$(".mcsb_"+d.idx+"_scrollbar:first"),
tabindex=!_isnumeric(o.scrollbuttons.tabindex) ? "" : "tabindex='"+o.scrollbuttons.tabindex+"'",
btnhtml=[
"",
"",
"",
""
],
btn=[(o.axis==="x" ? btnhtml[2] : btnhtml[0]),(o.axis==="x" ? btnhtml[3] : btnhtml[1]),btnhtml[2],btnhtml[3]];
if(o.scrollbuttons.enable){
mcsb_scrolltools.prepend(btn[0]).append(btn[1]).next(".mcsb_scrolltools").prepend(btn[2]).append(btn[3]);
}
},
/* -------------------- */
/* auto-adjusts scrollbar dragger length */
_setdraggerlength=function(){
var $this=$(this),d=$this.data(pluginpfx),
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")],
ratio=[mcustomscrollbox.height()/mcsb_container.outerheight(false),mcustomscrollbox.width()/mcsb_container.outerwidth(false)],
l=[
parseint(mcsb_dragger[0].css("min-height")),math.round(ratio[0]*mcsb_dragger[0].parent().height()),
parseint(mcsb_dragger[1].css("min-width")),math.round(ratio[1]*mcsb_dragger[1].parent().width())
],
h=oldie && (l[1]contentheight){contentheight=h;}
if(w>contentwidth){contentwidth=w;}
return [contentheight>mcustomscrollbox.height(),contentwidth>mcustomscrollbox.width()];
},
/* -------------------- */
/* resets content position to 0 */
_resetcontentposition=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")];
_stop($this); /* stop any current scrolling before resetting */
if((o.axis!=="x" && !d.overflowed[0]) || (o.axis==="y" && d.overflowed[0])){ /* reset y */
mcsb_dragger[0].add(mcsb_container).css("top",0);
_scrollto($this,"_resety");
}
if((o.axis!=="y" && !d.overflowed[1]) || (o.axis==="x" && d.overflowed[1])){ /* reset x */
var cx=dx=0;
if(d.langdir==="rtl"){ /* adjust left position for rtl direction */
cx=mcustomscrollbox.width()-mcsb_container.outerwidth(false);
dx=math.abs(cx/d.scrollratio.x);
}
mcsb_container.css("left",cx);
mcsb_dragger[1].css("left",dx);
_scrollto($this,"_resetx");
}
},
/* -------------------- */
/* binds scrollbar events */
_bindevents=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt;
if(!d.bindevents){ /* check if events are already bound */
_draggable.call(this);
if(o.contenttouchscroll){_contentdraggable.call(this);}
_selectable.call(this);
if(o.mousewheel.enable){ /* bind mousewheel fn when plugin is available */
function _mwt(){
mousewheeltimeout=settimeout(function(){
if(!$.event.special.mousewheel){
_mwt();
}else{
cleartimeout(mousewheeltimeout);
_mousewheel.call($this[0]);
}
},100);
}
var mousewheeltimeout;
_mwt();
}
_draggerrail.call(this);
_wrapperscroll.call(this);
if(o.advanced.autoscrollonfocus){_focus.call(this);}
if(o.scrollbuttons.enable){_buttons.call(this);}
if(o.keyboard.enable){_keyboard.call(this);}
d.bindevents=true;
}
},
/* -------------------- */
/* unbinds scrollbar events */
_unbindevents=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
namespace=pluginpfx+"_"+d.idx,
sb=".mcsb_"+d.idx+"_scrollbar",
sel=$("#mcsb_"+d.idx+",#mcsb_"+d.idx+"_container,#mcsb_"+d.idx+"_container_wrapper,"+sb+" ."+classes[12]+",#mcsb_"+d.idx+"_dragger_vertical,#mcsb_"+d.idx+"_dragger_horizontal,"+sb+">a"),
mcsb_container=$("#mcsb_"+d.idx+"_container");
if(o.advanced.releasedraggableselectors){sel.add($(o.advanced.releasedraggableselectors));}
if(o.advanced.extradraggableselectors){sel.add($(o.advanced.extradraggableselectors));}
if(d.bindevents){ /* check if events are bound */
/* unbind namespaced events from document/selectors */
$(document).add($(!_canaccessiframe() || top.document)).unbind("."+namespace);
sel.each(function(){
$(this).unbind("."+namespace);
});
/* clear and delete timeouts/objects */
cleartimeout($this[0]._focustimeout); _delete($this[0],"_focustimeout");
cleartimeout(d.sequential.step); _delete(d.sequential,"step");
cleartimeout(mcsb_container[0].oncompletetimeout); _delete(mcsb_container[0],"oncompletetimeout");
d.bindevents=false;
}
},
/* -------------------- */
/* toggles scrollbar visibility */
_scrollbarvisibility=function(disabled){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
contentwrapper=$("#mcsb_"+d.idx+"_container_wrapper"),
content=contentwrapper.length ? contentwrapper : $("#mcsb_"+d.idx+"_container"),
scrollbar=[$("#mcsb_"+d.idx+"_scrollbar_vertical"),$("#mcsb_"+d.idx+"_scrollbar_horizontal")],
mcsb_dragger=[scrollbar[0].find(".mcsb_dragger"),scrollbar[1].find(".mcsb_dragger")];
if(o.axis!=="x"){
if(d.overflowed[0] && !disabled){
scrollbar[0].add(mcsb_dragger[0]).add(scrollbar[0].children("a")).css("display","block");
content.removeclass(classes[8]+" "+classes[10]);
}else{
if(o.alwaysshowscrollbar){
if(o.alwaysshowscrollbar!==2){mcsb_dragger[0].css("display","none");}
content.removeclass(classes[10]);
}else{
scrollbar[0].css("display","none");
content.addclass(classes[10]);
}
content.addclass(classes[8]);
}
}
if(o.axis!=="y"){
if(d.overflowed[1] && !disabled){
scrollbar[1].add(mcsb_dragger[1]).add(scrollbar[1].children("a")).css("display","block");
content.removeclass(classes[9]+" "+classes[11]);
}else{
if(o.alwaysshowscrollbar){
if(o.alwaysshowscrollbar!==2){mcsb_dragger[1].css("display","none");}
content.removeclass(classes[11]);
}else{
scrollbar[1].css("display","none");
content.addclass(classes[11]);
}
content.addclass(classes[9]);
}
}
if(!d.overflowed[0] && !d.overflowed[1]){
$this.addclass(classes[5]);
}else{
$this.removeclass(classes[5]);
}
},
/* -------------------- */
/* returns input coordinates of pointer, touch and mouse events (relative to document) */
_coordinates=function(e){
var t=e.type,o=e.target.ownerdocument!==document && frameelement!==null ? [$(frameelement).offset().top,$(frameelement).offset().left] : null,
io=_canaccessiframe() && e.target.ownerdocument!==top.document && frameelement!==null ? [$(e.view.frameelement).offset().top,$(e.view.frameelement).offset().left] : [0,0];
switch(t){
case "pointerdown": case "mspointerdown": case "pointermove": case "mspointermove": case "pointerup": case "mspointerup":
return o ? [e.originalevent.pagey-o[0]+io[0],e.originalevent.pagex-o[1]+io[1],false] : [e.originalevent.pagey,e.originalevent.pagex,false];
break;
case "touchstart": case "touchmove": case "touchend":
var touch=e.originalevent.touches[0] || e.originalevent.changedtouches[0],
touches=e.originalevent.touches.length || e.originalevent.changedtouches.length;
return e.target.ownerdocument!==document ? [touch.screeny,touch.screenx,touches>1] : [touch.pagey,touch.pagex,touches>1];
break;
default:
return o ? [e.pagey-o[0]+io[0],e.pagex-o[1]+io[1],false] : [e.pagey,e.pagex,false];
}
},
/* -------------------- */
/*
scrollbar drag events
scrolls content via scrollbar dragging
*/
_draggable=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
namespace=pluginpfx+"_"+d.idx,
draggerid=["mcsb_"+d.idx+"_dragger_vertical","mcsb_"+d.idx+"_dragger_horizontal"],
mcsb_container=$("#mcsb_"+d.idx+"_container"),
mcsb_dragger=$("#"+draggerid[0]+",#"+draggerid[1]),
draggable,dragy,dragx,
rds=o.advanced.releasedraggableselectors ? mcsb_dragger.add($(o.advanced.releasedraggableselectors)) : mcsb_dragger,
eds=o.advanced.extradraggableselectors ? $(!_canaccessiframe() || top.document).add($(o.advanced.extradraggableselectors)) : $(!_canaccessiframe() || top.document);
mcsb_dragger.bind("contextmenu."+namespace,function(e){
e.preventdefault(); //prevent right click
}).bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" mspointerdown."+namespace,function(e){
e.stopimmediatepropagation();
e.preventdefault();
if(!_mousebtnleft(e)){return;} /* left mouse button only */
touchactive=true;
if(oldie){document.onselectstart=function(){return false;}} /* disable text selection for ie < 9 */
_iframe.call(mcsb_container,false); /* enable scrollbar dragging over iframes by disabling their events */
_stop($this);
draggable=$(this);
var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left,
h=draggable.height()+offset.top,w=draggable.width()+offset.left;
if(y0 && x0){
dragy=y;
dragx=x;
}
_ondragclasses(draggable,"active",o.autoexpandscrollbar);
}).bind("touchmove."+namespace,function(e){
e.stopimmediatepropagation();
e.preventdefault();
var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
_drag(dragy,dragx,y,x);
});
$(document).add(eds).bind("mousemove."+namespace+" pointermove."+namespace+" mspointermove."+namespace,function(e){
if(draggable){
var offset=draggable.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left;
if(dragy===y && dragx===x){return;} /* has it really moved? */
_drag(dragy,dragx,y,x);
}
}).add(rds).bind("mouseup."+namespace+" touchend."+namespace+" pointerup."+namespace+" mspointerup."+namespace,function(e){
if(draggable){
_ondragclasses(draggable,"active",o.autoexpandscrollbar);
draggable=null;
}
touchactive=false;
if(oldie){document.onselectstart=null;} /* enable text selection for ie < 9 */
_iframe.call(mcsb_container,true); /* enable iframes events */
});
function _drag(dragy,dragx,y,x){
mcsb_container[0].idletimer=o.scrollinertia<233 ? 250 : 0;
if(draggable.attr("id")===draggerid[1]){
var dir="x",to=((draggable[0].offsetleft-dragx)+x)*d.scrollratio.x;
}else{
var dir="y",to=((draggable[0].offsettop-dragy)+y)*d.scrollratio.y;
}
_scrollto($this,to.tostring(),{dir:dir,drag:true});
}
},
/* -------------------- */
/*
touch swipe events
scrolls content via touch swipe
emulates the native touch-swipe scrolling with momentum found in ios, android and wp devices
*/
_contentdraggable=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
namespace=pluginpfx+"_"+d.idx,
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")],
draggable,dragy,dragx,touchstarty,touchstartx,touchmovey=[],touchmovex=[],starttime,runningtime,endtime,distance,speed,amount,
dura=0,durb,overwrite=o.axis==="yx" ? "none" : "all",touchintent=[],touchdrag,docdrag,
iframe=mcsb_container.find("iframe"),
events=[
"touchstart."+namespace+" pointerdown."+namespace+" mspointerdown."+namespace, //start
"touchmove."+namespace+" pointermove."+namespace+" mspointermove."+namespace, //move
"touchend."+namespace+" pointerup."+namespace+" mspointerup."+namespace //end
],
touchaction=document.body.style.touchaction!==undefined && document.body.style.touchaction!=="";
mcsb_container.bind(events[0],function(e){
_ontouchstart(e);
}).bind(events[1],function(e){
_ontouchmove(e);
});
mcustomscrollbox.bind(events[0],function(e){
_ontouchstart2(e);
}).bind(events[2],function(e){
_ontouchend(e);
});
if(iframe.length){
iframe.each(function(){
$(this).bind("load",function(){
/* bind events on accessible iframes */
if(_canaccessiframe(this)){
$(this.contentdocument || this.contentwindow.document).bind(events[0],function(e){
_ontouchstart(e);
_ontouchstart2(e);
}).bind(events[1],function(e){
_ontouchmove(e);
}).bind(events[2],function(e){
_ontouchend(e);
});
}
});
});
}
function _ontouchstart(e){
if(!_pointertouch(e) || touchactive || _coordinates(e)[2]){touchable=0; return;}
touchable=1; touchdrag=0; docdrag=0; draggable=1;
$this.removeclass("mcs_touch_action");
var offset=mcsb_container.offset();
dragy=_coordinates(e)[0]-offset.top;
dragx=_coordinates(e)[1]-offset.left;
touchintent=[_coordinates(e)[0],_coordinates(e)[1]];
}
function _ontouchmove(e){
if(!_pointertouch(e) || touchactive || _coordinates(e)[2]){return;}
if(!o.documenttouchscroll){e.preventdefault();}
e.stopimmediatepropagation();
if(docdrag && !touchdrag){return;}
if(draggable){
runningtime=_gettime();
var offset=mcustomscrollbox.offset(),y=_coordinates(e)[0]-offset.top,x=_coordinates(e)[1]-offset.left,
easing="mcslinearout";
touchmovey.push(y);
touchmovex.push(x);
touchintent[2]=math.abs(_coordinates(e)[0]-touchintent[0]); touchintent[3]=math.abs(_coordinates(e)[1]-touchintent[1]);
if(d.overflowed[0]){
var limit=mcsb_dragger[0].parent().height()-mcsb_dragger[0].height(),
prevent=((dragy-y)>0 && (y-dragy)>-(limit*d.scrollratio.y) && (touchintent[3]*20 && (x-dragx)>-(limitx*d.scrollratio.x) && (touchintent[2]*230){return;}
speed=1000/(endtime-starttime);
var easing="mcseaseout",slow=speed<2.5,
diff=slow ? [touchmovey[touchmovey.length-2],touchmovex[touchmovex.length-2]] : [0,0];
distance=slow ? [(y-diff[0]),(x-diff[1])] : [y-touchstarty,x-touchstartx];
var absdistance=[math.abs(distance[0]),math.abs(distance[1])];
speed=slow ? [math.abs(distance[0]/4),math.abs(distance[1]/4)] : [speed,speed];
var a=[
math.abs(mcsb_container[0].offsettop)-(distance[0]*_m((absdistance[0]/speed[0]),speed[0])),
math.abs(mcsb_container[0].offsetleft)-(distance[1]*_m((absdistance[1]/speed[1]),speed[1]))
];
amount=o.axis==="yx" ? [a[0],a[1]] : o.axis==="x" ? [null,a[1]] : [a[0],null];
durb=[(absdistance[0]*4)+o.scrollinertia,(absdistance[1]*4)+o.scrollinertia];
var md=parseint(o.contenttouchscroll) || 0; /* absolute minimum distance required */
amount[0]=absdistance[0]>md ? amount[0] : 0;
amount[1]=absdistance[1]>md ? amount[1] : 0;
if(d.overflowed[0]){_drag(amount[0],durb[0],easing,"y",overwrite,false);}
if(d.overflowed[1]){_drag(amount[1],durb[1],easing,"x",overwrite,false);}
}
function _m(ds,s){
var r=[s*1.5,s*2,s/1.5,s/2];
if(ds>90){
return s>4 ? r[0] : r[3];
}else if(ds>60){
return s>3 ? r[3] : r[2];
}else if(ds>30){
return s>8 ? r[1] : s>6 ? r[0] : s>4 ? s : r[2];
}else{
return s>8 ? s : r[3];
}
}
function _drag(amount,dur,easing,dir,overwrite,drag){
if(!amount){return;}
_scrollto($this,amount.tostring(),{dur:dur,scrolleasing:easing,dir:dir,overwrite:overwrite,drag:drag});
}
},
/* -------------------- */
/*
select text events
scrolls content when text is selected
*/
_selectable=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,seq=d.sequential,
namespace=pluginpfx+"_"+d.idx,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent(),
action;
mcsb_container.bind("mousedown."+namespace,function(e){
if(touchable){return;}
if(!action){action=1; touchactive=true;}
}).add(document).bind("mousemove."+namespace,function(e){
if(!touchable && action && _sel()){
var offset=mcsb_container.offset(),
y=_coordinates(e)[0]-offset.top+mcsb_container[0].offsettop,x=_coordinates(e)[1]-offset.left+mcsb_container[0].offsetleft;
if(y>0 && y0 && xwrapper.height()){
_seq("on",40);
}
}
if(o.axis!=="y" && d.overflowed[1]){
if(x<0){
_seq("on",37);
}else if(x>wrapper.width()){
_seq("on",39);
}
}
}
}
}).bind("mouseup."+namespace+" dragend."+namespace,function(e){
if(touchable){return;}
if(action){action=0; _seq("off",null);}
touchactive=false;
});
function _sel(){
return window.getselection ? window.getselection().tostring() :
document.selection && document.selection.type!="control" ? document.selection.createrange().text : 0;
}
function _seq(a,c,s){
seq.type=s && action ? "stepped" : "stepless";
seq.scrollamount=10;
_sequentialscroll($this,a,c,"mcslinearout",s ? 60 : null);
}
},
/* -------------------- */
/*
mouse wheel event
scrolls content via mouse-wheel
via mouse-wheel plugin (https://github.com/brandonaaron/jquery-mousewheel)
*/
_mousewheel=function(){
if(!$(this).data(pluginpfx)){return;} /* check if the scrollbar is ready to use mousewheel events (issue: #185) */
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
namespace=pluginpfx+"_"+d.idx,
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_dragger=[$("#mcsb_"+d.idx+"_dragger_vertical"),$("#mcsb_"+d.idx+"_dragger_horizontal")],
iframe=$("#mcsb_"+d.idx+"_container").find("iframe");
if(iframe.length){
iframe.each(function(){
$(this).bind("load",function(){
/* bind events on accessible iframes */
if(_canaccessiframe(this)){
$(this.contentdocument || this.contentwindow.document).bind("mousewheel."+namespace,function(e,delta){
_onmousewheel(e,delta);
});
}
});
});
}
mcustomscrollbox.bind("mousewheel."+namespace,function(e,delta){
_onmousewheel(e,delta);
});
function _onmousewheel(e,delta){
_stop($this);
if(_disablemousewheel($this,e.target)){return;} /* disables mouse-wheel when hovering specific elements */
var deltafactor=o.mousewheel.deltafactor!=="auto" ? parseint(o.mousewheel.deltafactor) : (oldie && e.deltafactor<100) ? 100 : e.deltafactor || 100,
dur=o.scrollinertia;
if(o.axis==="x" || o.mousewheel.axis==="x"){
var dir="x",
px=[math.round(deltafactor*d.scrollratio.x),parseint(o.mousewheel.scrollamount)],
amount=o.mousewheel.scrollamount!=="auto" ? px[1] : px[0]>=mcustomscrollbox.width() ? mcustomscrollbox.width()*0.9 : px[0],
contentpos=math.abs($("#mcsb_"+d.idx+"_container")[0].offsetleft),
draggerpos=mcsb_dragger[1][0].offsetleft,
limit=mcsb_dragger[1].parent().width()-mcsb_dragger[1].width(),
dlt=o.mousewheel.axis==="y" ? (e.deltay || delta) : e.deltax;
}else{
var dir="y",
px=[math.round(deltafactor*d.scrollratio.y),parseint(o.mousewheel.scrollamount)],
amount=o.mousewheel.scrollamount!=="auto" ? px[1] : px[0]>=mcustomscrollbox.height() ? mcustomscrollbox.height()*0.9 : px[0],
contentpos=math.abs($("#mcsb_"+d.idx+"_container")[0].offsettop),
draggerpos=mcsb_dragger[0][0].offsettop,
limit=mcsb_dragger[0].parent().height()-mcsb_dragger[0].height(),
dlt=e.deltay || delta;
}
if((dir==="y" && !d.overflowed[0]) || (dir==="x" && !d.overflowed[1])){return;}
if(o.mousewheel.invert || e.webkitdirectioninvertedfromdevice){dlt=-dlt;}
if(o.mousewheel.normalizedelta){dlt=dlt<0 ? -1 : 1;}
if((dlt>0 && draggerpos!==0) || (dlt<0 && draggerpos!==limit) || o.mousewheel.preventdefault){
e.stopimmediatepropagation();
e.preventdefault();
}
if(e.deltafactor<5 && !o.mousewheel.normalizedelta){
//very low deltafactor values mean some kind of delta acceleration (e.g. osx trackpad), so adjusting scrolling accordingly
amount=e.deltafactor; dur=17;
}
_scrollto($this,(contentpos-(dlt*amount)).tostring(),{dir:dir,dur:dur});
}
},
/* -------------------- */
/* checks if iframe can be accessed */
_canaccessiframecache=new object(),
_canaccessiframe=function(iframe){
var result=false,cachekey=false,html=null;
if(iframe===undefined){
cachekey="#empty";
}else if($(iframe).attr("id")!==undefined){
cachekey=$(iframe).attr("id");
}
if(cachekey!==false && _canaccessiframecache[cachekey]!==undefined){
return _canaccessiframecache[cachekey];
}
if(!iframe){
try{
var doc=top.document;
html=doc.body.innerhtml;
}catch(err){/* do nothing */}
result=(html!==null);
}else{
try{
var doc=iframe.contentdocument || iframe.contentwindow.document;
html=doc.body.innerhtml;
}catch(err){/* do nothing */}
result=(html!==null);
}
if(cachekey!==false){_canaccessiframecache[cachekey]=result;}
return result;
},
/* -------------------- */
/* switches iframe's pointer-events property (drag, mousewheel etc. over cross-domain iframes) */
_iframe=function(evt){
var el=this.find("iframe");
if(!el.length){return;} /* check if content contains iframes */
var val=!evt ? "none" : "auto";
el.css("pointer-events",val); /* for ie11, iframe's display property should not be "block" */
},
/* -------------------- */
/* disables mouse-wheel when hovering specific elements like select, datalist etc. */
_disablemousewheel=function(el,target){
var tag=target.nodename.tolowercase(),
tags=el.data(pluginpfx).opt.mousewheel.disableover,
/* elements that require focus */
focustags=["select","textarea"];
return $.inarray(tag,tags) > -1 && !($.inarray(tag,focustags) > -1 && !$(target).is(":focus"));
},
/* -------------------- */
/*
dragger rail click event
scrolls content via dragger rail
*/
_draggerrail=function(){
var $this=$(this),d=$this.data(pluginpfx),
namespace=pluginpfx+"_"+d.idx,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent(),
mcsb_draggercontainer=$(".mcsb_"+d.idx+"_scrollbar ."+classes[12]),
clickable;
mcsb_draggercontainer.bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" mspointerdown."+namespace,function(e){
touchactive=true;
if(!$(e.target).hasclass("mcsb_dragger")){clickable=1;}
}).bind("touchend."+namespace+" pointerup."+namespace+" mspointerup."+namespace,function(e){
touchactive=false;
}).bind("click."+namespace,function(e){
if(!clickable){return;}
clickable=0;
if($(e.target).hasclass(classes[12]) || $(e.target).hasclass("mcsb_draggerrail")){
_stop($this);
var el=$(this),mcsb_dragger=el.find(".mcsb_dragger");
if(el.parent(".mcsb_scrolltools_horizontal").length>0){
if(!d.overflowed[1]){return;}
var dir="x",
clickdir=e.pagex>mcsb_dragger.offset().left ? -1 : 1,
to=math.abs(mcsb_container[0].offsetleft)-(clickdir*(wrapper.width()*0.9));
}else{
if(!d.overflowed[0]){return;}
var dir="y",
clickdir=e.pagey>mcsb_dragger.offset().top ? -1 : 1,
to=math.abs(mcsb_container[0].offsettop)-(clickdir*(wrapper.height()*0.9));
}
_scrollto($this,to.tostring(),{dir:dir,scrolleasing:"mcseaseinout"});
}
});
},
/* -------------------- */
/*
focus event
scrolls content via element focus (e.g. clicking an input, pressing tab key etc.)
*/
_focus=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
namespace=pluginpfx+"_"+d.idx,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent();
mcsb_container.bind("focusin."+namespace,function(e){
var el=$(document.activeelement),
nested=mcsb_container.find(".mcustomscrollbox").length,
dur=0;
if(!el.is(o.advanced.autoscrollonfocus)){return;}
_stop($this);
cleartimeout($this[0]._focustimeout);
$this[0]._focustimer=nested ? (dur+17)*nested : 0;
$this[0]._focustimeout=settimeout(function(){
var to=[_childpos(el)[0],_childpos(el)[1]],
contentpos=[mcsb_container[0].offsettop,mcsb_container[0].offsetleft],
isvisible=[
(contentpos[0]+to[0]>=0 && contentpos[0]+to[0]=0 && contentpos[0]+to[1]a");
btn.bind("contextmenu."+namespace,function(e){
e.preventdefault(); //prevent right click
}).bind("mousedown."+namespace+" touchstart."+namespace+" pointerdown."+namespace+" mspointerdown."+namespace+" mouseup."+namespace+" touchend."+namespace+" pointerup."+namespace+" mspointerup."+namespace+" mouseout."+namespace+" pointerout."+namespace+" mspointerout."+namespace+" click."+namespace,function(e){
e.preventdefault();
if(!_mousebtnleft(e)){return;} /* left mouse button only */
var btnclass=$(this).attr("class");
seq.type=o.scrollbuttons.scrolltype;
switch(e.type){
case "mousedown": case "touchstart": case "pointerdown": case "mspointerdown":
if(seq.type==="stepped"){return;}
touchactive=true;
d.tweenrunning=false;
_seq("on",btnclass);
break;
case "mouseup": case "touchend": case "pointerup": case "mspointerup":
case "mouseout": case "pointerout": case "mspointerout":
if(seq.type==="stepped"){return;}
touchactive=false;
if(seq.dir){_seq("off",btnclass);}
break;
case "click":
if(seq.type!=="stepped" || d.tweenrunning){return;}
_seq("on",btnclass);
break;
}
function _seq(a,c){
seq.scrollamount=o.scrollbuttons.scrollamount;
_sequentialscroll($this,a,c);
}
});
},
/* -------------------- */
/*
keyboard events
scrolls content via keyboard
keys: up arrow, down arrow, left arrow, right arrow, pgup, pgdn, home, end
*/
_keyboard=function(){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,seq=d.sequential,
namespace=pluginpfx+"_"+d.idx,
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent(),
editables="input,textarea,select,datalist,keygen,[contenteditable='true']",
iframe=mcsb_container.find("iframe"),
events=["blur."+namespace+" keydown."+namespace+" keyup."+namespace];
if(iframe.length){
iframe.each(function(){
$(this).bind("load",function(){
/* bind events on accessible iframes */
if(_canaccessiframe(this)){
$(this.contentdocument || this.contentwindow.document).bind(events[0],function(e){
_onkeyboard(e);
});
}
});
});
}
mcustomscrollbox.attr("tabindex","0").bind(events[0],function(e){
_onkeyboard(e);
});
function _onkeyboard(e){
switch(e.type){
case "blur":
if(d.tweenrunning && seq.dir){_seq("off",null);}
break;
case "keydown": case "keyup":
var code=e.keycode ? e.keycode : e.which,action="on";
if((o.axis!=="x" && (code===38 || code===40)) || (o.axis!=="y" && (code===37 || code===39))){
/* up (38), down (40), left (37), right (39) arrows */
if(((code===38 || code===40) && !d.overflowed[0]) || ((code===37 || code===39) && !d.overflowed[1])){return;}
if(e.type==="keyup"){action="off";}
if(!$(document.activeelement).is(editables)){
e.preventdefault();
e.stopimmediatepropagation();
_seq(action,code);
}
}else if(code===33 || code===34){
/* pgup (33), pgdn (34) */
if(d.overflowed[0] || d.overflowed[1]){
e.preventdefault();
e.stopimmediatepropagation();
}
if(e.type==="keyup"){
_stop($this);
var keyboarddir=code===34 ? -1 : 1;
if(o.axis==="x" || (o.axis==="yx" && d.overflowed[1] && !d.overflowed[0])){
var dir="x",to=math.abs(mcsb_container[0].offsetleft)-(keyboarddir*(wrapper.width()*0.9));
}else{
var dir="y",to=math.abs(mcsb_container[0].offsettop)-(keyboarddir*(wrapper.height()*0.9));
}
_scrollto($this,to.tostring(),{dir:dir,scrolleasing:"mcseaseinout"});
}
}else if(code===35 || code===36){
/* end (35), home (36) */
if(!$(document.activeelement).is(editables)){
if(d.overflowed[0] || d.overflowed[1]){
e.preventdefault();
e.stopimmediatepropagation();
}
if(e.type==="keyup"){
if(o.axis==="x" || (o.axis==="yx" && d.overflowed[1] && !d.overflowed[0])){
var dir="x",to=code===35 ? math.abs(wrapper.width()-mcsb_container.outerwidth(false)) : 0;
}else{
var dir="y",to=code===35 ? math.abs(wrapper.height()-mcsb_container.outerheight(false)) : 0;
}
_scrollto($this,to.tostring(),{dir:dir,scrolleasing:"mcseaseinout"});
}
}
}
break;
}
function _seq(a,c){
seq.type=o.keyboard.scrolltype;
seq.scrollamount=o.keyboard.scrollamount;
if(seq.type==="stepped" && d.tweenrunning){return;}
_sequentialscroll($this,a,c);
}
}
},
/* -------------------- */
/* scrolls content sequentially (used when scrolling via buttons, keyboard arrows etc.) */
_sequentialscroll=function(el,action,trigger,e,s){
var d=el.data(pluginpfx),o=d.opt,seq=d.sequential,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
once=seq.type==="stepped" ? true : false,
steplessspeed=o.scrollinertia < 26 ? 26 : o.scrollinertia, /* 26/1.5=17 */
steppedspeed=o.scrollinertia < 1 ? 17 : o.scrollinertia;
switch(action){
case "on":
seq.dir=[
(trigger===classes[16] || trigger===classes[15] || trigger===39 || trigger===37 ? "x" : "y"),
(trigger===classes[13] || trigger===classes[15] || trigger===38 || trigger===37 ? -1 : 1)
];
_stop(el);
if(_isnumeric(trigger) && seq.type==="stepped"){return;}
_on(once);
break;
case "off":
_off();
if(once || (d.tweenrunning && seq.dir)){
_on(true);
}
break;
}
/* starts sequence */
function _on(once){
if(o.snapamount){seq.scrollamount=!(o.snapamount instanceof array) ? o.snapamount : seq.dir[0]==="x" ? o.snapamount[1] : o.snapamount[0];} /* scrolling snapping */
var c=seq.type!=="stepped", /* continuous scrolling */
t=s ? s : !once ? 1000/60 : c ? steplessspeed/1.5 : steppedspeed, /* timer */
m=!once ? 2.5 : c ? 7.5 : 40, /* multiplier */
contentpos=[math.abs(mcsb_container[0].offsettop),math.abs(mcsb_container[0].offsetleft)],
ratio=[d.scrollratio.y>10 ? 10 : d.scrollratio.y,d.scrollratio.x>10 ? 10 : d.scrollratio.x],
amount=seq.dir[0]==="x" ? contentpos[1]+(seq.dir[1]*(ratio[1]*m)) : contentpos[0]+(seq.dir[1]*(ratio[0]*m)),
px=seq.dir[0]==="x" ? contentpos[1]+(seq.dir[1]*parseint(seq.scrollamount)) : contentpos[0]+(seq.dir[1]*parseint(seq.scrollamount)),
to=seq.scrollamount!=="auto" ? px : amount,
easing=e ? e : !once ? "mcslinear" : c ? "mcslinearout" : "mcseaseinout",
oncomplete=!once ? false : true;
if(once && t<17){
to=seq.dir[0]==="x" ? contentpos[1] : contentpos[0];
}
_scrollto(el,to.tostring(),{dir:seq.dir[0],scrolleasing:easing,dur:t,oncomplete:oncomplete});
if(once){
seq.dir=false;
return;
}
cleartimeout(seq.step);
seq.step=settimeout(function(){
_on();
},t);
}
/* stops sequence */
function _off(){
cleartimeout(seq.step);
_delete(seq,"step");
_stop(el);
}
},
/* -------------------- */
/* returns a yx array from value */
_arr=function(val){
var o=$(this).data(pluginpfx).opt,vals=[];
if(typeof val==="function"){val=val();} /* check if the value is a single anonymous function */
/* check if value is object or array, its length and create an array with yx values */
if(!(val instanceof array)){ /* object value (e.g. {y:"100",x:"100"}, 100 etc.) */
vals[0]=val.y ? val.y : val.x || o.axis==="x" ? null : val;
vals[1]=val.x ? val.x : val.y || o.axis==="y" ? null : val;
}else{ /* array value (e.g. [100,100]) */
vals=val.length>1 ? [val[0],val[1]] : o.axis==="x" ? [null,val[0]] : [val[0],null];
}
/* check if array values are anonymous functions */
if(typeof vals[0]==="function"){vals[0]=vals[0]();}
if(typeof vals[1]==="function"){vals[1]=vals[1]();}
return vals;
},
/* -------------------- */
/* translates values (e.g. "top", 100, "100px", "#id") to actual scroll-to positions */
_to=function(val,dir){
if(val==null || typeof val=="undefined"){return;}
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent(),
t=typeof val;
if(!dir){dir=o.axis==="x" ? "x" : "y";}
var contentlength=dir==="x" ? mcsb_container.outerwidth(false)-wrapper.width() : mcsb_container.outerheight(false)-wrapper.height(),
contentpos=dir==="x" ? mcsb_container[0].offsetleft : mcsb_container[0].offsettop,
cssprop=dir==="x" ? "left" : "top";
switch(t){
case "function": /* this currently is not used. consider removing it */
return val();
break;
case "object": /* js/jquery object */
var obj=val.jquery ? val : $(val);
if(!obj.length){return;}
return dir==="x" ? _childpos(obj)[1] : _childpos(obj)[0];
break;
case "string": case "number":
if(_isnumeric(val)){ /* numeric value */
return math.abs(val);
}else if(val.indexof("%")!==-1){ /* percentage value */
return math.abs(contentlength*parseint(val)/100);
}else if(val.indexof("-=")!==-1){ /* decrease value */
return math.abs(contentpos-parseint(val.split("-=")[1]));
}else if(val.indexof("+=")!==-1){ /* inrease value */
var p=(contentpos+parseint(val.split("+=")[1]));
return p>=0 ? 0 : math.abs(p);
}else if(val.indexof("px")!==-1 && _isnumeric(val.split("px")[0])){ /* pixels string value (e.g. "100px") */
return math.abs(val.split("px")[0]);
}else{
if(val==="top" || val==="left"){ /* special strings */
return 0;
}else if(val==="bottom"){
return math.abs(wrapper.height()-mcsb_container.outerheight(false));
}else if(val==="right"){
return math.abs(wrapper.width()-mcsb_container.outerwidth(false));
}else if(val==="first" || val==="last"){
var obj=mcsb_container.find(":"+val);
return dir==="x" ? _childpos(obj)[1] : _childpos(obj)[0];
}else{
if($(val).length){ /* jquery selector */
return dir==="x" ? _childpos($(val))[1] : _childpos($(val))[0];
}else{ /* other values (e.g. "100em") */
mcsb_container.css(cssprop,val);
methods.update.call(null,$this[0]);
return;
}
}
}
break;
}
},
/* -------------------- */
/* calls the update method automatically */
_autoupdate=function(rem){
var $this=$(this),d=$this.data(pluginpfx),o=d.opt,
mcsb_container=$("#mcsb_"+d.idx+"_container");
if(rem){
/*
removes autoupdate timer
usage: _autoupdate.call(this,"remove");
*/
cleartimeout(mcsb_container[0].autoupdate);
_delete(mcsb_container[0],"autoupdate");
return;
}
upd();
function upd(){
cleartimeout(mcsb_container[0].autoupdate);
if($this.parents("html").length===0){
/* check element in dom tree */
$this=null;
return;
}
mcsb_container[0].autoupdate=settimeout(function(){
/* update on specific selector(s) length and size change */
if(o.advanced.updateonselectorchange){
d.poll.change.n=sizessum();
if(d.poll.change.n!==d.poll.change.o){
d.poll.change.o=d.poll.change.n;
doupd(3);
return;
}
}
/* update on main element and scrollbar size changes */
if(o.advanced.updateoncontentresize){
d.poll.size.n=$this[0].scrollheight+$this[0].scrollwidth+mcsb_container[0].offsetheight+$this[0].offsetheight+$this[0].offsetwidth;
if(d.poll.size.n!==d.poll.size.o){
d.poll.size.o=d.poll.size.n;
doupd(1);
return;
}
}
/* update on image load */
if(o.advanced.updateonimageload){
if(!(o.advanced.updateonimageload==="auto" && o.axis==="y")){ //by default, it doesn't run on vertical content
d.poll.img.n=mcsb_container.find("img").length;
if(d.poll.img.n!==d.poll.img.o){
d.poll.img.o=d.poll.img.n;
mcsb_container.find("img").each(function(){
imgloader(this);
});
return;
}
}
}
if(o.advanced.updateonselectorchange || o.advanced.updateoncontentresize || o.advanced.updateonimageload){upd();}
},o.advanced.autoupdatetimeout);
}
/* a tiny image loader */
function imgloader(el){
if($(el).hasclass(classes[2])){doupd(); return;}
var img=new image();
function createdelegate(contextobject,delegatemethod){
return function(){return delegatemethod.apply(contextobject,arguments);}
}
function imgonload(){
this.onload=null;
$(el).addclass(classes[2]);
doupd(2);
}
img.onload=createdelegate(img,imgonload);
img.src=el.src;
}
/* returns the total height and width sum of all elements matching the selector */
function sizessum(){
if(o.advanced.updateonselectorchange===true){o.advanced.updateonselectorchange="*";}
var total=0,sel=mcsb_container.find(o.advanced.updateonselectorchange);
if(o.advanced.updateonselectorchange && sel.length>0){sel.each(function(){total+=this.offsetheight+this.offsetwidth;});}
return total;
}
/* calls the update method */
function doupd(cb){
cleartimeout(mcsb_container[0].autoupdate);
methods.update.call(null,$this[0],cb);
}
},
/* -------------------- */
/* snaps scrolling to a multiple of a pixels number */
_snapamount=function(to,amount,offset){
return (math.round(to/amount)*amount-offset);
},
/* -------------------- */
/* stops content and scrollbar animations */
_stop=function(el){
var d=el.data(pluginpfx),
sel=$("#mcsb_"+d.idx+"_container,#mcsb_"+d.idx+"_container_wrapper,#mcsb_"+d.idx+"_dragger_vertical,#mcsb_"+d.idx+"_dragger_horizontal");
sel.each(function(){
_stoptween.call(this);
});
},
/* -------------------- */
/*
animates content
this is where the actual scrolling happens
*/
_scrollto=function(el,to,options){
var d=el.data(pluginpfx),o=d.opt,
defaults={
trigger:"internal",
dir:"y",
scrolleasing:"mcseaseout",
drag:false,
dur:o.scrollinertia,
overwrite:"all",
callbacks:true,
onstart:true,
onupdate:true,
oncomplete:true
},
options=$.extend(defaults,options),
dur=[options.dur,(options.drag ? 0 : options.dur)],
mcustomscrollbox=$("#mcsb_"+d.idx),
mcsb_container=$("#mcsb_"+d.idx+"_container"),
wrapper=mcsb_container.parent(),
totalscrolloffsets=o.callbacks.ontotalscrolloffset ? _arr.call(el,o.callbacks.ontotalscrolloffset) : [0,0],
totalscrollbackoffsets=o.callbacks.ontotalscrollbackoffset ? _arr.call(el,o.callbacks.ontotalscrollbackoffset) : [0,0];
d.trigger=options.trigger;
if(wrapper.scrolltop()!==0 || wrapper.scrollleft()!==0){ /* always reset scrolltop/left */
$(".mcsb_"+d.idx+"_scrollbar").css("visibility","visible");
wrapper.scrolltop(0).scrollleft(0);
}
if(to==="_resety" && !d.contentreset.y){
/* callbacks: onoverflowynone */
if(_cb("onoverflowynone")){o.callbacks.onoverflowynone.call(el[0]);}
d.contentreset.y=1;
}
if(to==="_resetx" && !d.contentreset.x){
/* callbacks: onoverflowxnone */
if(_cb("onoverflowxnone")){o.callbacks.onoverflowxnone.call(el[0]);}
d.contentreset.x=1;
}
if(to==="_resety" || to==="_resetx"){return;}
if((d.contentreset.y || !el[0].mcs) && d.overflowed[0]){
/* callbacks: onoverflowy */
if(_cb("onoverflowy")){o.callbacks.onoverflowy.call(el[0]);}
d.contentreset.x=null;
}
if((d.contentreset.x || !el[0].mcs) && d.overflowed[1]){
/* callbacks: onoverflowx */
if(_cb("onoverflowx")){o.callbacks.onoverflowx.call(el[0]);}
d.contentreset.x=null;
}
if(o.snapamount){ /* scrolling snapping */
var snapamount=!(o.snapamount instanceof array) ? o.snapamount : options.dir==="x" ? o.snapamount[1] : o.snapamount[0];
to=_snapamount(to,snapamount,o.snapoffset);
}
switch(options.dir){
case "x":
var mcsb_dragger=$("#mcsb_"+d.idx+"_dragger_horizontal"),
property="left",
contentpos=mcsb_container[0].offsetleft,
limit=[
mcustomscrollbox.width()-mcsb_container.outerwidth(false),
mcsb_dragger.parent().width()-mcsb_dragger.width()
],
scrollto=[to,to===0 ? 0 : (to/d.scrollratio.x)],
tso=totalscrolloffsets[1],
tsbo=totalscrollbackoffsets[1],
totalscrolloffset=tso>0 ? tso/d.scrollratio.x : 0,
totalscrollbackoffset=tsbo>0 ? tsbo/d.scrollratio.x : 0;
break;
case "y":
var mcsb_dragger=$("#mcsb_"+d.idx+"_dragger_vertical"),
property="top",
contentpos=mcsb_container[0].offsettop,
limit=[
mcustomscrollbox.height()-mcsb_container.outerheight(false),
mcsb_dragger.parent().height()-mcsb_dragger.height()
],
scrollto=[to,to===0 ? 0 : (to/d.scrollratio.y)],
tso=totalscrolloffsets[0],
tsbo=totalscrollbackoffsets[0],
totalscrolloffset=tso>0 ? tso/d.scrollratio.y : 0,
totalscrollbackoffset=tsbo>0 ? tsbo/d.scrollratio.y : 0;
break;
}
if(scrollto[1]<0 || (scrollto[0]===0 && scrollto[1]===0)){
scrollto=[0,0];
}else if(scrollto[1]>=limit[1]){
scrollto=[limit[0],limit[1]];
}else{
scrollto[0]=-scrollto[0];
}
if(!el[0].mcs){
_mcs(); /* init mcs object (once) to make it available before callbacks */
if(_cb("oninit")){o.callbacks.oninit.call(el[0]);} /* callbacks: oninit */
}
cleartimeout(mcsb_container[0].oncompletetimeout);
_tweento(mcsb_dragger[0],property,math.round(scrollto[1]),dur[1],options.scrolleasing);
if(!d.tweenrunning && ((contentpos===0 && scrollto[0]>=0) || (contentpos===limit[0] && scrollto[0]<=limit[0]))){return;}
_tweento(mcsb_container[0],property,math.round(scrollto[0]),dur[0],options.scrolleasing,options.overwrite,{
onstart:function(){
if(options.callbacks && options.onstart && !d.tweenrunning){
/* callbacks: onscrollstart */
if(_cb("onscrollstart")){_mcs(); o.callbacks.onscrollstart.call(el[0]);}
d.tweenrunning=true;
_ondragclasses(mcsb_dragger);
d.cboffsets=_cboffsets();
}
},onupdate:function(){
if(options.callbacks && options.onupdate){
/* callbacks: whilescrolling */
if(_cb("whilescrolling")){_mcs(); o.callbacks.whilescrolling.call(el[0]);}
}
},oncomplete:function(){
if(options.callbacks && options.oncomplete){
if(o.axis==="yx"){cleartimeout(mcsb_container[0].oncompletetimeout);}
var t=mcsb_container[0].idletimer || 0;
mcsb_container[0].oncompletetimeout=settimeout(function(){
/* callbacks: onscroll, ontotalscroll, ontotalscrollback */
if(_cb("onscroll")){_mcs(); o.callbacks.onscroll.call(el[0]);}
if(_cb("ontotalscroll") && scrollto[1]>=limit[1]-totalscrolloffset && d.cboffsets[0]){_mcs(); o.callbacks.ontotalscroll.call(el[0]);}
if(_cb("ontotalscrollback") && scrollto[1]<=totalscrollbackoffset && d.cboffsets[1]){_mcs(); o.callbacks.ontotalscrollback.call(el[0]);}
d.tweenrunning=false;
mcsb_container[0].idletimer=0;
_ondragclasses(mcsb_dragger,"hide");
},t);
}
}
});
/* checks if callback function exists */
function _cb(cb){
return d && o.callbacks[cb] && typeof o.callbacks[cb]==="function";
}
/* checks whether callback offsets always trigger */
function _cboffsets(){
return [o.callbacks.alwaystriggeroffsets || contentpos>=limit[0]+tso,o.callbacks.alwaystriggeroffsets || contentpos<=-tsbo];
}
/*
populates object with useful values for the user
values:
content: this.mcs.content
content top position: this.mcs.top
content left position: this.mcs.left
dragger top position: this.mcs.draggertop
dragger left position: this.mcs.draggerleft
scrolling y percentage: this.mcs.toppct
scrolling x percentage: this.mcs.leftpct
scrolling direction: this.mcs.direction
*/
function _mcs(){
var cp=[mcsb_container[0].offsettop,mcsb_container[0].offsetleft], /* content position */
dp=[mcsb_dragger[0].offsettop,mcsb_dragger[0].offsetleft], /* dragger position */
cl=[mcsb_container.outerheight(false),mcsb_container.outerwidth(false)], /* content length */
pl=[mcustomscrollbox.height(),mcustomscrollbox.width()]; /* content parent length */
el[0].mcs={
content:mcsb_container, /* original content wrapper as jquery object */
top:cp[0],left:cp[1],draggertop:dp[0],draggerleft:dp[1],
toppct:math.round((100*math.abs(cp[0]))/(math.abs(cl[0])-pl[0])),leftpct:math.round((100*math.abs(cp[1]))/(math.abs(cl[1])-pl[1])),
direction:options.dir
};
/*
this refers to the original element containing the scrollbar(s)
usage: this.mcs.top, this.mcs.leftpct etc.
*/
}
},
/* -------------------- */
/*
custom javascript animation tween
lighter and faster than jquery animate() and css transitions
animates top/left properties and includes easings
*/
_tweento=function(el,prop,to,duration,easing,overwrite,callbacks){
if(!el._mtween){el._mtween={top:{},left:{}};}
var callbacks=callbacks || {},
onstart=callbacks.onstart || function(){},onupdate=callbacks.onupdate || function(){},oncomplete=callbacks.oncomplete || function(){},
starttime=_gettime(),_delay,progress=0,from=el.offsettop,elstyle=el.style,_request,tobj=el._mtween[prop];
if(prop==="left"){from=el.offsetleft;}
var diff=to-from;
tobj.stop=0;
if(overwrite!=="none"){_canceltween();}
_starttween();
function _step(){
if(tobj.stop){return;}
if(!progress){onstart.call();}
progress=_gettime()-starttime;
_tween();
if(progress>=tobj.time){
tobj.time=(progress>tobj.time) ? progress+_delay-(progress-tobj.time) : progress+_delay-1;
if(tobj.time