/*
silkScroll.js
	a smoothscroll type plugin with additional history management features - based on the mootools smoothscroll.js plugin

Author:
	Adam Fisher
	DPAC 2008

Licence:
	MIT style
*/


var SilkScroll=new Class({Implements:Options,options:{enableBack:true,historyInterval:500,scroll:{}},initialize:function(options,element){this.setOptions(options);element=$(element)||document;var doc=element.getDocument(),win=element.getWindow();this.links=(this.options.links)?$$(this.options.links):$$(doc.links);var location=win.location.href.match(/^[^#]*/)[0]+'#';var anchors=$$(doc.anchors);this.scrollPoints={};var scrollComplete={onComplete:function(){if(this.options.enableBack){if(this.anchor){win.location.hash='_'+this.anchor;}
this.timer=this.hashCheck.periodical(this.options.historyInterval,this);}}.bind(this)}
this.scroll=new Fx.Scroll(element,$merge(this.options.scroll,scrollComplete));anchors.each(function(anchor){this.scrollPoints[anchor.name]=anchor.getPosition(window);}.bind(this));this.links.each(function(link){if(link.href.indexOf(location)!=0||link.href==location)return;var target=link.href.substr(location.length);link.addEvent('click',function(e){new Event(e).stop();this.anchor=target;$clear(this.timer);this.move(target);}.bind(this));}.bind(this));if(window.location.hash&&this.scrollPoints[window.location.hash.substr(2)]){this.move(window.location.hash.substr(2));}},hashCheck:function(){var current=window.location.hash.match(/_(.+)/);if(current&&current[1]!=this.anchor&&this.scrollPoints[current[1]]){$clear(this.timer);this.anchor=current[1];this.scroll.start(this.scrollPoints[current[1]].x,this.scrollPoints[current[1]].y);}else if(!current&&this.anchor){this.scroll.start(0,0);this.anchor=false;}},move:function(target){var goToX=this.scrollPoints[target]?this.scrollPoints[target].x:0;var goToY=this.scrollPoints[target]?this.scrollPoints[target].y:0;this.scroll.start(goToX,goToY);this.anchor=target;}});