/*
  jQuery anchor handler - 0.4
  http://code.google.com/p/jquery-utils/

  (c) Maxime Haineault <haineault@gmail.com>
  http://haineault.com   

  MIT License (http://www.opensource.org/licenses/mit-license.php)

  Changelog
  =========
  - fixed bug in IE
  - added handleClick argument (default to false)
  - added stronger typecheck for the regexp variable
  - fixed variable scope of "handlers"
  - now using builtin hash window.location.hash instead of full URL
  - removed array support to make the code more consistant and less error prone
  - added "end" method to allow jQuery object chaining
  - the click handling now *returns* the callback function so it can halt event propagation
  - the API changed so I moved to version 0.4
*/

(function($){
    var hash = window.location.hash;
    var handlers = [];
	
	$.extend({
		anchorHandler: {
            end: function() { return $; },
			add: function(regexp, callback, handleClick) {
                if (handleClick) $('a[href~=#]').each(function(i,a){
                    var match = a.hash.match(regexp);
                    if (match) $(a).click(function(e){
                        return callback.apply(e, [match[0], this]); });});

				handlers.push({r: regexp, cb: callback});
				return $.anchorHandler;
			}
		}
	})(document).ready(function($){
		$.map(handlers, function(handler){
			var match = hash.match(handler.r) && hash.match(handler.r)[0] || false;
			if (match)  handler.cb.apply(this, [match, ($('a[href~='+hash+']').get(0) || false)]);});
    });
})(jQuery);
