function id(id){return document.getElementById(id);}
var loading="<center><span class='load'>Načítání...</span></center>";
var loadingimg="<center><span class='load'>&nbsp;&nbsp;&nbsp;</span></center>";

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

var xmlHttp;
var ajaxresponse;

function site(){return window.location.protocol+'//'+window.location.hostname;}

function ajax(url, ids, method, test)
{
	if (test == 1)
	{
		id('status').innerHTML = url;
	}
	
	if (window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest)
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	xmlHttp.onreadystatechange = function()
	{ 
		if (xmlHttp.readyState == 4)
		{
			if (xmlHttp.status == 200)
			{
				if (test == 'js')
				{
					eval(xmlHttp.responseText);
				}
				else if (test == 'text')
				{
					xmlHttp.responseText;
				}
				else
				{
					id(ids).innerHTML=xmlHttp.responseText;
				}
			}
		}
	}
	
	xmlHttp.open(method, url, true);
	xmlHttp.send(null);
}
var q;

function suggest()
{
	var sinput = trim(id('sinput').value, ' ');
	var nq = id('sinput').value;
	
	if (sinput.length > 1)
	{
		if (q != nq)
		{
			q = nq;
			ajax(site()+'/?type=ajax&mod=suggest&q='+q, '', 'GET', 'js');
		}
	}
	else
	{
		id('suggest').style.visibility = "hidden";
	}
}

function onpload()
{
	if (id('sinput'))
	{
		id('sinput').onblur = function () {setTimeout('suggexit()',1000)};
		id('sinput').focus();
	}
	
	if (id('fsearch'))
	{
		//id('fsearch').onclick = show_hide_fs;
	}
	
	//if (id('s1')){id('s1').onclick=function(){ajax(site()+'/?type=ajax&mod=search&section=1','','GET','js');return false;};}
	//if (id('s2')){id('s2').onclick=function(){ajax(site()+'/?type=ajax&mod=search&section=2','','GET','js');return false;};}
	//if (id('s3')){id('s3').onclick=function(){ajax(site()+'/?type=ajax&mod=search&section=3','','GET','js');return false;};}
	//if (id('s4')){id('s4').onclick=function(){ajax(site()+'/?type=ajax&mod=search&section=4','','GET','js');return false;};}
	if (id('sc2')){id('sc2').innerHTML=sc(2);}
	if (id('sc3')){id('sc3').innerHTML=sc(3);}
	

}

var suggestnum = 0;
var keypos = 0;

function suggexit()
{
	id('suggest').style.visibility="hidden";
	keypos = 0;
}

function gos()
{
	if (keypos > 0)
	{
		window.location = id("sugurl"+keypos).href;
		return false;
	}
	else
	{
		return true;	
	}
}

function show_hide_fs()
{
	if (id("fsearch").className == 'fshide' || id("fsearch").className == '')
	{
		// hide	
		// $("#fsearchf").slideUp();
		id("fsearch").className = 'fsshow';
		id('fsearchf').style.visibility="hidden";
		id('fsearchf').style.display="none";
		
		ajax(site()+'/?type=ajax&mod=search&full=0','','GET','js');
	}
	else
	{
		// show
		id("fsearch").className = 'fshide';
		id('fsearchf').style.display="block";
		id('fsearchf').style.visibility="visible";
		
		ajax(site()+'/?type=ajax&mod=search&full=1','','GET','js');
	}
}

function getkey(evt)
{
	suggest();
	
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	if (suggestnum != 0 && suggestnum >= keypos)
	{
		// down
		if (charCode == 40)
		{
			if (suggestnum > keypos)
			{
				keypos++;
				id("sug"+keypos).style.backgroundColor = "#9CC1E1";
			}
			
			if (keypos > 1)
			{
				id("sug"+(keypos-1)).style.backgroundColor = "";
			}	
		}
		// up
		else if(charCode == 38)
		{
			if (keypos > 1)
			{
				keypos--; 
				id("sug"+keypos).style.backgroundColor = "#9CC1E1";
				id("sug"+(keypos+1)).style.backgroundColor = "";
			}
		}
		// enter
		else if (charCode == 13)
		{
			window.location = id("sugurl"+keypos).href;
			return false;
		}
	}
	return false;
}

