﻿var nav = new function() {
	this.list = [];
	this.selObj = [0];
	
	this.sel = function(id) { // select element with id
		// alert("nav.sel("+id+")");
		var ss = this.get(id);
		if (!ss['page']) { // if their are children, get first child
			var ssC = this.child(id);
			if (ssC) {
				id = ssC['id'];
				ss = this.get(id);
			}
		}
		if (ss) { // element found
			// unselect all SelObj[]
			for (n in this.selObj) {
				var o = this.selObj[n];
				if (o) o.attr("class","btn"+n);
			}
			this.selObj.length = 1;
			
			this.contractAll();
			this.expand(id);
			var o = null;
			var n = null;
			var ss1 = ss;
			while (ssP = ss1['parent']) {
				this.expand(ssP);
				o = $("#\\~"+ssP);
				ss1 = this.get(ssP);
				var level = ss1['level'];
				if (o) {
					o.attr("class","sel"+level);
					this.selObj[level] = o;
				}
				
			}
			o = $("#\\~"+ss['id']);
			if (o) {
				level = ss['level'];
				o.attr("class","sel"+level);
				this.selObj[level] = o;
				return id;
			}
		}
		return false;
	};
	
	this.go = function(el,param,anchor) { // select element el and load page
		// alert("nav.go("+el+","+param+","+anchor+")");
		var id = el.attr("id").substr(1); // remove '~'
		var cl = el.attr("class");
		if (cl.substr(0,3) == "sel") { // element is already selected
			var n = this.get(id);
			if (!n['page']) return false; // element has no page, do nothing
			var lev = cl.substr(3)*1;
			if (this.selObj.length <= lev+1) return false; // no child active, do nothing
		}
		if (id = this.sel(id)) {
			if (isOpera) return true;
			var nn = this.get(id);
			if (nn) {
				if (typeof(_gaq) != 'undefined') {
					_gaq.push(['_trackPageview','/'+nn['title']]);
				}
				var u = nn['page']+"?cat="+id;
				if (typeof param == 'string' && param) u += '&'+param;
				if (typeof anchor == 'string' && anchor) u += '#'+anchor;
				loadPage(u);
				document.title = MainTitle+" - "+nn['titleLong'];
				$("#header_txt").text(nn['titleLong']);
				return false;
			}
		}
		return true;
	};
	
	this.goCat = function(id,param,anchor) { // go to page with "cat" == "id"
		// alert("nav.goCat("+id+")");
		var n = this.get(id);
		return this.go($("#\\~"+n['id']),param,anchor);
	};
	
	this.goPage = function(title,param,anchor) { // go to page with this "title" or "url"
		// alert(title);
		var id = this.findTitle(title);
		if (id) return this.goCat(id,param,anchor);
		return true;
	};
	
	this.findTitle = function(title) { // find page with this "title" or "url"
		for (n in this.list) {
			if ((this.list[n]['title'] == title) || (this.list[n]['page'] == title)) {
				return this.list[n]['id'];
			}
		}
		return 0;
	}
	
	this.get = function(id) {
		for (n in this.list) {
			var n1 = this.list[n];
			if (n1['id'] == id) {
				return n1;
			}
		}
		return null;
	};
	
	this.parent = function(id) {
		var n = this.get(id);
		if (n) return n['parent'];
		return null;
	};
	
	this.child = function(id) {
		for (n in this.list) {
			var n1 = this.list[n];
			if (n1['parent'] == id) {
				return n1;
			}
		}
		return null;
	};
	
	this.expand = function(id) { $("#sub_\\~"+id).show(); };
	
	this.contractAll = function() { $("#nav .switchcontent").hide(); };
};

