//= require <mootools-core-1.3-full-nocompat>
//= require <uiframework/EventDispatcher>
//= require <uiframework/UICEvent>
var BrowserManager = function(){
	this.Extends = EventDispatcher;
    
	this.initialize = function()
	{
		this.parent();
		this.currentHash = "";
	};

	this.historyInit = function(src)
	{
		if (src)
			this.historyIframeSource = src;
		this.currentHash = location.hash.replace(/\?.*$/, '');

		if (Browser.safari /* Safari */)
		{
			this.historyBackStack = [];
			this.historyBackStack.length = history.length;
			this.historyForwardStack = [];
			this.lastHistoryLength = history.length;
			this.isFirst = true;
		}
		this.dispatchEvent(new UICEvent(BrowserManager.URL_CHANGE, false, false, this.currentHash.replace(/^#/, '')));
		window.setInterval(this.checkHistory.bind(this), 100);
	};

	/**
     * Used for Safari browsers only
     */
	this.addHistory = function(hash)
	{
		this.historyBackStack.push(hash);
		this.historyForwardStack.length = 0;
		this.isFirst = true;
	};

	this.checkHistory = function()
	{
		var hash = "";
		if (Browser.safari/* Safari */ &&
			this.lastHistoryLength == history.length &&
			this.historyBackStack.length > this.lastHistoryLength)
			{
			this.historyBackStack.shift();
			if (!this.dontCheck)
			{
				var historyDelta = history.length - this.historyBackStack.length;
				this.lastHistoryLength = history.length;
				if (historyDelta)
				{
					var i = 0;
					this.isFirst = false;
					if (historyDelta < 0)
						for (i = 0; i < Math.abs(historyDelta); i++) this.historyForwardStack.unshift(this.historyBackStack.pop());
					else
						for (i = 0; i < historyDelta; i++) this.historyBackStack.push(this.historyForwardStack.shift());
					var cachedHash = this.historyBackStack[this.historyBackStack.length - 1];
					if (cachedHash != undefined)
						hash = location.hash.replace(/\?.*$/, '');
				}
				else if (this.historyBackStack[this.historyBackStack.length - 1] == undefined && !this.isFirst)
				{
					if (location.hash)
						hash = location.hash;
					else
						hash = "";
					this.isFirst = true;
				}
			}
		}
		else
			hash = location.hash.replace(/\?.*$/, '');
		if (hash != this.currentHash)
		{
			this.currentHash = hash;
			this.dispatchEvent(new UICEvent(BrowserManager.URL_CHANGE, false, false, this.currentHash.replace(/^#/, '')));
		}
	};

	this.getCurrentHash = function()
	{
		return this.currentHash.replace(/^#/, '');
	};

	this.setUrlFragment = function(hash)
	{
		var newHash = "";
		hash = decodeURIComponent(hash.replace(/\?.*$/, ''));
		if (Browser.safari /* Safari */)
			newHash = hash;
		else
		{
			newHash = '#' + hash;
			location.hash = newHash;
		}
		this.currentHash = newHash;
		if (Browser.safari /* Safari */)
		{
			this.dontCheck = true;
			this.addHistory(hash);
			var fn = function() {
				this.dontCheck = false;
			};
			fn = fn.bind(this);
			window.setTimeout(fn, 200);
			location.hash = newHash;
		}
		this.dispatchEvent(new UICEvent(BrowserManager.URL_CHANGE, false, false, hash));
	};
};
BrowserManager = new Class(new BrowserManager());
BrowserManager.URL_CHANGE = "urlChange";
