amapa="";oldmap="";
mam_mapu=false;
oldzoom=111;mzoom=0;
polozky_s=new Array();
nacteno=new Array();
polozky_i=new Array();
phmenu=new Array();
phmenu[0]=new Array("Informace o objektu","","i");
aktivovan_help=false;
posledni_vnorena_mapa="";

function ukaz_trasu(trasa)
	{
	frames.map.showNumbers(0);
	var i, ntrasa;
	if (trasa)
		{
		ntrasa=trasa.split(",");
		frames.map.setRoute(ntrasa);
		frames.map.showRoute(1);
		}
	else frames.map.showRoute(0);
	}

function vygeneruj()
	{
	var sp_ret="&zobrazit=0",sp_ar,xy;
	if (mas_mapu)
		{
		xy="&x="+Math.round(frames["map"].getXPos())+"&y="+Math.round(frames["map"].getYPos())+"&zoom="+frames["map"].getZoom();
		sp_ar=frames['map'].getMark();
		if(sp_ar[0]) sp_ret="&zobrazit=1&x_sp="+sp_ar[1]+"&y_sp="+sp_ar[2];
		var textik="http://www.webmapy.cz/app/sluknovsko/app/app.php?mapa="+amapa+xy+sp_ret;
		ooo=window.open("odkaz.php?ret="+escape(textik), "odkaz", "toolbar=no, menubar=no, location=no, directories=no, scrollbars=no, resizable=yes, status=no, width=800, height=111");
		}
	}

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=vyska_zalozek+"px";
			gid("fr"+i).style.display="block";
			gid("t"+i).style.background="url(../img0/zalozky/a.gif)";
			if (!i) gid("z"+i).style.background="url(../img0/zalozky/az.gif)";
			else gid("z"+i).style.background="url(../img0/zalozky/pa.gif)";
			if (cislo==zalozky.length-1) gid("z"+(i+1)).style.background="url(../img0/zalozky/ak.gif)";
			else gid("z"+zalozky.length).style.background="url(../img0/zalozky/pk.gif)";
			}
		else
			{
			gid("fr"+i).style.width="0";
			gid("fr"+i).style.height="0";
			gid("fr"+i).style.display="none";
			gid("t"+i).style.background="url(../img0/zalozky/p.gif)";
			if (!i) gid("z"+i).style.background="url(../img0/zalozky/pz.gif)";
			else if (i==cislo+1) gid("z"+i).style.background="url(../img0/zalozky/ap.gif)";
			else gid("z"+i).style.background="url(../img0/zalozky/pp.gif)";
			if (cislo==zalozky.length-1) gid("z"+(i+1)).style.background="url(../img0/zalozky/pk.gif)";
			}
		}
	if(cislo==zalozky.length-1 && !aktivovan_help)
		{
		aktivovan_help=true;
		frames[zalozky[zalozky.length-1]].location.href="help.php";
		}
	}

function gid(co)
	{
	if (document.getElementById(co)) return document.getElementById(co);
	else return false;
	}

function ukaz_prepinac(zoom)
	{
	if (mam_mapu)
		{
		if (oldzoom!=111) gid("prepinac"+oldmap+oldzoom).src='../img0/img/t_zl.gif';
		if (mzoom) gid("prepinac"+amapa+zoom).src='../img0/img/t_fial.gif';
		}
	}

function zprepinac(kolik,xmapa,switching)
	{
	var add=4;
	var kam,popis;
	var krok=6/kolik;
	var textik="";
	if (xmapa==main_map) kam="prepinac_r"; else kam="prepinac_v";
	popis=dmapy[xmapa];
	for (i=0;i<kolik;i++) textik=textik+"<a href='javascript:frames[\"map\"].setViewOnMap("+i+",frames[\"map\"].getXPos(),frames[\"map\"].getYPos(),\""+xmapa+"\");'><img src='../img0/img/t_zl.gif' alt='' width='"+(Math.floor(i+1)*krok+add)+"' height='"+(Math.floor(i+1)*krok+4)+"' title='Zoom "+i+"' border='0' name='prepinac"+xmapa+i+"' id='prepinac"+xmapa+i+"'  class=\"button\" style=\"padding:0px;margin:0px 1px;\"></a>";
	if(switching) mzoom=i;
	gid(kam).innerHTML = popis+":&nbsp;"+textik;
	}

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(1);
	}

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)
	{
	var sp_ret="&zobrazit=0",sp_ar,xy;
	if (mam_mapu) xy="&x="+Math.round(frames["map"].getXPos())+"&y="+Math.round(frames["map"].getYPos())+"&zoom="+frames["map"].getZoom();
	else xy="&x=&y=&zoom=";
	sp_ar=frames['map'].getMark();
	if(sp_ar[0]) sp_ret="&zobrazit=1&x_sp="+sp_ar[1]+"&y_sp="+sp_ar[2];
	document.cookie="is="+jak+";path=/";
	self.location.href=co+".php?mapa="+amapa+xy+"&zobrazit=0&is="+jak+sp_ret;
	}

