/**
 * jQuery history event v0.1
 * Copyright (c) 2008 Tom Rodenberg <tarodenberg gmail com>
 * Licensed under the GPL (http://www.gnu.org/licenses/gpl.html) license.
 *
 * http://plugins.jquery.com/project/historyevent
 *
 * - default currentHash() to location.href if no hash initially [alan, 2009/09/11]
 * - fixed initialization of previousNav on 1st browser back [alan, 2009/01/30]
 * - added getInterval() [alan]
 * - added maskevent parameter to $.history.add [alan]
 * - added getPrevious() [alan]
 * - IE6 fix to allow storage of hashes with the '?' character [alan, 2009/06/24]
 */
(function($) {
    var currentHash, previousNav, timer, hashTrim = /^.*#/, interval = 100;

    var msie = {
        iframe: null,
        getDoc: function() {
            return msie.iframe.contentWindow.document;
        },
        getHash: function() {
            return decodeURIComponent(msie.getDoc().location.hash);
        },
        setHash: function(hash) {
            var d = msie.getDoc();
            d.open();
            d.close();
            hash = encodeURIComponent(hash.replace(/^#/, ''));
            d.location.hash = hash;
        }
    };

    var historycheck = function() {
        var hash = msie.iframe ? msie.getHash() : location.hash;
        if (hash != currentHash) {
            previousNav = $.history.getCurrent();
            currentHash = hash;
            if (msie.iframe) {
                location.hash = currentHash;
            }
            var current = $.history.getCurrent();
            $.event.trigger('history', [current, previousNav]);
        }
    };

    $.history = {
        add: function(hash, maskevent) {
            hash = '#' + hash.replace(hashTrim, '');
            if (currentHash != hash) {
                previousNav = $.history.getCurrent();
                location.hash = currentHash = hash;
                if (msie.iframe) {
                    msie.setHash(currentHash);
                }
                if (!maskevent)
                  $.event.trigger('historyadd', [$.history.getCurrent(), previousNav]);
            }
            if (!timer) {
                timer = setInterval(historycheck, interval);
            }
        },
        getCurrent: function() {
            return currentHash.replace(hashTrim, '');
        },
        getPrevious: function() {
            return previousNav;
        },
        getInterval: function() {
            return interval;
        }
    };

    $.fn.history = function(fn) {
        $(this).bind('history', fn);
    };

    $.fn.historyadd = function(fn) {
        $(this).bind('historyadd', fn);
    };

    $(function() {
        currentHash = location.hash;
        if (currentHash == '' || currentHash == '#') currentHash = location.href;
        if ($.browser.msie) {
            msie.iframe = $('<iframe style="display:none" src="javascript:false;"></iframe>').prependTo('body')[0];
            msie.setHash(currentHash);
            currentHash = msie.getHash();
        }
    });
})(jQuery);

