
function AjaxHistory() {

	this.iframe = 0
	this.inputid = 'ajaxloc'
	this.iframeid = 'ajaxnav'
	this.href = window.location.href.split('#')[0]
	this.location = undefined

	this.locations = function() {
		var h1 = this.location
		var h2 = this.iframe ? document.getElementById(this.inputid).value : '--'
		var str = h1 + ' | ' + h2 + ' | ' + window.location.hash.slice(1)
		return str
	}

	this.init = function() {
		this.debug("init 1: " + this.locations())
		var loc = window.location.hash
		if (loc.indexOf('#') > -1) loc = loc.slice(1)
		if (loc.slice(-1) == '/') loc = loc.slice(0, -1)
		this.location = ''
		this.debug("loc = '" + loc + "', sethash = '" + this.sethash + "'", 'history')
		if (this.sethash == 0 || loc != this.sethash) {
			this.debug((this.sethash == 0 ? "new hash" : "set hash") + " '" + loc + "'")
			if (this.iframe) this.ignore_next_iframe = 1
			if (this.sethash != 0 || loc) ajax.go(loc)
		} else this.location = loc
		this.intervalid = window.setInterval(function() { AjaxHistory.checkHash.call(AjaxHistory); }, 200)
		this.debug("init 2: " + this.locations())
	}

	this.updateUrl = function(hash, params, callback, skip_ajax) {
		if (hash === undefined) hash = ''
		if (hash.slice(0, 1) == '#') hash = hash.slice(1)
		if (hash.indexOf('|') > -1) hash = hash.split('|').shift()
		hash = decodeURIComponent(hash)
		if (this.location == undefined) {
			this.location = ''
			this.debug('ignored updateUrl: ' + hash + ' -- ' + this.locations())
			return false
		}
		if (this.location == hash && this.sethash != 0) {
			this.debug('same updateUrl: [' + this.location + '] > [' + hash + ']')
			if (this.iframe && document.getElementById(this.inputid).value != hash) this.updateIframe(hash)
			return false
		}
		this.prevlocation = this.location
		this.location = hash
		this.updateHash(hash)
		if (this.iframe) this.updateIframe(hash)
		if (skip_ajax == undefined) ajax.go(hash, params, callback)
		return false
	}

	this.updateHash = function(hash) { if (window.location.hash !== hash) window.location.hash = hash; }

	this.updateIframe = function(hash) {
		this.debug('updateIframe: [' + document.getElementById(this.inputid).value + '] > [' + hash + ']')
		if (document.getElementById(this.inputid).value == hash) return
		document.getElementById(this.inputid).value = hash
		document.getElementById(this.iframeid).setAttribute('src', core.baseurl + "iframe/" + hash)
	}

	this.updateFromIframe = function() {
		core.debug.debug("iframe set ajaxloc to '" + get(this.inputid).value + "'" + (this.ignore_next_iframe ? ", ignoring it" : ""), 'history');
		if (!this.ignore_next_iframe) return
		this.updateIframe(this.location == undefined ? window.location.hash.slice(1) : this.location)
		delete this.ignore_next_iframe
	}

	this.checkHash = function() {
		if (this.location == undefined) this.location = ''
		var hash = this.location, newlocation
		if (this.iframe) {
			// with iframe: new url comes from input#ajaxloc (IE6: window.location.hash for #a?b=1 returns only #a)
			newlocation = document.getElementById(this.inputid).value
			if (hash == newlocation) 
				newlocation = window.location.href.indexOf('#') > -1 ? window.location.href.split('#')[1] : window.location.hash.slice(1)
		}
		// without iframe: new url comes from window.location.hash
		else newlocation = window.location.hash.slice(0, 1) == '#' ? window.location.hash.slice(1) : window.location.hash
		if (hash == newlocation) return
		this.debug('3. checkHash: [' + hash + '] > [' + newlocation + ']')
		this.updateUrl(newlocation)
	}

	this.debug = function(str) { return core.debug.debug(str, 'history'); }
}

var AjaxHistory = new AjaxHistory()

// backwards comp
function go(hash, params, callback, skip_ajax) {
	if (skip_ajax) return ajax.show(hash)
	return ajax.go(hash, params, callback)
}

