/**
 * jQuery.LocalScroll
 * Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
 * Licensed under GPL license (http://www.opensource.org/licenses/gpl-license.php).
 * Date: 1/4/2008
 *
 * @projectDescription Animated scrolling navigation, using anchors.
 * @author Ariel Flesler
 * @version 1.1.3
 *
 * @id jQuery.fn.localScroll
 * @param {Object} settings Hash of settings, it is passed in to jQuery.ScrollTo, none is required.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example $('ul.links').localScroll();
 *
 * @example $('ul.links').localScroll({ filter:'.animated', speed:400, axis:'x' });
 *
 * @example $.localScroll({ target:'#pane', axis:'xy', queue:true, event:'mouseover' });
 *
 * Notes:
 *	- The plugin requires jQuery.ScrollTo.
 *	- The hash of settings, is passed to jQuery.ScrollTo, so the settings are valid for that plugin as well.
 *	- jQuery.localScroll can be used if the desired links, are all over the document, it accepts the same settings.
 *  - If the setting 'persistent' is setted to true, then the binding will still work for later added anchors.
 *  - The setting 'speed' is deprecated, use 'duration' instead.
 **/
;(function( $ ){
		  
	$.localScroll = function( settings ){
		$('body').localScroll( settings );
	};
	
	//Most of these defaults, belong to jQuery.ScrollTo, check it's demo for an example of each option.
	//@see http://www.freewebs.com/flesler/jQuery.ScrollTo/
	$.localScroll.defaults = {//the defaults are public and can be overriden.
		target:null, //what to scroll (selector or element). Keep it null if want to scroll the whole window.
		filter:null, //filter some anchors out of the matched elements.
		duration:1000, //how long to animate.
		axis:'y',//which of top and left should be modified.
		event:'click',//on which event to react.
		persistent:false,//if true, links can be added later, and will still work.
		cancel: true//if false, the #some_id will appear on the status bar (this is not fully tested yet :( ).
	};
	
	$.fn.localScroll = function( settings ){
		settings = $.extend( {}, $.localScroll.defaults, settings );
		return (settings.persistent) 
					? this.bind( settings.event, function( e ){//use event delegation, more link can be added later.
						var a = $([e.target, e.target.parentNode]).filter(filter)[0];//if an anchor (valid) was clicked.
						return a && scroll.call( a, e );//do scroll.
					})
					: this.find('a')//bind concretely, to each matching link
							.filter( filter ).bind( settings.event, scroll ).end()
						  .end();
		
		function scroll( e ){
			var id = this.hash.slice(1),
				elem = document.getElementById(id) || $('[name=' + id +']')[0];
			if ( elem ){
				if( settings.onBefore )
					settings.onBefore.call( this, e, elem, $(settings.target||window) );
				if( settings.target )
					$(settings.target).scrollTo( elem, settings );//scroll this element
				else
					$.scrollTo( elem, settings );//if there's no target, just let $.scrollTo scroll the window.
				return !settings.cancel;
			}
		};
		function filter(){//is this link completely valid for THIS case ?
			return local( this ) && (!settings.filter || $(this).is( settings.filter ));
		};
	};
	
	function local( link ){//is this a local anchor ?
		return !!link.hash && location.href.replace(/#.*/,'') == link.href.replace(link.hash,'');
	};
	
})( jQuery );