mam_mapu=false;
oldzoom=10;mzoom=0;
polozky_s=new Array();

a_zalozka=0;
aktualni_mapa="";
lmapa=null;

function jazyk(jazyk)
	{
	document.cookie="lng="+jazyk+";path=/";
	self.location.href=self.location.href+(self.location.href.indexOf("?")>5?"&":"?")+"lng="+jazyk;
	}

function gen_str()
	{
	gid("retezec").innerHTML="mapa="+frames["map"].mapa+"&zobrazit=1&x="+Math.floor(frames["map"].getXPos())+"&y="+Math.floor(frames["map"].getYPos())+"&zoom="+frames["map"].getZoom();
	}

function prepniz(cislo)
	{
	var i;
	a_zalozka=cislo;
	for (i=0;i<zalozky.length;i++)
		{
		if (cislo==i)
			{
			gid("fr"+i).style.width="100%";
			gid("fr"+i).style.height="100%";
			gid("t"+i).style.background="url(../img0/"+aregion+"/zalozky/a.gif)";
			if (!i) gid("z"+i).style.background="url(../img0/"+aregion+"/zalozky/az.gif)";
			else gid("z"+i).style.background="url(../img0/"+aregion+"/zalozky/pa.gif)";
			if (cislo==zalozky.length-1) gid("z"+(i+1)).style.background="url(../img0/"+aregion+"/zalozky/ak.gif)";
			else gid("z"+zalozky.length).style.background="url(../img0/"+aregion+"/zalozky/pk.gif)";
			}
		else
			{
			gid("fr"+i).style.width="0px";
			gid("fr"+i).style.height="0px";
			gid("t"+i).style.background="url(../img0/"+aregion+"/zalozky/p.gif)";
			if (!i) gid("z"+i).style.background="url(../img0/"+aregion+"/zalozky/pz.gif)";
			else if (i==cislo+1) gid("z"+i).style.background="url(../img0/"+aregion+"/zalozky/ap.gif)";
			else gid("z"+i).style.background="url(../img0/"+aregion+"/zalozky/pp.gif)";
			if (cislo==zalozky.length-1) gid("z"+(i+1)).style.background="url(../img0/"+aregion+"/zalozky/pk.gif)";
			}
		}
	}

function viewChanged(cx,cy)
	{
	if(lmapa!=null)lmapa.view_ram(cx,cy);;
	}

function ukaz_prepinac(zoom)
	{
	if (mam_mapu)
		{
		if (oldzoom<=mzoom) gid("prepinac"+oldzoom).src='../img0/t_zl.gif';
		if (zoom<=mzoom) gid("prepinac"+zoom).src='../img0/'+aregion+'/img/t_fial.gif';
		}
	}

function zprepinac(kolik)
	{
	var krok=8/kolik,i;
	var textik="";
	gid("prepinac").innerHTML="";
	for (i=0;i<kolik;i++) textik=textik+"<a href='javascript:frames[\"map\"].setView("+i+",frames[\"map\"].getXPos(),frames[\"map\"].getYPos());'><img src='../img0/t_zl.gif' width='"+(Math.floor(i+1)*krok+4)+"' height='"+(Math.floor(i+1)*krok+4)+"' alt='Zoom "+i+"' border='0' name='prepinac"+i+"' id='prepinac"+i+"'  style=\"margin-right:3px;vertical-align:bottom;\"></a>";
	mzoom=i;
	gid("prepinac").innerHTML=textik;
	}

function mapChanged (jak,urovni)
	{
	var i;
	mam_mapu=true;
	zprepinac(urovni);
	ukaz_prepinac(frames['map'].getZoom());
	frames['map'].setMenu(phmenu);
	for(i=0;i<mapy.length;i++) if(mapy[i]==jak) break;
	document.pmapa.pmapa.selectedIndex=i;
	aktualni_mapa=jak;
	lmapa = new localmap("zalesi",new Array(3690482,5437389,3717682,5455149),170,111);
	//gid("prehledka").src="../img0/localmap/prehledka_"+jak+".gif";
	}

function zoomChanged (zoom,mppx)
	{
	if (oldzoom!=zoom) ukaz_prepinac(zoom)
	oldzoom=zoom;
	zmen_meritko(mppx);
	if(lmapa) lmapa.zoom=zoom;
	}

function mas_mapu()
	{
	if (mam_mapu) return true;
	else
		{
		self.status="Počkejte prosím, až se nahraje mapa.";
		return false;
		}
	}

function tiskni()
	{
	if (mas_mapu()) frames['map'].prn(true);
	}