function jazyk(jazyk)
	{
	document.cookie="lng="+jazyk+";path=/";
	self.location.href+=(self.location.href.indexOf("?")>0?"&":"?")+"lng="+jazyk;
	}

function vytvor_url()
	{
	var mapa;
	if(mas_mapu())
		{
		document.odesilac.purl.value="ww="+r_sirka+"&wh="+r_vyska+"&x="+Math.round(frames["map"].getXPos())+"&y="+Math.round(frames["map"].getYPos())+"&zoom="+frames["map"].getZoom()+"&mapa="+frames["map"].mapa;
		document.odesilac.mapa.value=frames["map"].mapa;
		document.odesilac.zoom.value=frames["map"].getZoom();
		document.odesilac.sx.value=Math.round(frames["map"].getXPos());
		document.odesilac.sy.value=Math.round(frames["map"].getYPos());
		document.odesilac.sm.value=Math.round(frames["map"].getXPos() - frames["map"].px2m_x(frames["map"].m2px_x(frames["map"].getXPos())-r_sirka));
		document.odesilac.vm.value=Math.round(frames["map"].px2m_y(frames["map"].m2px_y(frames["map"].getYPos())-r_vyska)-frames["map"].getYPos());
		document.odesilac.sirka_sipky.value=frames["map"].r_o_x[frames["map"].getZoom()];
		document.odesilac.vyska_sipky.value=frames["map"].r_o_y[frames["map"].getZoom()];
		if (window.innerWidth)
			{
			document.odesilac.sirka_okna.value=window.innerWidth-20;
			document.odesilac.vyska_okna.value=top.innerHeight-20;
			}
		else
			{
			document.odesilac.sirka_okna.value=document.body.clientWidth;
			document.odesilac.vyska_okna.value=top.document.body.clientHeight;
			}
		return true;
		}
	else return false;
	}// funkce pro zpracování událostí mapy

function viewChanged(cx,cy,vnorena_mapa)
	{
	var pole;
	if(vnorena_mapa && posledni_vnorena_mapa!=vnorena_mapa)
		{
		pole=vnorena_mapa.split(",");
		zprepinac(Number(pole[1]),pole[0],0);
		}
	else if (!vnorena_mapa && amapa==main_map) gid("prepinac_v").innerHTML="";
	if(cx && cy && lmapa) lmapa.view_ram(cx,cy);
	posledni_vnorena_mapa=vnorena_mapa;
	}

function mapChanged (jak,urovni)
	{
	amapa=jak;
	mam_mapu=true;
	gid("mapsel").value=jak;
	zprepinac(urovni,jak,1);
	ukaz_prepinac(frames['map'].getZoom());
	frames['map'].setMenu(phmenu);
	gid("prehledka").src="../img0/localmap/prehledka_"+jak+".gif";
	gid("nazevmapy").innerHTML=dmapy[jak];
	if(parametry[jak]) lmapa = new localmap(jak,parametry[jak],rozmery_navigatoru[0],rozmery_navigatoru[1]);
	else lmapa = new localmap(main_map,parametry[main_map],rozmery_navigatoru[0],rozmery_navigatoru[1]);
	setTimeout("frames['map'].zoomChanged();frames['map'].viewChanged();",150);
	if (frames["vyber"])
		{
		frames["vyber"].show_selected();
		if(frames["vyber"].document.getElementById("vylety_select")) frames["vyber"].document.getElementById("vylety_select").options[0].selected=true;
		}
	}

function zoomChanged (zoom,mppx)
	{
	if (oldzoom!=zoom) ukaz_prepinac(zoom)
	oldzoom=zoom;
	oldmap=amapa;
	zmen_meritko(mppx);
	if(lmapa) lmapa.zoom=zoom;
	}

function objectClicked(pco,akce)
	{
	var aakce=akce_objektu[pco[0]];
	if(aakce)
		{
		switch (aakce.substr(0,1))
			{
			case "1": window.open("http://"+aakce.substr(1)); break;
			case "2": alert((aakce.substr(1)).replace("@@@","\r\n")); break;
			case "3": self.status=pco[5]+". Žádné další informace k tomuto objektu nejsou k dispozici."; break;
			}
		}
	else
		{
		frames["objekt"].location.href="../app/objekt.php?id="+pco[0];
		}
	}// funkce pro obsluhu navigátoru

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(navigator.userAgent.indexOf("Gecko")==-1) this.node.parentNode.style.overflow="hidden";
	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 || rtop+rheight>=this.pheight-2 || rleft<(this.pwidth-this.max_width)/2 || rleft+rwidth>=(this.max_width-(this.max_width-this.pwidth)/2)-2) this.ram.style.border="none";
			//else this.ram.style.border="solid red 2px";

			if(rleft)this.ram.style.left=rleft+"px";
			if(rtop) this.ram.style.top=rtop+"px";
			if(rwidth) if(rwidth>0) this.ram.style.width=rwidth+"px";
			if(rheight) if(rheight>0) 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;
	}

function default_prehledka()
	{
	gid("prehledka").src='../img0/localmap/prehledka_'+main_map+'.gif';
	}
