var dom = true;
if (!document.getElementById) dom = false;
if (!document.getElementById && document.all) {document.getElementById = document.all; dom = true;}
isIE = (document.all && !window.opera) ? true:false;

function byId(what) {return document.getElementById(what);}
function byTag(what) {return document.getElementsByTagName(what);}

//if (isIE) document.write("<style>table.dynamic tr {behavior: url('/js/table-hover.htc');}</style>");

window.onload = init;

function init() {
	//vytvoreni elementu abbr v IE 
	makeAbbr();  
	//dynamicke efekty formularu
	dynamicForms();
	//dynamicke efekty tabulek v IE
	dynamicTables();
	//je-li stranka nactena v ramu, nacte se do top ramu
	if (top.length != self.length) top.location.href = self.location.href; 	
	//stavovy radek
	window.defaultStatus = "Shaman.cz | Tak trochu jinou cestou"; 
}

// kontrola prazdneho inputu
function isEmpty(e) {   
    return ((e == null) || (e.length == 0));
}

// kontrola emailu
function isEmail(mail)
{
    re = /^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,7}$/;
    return mail.search(re) == 0;
}

//vytvoreni elementu abbr v IE 
function makeAbbr() {
    var oldBodyText, newBodyText, reg
    if (isIE) {
      oldBodyText = document.body.innerHTML;
      reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
      newBodyText = oldBodyText.replace(reg,'<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>');
      document.body.innerHTML = newBodyText;
    }
  }

//prednacteni obrazku tlacitek, nasvecovani tlacitek, inputu, textarea a odkazovacich img
function dynamicForms(){
	if ((document.images) && (dom)){
		imgs=Array(1);
		y = 0;
		for (x = 0; x < byTag('input').length; x++)
    	{
			if (byTag('input')[x].getAttribute('type') == 'image')
			{
				//nacteni aktivniho tlacitka do pameti
				imgs[y] = new Image();
				path = byTag('input')[x].src;
				imgs[y].src = path.substring(0, path.length - 5) + "1.gif";
				y++;
				//na tlacitka zavesim fci
				byTag('input')[x].onmouseover = function() {changeButton(this, 1);}
				byTag('input')[x].onmouseout = function() {changeButton(this, 0);}
				byTag('input')[x].onfocus = function() {changeButton(this, 1);}
				byTag('input')[x].onblur = function() {changeButton(this, 0);}
			}
			else if ((byTag('input')[x].getAttribute('type') == 'text') || (byTag('input')[x].getAttribute('type') == 'password'))
			{
				// je-li policko vyhledavani "sp-q", odebere se nebo prida Najit...
				byTag('input')[x].onfocus = function() {this.style.background = "#fff5f3"; if ((this.name == "sp-q") && (this.value == "Najít...")) this.value = "";}
				byTag('input')[x].onblur = function() {this.style.background = "#fff"; if ((this.name == "sp-q") && (isEmpty(this.value))) this.value = "Najít...";}
				if (byTag('input')[x].className.indexOf('focused') > -1) byTag('input')[x].focus();
			}
		}
		for (x = 0; x < byTag('textarea').length; x++)
    	{
			byTag('textarea')[x].onfocus = function() {this.style.background = "#fff5f3";}
			byTag('textarea')[x].onblur = function() {this.style.background = "#fff";}
			if (byTag('textarea')[x].className.indexOf('focused') > -1) byTag('textarea')[x].focus();
		}	
		
		for (x = 0; x < byTag('img').length; x++)
    	{
			cesta = byTag('img')[x].src;
			if (cesta.substring(cesta.length - 6, cesta.length) == '-0.gif')
			{
				//nacteni aktivniho tlacitka do pameti
				imgs[y] = new Image();
				path = byTag('img')[x].src;
				imgs[y].src = path.substring(0, path.length - 5) + "1.gif";
				y++;
				//na tlacitka zavesim fci
				byTag('img')[x].onmouseover = function() {changeButton(this, 1);}
				byTag('img')[x].onmouseout = function() {changeButton(this, 0);}
				byTag('img')[x].onfocus = function() {changeButton(this, 1);}
				byTag('img')[x].onblur = function() {changeButton(this, 0);}
			}
		}	
	}
}

//dynamicke efekty na radku tabulky v IE 
function dynamicTables() {
	if ((isIE) && (dom))
	{
		for (x = 0; x < byTag('table').length; x++)
	    {
			if (((' '+byTag('table')[x].className+'').indexOf("dynamic") != -1)) 
			{
				trs = byTag('table')[x].getElementsByTagName('tr');
				for(var i = 0; i < trs.length; i++)
				{
		            trs[i].onmouseover = function() {this.style.background = "#fff1ef";}
		            trs[i].onmouseout = function() {this.style.background = "#fff";}
        		}

			}
		}
	}
}

// zmena obrazku u form. tlacitka - u puvodni cesty zmenim stav a vratim ji
function changeButton(that, what) {
	if (dom) 
	{
		path = that.src;
		path = path.substring(0, path.length - 5) + what + ".gif";
		that.src = path;
	}
}

// kontrola vyhl. formulare
function checkSearch(form) {
	if ((isEmpty(form[0].value)) || (form[0].value == "Najít..."))
		{
		alert("Je třeba zadat hledané slovo.");
		form[0].focus();
		return false;
		}
	else return true;
}

// klik pravym tlacitkem vede k zrcadlu
function kZrcadlu(en) { 
	if (isIE) { 
		var tlacitko = event.button;
		if (tlacitko == 2) tlacitko++; 
	}
	else var tlacitko = en; 
	server = window.location.toString().substring(0, 20);
	if ((tlacitko == 3) && (window.location != server + "/zrcadlo/") && (confirm("Chcete se pokusit projít Zrcadlem?"))) location.href = server + "/zrcadlo/"; 
	return false; 
}