function zmen_meritko(mppx)
		{
		gid("gmeritko").border=1;
		var delka=maxmer*mppx;
		var rad=Math.floor(Math.log(delka)/Math.log(10));
		var kd=delka/(Math.pow(10,rad));
		var odecti=0;
		if (rad>2)
			{
			jednotka="km";
			odecti=3;
			}
		else jednotka="m";
		gid("gjednotka").innerHTML=jednotka;
		gid("gnula").innerHTML="0";
		if (kd==10) {kd=1;rad++}
		if (kd>=5)
			{
			gid("gmeritko").src="../img0/meritka/c50.gif";
			gid("gdelka").innerHTML=5*Math.pow(10,(rad-odecti));
			gid("gmeritko").style.width=Math.round(5*Math.pow(10,rad)/mppx)+"px";
			}
		else if (kd>=2)
			{
			gid("gmeritko").src="../img0/meritka/c20.gif";
			gid("gdelka").innerHTML=2*Math.pow(10,rad-odecti);
			gid("gmeritko").style.width=Math.round(2*Math.pow(10,rad)/mppx)+"px";
			}
		else
			{
			gid("gmeritko").src="../img0/meritka/c100.gif";
			gid("gdelka").innerHTML=Math.pow(10,rad-odecti)
			gid("gmeritko").style.width=Math.round(Math.pow(10,rad)/mppx)+"px";
			}
		}

function zmen_s(co,jak)
	{
	if (mas_mapu())
		{
		document.cookie="is="+jak+";path=/";
		self.location.href=co+".php?x="+Math.round(frames["map"].getXPos())+"&y="+Math.round(frames["map"].getYPos())+"&zoom="+frames["map"].getZoom()+"&no=0&nic=nic&is="+jak;
		}
	}


function odesli_mokoli()
	{
	if (document.okoli.elements[1].checked | document.okoli.elements[2].checked | document.okoli.elements[3].checked  | document.okoli.elements[4].checked | document.okoli.elements[5].checked | document.okoli.elements[6].checked)
		{
		document.okoli.sx.value=frames['map'].getXPos();
		document.okoli.sy.value=frames['map'].getYPos();
		o_okno('','seznam',502,500,5);
		return true;
		}
	else {alert ("Zvolte alespoň jednu kategorii objektů pro vyhledávání."); return false}
	}

function prep_mokoli(hodnota)
	{
	for (i=1; i<7; i++)
		{
		document.okoli.elements[i].checked=hodnota;
		}
	}

function check_mokoli()
	{
	km=document.okoli.radius.value;
	km=km.replace(",",".");
	if (!(isFinite(km) & km>=0.1 & km<=20))
		{
		alert ("Vložte prosím číslo v rozsahu 0.1 – 20.");
		document.okoli.radius.value=1;
		}
	}
function napln_polozky(cim)
	{
	var i,j,ppole;
	obsah=new Array();
	for (i=0;i<cim.length;i++)
		{
		for (j=0;j<cim[i].length;j++)
			{
			ppole=cim[i][j].split("#");
			if (j==0) document.ukaz.elements[i].options[0]=new Option(cim[i][j],"",false,true);
			else if (ppole[2]!=0)document.ukaz.elements[i].options[document.ukaz.elements[i].options.length]=new Option(ppole[5],cim[i][j],false,false);
			}
		}
	}

function ukaz_cp(xy)
		{
		var xy;
		xy=xy.split(",");
		frames.map.setMarkOnMap("max",xy[0],xy[1],xy[2]);
		}

	function objectClicked(ido,akce)
		{
		gid("info").innerHTML="Název objektu:&nbsp;<b>"+ido[5]+"</b>";
		}

	function switchLayer(pole,jak)
		{
		var i;
		for (i=0;i<polozky_s[pole].length;i++)
			{
			a_polozka=polozky_s[pole][i].split("#");
			with (eval ("document.ukaz."+pole)) {options[options.length]=new Option(a_polozka[5],polozky_s[pole][i],false,false)};
			}
		}

	function ukazx(hodnota)
		{
		hodnota=hodnota.split("#");
		frames['map'].setMarkOnMap(hodnota[1],hodnota[2],hodnota[3],"myjavsko");
		objectClicked(hodnota,0);
		}

	function indexuj_odkazy(pole) {}

	function vyber_mapu(cislo)
		{
		frames["map"].changeMap(mapy[cislo]);
		}

// Navigátor:
lmapa=null;

function rf() {return false;}

