function $_(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function onload_ready(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function xpopup(url,name) {
	pos_left = (screen.width) ? (screen.width-750)/2 : 0;
	pos_top  = (screen.height) ? (screen.height-525)/3 : 0;

	settings = "width=750,height=525,top=" + pos_top + ",left=" + pos_left + ",scrollbars=no";
	win = window.open(url,"p"+name,settings);
	win.focus();
}

function xpopup2(url) {
	pos_left = (screen.width) ? (screen.width-750)/2 : 0;
	pos_top  = (screen.height) ? (screen.height-525)/3 : 0;

	settings = "width=750,height=525,top=" + pos_top + ",left=" + pos_left + ",scrollbars=no";
	win = window.open(url,"team",settings);
	win.focus();
}

function toggle(el) {
	$_(el).className = $_(el).className == "" ? "active" : "";
}

function show_elements(obj) {
	obj.className = "active";
}

function hide_elements(obj) {
	obj.className = "";
}

function tree_list() {
	if (!document.getElementsByTagName || !document.createTextNode) return;
	if ($_("tree-list")) {
		var items = document.getElementById("tree-list").getElementsByTagName("div");
		var items_length = items.length;

		for (i = 0; i < items_length; i++) {
			if(items[i].className.indexOf("item") != -1) {
				items[i].onmouseover = function() {	
					this.className = this.className + " hover";

					var id = this.id.substr(this.id.indexOf("_") + 1,this.id.length) || 0;
					var obj = $_("title_" + id);
					var objl = position_left(this) || 0;
					var objw = obj.offsetWidth || 0;
					var w = page_width() || 0;
					
					obj.style.marginLeft = 0 + "px";
					
					if(obj) {
						if((w - objl) < objw) {
							obj.style.marginLeft = 60 - objw + "px";
							obj.style.textAlign = "right";
						} else if ((objl < (w/2)) && (objl > objw)) {
							obj.style.marginLeft = 60 - objw + "px";
							obj.style.textAlign = "right";
						} else {
							obj.style.marginLeft = -10 + "px";
							obj.style.textAlign = "left";
						}
					}
				}
				items[i].onmouseout = function() {
					this.className = this.className.substr(0,this.className.indexOf(" hover"));
				}
			}
		}
	}
}

var pos=0;

function content_size(){
	var h = page_height();
	var tmp = $_("top") ? $_("top").offsetHeight : 0;
	tmp = tmp + ($_("bottom") ? $_("bottom").offsetHeight : 0);

	if ($_("middle")) $_("middle").style.height = h >= 150 + tmp ? h - tmp + "px" : 150 + "px";
	
	var d = $_("middle").offsetHeight - $_("tree-scroll").offsetHeight;
	
	if(d < 0) $_("scroll-bottom").className = "colored";
		else $_("scroll-bottom").className = "deactive";
		
	if(d < 0 && pos < 0) $_("scroll-top").className = "colored";
		else $_("scroll-top").className = "deactive";
}

function scroll_bottom() {
	var obj = $_("tree-scroll");
	var d = $_("middle").offsetHeight - $_("tree-scroll").offsetHeight;
	if(d < 0 && pos > d-40){
		pos = pos - 90;
		obj.style.top = pos + "px";
	}
	if(pos <= d-40){
		$_("scroll-bottom").className = "deactive";
	}
	
	if(d < 0 && pos < 0) $_("scroll-top").className = "colored";
		else $_("scroll-top").className = "deactive";

	//scroll_time=setTimeout("scroll_bottom()", 5);
}
function scroll_top() {
	var obj = $_("tree-scroll");
	var d = $_("middle").offsetHeight - $_("tree-scroll").offsetHeight;
	
	if(pos < 0){
		pos = pos + 90;
		obj.style.top = pos + "px";
	}
	if(pos >= 0){
		$_("scroll-top").className = "deactive";
	}
	
	if(d < 0 && pos >= 0) $_("scroll-bottom").className = "colored";
		else $_("scroll-bottom").className = "deactive";

	//scroll_time=setTimeout("scroll_top()", 5);
}
function scroll_stop() {
	clearTimeout(scroll_time);
}

onresize = function(){
	content_size();
}
onload_ready(content_size);
onload_ready(tree_list);

/*-- element position --*/
function position_left(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}
	
function position_top(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
		    curtop += obj.offsetTop
		    obj = obj.offsetParent;
	    }
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}

/*-- page size --*/
function page_width(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	return w;
}

function page_height(){
	var de = document.documentElement;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	return h;
}

