var hfbizmapisshowed = 'no';

function $(id) {
    return document.getElementById(id);
}; 

MapAppLoader.onStartApplication = function() {
    $("status").innerHTML = "Zobrazuji mapu, čekejte ...";
    initOptions();
    var mapidshow = document.getElementById('mapidshow').innerHTML;
    HSLayers.Apps.MapPortal.Rincon.ShowObject.showMap(mapidshow);
    //HSLayers.Apps.MapPortal.Rincon.ShowObject.showMap("<?php echo $dr ?>");
};

function afterSearch() {
    $("map").style.visibility = "visible";
    $("status").style.display = "none";
    $("status").innerHTML = "";
};

function getPopupContent(dataObject) {
    var content = "";
    if (dataObject != null) {
        if (dataObject.objectId != "") {
            //content = "Informace o objektu ID=" + dataObject.objectId;
            content = '<div id="mapbuble" style="width: 300px; height: 180px; overflow: auto;">'+ dataObject.objectId + '</div>';
            loadData2Buble();
            
        }
    }
    return content;
};

function initOptions() {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.initOptions({
        
        // cesta k obrazkum
        imgPath: "http://apps.hfbiz.cz/glog/JavaScript/hslayers/trunk/HSimg/",

        // callback funkce, ktera se zavola po vyhledani pozadovaneho objektu
        afterSearch: afterSearch,
        
        // zakladni URL pro volani funkcionality implementovane na serverove strane
        // !!! NEMENIT !!!
        baseUrl: "http://apps.hfbiz.cz/glog/glog",
        
        // id HTML elementu (DIV), do ktereho bude vygenerovane mapove okno
        containerId: "map",
        
        // callback funkce, ktera se zavola pri zobrazeni informacni bubliny
        getPopupContent: getPopupContent,
        
        // nazev projektu na serveru, ktery obsahuje definici prislusnych dat
        // !!! NEMENIT !!!
        projectName: "RinconSumperk16",
        
        // urcuje jestli budou zobrazeny vsechny objektu (POI) v mape, nebo 
        // pouze pozadovany objekt (false - pouze pozadovany, true - vsechny 
        // objekty)
        showObjectsInMap: true
    });
};

function onLoad() {
    MapAppLoader.afterDocLoaded();
};

function showObjectsInMap() {
    $("status").innerHTML = "Inicializuji mapovou knihovnu, čekejte ...";        

    MapAppLoader.load({
        uiLibrary: "",
        proxy: "http://" + location.hostname + "/mapy/proxy.php?url=",
        scripts: [
            "http://apps.hfbiz.cz/glog/Glog/MapPortal/www/mapportal-core.js"
        ]
    });
};

function showmap(){
  document.getElementById('map_view_transparent').style.display = '';
  document.getElementById('map_view').style.display = '';
  
  mappos();
  if(hfbizmapisshowed == 'no'){
    showObjectsInMap();
    hfbizmapisshowed = 'yes';
  }
  

}

function mappos(){
  
  if(document.getElementById('map_view_transparent').style.display != 'none'){
  
  	if (window.innerHeight){
      galSizeH = window.innerHeight;
      galSizeW = window.innerWidth;
  	} else if (document.documentElement && document.documentElement.clientHeight){
  		galSizeH = document.documentElement.clientHeight;
  		galSizeW = document.documentElement.clientWidth;
  	} else if (document.body && document.body.clientHeight){
  		galSizeH = document.body.clientHeight;
  		galSizeW = document.body.clientWidth;
    }
    if(galSizeH == 0){
    } else {
      document.getElementById('map_view_transparent').style.height = (galSizeH) + 'px';
      document.getElementById('map_view_transparent').style.width = (galSizeW) + 'px';
      /*
      posmapH = ((galSizeH) - 512) / 2;
      posmapW = ((galSizeW) - 652) / 2;
      */

      mapSizeObH = galSizeH - 80;
      mapSizeObW = galSizeW - 80;

      document.getElementById('map_view').style.height = (mapSizeObH) + 'px';
      document.getElementById('map_view').style.width = (mapSizeObW) + 'px';
      document.getElementById('map_view_close').style.width = (mapSizeObW) + 'px';

      mapSizeH = mapSizeObH - 32;
      mapSizeW = mapSizeObW - 12;

      document.getElementById('map').style.height = (mapSizeH) + 'px';
      document.getElementById('map').style.width = (mapSizeW) + 'px';
      

      posmapH = ((galSizeH) - mapSizeObH) / 2;
      posmapW = ((galSizeW) - mapSizeObW) / 2;

      if (document.documentElement && document.documentElement.scrollTop) {
        winScroll = document.documentElement.scrollTop;
      } else if (document.body) {
        winScroll = document.body.scrollTop;
      } else {
        winScroll = 0;
      }
      
      document.getElementById('map_view_transparent').style.top = (winScroll) + 'px';
      
      document.getElementById('map_view').style.top = (winScroll + posmapH) + 'px';
      document.getElementById('map_view').style.left = (posmapW) + 'px';
      
    }
    
    setTimeout('mappos()',100);
  
  }
}


function closemapshow(){
  document.getElementById('map_view_transparent').style.display = 'none';
  document.getElementById('map_view').style.display = 'none';

}

function checkKeyMap(evt){

  var charCode = (evt.which) ? evt.which : event.keyCode

  if(charCode == 27){
    closemapshow();
  } 
}

function loadData2Buble(){
  if(document.getElementById('mapbuble')){

    myDiv = document.getElementById('mapbuble');
    var iddr = myDiv.innerHTML;
    myDiv.innerHTML = "Load data...";
  
    if (xmlHttp)
    {
      try
      {
  
        var params = "&dr=" + iddr;
        xmlHttp.open('GET', 'http://' + document.location.host + '/mapy/ajax.php?akce=dr_info' + params, true);
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(null);
  
  
      }
      catch (e)
      {
        alert("Can't connect to server 2:\n" + e.toString());
      }
    }      
    

  } else {
    setTimeout('loadData2Buble()',50);
  }
}
  