function localmap(nazev,params,pwidth,pheight)
	{
	this.ld_x=params[0];
	this.ld_y=params[1];
	this.ph_x=params[2];
	this.ph_y=params[3];
	this.main_map_width=style2px(gid("map").style.width)-18;
	this.main_map_height=style2px(gid("map").style.height)-18;
	this.pwidth=pwidth;
	this.pheight=pheight;
	this.size_x=this.ph_x-this.ld_x;
	this.size_y=this.ph_y-this.ld_y;
	this.mppx=frames["map"].mppx;
	this.amppx=(this.size_x/this.pwidth+this.size_y/this.pheight)/2;

	this.node=document.getElementById("lmap");
	this.node.innerHTML="";
	this.node.style.position="absolute";
	this.node.style.left="0px";
	this.node.parentNode.style.position="relative";
	this.node.parentNode.style.height=pheight+"px";
	this.node.parentNode.style.width=pwidth+"px";
	if (this.node.style.cursor) this.node.style.cursor="pointer";
	this.max_width=style2px(this.node.parentNode.parentNode.style.width);

	this.ram=document.createElement("img");
	this.ram.style.position="absolute";
	this.ram.style.zIndex=99;
	this.ram.style.border="red 2px solid";
	this.ram.src="../img0/mezera.gif";
	this.ram.style.visibility="hidden";
	this.ram.title="Kliknutím případně kliknutím a tažením změníte výřez mapy.";
	if (this.node.style.cursor) this.ram.style.cursor="pointer";
	this.node.appendChild(this.ram);

	this.mapnode=document.createElement("img");
	this.mapnode.style.position="absolute";
	this.mapnode.src="../img0/localmap/"+nazev+".gif";
	this.mapnode.style.width=pwidth+"px";
	this.mapnode.style.height=pheight+"px";
	this.mapnode.style.border="none";
	this.mapnode.style.zIndex=10;
	this.mapnode.title=this.ram.title;
	this.node.appendChild(this.mapnode);

	this.m2px_x=m2px_x; this.m2px_y=m2px_y; this.px2m_x=px2m_x; this.px2m_y=px2m_y;
	this.view_ram=view_ram;

	this.node.onmousedown=dragstart;
	this.ram.onmousedown=dragstart;
	this.node.oncontextmenu=rf;
	this.ram.oncontextmenu=rf;
	}

	function m2px_x(souradnice_m) {return(souradnice_m-this.ld_x)/(this.size_x/this.pwidth);}
	function m2px_y(souradnice_m) {return(this.ph_y-souradnice_m)/(this.size_y/this.pheight);}
	function px2m_x(souradnice_px) {return((souradnice_px*this.size_x)/this.pwidth+this.ld_x);}
	function px2m_y(souradnice_px) {return(this.size_y+this.ld_y-(souradnice_px*this.size_y/this.pheight));}

	function view_ram(sx,sy)
		{

		if(!this.dragging)
			{
			this.rcenter_x=Math.round(this.m2px_x(sx));
			this.rcenter_y=Math.round(this.m2px_y(sy));
			var rwidth=Math.round((this.main_map_width*this.mppx[this.zoom])/this.amppx);
			var rheight=Math.round((this.main_map_height*this.mppx[this.zoom])/this.amppx);
			var rleft=Math.round(this.rcenter_x-rwidth/2)-2;
			var rtop=Math.round(this.rcenter_y-rheight/2)-2;
			if(rtop<=0)
				{
				this.ram.style.borderTop="none";
				rheight+=rtop;
				rtop=0;
				}
			else this.ram.style.borderTop="solid red 2px";

			if(rtop+rheight>=this.pheight-2)
				{
				this.ram.style.borderBottom="none";
				rheight=this.pheight-rtop-2;
				//rheight+=rtop==0?2:0;
				}
			else this.ram.style.borderBottom="solid red 2px";

			if(rleft<(this.pwidth-this.max_width)/2)
				{
				this.ram.style.borderLeft="none";
				rwidth-=(this.pwidth-this.max_width)/2-rleft;
				rleft=(this.pwidth-this.max_width)/2;
				}
			else this.ram.style.borderLeft="solid red 2px";

			if(rleft+rwidth>=(this.max_width-(this.max_width-this.pwidth)/2)-2)
				{
				this.ram.style.borderRight="none";
				rwidth=(this.max_width-(this.max_width-this.pwidth)/2)-2-rleft;
				}
			else this.ram.style.borderRight="solid red 2px";

			if(rleft)this.ram.style.left=rleft+"px";
			if(rtop) this.ram.style.top=rtop+"px";
			if(rwidth) this.ram.style.width=rwidth+"px";
			if(rheight) this.ram.style.height=rheight+"px";
			this.ram.style.visibility="visible";
			}
		}

