var startJS = false;

if (document.getElementById) {
	if (document.styleSheets) {
		/* MZ: application/xhtml+xml IE: text/html */
		if (document.styleSheets[0].cssRules) {
			document.styleSheets[0].insertRule("@import url('workjs.css') screen;",0);
			startJS = true;
		}
		else if (document.styleSheets[0].rules) {
			document.styleSheets[0].addImport("workjs.css",0);
			startJS = true;
		}
	}
	else {		
		/* OPERA: text/html */
		//document.write('<link rel="stylesheet" type="text/css" href="css/workjs.css" media="all"/>');
	}
}


function init() {
	if (document.getElementById && styl()) {
		tableCollSwitchInit();
		postDrawMenu();
	}
}


function styl() {
	if (!startJS) return false;
	if (!document.styleSheets) return true;
	return !document.styleSheets[0].disabled;
}


function opentab(el) {
	if (styl()) {
		el.blur();
		tabId = getHref(el);
		if (tabs = getParentByClassName(el,"tabs").getElementsByTagName("a")) {
			for (i=0;i<tabs.length;i++) {
				if (getHref(tabs[i]) == tabId) {
					if (!hasClassName(tabs[i],"open")) addClassName(tabs[i],"open");
				}
				else removeClassName(tabs[i],"open");
			}
		}

		return setOpenToTab(tabId);
	} else return true;
}

var cTimer;
var cTimerMap;
var cTimerMapMenu;
var cTimerInfoTab;

function showtab(tabId) {
	if (styl()) {
		clearTimeout(cTimer);
		return setOpenToTab(tabId);
	} else return true;
}

function closetab() {
	if (styl()) {
		cTimer=setTimeout("showtab('langs-info')",800);
	}
}


function setOpenToTab(tabId) {
	if (tab = document.getElementById(tabId)) {
		if (tabs = getElementsByClassName(getParentByClassName(tab,"box"),"tab","div")) {
			for (i=0;i<tabs.length;i++) {
				if (tabs[i].id == tabId) {
					if (!hasClassName(tabs[i],"open")) addClassName(tabs[i],"open");
				}
				else removeClassName(tabs[i],"open");
			}
		}
		return false;
	} else return true;
}

function collapse(el) {
	if (styl()) {
		el.blur();
		if (box = getParentByClassName(el,"box")) {
			if (!hasClassName(box,"collapsed")) addClassName(box,"collapsed");
			else removeClassName(box,"collapsed");
			return false;	
		}
	}
}

function aKraj(el) {
	if (styl()) {
		clearTimer();
		cr = document.getElementById("mapa-cr");
		id = getHref(el);
		if (cr.className != id) {
			do_dKraj();
			addClassName(cr,id);
			cTimerMapMenu=setTimeout("do_aMapMenu(id)",1000);
		}
		return false;
	}
}

function aMapMenu(el) {
	if (styl()) {
		id = getHref(el);
		do_aMapMenu(id);
		return false;
	}
}

function do_aMapMenu(id) {
	if (el=document.getElementById(id)) {
		if (!hasClassName(el,"open")) {
			if (crm = document.getElementById("mapa-cr-menu")) addClassName(crm,id);
			addClassName(el,"open");
		}
	}
}

function do_dMapMenu(id) {
	if (el=document.getElementById(id)) {
		if (hasClassName(el,"open")) {
			removeClassName(el,"open");
			document.getElementById("mapa-cr-menu").className="";
		}
	}
}
	
function do_dKraj() {
	el = document.getElementById("mapa-cr");
	if (el.className) {
		clearTimer();
		closeMenu();
		do_dMapMenu(el.className);
		document.getElementById("mapa-cr").className = "";
	}
}

function dKraj() {
	if (styl()) {
		cTimerMap=setTimeout("do_dKraj()",200);
	}
}

function clearTimer () {
	clearTimeout(cTimerMap);
	clearTimeout(cTimerMapMenu);
}



var lastOpenInfoTab="";
var lastOverRow="";

function openitab(el) {
	if (styl()) {
		clearTimeout(cTimerInfoTab);
		
		els = el.parentNode.getElementsByTagName(el.tagName);
		
		for(act=0;act<els.length;act++) {
			if (el==els[act]) break;
		}

		if (p = getParentByClassName(el.parentNode,"tab","div")) {
			a = getElementsByClassName(p,"info-tab","div");

			if (a.length > act) {
				if (!hasClassName(a[act],"open")) {
					do_closeitab()
					addClassName(a[act],"open");
					addClassName(el,"hover");
					lastOpenInfoTab = a[act];
					def = (document.all)?321:300;
					if (document.body.clientWidth < 920) {
						if (document.body.clientWidth < 810) a[act].style.width = def - (920-810) +"px";
						else a[act].style.width = def - (920-document.body.clientWidth) +"px";
					}
					else a[act].style.width="";
					lastOverRow = el;
				}
			}
		}

	} else return true;
}

