mapLocInitialized = false;
mapRedy = false;

MapAppLoader.onStartApplication = function() {
    startApplication();
    mapRedy = true;
};


MapAppLoader.load({
    uiLibrary: "",
    proxy: "http://" + location.hostname + "/mapy/proxy.php?url=",
    scripts: [
        "http://apps.hfbiz.cz/glog/Glog/MapPortal/www/mapportal-core.js"
    ],
    debug: false
});


function afterSearch() {
    HSLayers.Apps.Utility.getElement("map").style.visibility = "visible";
};


function beforeSearch(request) {
    var continueSearch = true;
    if (request.fromMap) {
        var souradniceVyrezu = request.fromMap.minx + " " + request.fromMap.miny + "," +
                               request.fromMap.minx + " " + request.fromMap.maxy + "," +
                               request.fromMap.maxx + " " + request.fromMap.maxy + "," +
                               request.fromMap.maxx + " " + request.fromMap.miny + "," +
                               request.fromMap.minx + " " + request.fromMap.miny;

        document.getElementById('mapRegion').value = souradniceVyrezu;
        document.getElementById('inputText').value = 'Výběr na mapě';
        
        close_element('map_select_div');
        continueSearch = false;
    }
    return continueSearch;
};


function hideControlPanel() {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setControlPanelVisible(false);
};

function getObjectId() {
    var objectId = HSLayers.Apps.Utility.getElement("objectid").value;
    if (objectId == "") {
        objectId = null;
    }
    return objectId;
};


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 getSkatId() {
    return HSLayers.Apps.Utility.getElement("skatid").value;
};


function getVisibleSkatArray() {
    return [{layerName:"poi",skat: getSkatId()}];
};

function initOptions(
    mapDivId, showObjectInMap, defaultControl, afterSearchFunc, zoomChangeFunc) {

    HSLayers.Apps.MapPortal.Rincon.ShowObject.initOptions({

        // callback funkce, ktera se zavola po zobrazeni mapy
        afterLoad: afterLoad,
        
        // callback funkce, ktera se zavola po vyhledani pozadovaneho objektu
        afterSearch: afterSearchFunc,
        
        // callback funkce, ktera se zavola pred vyhledanim pozadovaneho objektu
        // a po zadani vyhledavaciho vyrezu v mape
        beforeSearch: beforeSearch,
        
        // 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: mapDivId,
        
        // nastaveni viditelnosti nastrojove listy v mape
        controlPanelVisible: false,
        
        // nastroj, ktery je implicitne zapnuty :
        //   "info"                     - zapne nastroj info
        //   "" , nebo nezadana hodnota - zapne nastroj "navigation" (pohyb 
        //                                v mape - pan, zoom)
        defaultControl: defaultControl,
        
        // cesta k obrazkum, ktere nahrava mapova knihovna
        // !!! NEMENIT !!!
        imgPath: "http://apps.hfbiz.cz/glog/JavaScript/hslayers/trunk/HSimg/",

        // callback funkce, ktera se zavola pri zobrazeni informacni bubliny
        getPopupContent: getPopupContent,
        
        // nazev projektu na serveru, ktery obsahuje definici prislusnych dat
        // !!! NEMENIT !!!
        projectName: "HledejUbytovani",
        
        // urcuje jestli budou zobrazeny vsechny objektu (POI) v mape, nebo 
        // pouze pozadovany objekt (false - pouze pozadovany, true - vsechny 
        // objekty)
        showObjectsInMap: showObjectInMap,
        
        // callback funkce, ktera se zavola po zmene zoomLevelu
        zoomChange: zoomChangeFunc            
    });
};


function onLoad() {
    MapAppLoader.afterDocLoaded();
};


function setActiveMap(mapDivId) {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setActiveMap(mapDivId);
};


function setBackground(backgroundId) {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setBackgroundLayer(backgroundId);
};


function setVisibleSkat(skatid) {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setVisibleSkat(
        getVisibleSkatArray(),
        true
    );
};


function setZoom(id) {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.zoomTo(
        HSLayers.Apps.Utility.getElement(id).value
    );
};


function showControlPanel() {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setControlPanelVisible(true);
};


function showMap1() {

    initOptions("map1", true, "", afterSearch, zoomChange);

    HSLayers.Apps.MapPortal.Rincon.ShowObject.showMap(
        getObjectId(), getVisibleSkatArray(), null
    );
};


function showObjectInMap() {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.showObjectInMapById(
        getObjectId()
    );
};


function startApplication(mapDivId) {

};


function zoomChange(zoom) {
    HSLayers.Apps.Debug.log("Aktuální zoom je " + zoom);
};



