/*
Copyright (c) 2007 Brian Dillard and Brad Neuberg:
Brian Dillard | Project Lead | bdillard@pathf.com | http://blogs.pathf.com/agileajax/
Brad Neuberg | Original Project Creator | http://codinginparadise.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
window.dhtmlHistory = {
	initialize: function() {
		if (this.isIE) {
			if (!historyStorage.hasKey(this.PAGELOADEDSTRING)) {
				this.fireOnNewListener = false;
				this.firstLoad = true;
				historyStorage.put(this.PAGELOADEDSTRING, true);
			}
			else {
				this.fireOnNewListener = true;
				this.firstLoad = false;   
			}
		}
	},
	addListener: function(listener) {
		this.listener = listener;
		if (this.fireOnNewListener) {
			this.fireHistoryEvent(this.currentLocation);
			this.fireOnNewListener = false;
		}
	},
	add: function(newLocation, historyData) {
		if (this.isSafari) {
			newLocation = this.removeHash(newLocation);
			historyStorage.put(newLocation, historyData);
			this.currentLocation = newLocation;
			window.location.hash = newLocation;
			this.putSafariState(newLocation);
		} else {
			var that = this;
			var addImpl = function() {
				if (that.currentWaitTime > 0) {
					that.currentWaitTime = that.currentWaitTime - that.WAIT_TIME;
				}
				newLocation = that.removeHash(newLocation);
				if (document.getElementById(newLocation)) {
					var e = "Exception: History locations can not have the same value as _any_ IDs that might be in the document,"
					+ " due to a bug in IE; please ask the developer to choose a history location that does not match any HTML"
					+ " IDs in this document. The following ID is already taken and cannot be a location: " + newLocation;
					throw e; 
				}
				historyStorage.put(newLocation, historyData);
				that.ignoreLocationChange = true;
				that.ieAtomicLocationChange = true;
				that.currentLocation = newLocation;
				window.location.hash = newLocation;
				if (that.isIE) {
					that.iframe.src = "resources/blank.html?" + newLocation;
				}
				that.ieAtomicLocationChange = false;
			};
			window.setTimeout(addImpl, this.currentWaitTime);
			this.currentWaitTime = this.currentWaitTime + this.WAIT_TIME;
		}
	},
	isFirstLoad: function() {
		return this.firstLoad;
	},
	getVersion: function() {
		return "0.5";
	},
	getCurrentLocation: function() {
		if (this.isSafari) {
			var currentLocation = this.getSafariState();
		}
		else {
			var currentLocation = this.removeHash(window.location.hash);
		}
		return currentLocation;
	},
	fireDebugMode: function() {
		var debugStyle = {
			left: 'auto',
			right: 'auto',
			width: '800px',
			height: '100px',
			border: '1px solid black',
			position: 'static'
		};
		for (var key in debugStyle) {
			var val = debugStyle[key];
			if (typeof val == 'string') {
				historyStorage.storageField.style[key] = val;
				if (this.isIE) {
					this.iframe.style[key] = debugStyle[key];
				}
				else if (this.isSafari) {
					val = (key == 'height' ? '30px' : val); 
					this.safariStack.style[key] = val;
				}
			}
		}
	},
	PAGELOADEDSTRING: "DhtmlHistory_pageLoaded",
	WAIT_TIME: 200,
	debugging: false,
	isIE:  ( document.all && navigator.userAgent.toLowerCase().indexOf('msie')!=-1 ),
	isOpera: ( navigator.userAgent.toLowerCase().indexOf('opera')!=-1),
	isSafari: ( navigator.userAgent.toLowerCase().indexOf('safari')!=-1),
	listener: null,	
	pollHandle: null,
	currentWaitTime: 0,
	currentLocation: null,
	iframe: null,
	safariHistoryStartPoint: null,
	safariStack: null,
	ignoreLocationChange: null,
	fireOnNewListener: null,
	firstLoad: null,
	ieAtomicLocationChange: null,
	createIE: function(initialHash) {
		if (this.isIE) {
			this.WAIT_TIME = 400;
			var rshIframeID = "rshIDHistoryFrame";
			var rshIframeHTML = '<iframe name="' + rshIframeID + '" id="' + rshIframeID + '" style="' + historyStorage.invisibilityStyles
				+ '" src="resources/blank.html?' + initialHash + '"></iframe>'
			;
			document.write(rshIframeHTML);
			this.iframe = document.getElementById(rshIframeID);
		}
	},
	createOpera: function() {
		if (this.isOpera) {
			var imgHTML = '<img src="javascript:location.href=\'javascript:dhtmlHistory.checkLocation();\';" style="visibility:hidden" />';
			document.write(imgHTML);
		}
	},
	createSafari: function() {
		if (this.isSafari) {
			this.WAIT_TIME = 400;
			this.safariHistoryStartPoint = history.length;
			var stackID = "rshSafariStack";
			var stackHTML = '<input type="text" style="' + historyStorage.invisibilityStyles + '" id="' + stackID + '" value="[]"/>';
			document.write(stackHTML);
			this.safariStack = document.getElementById(stackID);
		}
	},
	getSafariStack: function() {
		var r = this.safariStack.value;
		return historyStorage.parseJSON(r);
	},
	getSafariState: function() {
		var stack = this.getSafariStack();
		return stack[history.length - this.safariHistoryStartPoint - 1];
	},			
	putSafariState: function(newLocation) {
	    var stack = this.getSafariStack();
	    stack[history.length - this.safariHistoryStartPoint] = newLocation;
	    this.safariStack.value = historyStorage.toJSONString(stack);
	},
	create: function() {
		this.createSafari();
		this.createOpera();
		var initialHash = this.getCurrentLocation();
		this.currentLocation = initialHash;
		this.createIE(initialHash);
		var that = this;
		window.onunload = function() {
			that.firstLoad = null;
		};
		if (this.isIE) {
			this.ignoreLocationChange = true;
		} else {
			if (!historyStorage.hasKey(this.PAGELOADEDSTRING)) {
				this.ignoreLocationChange = true;
				this.firstLoad = true;
				historyStorage.put(this.PAGELOADEDSTRING, true);
			} else {
				this.ignoreLocationChange = false;
				this.fireOnNewListener = true;
			}
		}
		var that = this;
		var locationHandler = function() {
			that.checkLocation();
		};
		this.pollHandle = setInterval(locationHandler, 100);
	},
	fireHistoryEvent: function(newHash) {
		var historyData = historyStorage.get(newHash);
		this.listener.call(null, newHash, historyData);
	},
	checkLocation: function() {
		if (!this.isIE && this.ignoreLocationChange) {
			this.ignoreLocationChange = false;
			return;
		}
		if (!this.isIE && this.ieAtomicLocationChange) {
			return;
		}
		var hash = this.getCurrentLocation();
		if (hash == this.currentLocation) {
			return;
		}
		this.ieAtomicLocationChange = true;
		if (this.isIE && this.getIFrameHash() != hash) {
			this.iframe.src = "resources/blank.html?" + hash;
		}
		else if (this.isIE) {
			return;
		}
		this.currentLocation = hash;
		this.ieAtomicLocationChange = false;
		this.fireHistoryEvent(hash);
	},
	getIFrameHash: function() {
		var doc = this.iframe.contentWindow.document;
		var hash = String(doc.location.search);
		if (hash.length == 1 && hash.charAt(0) == "?") {
			hash = "";
		}
		else if (hash.length >= 2 && hash.charAt(0) == "?") {
			hash = hash.substring(1);
		}
		return hash;
	},
	removeHash: function(hashValue) {
		var r;
		if (hashValue == null || hashValue == undefined) {
			r = null;
		}
		else if (hashValue == "") {
			r = "";
		}
		else if (hashValue.length == 1 && hashValue.charAt(0) == "#") {
			r = "";
		}
		else if (hashValue.length > 1 && hashValue.charAt(0) == "#") {
			r = hashValue.substring(1);
		}
		else {
			r = hashValue;
		}
		return r;
	},
	iframeLoaded: function(newLocation) {
		if (this.ignoreLocationChange) {
			this.ignoreLocationChange = false;
			return;
		}
		var hash = String(newLocation.search);
		if (hash.length == 1 && hash.charAt(0) == "?") {
			hash = "";
		}
		else if (hash.length >= 2 && hash.charAt(0) == "?") {
			hash = hash.substring(1);
		}
		if (this.pageLoadEvent != true) {
			window.location.hash = hash;
		}
		this.fireHistoryEvent(hash);
	}
};
window.historyStorage = {
	put: function(key, value) {
		this.assertValidKey(key);
		if (this.hasKey(key)) {
			this.remove(key);
		}
		this.storageHash[key] = value;
		this.saveHashTable(); 
	},
	get: function(key) {
		this.assertValidKey(key);
		this.loadHashTable();
		var value = this.storageHash[key];
		return (value == undefined
			? null
			: value
		);
	},
	remove: function(key) {
		this.assertValidKey(key);
		this.loadHashTable();
		delete this.storageHash[key];
		this.saveHashTable();
	},
	reset: function() {
		this.storageField.value = "";
		this.storageHash = {};
	},
	hasKey: function(key) {
		this.assertValidKey(key);
		this.loadHashTable();
		return (typeof this.storageHash[key] != "undefined");
	},
	isValidKey: function(key) {
		return typeof key == "string";
	},
	invisibilityStyles: 'position: absolute; top: -1000px; left: -1000px; width: 1px; height: 1px;',
	storageHash: {},
	hashLoaded: false, 
	storageField: null,
	setup: function() {
		var textareaID = "rshStorageField";
		var textareaHTML = '<textarea id="' + textareaID + '" style="' + this.invisibilityStyles + '"></textarea>';
		document.write(textareaHTML);
		this.storageField = document.getElementById(textareaID);
	},
	assertValidKey: function(key) {
		if (!this.isValidKey(key)) {
			throw "Please provide a valid key for window.historyStorage, key= " + key;
		}
	},
	loadHashTable: function() {
		if (!this.hashLoaded) {	
			var serializedHashTable = this.storageField.value;
			if (serializedHashTable != "" && serializedHashTable != null) {
				this.storageHash = this.parseJSON(serializedHashTable);
			}
			this.hashLoaded = true;
		}
	},
	saveHashTable: function() {
		this.loadHashTable();
		var serializedHashTable = this.toJSONString(this.storageHash);
		this.storageField.value = serializedHashTable;
	},
	toJSONString: function(s) {
		if (s.toJSONString) {
			return s.toJSONString();
		}
		else {
			var e = "No JSON stringify method defined."
			throw e;
		}
	},
	parseJSON: function(s) {
		if (s.parseJSON) {
			return s.parseJSON();
		}
		else {
			var e = "No JSON parse method defined."
			throw e;
		}
	}
};
window.historyStorage.setup();
window.dhtmlHistory.create();

