var bublina = false;

function Init (ScriptType) {
	var s;
	s = document.getElementById('footer').innerHTML;
	s = s + " <a class=\"button tisk\" href=\"javascript:void(0)\" onclick=\"PrintPreview();return false;\" title=\"Vytiskne stránku / zobrazí tiskovou verzi\">Tisk</a>\n";
	if (ScriptType == 2) {s = s + " <a class=\"button close\" href=\"javascript:void(0)\" onclick=\"window.close();return false;\" title=\"Zavře toto okno (tab)\">Zavřít</a>\n";};
	if (ScriptType == 1) {
		s = s + " <a class=\"button\" href=\"javascript:void(0)\" onclick=\"SetO(/jr[2]/i);return false;\" title=\"Přepíná zobrazení sloupce odjezdů Pracovní dny\">pracovní dny</a>\n";
		s = s + " <a class=\"button\" href=\"javascript:void(0)\" onclick=\"SetO(/jr[34]/i);return false;\" title=\"Přepíná zobrazení sloupce odjezdů Prázdniny\">prázdniny</a>\n";
		s = s + " <a class=\"button\" href=\"javascript:void(0)\" onclick=\"SetO(/jr[56]/i);return false;\" title=\"Přepíná zobrazení sloupců odjezdů Sobota a Neděle\">nepracovní dny</a>\n";
		s = s + " <a class=\"button\" href=\"javascript:void(0)\" onclick=\"SetE('span','vuz','inline');return false;\" title=\"Přepíná zobrazení detailů o trakci vozidla\">vozidla</a>\n";
		s = s + " <a class=\"button\" href=\"javascript:void(0)\" onclick=\"SetE('span','dop','inline');return false;\" title=\"Přepíná zobrazení detailů o dopravci\">dopravci</a>\n";
		};
	document.getElementById('footer').innerHTML = s;
	if (ScriptType == 3) {
		document.getElementById('swapbutton').innerHTML = '<input type="button" class="submit swapbutton" value="&darr;&uarr;" title="Vyměnit odjezdovou a příjezdovou zastávku" onclick="Swap();">';
		LoadingOff();
		};
	};
function SetO (re) {
	var els, el;
	els = document.getElementsByTagName("th");
	for (var i = 0; i < els.length; i++) {el = els[i]; if (el.className.match(re)) {el.style.display = el.style.display ? (el.style.display == 'none' ? 'table-cell' : 'none') : 'none'};};
	els = document.getElementsByTagName("td");
	for (var i = 0; i < els.length; i++) {el = els[i]; if (el.className.match(re)) {el.style.display = el.style.display ? (el.style.display == 'none' ? 'table-cell' : 'none') : 'none'};};
	};
function SetE (b, c, d) {
	var els, el;
	els = eval ('document.getElementsByTagName("' + b + '")');
	for (var i = 0; i < els.length; i++) {el = els[i]; if (el.className == c) {el.style.display = el.style.display != d ? d : 'none';};};
	};
function SetS () {
	document.getElementById('menu').style.display = document.getElementById('menu').style.display == 'none' ? 'block' : 'none';
	document.getElementById('main').className = document.getElementById('main').className == 'main2' ? 'main1' : 'main2';
	};
function StandardView () {
	var el;
	if (el = document.getElementById('print_preview')) {el.disabled = true;};	
	if ( (el = document.getElementById('piktogram1')) && (w = el.src.match(/img\/b(\w*).gif$/)) ) {el.src = 'img/n' + w[1] + '.gif'};
	if ( (el = document.getElementById('piktogram2')) && (w = el.src.match(/img\/b(\w*).gif$/)) ) {el.src = 'img/n' + w[1] + '.gif'};
	if (el = document.getElementById('message')) {el.parentNode.removeChild(el);};
	};
function PrintPreview () {
	var el, k, w;
	el = document.createElement ('a');
	el.id = 'message';
	el.name = 'message';
	el.className = 'button close';
	el.appendChild (document.createTextNode ('Přepnout zpět do standardního zobrazení'));
	el.onclick = function () {StandardView();};
	el.title = 'Zobrazí standardní verzi stránky';
	k = document.getElementById('main');
	k.parentNode.insertBefore (el,k);
	window.location.href = '#message';
	if (el = document.getElementById('print_preview')) {el.disabled = true; el.disabled = false;};
	if ( (el = document.getElementById('piktogram1')) && (w = el.src.match(/img\/n(\w*).gif$/)) ) {el.src = 'img/b' + w[1] + '.gif'};
	if ( (el = document.getElementById('piktogram2')) && (w = el.src.match(/img\/n(\w*).gif$/)) ) {el.src = 'img/b' + w[1] + '.gif'};
	window.print();
	};
