(function($){
/* xstory : plugin for jQuery
 * Copyright (c) 2007 Martin Panel <http://xorax.info>
 * Dual licensed under the MIT <http://www.opensource.org/licenses/mit-license.php>
 * and GPL <http://www.opensource.org/licenses/gpl-license.php> licenses.
 */
$.xstory = {
	currentHash: null,
	callback: null,
	_check: null,
	frameId: 'jQuery_xstory',
	
	init: function(cb){
		this.callback = cb;
		var currentVirtualHash = location.hash+'';
		
		this.currentHash = currentVirtualHash;
		if($.browser.msie) {
			// To stop the callback firing twice during initilization if no hash present
			if (this.currentHash == '') this.currentHash = '#';
		
			$("body").append('<iframe id="'+this.frameId+'" style="display: none;"></iframe>');
			var iframe = $('#'+this.frameId)[0].contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = currentVirtualHash;
		} else if($.browser.opera && $.browser.version > 9.19 && $.browser.version < 9.5){
			$("body").append('<img src="javascript:location.href=\'javascript:$.xstory._initCheck();\';" width="0" height"0" alt="History fix for Opera" />');
		}
		//callback(currentVirtualHash.replace(/^#/, ''));
		
		var self = this;
		if(!this._check){
      this._check = $.browser.msie
      ? function() {
        // On IE, check for location.hash of iframe
        var iframe = $('#'+self.frameId)[0].contentWindow.document;
        var currentVirtualHash = iframe.location.hash+'';
        if(currentVirtualHash != this.currentHash) {
          location.hash = this.currentHash = currentVirtualHash;
          this.callback(currentVirtualHash.replace(/^#/, ''));
        }
      }
      : function() {
        // otherwise, check for location.hash
        var currentVirtualHash = location.hash+'';
        if(currentVirtualHash != this.currentHash) {
          this.currentHash = currentVirtualHash;
          this.callback(currentVirtualHash.replace(/^#/, ''));
        }
      };
    }
    setInterval(function () { self._check(); }, 100);
	},
	load: function(hash, noCallback){
	  var self = this;
		var load2 = $.browser.msie
		? function(hash){
			var iframe = $('#'+self.frameId)[0].contentWindow.document;
			iframe.open();
			iframe.close();
			iframe.location.hash = '#'+hash;
		}
		: function(){};
		
		this.load = function(hash, noCallback){
			this.currentHash = '#'+hash;
			load2(hash);
			if(!noCallback) this.callback(hash);
			location.hash = '#'+hash;
		};
		this.load(hash);
	}
};
})(jQuery);