// Main.js
// Copyright (c) 2005  Position s.r.o.
// All rights reserved.

var MapMode;
var Cmd;
var CmdParam;
var ControlsDisabled;
var Path;

function EnableControls()
{
	if(ControlsDisabled) 
	{
	  var o = document.getElementById('idControlLeft');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlRight');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlUp');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlDown');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlZoomIn');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlZoomOut');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlZoom');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlPan');
	  if(o) o.style.visibility='visible';
	  o = document.getElementById('idControlLevels');
	  if(o) o.style.visibility='visible';
	  ControlsDisabled=false;
	}
}

function DisableControls()
{
	if(!ControlsDisabled) 
	{
	  var o = document.getElementById('idControlLeft');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlRight');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlUp');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlDown');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlZoomIn');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlZoomOut');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlZoom');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlPan');
	  if(o) o.style.visibility='hidden';
	  o = document.getElementById('idControlLevels');
	  if(o) o.style.visibility='hidden';
	  ControlsDisabled=true;
	}
}

function preloadImages(the_images_array) 
{
   for(var loop = 0; loop < the_images_array.length; loop++)
   {
    	var an_image = new Image();
	    an_image.src = the_images_array[loop];
   }
}

function MapLoaded()
{
  if((idPreviewMap)&&(idMainMap)&&(idPreviewMap.UpdatePreview))
  {
    idPreviewMap.UpdatePreview(GetMapState(),idMainMap.PreviewRegion,GetMapObjects(),idMainMap.PreviewLeft,idMainMap.PreviewTop,idMainMap.PreviewRight,idMainMap.PreviewBottom);
  }
  if((idMapPanelFrame)&&(idMapPanelFrame.MapLoaded))
  {
    idMapPanelFrame.MapLoaded();  
  }
}

function SetClientGraphic(html)
{
  if((idMainMap)&&(idMainMap.SetClientGraphic)) idMainMap.SetClientGraphic(html);
}

function GetMapObjects()
{
  if((idMainMap)&&(idMainMap.GetMapObjects)) return idMainMap.GetMapObjects();
  return '';
}

function GetPreviewMapObjects()
{
  if((idPreviewMap)&&(idPreviewMap.GetMapObjects)) return idPreviewMap.GetMapObjects();
  return '';
}

function EscGeoCode(gs)
{
  gs=escape(gs);
  gs=gs.replace(/\+/g,"%2B");
  return gs;
}

function RunProcess(url)
{
	o = document.getElementById('idProcess');
	if(o) o.src=url;
}

function MapContainerLeft()
{
  var left=0; 
  if((idMainMap)&&(idMainMap.ContainerLeft)) 
    left=idMainMap.ContainerLeft;
  return left;
}

function MapContainerTop()
{
  var top=0;
  if((idMainMap)&&(idMainMap.ContainerTop)) 
    top=idMainMap.ContainerTop;
  return top;
}

function LocateMapObjects(objs, scale, disp)
{
  if(disp)
  {
    SetMapObjects(objs);
    SetPreviewMapObjects(objs);
    RunProcess(Path+'/src/mapobjects.php?STATE='+GetMapState()+'&PW_STATE='+GetPreviewMapState()+'&L='+MapContainerLeft()+'&T='+MapContainerTop()+'&OBJS='+EscGeoCode(objs)+'&SCALE='+scale+'&LOCATE=1');
  }
  else RunProcess(Path+'/src/locate.php?STATE='+GetMapState()+'&L='+MapContainerLeft()+'&T='+MapContainerTop()+'&OBJS='+EscGeoCode(objs)+'&SCALE='+scale);
}

function DisplayMapObjects(objs)
{
  if((GetMapObjects()==objs)&&(GetPreviewMapObjects()==objs)) return;
  SetMapObjects(objs);
  SetPreviewMapObjects(objs);
  RunProcess(Path+'/src/mapobjects.php?STATE='+GetMapState()+'&PW_STATE='+GetPreviewMapState()+'&L='+MapContainerLeft()+'&T='+MapContainerTop()+'&OBJS='+EscGeoCode(objs));
}

function SetMapObjects(objs)
{
  if((idMainMap)&&(idMainMap.SetMapObjects)) idMainMap.SetMapObjects(objs);
}

function SetPreviewMapObjects(objs)
{
  if((idPreviewMap)&&(idPreviewMap.SetMapObjects)) idPreviewMap.SetMapObjects(objs);
}

