
function AjaxHistory() {

	this.iframe = 0
	this.inputid = 'ajaxloc'
	this.iframeid = 'ajaxnav'
	this.href = window.location.href.split('#')[0]
	this.location = undefined

	this.locations = function() {
		//if (!this.iframe) return '[no iframe]'
		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, ajaxloc = this.iframe ? get(this.inputid).value : undefined
		if (loc.indexOf('#') > -1) loc = loc.slice(1)
		if (loc.slice(-1) == '/') loc = loc.slice(0, -1)
		this.location = ''
		this.debug("loc = '" + loc + "'", 'history')
		if (ajaxloc == undefined) ajaxloc = this.sethash
		if (this.firstrun) { this.debug("firstrun to '" + loc + "'"); ajax.go(loc); }
		else if (loc != ajaxloc) { this.debug("loading new hash '" + loc + "'"); ajax.go(loc); }
		else this.location = ajaxloc
		if (!this.firstrun) this.intervalid = window.setInterval(function() { AjaxHistory.checkHash.call(AjaxHistory); }, 200)
		this.debug("init 2: " + this.locations())
	}

	this.updateUrl = function(hash, params, callback, skip_ajax) {
		//this.debug("updateUrl 0: " + this.locations())
		if (hash === undefined) hash = ''
		if (hash.slice(0, 1) == '#') hash = hash.slice(1)
		if (hash.indexOf('|') > -1) hash = hash.split('|').shift()
		if (this.location == undefined) {
			// init() not called yet, ignore
			this.location = ''
			this.debug('ignored updateUrl: ' + hash + ' -- ' + this.locations())
			return false
		}
		if (this.location == hash && this.firstrun == undefined) {
			this.debug('same updateUrl: [' + this.location + '] > [' + hash + ']')
			return false
		}
		if (this.firstrun) delete this.firstrun
		//this.debug('updateUrl 1: [' + this.location + '] > [' + hash + ']')
		this.prevlocation = this.location
		this.location = hash
		this.updateHash(hash)
		if (this.iframe) this.updateIframe(hash);
		//this.debug("updateUrl 2: " + this.locations())
		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) {
		if (document.getElementById(this.inputid).value == hash) return
		//this.debug('updateIframe: ' + document.getElementById(this.inputid).value + '|' + document.getElementById(this.iframeid).src + ' > ' + hash)
		document.getElementById(this.inputid).value = hash
		document.getElementById(this.iframeid).setAttribute('src', core.baseurl + "iframe/" + hash)
	}

	this.checkHash = function() {
		if (this.location == undefined) this.location = ''
		if (this.iframe) {
			// with iframe: new url comes from input#ajaxloc (IE6: window.location.hash for #a?b=1 returns only #a)
			var hash = this.location
			var 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)
				if (hash == newlocation) return
				// window.location atallitas + ujratoltes
				////this.debug('checkHash: ' + window.location.href.split('#')[0] + newlocation)
				//window.location.reload()
				////this.debug("href would be: " + window.location.href.split('#')[0] + (newlocation.length ? newlocation : '_'))
				////this.debug("newlocation: " + newlocation)
				////window.location.href = window.location.href.split('#')[0] + (newlocation.length ? newlocation : '_')
				//window.location.href = window.location.href.split('#')[0] + '#' + newlocation
				//window.location.hash = '#' + newlocation
				//setTimeout(function() { window.location.reload() }, 0)
				////clearInterval(this.intervalid)
				////return
			}
		} else {
			// without iframe: new url comes from window.location.hash
			var hash = this.location
			var newlocation = window.location.hash.slice(0, 1) == '#' ? window.location.hash.slice(1) : window.location.hash
			if (hash == newlocation && (this.firstrun == undefined || !hash.length)) 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)
}