/*function addfav(url, title)
{
	if (document.all)
	{
		window.external.AddFavorite(url, title);
	}
}*/



function addfav(url, title)
{
	//title = document.title; 
	//url = window.location.href;

 if (window.sidebar) { // Mozilla Firefox Bookmark
	 window.sidebar.addPanel(title, url,"");
 } else if( window.external ) { // IE Favorite
	 window.external.AddFavorite( url, title); }
 else if(window.opera && window.print) { // Opera Hotlist
	 return true; }
}


function addtobox(id, name, url)
{
	ajax(site()+'/?type=ajax&mod=box&id='+id+'&name='+name+'&url='+url, '', 'GET', 'js');
}

/*function addtobox(id)
{
	ajax(site()+'/?type=ajax&module=box&id='+id, '', 'GET', 'js');
}*/

function addtoboxlink(id)
{
	ajax(site()+'/?type=ajax&module=box&from=detail&id='+id, '', 'GET', 'js');
}

function del_bi(i, del, id)
{
	ajax(site()+'/?type=ajax&mod=box&id='+i+'&delete='+del+'&item_id='+id, '', 'GET', 'js');
}

function lupa(typ)
{
	if (typ == 1)
	{
	document.write('<center><a href="http://obchodnirejstrik.cz/?red=http%3A%2F%2Fkristalova.lupa.cz%2Fnominace%2F" target="_blank"><img src="http://obchodnirejstrik.cz/img/88x31_nominace.gif" style="margin-top:5px;" /></a> <br><font color="#cc0000">Nominujte obchodnirejstrik.cz v kategorii Vyhledávače a databáze. Více informací <a href="http://obchodnirejstrik.cz/nominace/">zde</a>.<br></font>  </center>');
	}
	else
	{
	document.write('<center><a href="http://obchodnirejstrik.cz/?red=http%3A%2F%2Fkristalova.lupa.cz%2Fnominace%2F" target="_blank"><img src="http://obchodnirejstrik.cz/img/200x200_nominace.gif" /></a><br><br><font color="#cc0000">Nominujte obchodnirejstrik.cz v kategorii Vyhledávače a databáze. <br>Více informací <a href="http://obchodnirejstrik.cz/nominace/">zde</a>.<br></font>  </center>');
	}
}

function altlink(href, title)
{
	document.write('- <a href="'+href+'" rel="nofollow" target="_blank" style="color:#6666cc;font-size:12px; margin-left:10px;margin-bottom:5px;">'+title+'</a> &raquo;');
}


function sc(id)
{
	if (id == 1)
	{
		document.write('<br /><br /><br /><iframe src="http://www.skotska-whisky.cz/?advert=10" scrolling="no" allowtransparency="true" marginwidth="0" marginheight="0" frameborder="0" style="border-width:0;border:none;width:745px;height:104px;"></iframe>');
	}
	else if (id == 2)
	{
		var content = '<a href="http://upv.cz/cs/sluzby-uradu/databaze-on-line.html" title="Ochranné známky" target="_blank">Ochranné známky</a>';
	}
	else if (id == 3)
	{
		var content = '<a href="http://www.mapy.cz/" title="Mapy.cz" target="_blank">Mapy.cz</a>';
	}
	
	if (content != '')
	{
		return content;	
	}
}

$(document).ready(function() {

	if ($(".adhelp").length)
	{
		/*
		$(".adhelp").fadeOut(0);
		$(".adhelp").fadeIn("slow");
		*/
	}
});

function watchman_link(name, ic, rc, id)
{
	if (id == null)
	{
		id = ic;
	}
	
	if (rc == null)
	{
		rc = '';
	}
	
	$.ajax({
		url: site()+"?type=ajax&mod=admin&cat=add&name="+name+"&ic="+ic+"&rc="+rc+"&eid="+id,
		dataType: "script",
		success: function(html){ }
	});
}