function RenderMapObjects(objects, preview) 
{
  if(preview)
  {
    if((idPreviewMap)&&(idPreviewMap.RenderMapObjects)) idPreviewMap.RenderMapObjects(objects,preview);
  }
  else
  {
    if((idMainMap)&&(idMainMap.RenderMapObjects)) idMainMap.RenderMapObjects(objects,preview);
  }
}

function SetMapObjectsHTML(html)
{
  if((idMainMap)&&(idMainMap.SetMapObjectsHTML)) idMainMap.SetMapObjectsHTML(html);
}

function SetPreviewMapObjectsHTML(html)
{
  if((idPreviewMap)&&(idPreviewMap.SetMapObjectsHTML)) idPreviewMap.SetMapObjectsHTML(html);
}

function ClearMapObjects()
{
  if((idMainMap)&&(idMainMap.ClearMapObjects)) idMainMap.ClearMapObjects();
  if((idPreviewMap)&&(idPreviewMap.ClearMapObjects)) idPreviewMap.ClearMapObjects();
}

function RenderMapObject(type,x,y,preview)
{
  if((idMapPanelFrame)&&(idMapPanelFrame.RenderMapObject))
  {
    return idMapPanelFrame.RenderMapObject(type,x,y,preview);
  }
  return '';
}

function RenderMapObjectDescr(x,y,type)
{
  var html='';
  if((idMapPanelFrame)&&(idMapPanelFrame.RenderMapObjectDescr))
    html=idMapPanelFrame.RenderMapObjectDescr(x,y,type);
  if((html=='')&&(idPanel)&&(idPanel.RenderMapObjectDescr))
    html=idPanel.RenderMapObjectDescr(x,y,type);
  if(html!='')
  {
    if(html.substring(0,1)=='#') html=html.substring(1,html.length);
    else 
    {
      html=html+'<p class="csDescrMenu">';
      html=html+'<a href="javascript:if((parent)&&(parent.SearchNearest)) parent.SearchNearest('+x+','+y+');">Najít nejbližší</a>';
      html=html+'</p>';
    }
  }
  return html;
}

function FormatScale(s)
{
  s=String(s);
  var i=s.indexOf('.');
  if(i<0) i=s.indexOf(',');
  if(i>=0) s=s.substring(0,i);

  var i,j;
  var o='';
  j=0;
  for (i=s.length-1;i>=0; i--) {
    if((j!=0)&&((j%3)==0)) o=' '+o;
    o=s.charAt(i)+o;
    j++;
  }
  return o;  
}

function RenderScale(width, text, current)
{
  var o = document.getElementById('idControlScale');
  if(o) 
  {
    var w1=width/3;
    var w2=width-2*w1;
    var alt='1 : '+FormatScale(current);
    var html='';
    html=html+'<img src="images/scale_b.gif" width="'+w1+'" height="11" alt="'+alt+'" align="top">';
    html=html+'<img src="images/scale_w.gif" width="'+w2+'" height="11" alt="'+alt+'" align="top">';
    html=html+'<img src="images/scale_b.gif" width="'+w1+'" height="11" alt="'+alt+'" align="top">';
    html=html+'&nbsp;&nbsp;'+text;    
    o.innerHTML = html;
  }
}

function RenderLevels(levels, currentlevel) 
{
  if(levels.length) 
	{
		var i,j,l;
		var f=-1;
    var sLevels = "";
    for (j=1,i=levels.length-1;i>=0;i--,j++)
		{
			l=String(Math.round(j*18/levels.length));
			if (l==0) l=1;
			if (i==currentlevel) { l='Now'; f=i; }
			if(j==1) sLevels += '<img src="images/zoomDown2.gif" alt="1 : '+FormatScale(levels[i])+'" class="HandCursor" width="34" height="7" onclick="return GotoScale('+levels[i]+')"><br>';
      sLevels += '<img src="images/zoom'+l+'.gif" alt="1 : '+FormatScale(levels[i])+'" class="HandCursor" width="34" height="9" onclick="return GotoScale('+levels[i]+')"><br>';
    }       
    if((idMainMap)&&(!idMainMap.MapScalable))
    {    	
			if(f==0) idMainMap.DisableZoomIn=true;
			if(f==levels.length-1) idMainMap.DisableZoomOut=true;
		}
    var o = document.getElementById('idControlLevels');
    if (o) o.innerHTML = sLevels;
  }
}

