/**
 * the nativeSupport check was borrowed from the js written by Ben Alman
 * @see http://github.com/cowboy/jquery-hashchange/raw/v1.3/jquery.ba-hashchange.js
 */
(function($) {
    var defaults = {
        pollInterval: 150,
        eventname: 'onhashchange',
        pollId: null,
        nativeSupport: ('onhashchange' in window) && 
            (!document.documentMode || document.documentMode > 7)
    };

    var pollSetup = function(opts) {
        var callee = arguments.callee,
          that = this;

        if (callee._cachedHash !== undefined &&
            callee._cachedHash != location.hash) {
            $(window).trigger(opts.eventname,
                              [location.hash]);
        }
        callee._cachedHash = location.hash;

        opts.pollId = setTimeout(function() {
            callee.call(that, opts);
        }, opts.pollInterval);
    };

    var nativeSetup = function(opts) {
        window.onhashchange = function() { 
            $(window).trigger(opts.eventname, [location.hash]);
        };
    };

    var setup = false,
      doSetup = function(opts) {
          if (opts.nativeSupport) {
              nativeSetup(opts);
          } else {
              pollSetup(opts);
          }
          setup = true;
    };

    $.fn.hashchange = function(fn, settings) {
        $(this).bind('onhashchange',fn);
        if (!setup) {            
            doSetup($.extend(defaults, settings));
        }
        return this;
    }
})(jQuery);