function LoadingOn (t) {document.writeln('<div id="loading">'+t+'<\/div>');};
function LoadingOff () {document.getElementById('loading').style.display='none';};
function Swap () {
	if (document.QueryForm.odjezd && document.QueryForm.prijezd) {
		var p = document.QueryForm.odjezd.value;
		document.QueryForm.odjezd.value = document.QueryForm.prijezd.value;
		document.QueryForm.prijezd.value = p;
		}
	else if (document.QueryForm.odjezdS && document.QueryForm.prijezdS) {
		var n, t = new Array (), v = new Array ();
		n = document.QueryForm.odjezdS.length;
		for (var i = 0; i < n; i++) {
			t[i] = document.QueryForm.odjezdS.options[i].text;
			v[i] = document.QueryForm.odjezdS.options[i].value;
			};
		document.QueryForm.odjezdS.length = document.QueryForm.prijezdS.length;
		for (var i = 0; i < document.QueryForm.prijezdS.length; i++) {
			document.QueryForm.odjezdS.options[i].text = document.QueryForm.prijezdS.options[i].text;
			document.QueryForm.odjezdS.options[i].value = document.QueryForm.prijezdS.options[i].value;
			};
		document.QueryForm.prijezdS.length = n;
		for (var i = 0; i < n; i++) {
			document.QueryForm.prijezdS.options[i].text = t[i];
			document.QueryForm.prijezdS.options[i].value = v[i];
			};
		};
	};
function Switch (source, which) {
	var el = document.getElementById(which+'H');
	source.parentNode.disabled = 'true';	
	source.parentNode.style.display = 'none';
	el.name = which;
	el.type = 'text';
	el.focus();
	};

function SetCookie (key, value, days, path, domain, secure) {
	value = encodeURIComponent(value);
	value+='; domain='+domain;
	value+='; path='+path;
	var date = new Date();
	date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
	value+='; expires=' + date.toGMTString();
	if (secure) value+='; secure';
	document.cookie = key + '=' + value;
	return true;
	}
function GetCookie (key) {
	var jednaKukina, hodnota;
	var kukiny = document.cookie.split("; ");
	for (i in kukiny) {
		jednaKukina = kukiny[i].split("=");
		if (jednaKukina[0] == key) {hodnota = jednaKukina[1];};
		return hodnota;
		}
	}
function Show (z) {
	SetCookie ('location', z, 1, '/', 'jrbrno.cz', false);
	var o = window.open ('', 'idsjmk.jrbrno.cz:mapa');
	o.focus();
	try {o.location.href == null;} catch (e) {return false;};
	o.location.href = 'http://idsjmk.jrbrno.cz'
	return true;
	}

function GetElementPosition (el) {
	var position = [0, 0];
	while (el.offsetParent !== null) {
		position[0] += el.offsetLeft;
		position[1] += el.offsetTop;
		el = el.offsetParent;
	}
	return position;
}

function GetElementFromEvent (event) {
	var el = false;
	if (window.event && window.event.srcElement) {el = window.event.srcElement} 
		else if (event && event.target) {el = event.target}
			else return false;
	return el;
}

function AddEvent (el, event, fn) {
  if (el.addEventListener) {
    el.addEventListener (event, fn, false);
    return true;
  } else if (el.attachEvent) {
		var r = el.attachEvent ("on" + event, fn);
    return r;
  } else {return false;}
}

function MakeNiceTitles () {
	if (!document.createElement || !document.getElementsByTagName) return false;
	var el;
	var els = document.links || document.getElementsByTagName ("a");
	for (var i = 0; i < els.length; i++) {
		el = els[i]; 
		if (el.title) {
			el.setAttribute ("nicetitle", el.title);
			el.removeAttribute ("title");
			AddEvent (el, "mouseover", ShowNiceTitle);
			AddEvent (el, "focus", ShowNiceTitle);
			AddEvent (el, "mouseout", HideNiceTitle);
			AddEvent (el, "blur", HideNiceTitle);
		}
	}
}

function ShowNiceTitle (event) {
	var el = GetElementFromEvent (event);
	var position = GetElementPosition (el);
	if (!bublina) {
		bublina = document.createElement ("div");
		bublina.className = "nicetitle";
		document.getElementsByTagName("body")[0].appendChild (bublina);
	}
	HideNiceTitle ();
	var wd = document.getElementsByTagName("body")[0].offsetWidth;
  while (el.nodeName.toLowerCase() != "a") {el = el.parentNode;}
  if (!el) return false;
  bublina.innerHTML = ParseToolTip (el.getAttribute ("nicetitle"));
	bublina.style.display = "block";
	var wb = bublina.offsetWidth;
	bublina.style.left = (wd - position[0] - wb < 20 ? wd - wb - 5 : position[0] + 20) + "px";
	bublina.style.top = (position[1] + 25) + "px";
	return true;
}

function HideNiceTitle (event) {
	if (bublina) {bublina.style.display = "none";}
	return true;
}

function ParseToolTip (text) {
	var c = "";
	var a = text.replace (/−>/gi, "&rarr;");
	a = a.replace (/  /gi, " <br>");
	a = a.split (" ");
	switch (a[0]) {
		case "tramvaj":	c = "d"; break;
		case "trolejbus":	c = "t"; break;
		case "autobus":	c = "a"; break;
		case "noční":	c = "n"; a[0] = a[0] + ' ' + a[1]; a[1] = a[2]; a[2] = ''; break;
		case "loď":	c = "l"; break;
		case "vlak":	c = "v"; break;
		case "info":	c = "i"; break;
	}
	if (c.length > 0) {
		a[1] = a[1].replace (/\//gi, " / ");
		a[0] = '<span class="' + c + '">' + a[0];
		a[1] = '<b>' + a[1] + '</b></span>';
	}
	return (a.join (" "));
}

AddEvent (window, "load", MakeNiceTitles);