function SetMapMode(mode)
{
  var o = document.getElementById('idControlPan');
  if(o)
  {
	  if(mode=='P') o.src="images/pan_.gif";
	  else				  o.src="images/pan.gif";
	}
	
  var o = document.getElementById('idControlZoom');
  if(o)
  {
	  if(mode=='Z+') o.src="images/zoom_.gif";
	  else				   o.src="images/zoom.gif";
	}
	MapMode=mode;
}

function MapDisplayed()
{
	return (idMainMap);
}

function GetMapMode()
{
	return MapMode;
}

function Country()
{
  if((idMainMap)&&(idMainMap.Country)) return idMainMap.Country();
  return '';
}

function GetMapState()
{
  if((idMainMap)&&(idMainMap.MapState)) return idMainMap.MapState();
  return '';
}

function SetMapState(state)
{
  if((idMainMap)&&(idMainMap.SetMapState)) idMainMap.SetMapState(state);
}

function GetPreviewMapState()
{
  if((idMainMap)&&(idMainMap.MapState)) return idMainMap.PreviewMapState();
  return '';
}

function SetPreviewMapState(state)
{
  if((idMainMap)&&(idMainMap.SetPreviewMapState)) idMainMap.SetPreviewMapState(state);
}

function PanPreview(x, y)
{
  if((idMainMap)&&(idMainMap.PanPreview)) return idMainMap.PanPreview(x, y);
  else return false;
}

function GetPanMap()
{
  return ((document.idMainControl)&&(document.idMainControl.panmap)&&(document.idMainControl.panmap.value=='1') ? true : false);
}

function SetPanMap(v)
{
  if((document.idMainControl)&&(document.idMainControl.panmap))
  {
    document.idMainControl.panmap.value=(v ? '1' : '0');
    return true;
  }
  return false;
}

function GotoScale(scale)
{
  if((idMainMap)&&(idMainMap.GotoScale)) return idMainMap.GotoScale(scale);
  else return false;
}

function GotoRegion(region)
{
  if((idMainMap)&&(idMainMap.GotoRegion)) return idMainMap.GotoRegion(region);
  else return false;
}

function SetTheme(theme)
{
  CloseMapPanel();
  if((idMainMap)&&(idMainMap.SetTheme)) return idMainMap.SetTheme(theme);
  else return false;
}

function UpClick() {
  if((idMainMap)&&(idMainMap.MapUp)) idMainMap.MapUp();
}

function DownClick() {
  if((idMainMap)&&(idMainMap.MapDown)) idMainMap.MapDown();
}

function LeftClick() {
  if((idMainMap)&&(idMainMap.MapLeft)) idMainMap.MapLeft();
}

function RightClick() {
  if((idMainMap)&&(idMainMap.MapRight)) idMainMap.MapRight();
}

function ZoomInClick() {
  if((idMainMap)&&(idMainMap.MapZoomIn)) idMainMap.MapZoomIn();
}

function ZoomOutClick() {
  if((idMainMap)&&(idMainMap.MapZoomOut)) idMainMap.MapZoomOut();
}

function MaxDetailClick() {
  if((idMainMap)&&(idMainMap.MapMaxDetail)) idMainMap.MapMaxDetail();
}

function MinDetailClick() {
  if((idMainMap)&&(idMainMap.MapMinDetail)) idMainMap.MapMinDetail();
}

function PreviousViewClick() {
  if((idMainMap)&&(idMainMap.MapPreviousView)) idMainMap.MapPreviousView();
}

function NextViewClick() {
  if((idMainMap)&&(idMainMap.MapNextView)) idMainMap.MapNextView();
}

function RefreshClick() {
  if((idMainMap)&&(idMainMap.MapRefresh)) idMainMap.MapRefresh();
}

function ZoomBtnClick() {
  if((idMainMap)&&(idMainMap.ModeZoomIn)) idMainMap.ModeZoomIn();
}

function PanBtnClick() {
  if((idMainMap)&&(idMainMap.ModePan)) idMainMap.ModePan();
}

function MapLocate(id)
{
  if((idMainMap)&&(idMainMap.MapLocate)) idMainMap.MapLocate(id);
}

function ShowHTMLDescr(x,y,html,timeout) {
  if((idMainMap)&&(idMainMap.ShowHTMLDescr)) idMainMap.ShowHTMLDescr(x,y,html,timeout);
}

