/**
 * 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
 *
 * - 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 trimHash = function(target_str){
		var index = target_str.indexOf("#");
		return target_str.substr(index+1);
		
	};
    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(/^#/, ''));
			hash = encodeURIComponent(trimHash(hash));
            d.location.hash = hash;
        }
    };

	//check when back button is clicked
    var historycheck = function() {
        var hash = msie.iframe ? msie.getHash() : location.hash; 
            if(hash.substr(0,1)!="#"){
    			hash = '#' + hash;
            }
        //console.log((hash + "vs " + currentHash));
        if (hash != currentHash) {
			//alert((hash + "vs " + 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) {
            //console.log("hash");
            //console.log(hash);
            //hash = '#' + hash.replace(hashTrim, '');
            if(hash.substr(0,1)!="#"){
    			hash = '#' + hash;
            }
			//console.log("hash after trim");
            //console.log("hash: ",hash);
			//console.log("currentHash: "+ currentHash);
            if (currentHash != hash) {
                previousNav = $.history.getCurrent();
                //console.log("previousNav:", previousNav);
                location.hash = currentHash = hash;
                if (msie.iframe) {
                    msie.setHash(currentHash);
                }
                if (!maskevent){
                    //console.log("no mask event?")
                    $.event.trigger('historyadd', [$.history.getCurrent(), previousNav]); 
				    //default: maskevent = false,
				    //this add the new link to history
				}
            }
			
            if (!timer) {
                timer = setInterval(historycheck, interval);
            }
        },
        getCurrent: function() {
			//alert("get current replace...:" + currentHash.replace(hashTrim, ''));
            //return currentHash.replace(hashTrim, '');
			//alert("testing trimming hash: "+ trimHash(currentHash));
			return trimHash(currentHash);
        },
        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 ($.browser.msie) {
            msie.iframe = $('<iframe style="display:none" src="javascript:false;"></iframe>').prependTo('body')[0];
            msie.setHash(currentHash);
            currentHash = msie.getHash();
        }
    });
})(jQuery);

