var dontJump=0;
var adminString='';
var adminMode=0;
var dummyGoToNrTx='var s="";';
var dummySwitchStringTx='var s="";';


//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
function loadedImg2(f)
 {
  f.style.display="inline";
 }
//=============================================================================
function initMap()
 {

 if (Layer==-1)
  {
  for (i=0; i<overlayTypes.length; i++)
   for (j=0; j<Layers.length;j++)
    imgLoad("over_image_"+i+"_"+j,"./img/"+overlayTypes[i]+''+j+'.gif','');
  gray.src="./img/gray.gif";

  }



  if (!window.innerWidth)
   {
    w=document.body.clientWidth;
    h=document.body.clientHeight-document.getElementById('top_foot').offsetTop;
   } else
   {
    w=window.innerWidth;
    h=window.innerHeight-document.getElementById('top_foot').offsetTop; //map2.offsetTop;
   }
  if (Layer!=-1 && w>Layers[Layer][6])
   {
    map2.style.width=Layers[Layer][6]+'px';
    map3.style.width=(Layers[Layer][6]+pic_width)+'px';
    map2.style.left=parseInt((w-Layers[Layer][6])/2)+'px';
   } else
   {
    map2.style.width=w+'px';
    map3.style.width=(w+pic_width)+'px';
    map2.style.left='0px';
   }

//  alert(Layer);
  if (Layer!=-1 && h>Layers[Layer][7])
   {
    map2.style.height=Layers[Layer][7]+'px';
    map3.style.height=(Layers[Layer][7]+pic_height)+'px';
    map2.style.top=parseInt(document.getElementById('top_foot').offsetTop)+'px'; //(h-Layers[Layer][7])/2+
   } else
   {
    map2.style.height=h+'px';
    map3.style.height=(h+pic_height)+'px';
    map2.style.top=document.getElementById('top_foot').offsetTop+'px';
   }
  xVisSize=map2.offsetWidth;
  yVisSize=map2.offsetHeight;
  xVisSize2=xVisSize/2;
  yVisSize2=yVisSize/2;


  xSize=parseInt((map3.offsetWidth+pic_width)/pic_width);
  ySize=parseInt((map3.offsetHeight+pic_height)/pic_height);
  y_center_correction= yVisSize2 % pic_height;
  x_center_correction= xVisSize2 % pic_width;
//-------------------------------

  s='';
  for (i=0;i<ySize;i++)
   {
    for (j=0;j<xSize;j++)
     {
      s=s+'<img id="q_'+i+''+j+'" src="./img/gray.gif" onload="loadedImg2(this)" style="width:'+pic_width+'px;height:'+pic_height+'px;left:'+(j*pic_width)+'px  ;top:  '+(i*pic_height)+'px;" />';
     }
   }
  s=s+'<div id="over" style="width:'+(xSize*pic_width)+'px;height:'+(ySize*pic_height)+'px;"></div><img src="./img/arrow.gif" id="arrow" width="40" height="40" style="width:40px;height:40px;position:absolute;display:none;top:0px;left:0px;z-index:1" /><br style="clear:left"/>';
  map3.innerHTML=s;
  //-------------------------------
  arrow=document.getElementById("arrow");
  over=document.getElementById("over");
  //-------------------------------
  for (ii=0;ii<xSize;ii++)
   {
    pics[ii]=new Array();
    srcs[ii]=new Array();
    tester[ii]=new Array();
    for (ji=0;ji<ySize;ji++)
     {
      srcs[ii][ji]='';
      s='q_'+ji+ii;
//      u='p_'+ji+ii;
      f=document.getElementById(s);
      pics[ii][ji]=f;
//      tester[ii][ji]=document.getElementById(u);
       tester[ii][ji]=new Image();
     }
   }
  //-------------------------------
 }
//=============================================================================
function initHandlers()
 {
 // rsb.onmousedown  = dragRSB;
 // bsb.onmousedown  = dragBSB;
  over.onmouseup = unmagnify;
  over.onmousedown = dragMap;
  over.ondblclick  = magnify;

if (over.addEventListener)
        over.addEventListener('touchstart', touchMap, false);

if (over.addEventListener)
        over.addEventListener('DOMMouseScroll', wheel, false);

  over.onmousewheel = wheel;

  arrow.ondblclick  = magnify;
  document.oncontextmenu=new Function("return false");

 }
//=============================================================================
//=============================================================================
//=============================================================================
//=============================================================================
function initAllObjects()
 {
 // rsb=document.getElementById("scroll_ver");
 // bsb=document.getElementById("scroll_hor");
  map3=document.getElementById("map3");
  map2=document.getElementById("map2");
  floater=document.getElementById("floater");
 // rsbm=document.getElementById("rsbm");
 // bsbm=document.getElementById("bsbm");
 // helper=document.getElementById("helper");
 // hardlink=document.getElementById("hardlink");
  initMap();
  initHandlers();
 }
