/*
 http://techfoolery.com/archives/2006/08/11/2021/
http://www.dtsn.co.uk/2008/03/14/scriptaculous-scrollto-tutorial/#
 
 http://www-dev.apple.com/macosx/features/300.html
*/
/** 
 * Easing Equations for Script.aculo.us
 * @author Brian Crescimanno <brian.crescimanno@gmail.com>
 * @version 0.8.1
 * @revised November 20, 2008
 * @copyright 2008 Brian Crescimanno, all rights reserved
 *
 * Released under terms of the BSD License
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * The math for these equations was created by Robert Penner
 * http://www.robertpenner.com/profmx
 * 
 * -----------------------------------------------------------------------------------------
*/

Effect.Transitions.customExponentialEaseOut = function(pos){	
	if(pos==0) return 0;
	if(pos==1) return 1;
	
	pos+=pos*3;
	return -Math.pow(2, -10 * pos) + 1
}


AC.AnchorPageScroller = Class.create();
Object.extend(AC.AnchorPageScroller.prototype, AC.ViewMaster.Viewer.prototype);

Object.extend(AC.AnchorPageScroller.prototype, {
			  triggerClassName: "scrollToAnchor",
	 _triggerClicked: function(evt) {
	 var trigger = evt.element();
	 if(AC.Detector.isIEStrict() && evt.type === "mouseup") {
		 if(trigger && trigger.nodeName.toUpperCase() === 'A' ) {
			 trigger = trigger.down("."+this.triggerClassName);
		 }
	 }
	 else {
		 while (trigger && trigger.nodeName.toUpperCase() != 'A' && trigger.nodeName.toUpperCase() != 'BODY') {
		 trigger = trigger.parentNode;
		 }
	 }
	 //ignore if the element is not a trigger
	 if (trigger && trigger.href && Element.Methods.hasClassName(trigger, this.triggerClassName)) {
			  
			  var parts = trigger.href.split("#");
			  
			  if(parts.length === 2) {
			  Event.stop(evt);
			  
			  this._currentDestination = parts[1];
			  var options =  {
					duration: 1.5,
				  transition: Effect.Transitions.customExponentialEaseOut,
				afterFinish: this.afterScroll.bind(this)
				  };
				  
			  
					  new Effect.ScrollTo(this._currentDestination,options);
			  }
			  
									 
	 } 
	 
 },
afterScroll: function() {
	var windowLocationParts = window.location.href.split("#");
	windowLocationParts[1] = this._currentDestination;
	window.location.href = (windowLocationParts[0]+"#"+windowLocationParts[1]);
	delete this._currentDestination;
},
  sectionWithId: function(initialId) {
	  return null;
  }

});


Event.onDOMReady(function() {
				 AC.AnchorPageScroller.defaultAnchorScroller = new AC.AnchorPageScroller();
				 });
