setDefault('locator', null);
setDefault('historytimer', null);

function write_historyframe() {
//	if (document.iframesfix) {
//		var windowlocator = new PageLocator("window.location.href", "#");
//		document.write("<iframe id='ajaxhistory' name='ajaxhistory' src='../blank.html?hash=" + windowlocator.getHash() + "' style='display: none;'><\/iframe>"); 
//	}
}


/* -------------------- Page locator ------------------------------------------------------------------------------------------------------------------------ */

function page_locator(property, divchar) {
	this.property = property;
	this.defaultquery = 1;
	this.divchar = divchar;
}

page_locator.prototype.getLocation = function() {
	return eval(this.property);
}

page_locator.prototype.getHash = function() {
	var url = this.getLocation();
	if (url.indexOf(this.divchar) > -1) {
		var url_elements = url.split(this.divchar);
		return url_elements[url_elements.length - 1];
	} else {
		return this.defaultquery;
	}
}

page_locator.prototype.getHref = function() {
	var url = this.getLocation();
	var url_elements = url.split(this.divchar)
	return url_elements[0];
}

page_locator.prototype.makeNewLocation = function(new_query) {
	return this.getHref() + this.divchar + new_query;
}

function set_history(caller, type) {
//	if (!document.iframesfix) {
		switch (type) {
			case 'metanarrative':
			case 'solotext':
			case 'stafflist':
		 		var c = caller.getAttribute('href');
				c = c.substring(c.lastIndexOf('/') + 1).replace(/.html/,'');
				if (this.location.hash.indexOf(c) == -1) {
					this.location.hash = c;
				}
				break;
			case 'regulartext':
				var c = caller.getAttribute('href');
				c = c.substring(c.lastIndexOf('/') + 1).replace(/.html/,'');
				if (this.location.hash.indexOf('|') == -1) {
					var x = this.location.hash.replace(/#/,'');
					this.location.hash = x + '|' + c;
				} else {
					var x = this.location.hash.substring(0, this.location.hash.indexOf('|') + 1) + c;
					x = x.replace(/#/,'');
					this.location.hash = x;
				}
				break;
		}
//	} else {
//	}
}

function clear_history(type) {
//	if (!document.iframesfix) {
		switch (type) {
			case 'solotext':
				this.location.hash = '0';
				break;
			case 'regulartext':
			default:
				if (this.location.hash.indexOf('|') != -1) {
					var c = new String(this.location.hash.substring(0, this.location.hash.indexOf('|')));
					c = c.replace(/#/,'');
					this.location.hash = c;
				}
				break;
		}
//	}
}



/* -------------------- Ajax history ------------------------------------------------------------------------------------------------------------------------ */

function ajaxhistory_hash() {
	locator = new page_locator("window.location.href", "#");
	historytimer = new Timer(this);
	this.checkWhetherChanged(0);
}

ajaxhistory_hash.prototype.checkWhetherChanged = function(location) {
	if (locator.getHash() != location) {
		var h = new String(locator.getHash());
		this.update_location(h);
	}

//	historytimer.setTimeout("checkWhetherChanged", 200, locator.getHash());
}


ajaxhistory_hash.prototype.update_location = function(h) {
	h = h.replace(/#/,'');
	
	if (locator.getHash() == locator.defaultquery) {
		if (active_metanarrative != null || active_contentpage != null) {
			document.location = self.location;
		}
	} else {
		var pp = h.split('|');
		var aa = document.getElementsByTagName('a');
		
		if (pp[0].indexOf('.') == -1) {
			pp[0] += '.html';
		}
		for (var a = 0; a < aa.length; a++) {
			var c = aa[a].getAttribute('href');
			if (c != null) {
				c = c.substring(c.lastIndexOf('/') + 1);
				if (c == pp[0]) {
					switch (aa[a].getAttribute('rel')) {
						case 'metanarrative':
							load_metanarrative(aa[a], pp[1]);
							break;
						case 'solotext':
							load_solotext(aa[a], pp[1]);
							break;
						case 'stafflist':
							load_stafflist(aa[a], pp[1]);
							break;
					}
					break;
				}
			}
		}
	} 
}

function continue_load(pp) {
	var aa = document.getElementsByTagName('a');
	if (pp.indexOf('.') == -1) {
		pp += '.html';
	}
	for (var a = 0; a < aa.length; a++) {
		var c = aa[a].getAttribute('href');
		if (c != null) {
			c = c.substring(c.lastIndexOf('/') + 1);
			if (c == pp) {
				switch (aa[a].getAttribute('rel')) {
					case 'stafflisting':
						load_listingtext(aa[a]);
						break;
					case 'regulartext':
					default:
						load_regulartext(aa[a]);
						break;
				}
				break;
			}
		}
	}
}


	
	
	/* AjaxIframesFixer */
		function AjaxIframesFixer(iframeid) {
			this.iframeid = iframeid;
			if (document.getElementById('ajaxnav')) {
//				this.fixLinks();
//				this.locator = new page_locator("document.frames['" + this.iframeid + "'].getLocation()", "?hash=");
				this.locator = new page_locator("document.frames['" + this.iframeid + "'].location.href", "?hash=");
				this.windowlocator = new page_locator("window.location.href", "#");
				this.timer = new Timer(this);
				
				this.delayInit(); // required or IE doesn't fire
			}
		}
		AjaxIframesFixer.prototype.fixLinks = function (iframeid) {
			var links = document.getElementsByTagName("A");
			for(var i=0; i<links.length; i++) {
				var href = links[i].getAttribute("href");
//				var hash = href.substr(href.indexOf("hash=")+5);
				var hash = href.substring(href.lastIndexOf('/') + 1).replace(/.html/,'');
//				links[i].setAttribute("onmousedown","javascript:document.getElementById('"+this.iframeid+"').setAttribute('src', '../blank.html?hash="+hash+"');");
			}
		}
		AjaxIframesFixer.prototype.delayInit = function(){
			this.timer.setTimeout("checkBookmark", 100, "");
		}
		AjaxIframesFixer.prototype.checkBookmark = function(){
			window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
			this.checkWhetherChanged(0);
		}
		AjaxIframesFixer.prototype.checkWhetherChanged = function(location){
			if(this.locator.getHash() != location) {
//				doGetPage(this.locator.getHash());
//				window.location = this.windowlocator.makeNewLocation(this.locator.getHash());
//				window.location = document.getHref() + this.locator.getHash();
				var anch = document.getElementsByTagName("a");
				var h = new String(this.locator.getHash());
				h = h.replace(/#/,'');
				
				document.location.hash = h;
				
				if (h.indexOf('|') != -1) {
					var l = h.substring(h.indexOf('|') + 1);
					if (l.indexOf('.') == -1) {
						l = l + '.html';
					}
					
					for (var a = 0; a < anch.length; a++) {
 						var c = anch[a].getAttribute('href');
				 		c = c.substring(c.lastIndexOf('/') + 1);
						if (c == l) {
							loadcontent(anch[a], true);
						}
					}
				} else {
					closecontentwindow();
					var l = h.substring(h.indexOf('|') + 1);
					if (l.indexOf('.') == -1) {
						l = l + '.html';
					}
					
					for (var a = 0; a < anch.length; a++) {
 						var c = anch[a].getAttribute('href');
				 		c = c.substring(c.lastIndexOf('/') + 1);
						if (c == l) {
							activatetertiarynav(anch[a], true);
						}
					}
				}
//				document.location.hash = location;
			}
			this.timer.setTimeout("checkWhetherChanged", 500, this.locator.getHash());
		}
	/* END AjaxIframesFixer */
	

	function ajax_bookmarking() {
		if (!document.getElementById || !document.getElementsByTagName) return;
		if (document.iframesfix) {
//			fix = new AjaxIframesFixer('ajaxhistory');
			fix = new ajaxhistory_hash();
		} else {
			fix = new ajaxhistory_hash();
		}
	}
	
	var detect = navigator.userAgent.toLowerCase();
	if (detect.indexOf("msie") > -1) document.iframesfix = true;

	addEvent(window, "load", ajax_bookmarking);