
window.ajaxHistory = {
	
	isIE: false,
	/*Current hash location, without the "#" symbol.*/
	currentLocation: null,
	ignoreLocationChange: null,
	watcherStarted: null,
	iframe: null,
	historyListener: null,
	disableWatcher: null,

	initialize: function() {
		var userAgent = navigator.userAgent.toLowerCase();
		this.isIE = /msie/.test(userAgent);

		/*Disabling watcher for Acceptance tests (setInterval causing timeout)*/
		var patt = new RegExp("htmlunit");
		this.disableWatcher = patt.test(userAgent);

		this.ignoreLocationChange = false;
		this.watcherStarted = false;
		/*IE doesn't add url fragment changes to history, have to create hidden iframe*/
		if (this.isIE) {
			this.initIE();
		}

		//if (this.getCurrentLocation().length > 0) {
			this.startWatcher();
		//}
	},

	initIE: function() {
		var iframeID = "ajaxHistoryIFrameID";
		document.write("<iframe style='border: 0px; width: 1px; height: 1px; display: none;'"
				+ "id='" + iframeID + "' "
				+ "src='ajaxHistory.html?" + this.getCurrentLocation() + "'" 
				+ "></iframe>");
		this.iframe = document.getElementById(iframeID);
	},

	/*Monitors url changes caused by browser's back/forward history navigation events or manual changes of the hash part of the url*/
	startWatcher: function() {
		this.watcherStarted = true;
		var that = this;
		var historyWatcher = function() {
			that.checkHistoryAction();
		};
		if (!this.disableWatcher) {
			this.watcherID = setInterval(historyWatcher, 200);
		} /*else {
			setTimeout(historyWatcher, 200);
		}*/
	},

	add: function(newLocation) {
		window.location.hash = newLocation;
		/*Synchronizing our location with new browser's location*/
		this.currentLocation = newLocation;
		/*Ignoring location change since it was not done by the user*/
		this.ignoreLocationChange = true;

		/*Change the location of hidden iFrame in case of IE*/
		if (this.isIE) {
			this.iframe.src = "ajaxHistory.html?" + newLocation;
		}

		if (!this.watcherStarted) {
			this.startWatcher();
		}
	},
	
	getCurrentLocation: function() {
		/*Can't use window.location.hash here because it returns unescaped string in FF (but works in IE)*/
		var url = window.location.href;
		var i = url.indexOf("#");
		return (i > 0 ? url.substr(i+1) : "");
	},

	getIframeHash: function() {
		var doc = this.iframe.contentWindow.document;
		var hash = String(doc.location.search);
		var i = hash.indexOf("?");
		return (i == 0 ? hash.substr(1) : "");
	},

	checkHistoryAction: function() {
		if (this.ignoreLocationChange) {
			this.ignoreLocationChange = false;
			return;
		}

		var hash = this.getCurrentLocation();
		/*Do nothing if URL hasn't changed*/
		if (hash == this.currentLocation) {
			return;
		}

		/*When using Browser back button to navigate to the root of ajax page (hash is empty), need to bring page to the original state. 
		Easiest way is just to reload the page. Drawback: it clears forward history in IE. Other possibilities: 1. load page with ajax; 
		2. hide contentDiv instead of clearing it when performing the search and then bring it back (need major change in tests)*/
		if (hash == "" && this.currentLocation != null) {
			var replaceURL = window.location.href;
			if (this.isIE) { //replaceURL.indexOf("#")+1 == replaceURL.length
				/*taking off trailing # in case of IE*/
				replaceURL = replaceURL.slice(0, -1);
			}
			window.location.replace(replaceURL);
		}

		/*If url is manually modified in IE, update the iFrame*/
		if (this.isIE && hash != this.getIframeHash()) {
			this.iframe.src = "ajaxHistory.html?" + hash;
		}
		this.applyHistoryEvent(hash);
	},

	addListener: function(historyListener) {
		 this.historyListener = historyListener;
	},

	applyHistoryEvent: function(newHash) {
		if(this.historyListener != null){
			this.currentLocation = newHash;
			this.historyListener.call(null, newHash);
		}
	},

	iframeLoaded: function(iframeLocation) {
		var i = iframeLocation.indexOf("?");
		hash = (i == 0 ? iframeLocation.substr(1) : "");

		/*If location in iFrame has changed and it different from the current location
		it means that history browsing event took place.
		Changing url hash of the main window so it would be picked up by the watcher*/
		if (hash != this.getCurrentLocation()) {
			window.location.hash = hash;
		}
	}

}