// zobrzovaci mapa
function showmap(mapidshowSetRec){
  

  if((document.getElementById('mapidshow')) && (mapidshowSetRec == undefined)){
     var mapidshowSetRec = document.getElementById('mapidshow').innerHTML;
  }
  
  document.getElementById('map_view_transparent').style.display = '';
  document.getElementById('map_view').style.display = '';
  
  mappos();
  
  if(mapLocInitialized == false){
  
    initOptions("map", true, "", afterSearch, zoomChange);
  
    HSLayers.Apps.MapPortal.Rincon.ShowObject.showMap(
        mapidshowSetRec, null, null
    );
    
  
  } else {
    HSLayers.Apps.MapPortal.Rincon.ShowObject.setActiveMap('map');
    HSLayers.Apps.MapPortal.Rincon.ShowObject.showObjectInMapById(mapidshowSetRec);

  }

}

function activateMove(){
  $(function(){
      $("#map_select_div").draggable({ handle: 'div.map_select_move' });
    }
  )
}

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';


      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 loadData2Buble(){
  if(document.getElementById('mapbuble')){
    
    var iddr = document.getElementById('mapbuble').innerHTML;
    document.getElementById('mapbuble').innerHTML = "Load data...";
    
    var params = "&dr=" + iddr;
    $.get('http://' + document.location.host + '/mapy/ajax.php?akce=dr_info' + params, 
      function(xml) {
        $(xml).find('response').each(function(){
          document.getElementById('mapbuble').innerHTML = $(this).find('buble').text();
       });
  		}, "xml");
  
    

  } else {
    setTimeout('loadData2Buble()',50);
  }
}


// vyberova mapa
function showMapSelect(){
  if(mapRedy == true){
    if(!document.getElementById('map_select_div')){
    
      var mapSelectDiv = document.createElement('div');
      mapSelectDiv.setAttribute('id', 'map_select_div');
      document.body.appendChild(mapSelectDiv);
      
      document.getElementById('map_select_div').style.position = 'absolute';
      
      document.getElementById('map_select_div').style.zIndex = 15;
      
      document.getElementById('map_select_div').style.height = '495px';
      document.getElementById('map_select_div').style.width = '750px';
      
      document.getElementById('map_select_div').style.border = '5px solid #cccccc';
      
  
      
      
      //var html2MapDiv = '<div class="map_select_move" style="cursor:move;" onmouseover="activateMove()" >';
      var html2MapDiv = '<div class="map_select_move">';
            html2MapDiv += '<div><a name="selectMap"></a><strong>Výběrová mapa</strong> <span class="napoveda">Kliknutím a tažením myši vyberte oblast na mapě</span> <a href="javascript:close_element(\'map_select_div\');" style="position:absolute; top:0px; right:0px;" title="zavřít">X</a></div>';
          html2MapDiv += '</div>';
          html2MapDiv += '<div id="select_map" style="width: 750px; height: 470px; background: #cccccc url(/images/gallery/loading.gif) no-repeat center center;">';
            html2MapDiv += '<div style="z-index: 750; position: absolute; top: 150px; left: 150px; border: solid 1px red; background: white; padding: 5px;" id=\"upozorneni_mapy\"><h3>Kliknutím a tažením myši vyberte oblast na mapě</h3></div>';
          html2MapDiv += '</div>';
          //html2MapDiv += '';
          
      document.getElementById('map_select_div').innerHTML = html2MapDiv;
      
      document.getElementById('map_select_div').style.backgroundColor = '#cccccc';
    
  
  
    	if (window.innerHeight){
        windowW = window.innerWidth;
    	} else if (document.documentElement && document.documentElement.clientHeight){
    		windowW = document.documentElement.clientWidth;
    	} else if (document.body && document.body.clientHeight){
    		windowW = document.body.clientWidth;
      }
    
      
      var mapTopPosition = mouseYJ + 15;
      var mapLeftPosition = mouseXJ;
      if((mapLeftPosition + 800) > windowW){
        mapLeftPosition = windowW - 800; 
      }
      document.getElementById('map_select_div').style.top = mapTopPosition + 'px';
      document.getElementById('map_select_div').style.left = mapLeftPosition + 'px';
  
      // inicializace parametru pro zobrazeni mapy
      initOptions("select_map", false, "info", null, zoomChange);
  
      // Zobrazeni mapy v pozadovanem zoomu
      HSLayers.Apps.MapPortal.Rincon.ShowObject.showMap(
          null, [], 2
      );
  
  
    }
    
    if(document.getElementById('map_select_div')){
      HSLayers.Apps.MapPortal.Rincon.ShowObject.setActiveMap('select_map');
      $("#map_select_div").show(1000);
    }
    
    document.location.href = "#selectMap";
  } else {
    setTimeout("showMapSelect()", 500);
  }
  
}

function afterLoad(){
  if(document.getElementById("select_map")){
    document.getElementById("select_map").style.background = "";
    setTimeout(function(){
      $("#upozorneni_mapy").hide('highlight', '', 500);
    }, 3000);
  }

  if(document.getElementById('map_view').style.display == ''){
    document.getElementById("map").style.background = "";
    mapLocInitialized = true;
  }

};

function rmMapRegion(){
  if(document.getElementById('mapRegion').value != ""){
    document.getElementById('mapRegion').value = '';
    document.getElementById('inputText').value = '';
    document.getElementById('inputText').focus();
  }
}
