// JavaScript Document

if( typeof Prototype == "undefined" )
	throw "ScrollToAnchor requires the PrototypeJS framework (http://www.prototypejs.org)";

if( typeof Effect == "undefined" )
	throw "ScrollToAnchor requires the Script.aculo.us effects framework (http://script.aculo.us)";

var ScrollToAnchor = Class.create(
{
	targetId: null,
	
	target: null,
	
	initialize: function( element )
	{
		this.element = $( element );
		this.initDOM();
		if( !this.target )
			return; // target is missing.
		this.initEvents();
	},
	
	initDOM: function()
	{
		var href = this.element.getAttribute( "href" );
		var slashPos = href.lastIndexOf( "/" );
		
		if( slashPos > -1 )
			href = href.substr( slashPos+1, href.length-slashPos );
		
		this.targetId = href.replace( "#", "" );
		this.target = $( this.targetId );
	},
	
	initEvents: function()
	{
		this.element.observe( "click", this.handleClick.bind( this ) );
	},
	
	handleClick: function( e )
	{
		Event.stop( e );
		Effect.ScrollTo( this.target, { offset: -30, duration: 0.5 } );
	}
});

document.observe( "dom:loaded", function(){ $$( "a.scroll-to-anchor" ).each( function( el ){ new ScrollToAnchor( el ) } ) } );