//=============================================================================
function startGeneral()
 {
  //advertBox();
  startMaps();
 }
//=============================================================================

function startMaps()
 {
  var a=new Array();
  var r3=/[?&#]zoom=([0-9]*)([&#]|$)/
  var r14=/[?&#]map=([0-9]*)([&#]|$)/
  var s='';
  s=s+document.location;
    if ((a=s.match(r14)) && !dontJump)
     {
      dontJump=2;

      if (parseInt(a['1'])!=mapaId)
       {
        switchMapsTo(parseInt(a['1']));
        return;
       }
     }
  initAllObjects();
    if ((a=s.match(r3)) && !dontJump)
     {
      loadLayer(Layers.length-a['1']);
     } else
      loadLayer(5);
  eval(dummySwitchStringTx);
  getOverlay();
 }
//=============================================================================
//=============================================================================
function startMapsGoToNr()
 {
  initAllObjects();
  loadLayer(1);
  eval(dummySwitchStringTx);
  getOverlay();
 }
//=============================================================================
function resizeMap()
 {
  initMap();

  initHandlers();
  x_center_correction= xVisSize2 % pic_width;
  y_center_correction= yVisSize2 % pic_height;
  basisX=-1;
  basisY=-1;
  centerTo(centerX,centerY);
  if (adminMode) initOverlayAdmin(); else initOverlay();
 }
//=============================================================================
function switchMapsTo(i)
 {
  var s;
  if (mapaId==i) return;
  switch(i)
   {
    default:
    case 0:
      s='vars.js';
      closeInfoBox();
      closeSearchBox();
      document.getElementById('srcTextCR').style.display='none';
      document.getElementById('srcText').style.display='inline';
      document.getElementById('srcTextBrno').style.display='none';
      document.getElementById('mainNameCR').style.display='none';
      document.getElementById('mainName').style.display='inline';
      document.getElementById('mainNameBrno').style.display='none';
     break;
    case 1:
      s='vars_cr.js';
      closeInfoBox();
      closeSearchBox();
      document.getElementById('srcText').style.display='none';
      document.getElementById('srcTextCR').style.display='inline';
      document.getElementById('srcTextBrno').style.display='none';
      document.getElementById('mainName').style.display='none';
      document.getElementById('mainNameCR').style.display='inline';
      document.getElementById('mainNameBrno').style.display='none';
     break;
    case 2:
      s='vars_brno.js';
      closeInfoBox();
      closeSearchBox();
      document.getElementById('srcText').style.display='none';
      document.getElementById('srcTextCR').style.display='none';
      document.getElementById('srcTextBrno').style.display='inline';
      document.getElementById('mainName').style.display='none';
      document.getElementById('mainNameCR').style.display='none';
      document.getElementById('mainNameBrno').style.display='inline';
     break;
   }
  mapaId=i;
  drawLocation();
  if (Layer!=-1)
   document.getElementById("layer_switch_"+Layer).style.visibility="hidden";
  Zavora=1;
  onlineRequest("vars","/"+s+'?');
 }
//=============================================================================
//=============================================================================
function switchMapsGoToNr(i)
 {
  var s;
  if (mapaId==i) return;
  switch(i)
   {
    default:
    case 0:
      s='vars.js';
      closeSearchBox();
      closeAdvertBox();
      document.getElementById('srcTextCR').style.display='none';
      document.getElementById('srcText').style.display='inline';
      document.getElementById('srcTextBrno').style.display='none';
      document.getElementById('mainNameCR').style.display='none';
      document.getElementById('mainName').style.display='inline';
      document.getElementById('mainNameBrno').style.display='none';
     break;
    case 1:
      s='vars_cr.js';
      closeSearchBox();
      closeAdvertBox();
      document.getElementById('srcText').style.display='none';
      document.getElementById('srcTextCR').style.display='inline';
      document.getElementById('srcTextBrno').style.display='none';
      document.getElementById('mainName').style.display='none';
      document.getElementById('mainNameCR').style.display='inline';
      document.getElementById('mainNameBrno').style.display='none';
     break;
    case 2:
      s='vars_brno.js';
      closeSearchBox();
      closeAdvertBox();
      document.getElementById('srcText').style.display='none';
      document.getElementById('srcTextCR').style.display='none';
      document.getElementById('srcTextBrno').style.display='inline';
      document.getElementById('mainName').style.display='none';
      document.getElementById('mainNameCR').style.display='none';
      document.getElementById('mainNameBrno').style.display='inline';
     break;
   }
  mapaId=i;
  if (Layer!=-1)
   document.getElementById("layer_switch_"+Layer).style.visibility="hidden";
  Zavora=2;
  onlineRequest("vars","/"+s+'?');
 }
//=============================================================================
function continueSwitchMapsGoToNr()
 {
  startMapsGoToNr();
  goToNr(activeFacility,jumpToIt);
 }
function continueSwitchMapsTo()
 {
  if (dontJump!=2) dontJump=1;
  startMaps();
 }
//=============================================================================
function switchToLang(i)
 {
  closeInfoBox();
  document.getElementById('flag_'+activeLang).style.display='inline';
  document.getElementById('flag_'+i).style.display='none';
  activeLang=i;
  langZavora=1;
  onlineRequest("vars","/lang_"+i+".js?");
 }
//=============================================================================
function continueSwitchToLang()
 {
  reloadTexts();
  drawLocation();
 }
//=============================================================================
function drawLocation(force)
 {
  s='map='+mapaId+'&lng='+activeLang+'&ix='+centerX+'&iy='+centerY+'&zoom='+(Layers.length-Layer)+'&o='+overlayDrawMask;
  if (arrowVisible>0) s+='&ax='+arrowX+'&ay='+arrowY;
  if (activeFacility) s+='&f='+activeFacility;
  if (searchedString) s+='&flt='+searchedString;
  if (!firstAttempt || force==1)
  window.location.hash=s;
 }
//=============================================================================
function jumpByLocation()
 {

  var r1=/[?]([0-9]*)$/
  var r2=/[?]([^&=]*)$/

  var r3=/[?&#]zoom=([0-9]*)([&#]|$)/
  var r4=/[?&#]name=([^&#]*)([&#]|$)/
  var r5=/[?&#]uid=([0-9]*)([&#]|$)/
  var r6=/[?&#]x=([-.0-9]*)([&#]|$)/
  var r7=/[?&#]y=([-.0-9]*)([&#]|$)/
  var r8=/[?&#]ix=([-.0-9]*)([&#]|$)/
  var r9=/[?&#]iy=([-.0-9]*)([&#]|$)/
  var r10=/[?&#]ax=([-.0-9]*)([&#]|$)/
  var r11=/[?&#]ay=([-.0-9]*)([&#]|$)/
  var r12=/[?&#]f=([0-9]*)([&#]|$)/
  var r13=/[?&#]o=([0-9]*)([&#]|$)/
  var r15=/[?&#]lng=([0-9]*)([&#]|$)/
  var r16=/[?&#]flt=([^&#]+)([&#]|$)/
  var r17=/[?&#]arr=1([&#]|$)/

  var a=new Array();
  var s='';
  s=s+document.location;



  jumpToJTSK(initialJTSKX,initialJTSKY,-1);

  if (dontJump==1)
   {
    dontJump=0;
    drawLocation(1);
    return;
   }
  dontJump=0;

   var arr=0;
   if ((a=s.match(r17)))
    {
     arr=1;
    }

   if ((a=s.match(r15)))
    {
     switchToLang(parseInt(a['1']));
    }

  if (a=s.match(r1))
   {
    switchToLayer(1);
    jumpToUID(parseInt(a['1']));
   } else
  if (a=s.match(r2))
   {
    switchToLayer(1);
    document.getElementById('FSrc').value=a['1'].replace(/\+/," ").replace(/%20/," ");
    findIt();
   } else
   {
    if (a=s.match(r3))
     {
      switchToLayer(Layers.length-a['1']);
     }
    if (a=s.match(r4))
     {
      document.getElementById('FSrc').value=a['1'];
      findIt();
     }
    if (a=s.match(r5))
     {
    jumpToUID(parseInt(a['1']));
     }
    if (a=s.match(r10))
     {
      x=a['1'];
      if (a=s.match(r11))
        setSipka(x,a['1'],1);
     }

    if (a=s.match(r6))
     {
      x=a['1'];
      if (a=s.match(r7))
        jumpToJTSK(x,a['1'],arr);
     }   else
    if (a=s.match(r8))
     {
      x=a['1'];
      if (a=s.match(r9))
        centerTo(x,a['1']);
     }
    if (a=s.match(r12))
     {
      goToNr(parseInt(a['1']));
     }
    if (a=s.match(r13))
     {
      setOverlay(parseInt(a['1']));
     }
    if (a=s.match(r16))
     {
      document.getElementById('FSrc').value=unescape(a['1']);
      findGlobal('FSrc');
     }
   }
  drawLocation(1);
 }
//=============================================================================
var Texts=new Array();
function documentWrite(id,style)
 {
  document.write('<span id="'+id.replace(/[^a-zA-Z0-9]/g,'')+'" style="'+style+'">');
  eval('document.write('+id+');');
  document.write('</span>');
  Texts[''+Texts.length]=id;
 }
//=============================================================================
function reloadTexts()
 {
  var i;
  for (i=0; i<Texts.length; i++)
   {
    a=Texts[''+i];
    if (document.getElementById(a.replace(/[^a-zA-Z0-9]/g,'')))
    document.getElementById(a.replace(/[^a-zA-Z0-9]/g,'')).innerHTML=eval(''+Texts[''+i]+';');
   }
 }