function ShowMapObjectsDescr(objects, id, timeout) {
  if((idMainMap)&&(idMainMap.ShowMapObjectsDescr)) idMainMap.ShowMapObjectsDescr(objects, id, timeout);
}

function ShowMapObjectDescr(x,y,id, timeout) {
  if((idMainMap)&&(idMainMap.ShowMapObjectDescr)) idMainMap.ShowMapObjectDescr(x,y,id,timeout);
}


function SearchNearestClick() {
  var x='';
  var y='';
  if(idMainMap)
  {
    if(idMainMap.PopupMenuX) x=idMainMap.PopupMenuX;
    if(idMainMap.PopupMenuY) y=idMainMap.PopupMenuY;
  }
  SearchNearest(x,y);
}

function SearchNearest(x,y) {
  HideMapPanel();
  SetPanelURL(Path+'/panels/menu_nearest.php?X='+x+'&Y='+y+'&STATE='+GetMapState());
}

function ShowMapPanel() {
	var o = document.getElementById('idMapPanel');
	if(o) 
  {
    if((idMainMap)&&(idMainMap.ContainerOrigin)) idMainMap.ContainerOrigin(0,-100);
    o.style.visibility = 'visible';	
  }
  EndWait();
}

function HideMapPanel() {
	var o = document.getElementById('idMapPanel');
	if(o) 
  {
    if((idMainMap)&&(idMainMap.ContainerOrigin)) idMainMap.ContainerOrigin(0,0);
    o.style.visibility = 'hidden';	
  }
  EndWait();
}

function OpenMapPanel(url) {
  ShowMapPanel();
	o = document.getElementById('idMapPanelFrame');
	if(o) o.src=url;
	StartWait();
}

function SetPanelURL(url) {
	o = document.getElementById('idPanel');
	if(o) o.src=url;
}

function SetMapPanelURL(url) {
	o = document.getElementById('idMapPanelFrame');
	if(o) o.src=url;
	StartWait();
}

function CloseMapPanel() {
  ClearMapObjects();
  HideMapPanel();
	o = document.getElementById('idMapPanelFrame');
	if(o) o.src='';
}

function HiliteImg(o,img)
{
  if((o)&&(img!='')) o.src='images/'+img+'_.gif';
}

function DimImg(o,img)
{
  if((o)&&(img!='')) o.src='images/'+img+'.gif';
}

function PopupMapMenu(id,x,y)
{
  DisableMenus();
  PopupMenu(id,x+39+2,y+38+2);
}

function DisableMenus()
{
  HideMenu('idMainPopupMenu');
}

function StartWait()
{
   var o=document.getElementById('idWaiting');
   if(o) o.style.visibility = 'visible';
}

function EndWait()
{
   var o=document.getElementById('idWaiting');
   if(o) o.style.visibility = 'hidden';
}

function HideWaitMessage()
{
   var o=document.getElementById('idWaitPage');
   if(o) o.style.visibility = 'hidden';
}

function Nop(e) 
{
 if(e) 
 {
   e.cancelBubble = true;
   if(e.stopPropagation) e.stopPropagation();
 }
 return false;
}

function Init()
{
   var the_images = new Array(
	   'images/pan.gif',
	   'images/pan_.gif',
	   'images/zoom.gif',
	   'images/zoom_.gif',
	   'images/zoomNow.gif',
	   'images/zoomDown2.gif',
	   'images/zoom1.gif',
	   'images/zoom2.gif',
	   'images/zoom3.gif',
	   'images/zoom4.gif',
	   'images/zoom5.gif',
	   'images/zoom6.gif',
	   'images/zoom7.gif',
	   'images/zoom8.gif',
	   'images/zoom9.gif',
	   'images/zoom10.gif',
	   'images/zoom11.gif',
	   'images/zoom12.gif',
	   'images/zoom13.gif',
	   'images/zoom14.gif',
	   'images/zoom15.gif',
	   'images/zoom16.gif',
	   'images/zoom17.gif',
	   'images/zoom18.gif',
	   'images/closebtn.gif',
	   'images/closebtn_.gif',
	   'images/logoPosition_.gif'
   );
   preloadImages(the_images);
	 SetMapMode('Z+');
	 SetPanMap(true);
   HideWaitMessage();
   
   switch(Cmd)
   {
     case 'showmappanel': ShowMapPanel(); break;
   }
}

MapMode='';
window.onload = Init;
document.onselectstart = Nop;
document.onselect 	   = Nop;
document.onmousedown   = DisableMenus;