function drag(e)
	{
	e.cancelBubble=true;
	this.end=end_drag;
	this.draw=draw_drag;
	this.map=lmapa;
	this.start_page_x=e.pageX;
	this.start_page_y=e.pageY;
	this.start_nlayer_x=e.nlayerX;
	this.start_nlayer_y=e.nlayerY;
	this.drag_type= (e.currentTarget==this.map.ram && e.mouse_button==2) ? "move" : "zoom";
	if (this.drag_type=="move") if (this.map.node.style.cursor) {this.map.ram.style.cursor="move";this.map.node.style.cursor="move";}
	this.map.node.onmousemove=dragging;
	this.map.ram.onmousemove=dragging;
	document.body.onmouseup=dragend;
	this.map.ram.onmouseup=dragend;
	this.offset_x=0;
	this.offset_y=0;
	this.start_pos_x=style2px(this.map.ram.style.left);
	this.start_pos_y=style2px(this.map.ram.style.top);
	}

	function draw_drag(e)
		{
		this.offset_x=e.pageX-this.start_page_x;
		this.offset_y=e.pageY-this.start_page_y;
		switch (this.drag_type)
			{
			case "zoom":
				this.map.ram.style.left=(this.offset_x<0 ? this.start_nlayer_x+this.offset_x : this.start_nlayer_x)+"px";
				this.map.ram.style.top=(this.offset_y<0 ? this.start_nlayer_y+this.offset_y : this.start_nlayer_y)+"px";
				this.map.ram.style.width=Math.abs(this.offset_x)+"px";
				this.map.ram.style.height=Math.abs(this.offset_y)+"px";
				self.status="Vyberte výřez v mapě.";
				break;
			case "move":
				if(this.start_pos_x+this.offset_x>0 && this.offset_x+this.start_pos_x+style2px(this.map.ram.style.width)<this.map.pwidth) this.map.ram.style.left=(this.start_pos_x+this.offset_x)+"px";
				if(this.start_pos_y+this.offset_y>0 && this.offset_y+this.start_pos_y+style2px(this.map.ram.style.height)<this.map.pheight) this.map.ram.style.top=(this.start_pos_y+this.offset_y)+"px";
				frames["map"].go2(this.map.px2m_x(style2px(this.map.ram.style.left)+style2px(this.map.ram.style.width)/2),this.map.px2m_y(style2px(this.map.ram.style.top)+style2px(this.map.ram.style.height)/2));
				break;
			}
		}

	function end_drag(e)
		{
		if (Math.abs(this.offset_x)<2 && Math.abs(this.offset_y)<2)
			{
			frames["map"].setView(frames["map"].getZoom,this.map.px2m_x(e.nlayerX),this.map.px2m_y(e.nlayerY));
			}
		else switch (this.drag_type)
			{
			case "zoom":
				var i;
				var smx=Math.abs(this.map.px2m_x(this.start_nlayer_x)-this.map.px2m_x(this.start_nlayer_x+this.offset_x))/this.map.main_map_width;
				var smy=Math.abs(this.map.px2m_y(this.start_nlayer_y)-this.map.px2m_y(this.start_nlayer_y+this.offset_y))/this.map.main_map_height;
				var tmppx=(smx+smy)/2;
				var mindif=this.map.mppx[0];
				var target_zoom=0;
				for (i=0;i<this.map.mppx.length;i++) if (Math.abs(tmppx-this.map.mppx[i])<mindif)
					{
					mindif=Math.abs(tmppx-this.map.mppx[i]);
					target_zoom=i;
					}
				setTimeout('frames["map"].setView('+target_zoom+','+this.map.px2m_x(this.start_nlayer_x+this.offset_x/2)+','+this.map.px2m_y(this.start_nlayer_y+this.offset_y/2)+')',100);
				break;
			case "move":
				if (this.map.node.style.cursor)
					{
					this.map.ram.style.cursor="pointer";
					this.map.node.style.cursor="pointer"
					}
				break;
			}
		this.map.node.onmousemove=null;
		this.map.ram.onmousemove=null;
		document.body.onmouseup=null;
		this.map.ram.onmouseup=null;
		}

function repair_event(e)
	{
	if(!e.currentTarget) e.currentTarget=e.srcElement;
	if(isNaN(e.pageX)) {e.pageX=e.clientX+document.body.scrollLeft; e.pageY=e.clientY+document.body.scrollTop;}
	if(isNaN(e.layerX)) {e.layerX=e.offsetX; e.layerY=e.offsetY;}
	e.mouse_button=e.button;
	if (e.button==1 && navigator.appName=="Microsoft Internet Explorer") e.mouse_button=0;
	if(e.currentTarget==lmapa.ram)
		{
		e.nlayerX=e.layerX+style2px(lmapa.ram.style.left);
		e.nlayerY=e.layerY+style2px(lmapa.ram.style.top);
		}
	else
		{
		e.nlayerX=e.layerX;
		e.nlayerY=e.layerY;
		}
	return e;
	}

function dragstart(e)
	{
	e=repair_event(e||window.event);
	lmapa.dragging=new drag(e);
	return false;
	}

function dragend(e)
	{
	e=repair_event(e||window.event);
	lmapa.dragging.end(e);
	delete lmapa.dragging;
	return false;
	}

function dragging(e)
	{
	e=repair_event(e||window.event);
	lmapa.dragging.draw(e);
	return false;
	}