(function($){var t=$.tools.scrollable;t.navigator={conf:{navi:'.navi',naviItem:null,activeClass:'active',indexed:false,idPrefix:null,history:false}};function find(root,query){var el=$(query);return el.length<2?el:root.parent().find(query);}
$.fn.navigator=function(conf){if(typeof conf=='string'){conf={navi:conf};}
conf=$.extend({},t.navigator.conf,conf);var ret;this.each(function(){var api=$(this).data("scrollable"),navi=conf.navi.jquery?conf.navi:find(api.getRoot(),conf.navi),buttons=api.getNaviButtons(),cls=conf.activeClass,history=conf.history&&$.fn.history;if(api){ret=api;}
api.getNaviButtons=function(){return buttons.add(navi);};function doClick(el,i,e){api.seekTo(i);if(history){if(location.hash){location.hash=el.attr("href").replace("#","");}}else{return e.preventDefault();}}
function els(){return navi.find(conf.naviItem||'> *');}
function addItem(i){var item=$("<"+(conf.naviItem||'a')+"/>").click(function(e){doClick($(this),i,e);}).attr("href","#"+i);if(i===0){item.addClass(cls);}
if(conf.indexed){item.text(i+1);}
if(conf.idPrefix){item.attr("id",conf.idPrefix+i);}
return item.appendTo(navi);}
if(els().length){els().each(function(i){$(this).click(function(e){doClick($(this),i,e);});});}else{$.each(api.getItems(),function(i){addItem(i);});}
api.onBeforeSeek(function(e,index){setTimeout(function(){if(!e.isDefaultPrevented()){var el=els().eq(index);if(!e.isDefaultPrevented()&&el.length){els().removeClass(cls).eq(index).addClass(cls);}}},1);});function doHistory(evt,hash){var el=els().eq(hash.replace("#",""));if(!el.length){el=els().filter("[href="+hash+"]");}
el.click();}
api.onAddItem(function(e,item){item=addItem(api.getItems().index(item));if(history){item.history(doHistory);}});if(history){els().history(doHistory);}});return conf.api?ret:this;};})(jQuery);
