var navEntryMover;

function setNavigationLocation(locate) {
	var rel = locate.pathname.substr(window.location.pathname.lastIndexOf("/")+1);
	var hash = locate.hash;
	if (hash)
		rel = rel.concat(hash);
	if (document.navi)
		document.navi.setCurrentUrl(rel);
}

function changePage(target) {
	if (!target)
		return;
	if (document.navi)
		document.navi.setCurrentUrl(target);
	parent.content.location.href = target;
}

function Navigation(menu) {
	this.entries = menu;
	this.extendInited = null;

	this.initHRefs = function () {
		var cnt = this.entries.length;
		var i;
		for (i = 0; i < cnt; i++) {
			var aTag = this.entries[i].element;
			if (aTag && aTag.tagName == 'A') {
				aTag.href = 'javascript:changePage("'+this.entries[i].url+'")';
				aTag.target = "_self";
			}
		}
	}
	this.initLayoutTree = function () {
		var cnt = this.entries.length;
		var lvls = Array();
		var i;
		var par;
		var root = new NaviEntry(null, 0, null, null);
		root.childs = Array();
		lvls[0] = root;
		for (i = 0; i < cnt; i++) {
			entry = this.entries[i];
			lvls[entry.level] = entry;
			par = lvls[entry.level-1];
			entry.parent = par;
			par.childs[par.childs.length] = entry;
			entry.childs = Array();
		}
		return root;
	}
	this.setCurrentUrl = function (rurl) {
		if (!this.extendInited) {
			//extendInited = initNavExtends( navi );
		}
		var pos = -1;
		var cnt = this.entries.length;
		var i;
		for (i = 0; i < cnt; i++) {
			if (this.entries[i].url == rurl)
				pos = i;
			this.entries[i].unsetCurrent();
		}
		if (pos == -1) {
			var rurl2 = rurl.substr(0,rurl.indexOf('#'));
			for (i = 0; i < cnt && pos == -1; i++) {
				if (rurl.substr(0, this.entries[i].url.indexOf('#')) == rurl2)
					pos = i;
			}
		}
		if (pos >= 0) {
			this.entries[pos].setCurrent();
			var lvl;
			lvl = this.entries[pos].level+1;
			for (i = pos; i >= 0; i--) {
				if (this.entries[i].level < lvl) {
					this.entries[i].showChilds();
					lvl = this.entries[i].level;
			  }
			  else
				this.entries[i].hideChilds();
			}
		}
		if (pos < cnt-1) {
			for (i = pos+1; i < cnt; i++)
				this.entries[i].hideChilds();
		}
	}
	
	this.layout = this.initLayoutTree();
	this.initHRefs();
}

function NaviEntry(_id, _levl, _url,_title) {
	this.id    = _id;
	this.level = _levl;
	this.url   = _url;
	this.title = _title;
	if ( _id ) {
		this.element  = document.getElementById( _id );
		this.subElement = document.getElementById( "sub"+_id );;
	}
	else {
		this.element = null;
		this.subElement = null;
	}
	//this.height   = this.element.offsetHeight;
	//this.width    = this.element.offsetWidth;
	//this.subHeight= this.subElement.scrollHeight;
	//this.subWidth = this.subElement.scrollWidth;

	this.unsetCurrent = function() {
		if (this.element)
			this.element.className = this.element.className.replace(/\s*curr/, "");
	}
	this.setCurrent = function() {
		if (this.element)
			this.element.className = this.element.className.concat(" curr");
	}
	this.show = function() {
		if (this.element)
			this.element.style.display = "";
	}
	this.hide = function() {
		if (this.element)
			this.element.style.display = "none";
	}
	this.showChilds = function() {
		var mover;
		
		if (this.subElement)
			this.subElement.style.display = "";
	}
	this.hideChilds = function () {
		var mover;
		
		if (this.subElement)
			this.subElement.style.display = "none";
	}
}

function Mover() {
	this.element  = 0;
	this.distance = 0;
	this.step     = 0;
	
	this.calc = function () {
		dist = abs(this.distance);
		step = abs(this.step);

		if (distance < 0)
			step = -step;
		if (distance > step*step/2)
			step++;
		if (distance < step*(step-1)/2)
			step--;
		
		distance -= step;
		
		if (this.distance > 0)
			this.distance = distance;
		else
			this.distance = -distance;
		if (this.step > 0)
			this.step = step;
		else
			this.step = -step;
		return distance > 0 || step > 1;
	}
	this.moveX = function ( element, distance ) {
		this.element = element;
		this.distance = distance;
		this.step = 0;
		this.timer = window.setInterval("this.moveXImpl()", 100);
	}
	this.moveXImpl = function() {
		if (!this.calc())
			window.clearInterval(this.timer);
		this.element.offsetLeft -= this.step;
	}
	this.moveHeight = function ( element, distance ) {
		this.element = element;
		this.distance = distance;
		this.step = 0;
		this.timer = window.setInterval("this.moveXImpl()", 100);
	}
	this.moveHeightImpl = function() {
		if (!this.calc())
			window.clearInterval(this.timer);
		this.element.height -= this.step;
	}
}