function do_closeitab() {
	if (styl()) {
		if (lastOpenInfoTab!="") {
			removeClassName(lastOpenInfoTab,"open");
			removeClassName(lastOverRow,"hover");
			lastOpenInfoTab="";
			lastOverRow="";
		}
	}
}

function closeitab() {
	if (styl()) {
		cTimerInfoTab=setTimeout("do_closeitab()",1000);
	}
}


function nocloseitab() {
	if (styl()) {
		clearTimeout(cTimerInfoTab);
	}
}



function tableCollSwitchInit(){
	var i;
	var opentdtext;
	th = getElementsByClassName(document,"open","th");
	if (th.length > 0) {
		for (i=0;i<th.length;i++) {
			ths = getElementsByClassName(th[i].parentNode,"sw","th");
			if (ths.length > 0) {
				/* menu */
				var ul = document.createElement("ul");
				for (j=0;j<ths.length;j++) {
					var a = document.createElement("a");
					if (hasClassName(ths[j],"open")) opentdtext=ths[j].childNodes[0].nodeValue;
					txt = ths[j].childNodes[0].nodeValue;
					a.appendChild(ths[j].childNodes[0])
					var sp=document.createElement("span")
					sp.className="swth";
					sp.appendChild(document.createTextNode(txt));
					ths[j].appendChild(sp);
					ths[j].childNodes[0]
					a.href = "#"+j;
					a.onclick = tableCollSwitch;
					var li = document.createElement("li");
					li.appendChild(a);
					ul.appendChild(li);
				}
				var ulm = document.createElement("ul");
				var li = document.createElement("li");
				var a = document.createElement("a");
				var s = document.createElement("span");
				s.appendChild(document.createTextNode(opentdtext));
				s.className="anchor";
				a.appendChild(s);
				a.href="#menu-sloupcu";
				a.onclick=tableNone;
				a.title="Kliknutím rozbalíte menu s výběrem sloupců k zobrazení.";
				li.appendChild(a);
				li.appendChild(ul);
				ulm.appendChild(li);
				ulm.className="menu onclick";
				th[i].appendChild(ulm);
				
				/* select 

				var s = document.createElement("select");
				for (j=0;j<ths.length;j++) {
					var o = document.createElement("option");
					o.appendChild(ths[j].childNodes[0])
					if (hasClassName(ths[j],"open")) o.selected="selected";
					o.value = j;
					s.appendChild(o);
				}
				s.onchange = tableCollSwitch;
				th[i].appendChild(s);
				*/
			}
		}
	}
}

function tableNone() {
	return false;
}

function tableCollSwitch (event) {
	var val = getHref(this);
	//var val = this.value;
	var lastTD = 1;
	tds = getElementsByClassName(document,"sw","td");
	if (tds.length>0) {
		var i=0;
		while (i<tds.length) {
			atds = getElementsByClassName(tds[i].parentNode,"sw","td");
			lastTD = atds.length;
			for (j=0;j<lastTD;j++) {
				if (j == val) {
					if (!hasClassName(tds[i+j],"open")) addClassName(tds[i+j],"open");
				}
				else if (hasClassName(tds[i+j],"open")) removeClassName(tds[i+j],"open");
			}
			i=i+lastTD;
		}
	}
	th = getElementsByClassName(document,"open","th");
	for (i=0;i<th.length;i++) {
		/* menu */
		m = getElementsByClassName(th[i],"menu","ul");
		m[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0].nodeValue = this.childNodes[0].nodeValue;

		/* select 
		m = th[i].getElementsByTagName("select");
		m[0].value = val;
		*/
	}
	closeMenu();
	return false;
}









function getHref(el) {
	href=el.getAttribute("href");
	return href.substr(href.lastIndexOf("#")+1);

}

/*-[ class manage ]------------------------------------------*/

function hasClassName (el, name) {
	var i, list;

	list = el.className.split(" ");
	for (i = 0; i < list.length; i++)
	if (list[i] == name) return true;
	return false;
};

function removeClassName (el, name, c) {
	var i, j, curList, newList;
	if (el.className == "") return;

	newList = new Array();
	curList = el.className.split(" ");
	j=0;
	for (i = 0; i < curList.length; i++) 
		if (curList[i] != name) 
			newList[i-j]=curList[i];
	    else j++;
	if (c!=1)el.className = newList.join(" ");
	else return newList.join(" ");
};


function addClassName (el, name) {
	if ((el.className == "") || (el.className == " ")) el.className = name;
	else el.className += " " + name;
};

/*-----------------------------------------------------------*/

function getParentByClassName(el,name) {
	while (el !=null && el.parentNode != null && !hasClassName(el,name)) {
		el = el.parentNode;
	}
	if (el !=null && el.parentNode != null)	return el;
	else return false;
}

function getElementsByClassName(el,class_name,tag_name) {
	var all_obj,ret_obj=new Array(),j=0;
	all_obj=el.getElementsByTagName(tag_name);
	for(i=0;i<all_obj.length;i++) {
	    if(hasClassName(all_obj[i],class_name)) {
	      ret_obj[j]=all_obj[i];
	      j++;
		}
	}
	if (ret_obj.length>0) return ret_obj; else return false;
}