/**
 * jQuery.LocalScroll
 * Copyright (c) 2007 Ariel Flesler - aflesler(at)gmail(dot)com
 * Licensed under GPL license (http://www.opensource.org/licenses/gpl-license.php).
 * Date: 10/24/2007
 *
 * @projectDescription Same page links auto scrolling.
 *
 * @author Ariel Flesler
 * @version 1.0
 *
 * @id jQuery.fn.localScroll
 * @param {Object} settings Hash of settings, it is passed in to jQuery.ScrollTo.
 *	 @option {String|DOMElement} target optional:The element to scroll, by default: the whole document.
 *	 @option {Function} onbefore optional: Function called before scrolling.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 *
 * @example $('ul.links').localScroll({ speed:600 });
 *
 * Notes:
 *	- The idea, and parts of the code were taken from http://www.learningjquery.com/2007/09/animated-scrolling-with-jquery-12
 *	- The plugin requires jQuery.ScrollTo.
 *  - jQuery.localScroll will turn every suitable link in the page, into a scrolling link.
 **/
(function( $ ){
		  
	$.localScroll = function( settings ){
		$('body').localScroll( settings );
	};
	
	$.fn.localScroll = function( settings ){
		settings = $.extend({target:'html,body'}, settings);
		settings.target = $(settings.target);
		
		return this.find('a[href*=#]:not([href=#])').click(function( e ){//added Anton S modification
			if ( samePage(this) ){
				var id = this.hash.slice(1),
					element = document.getElementById(id) || $('[name=' + id +']')[0];
				if ( element ){
					if( settings.onbefore )
						settings.onbefore.call( this, e, element, settings.target );
					settings.target.scrollTo( element, settings );
					return false;
				}
			}
		}).end();
	};
	
	function samePage( link ){
		return location.href.replace(location.hash,'') == link.href.replace(link.hash,'');
	};
	
})( jQuery );