// JavaScript Document
var map = null;
var geocoder = null;

function initialize(address, title, settedWidth, settedHeight) {

  var customWidth = '600';
  if(settedWidth)
    customWidth = settedWidth;
  var customHeight = '350';
  if(settedWidth)
    customHeight = settedHeight;
  
  var mapDiv = document.getElementById("MAP");
  
  var maskDiv = document.createElement("div");
  maskDiv.setAttribute("id", "MASK");
  maskDiv.style.width = "100%";
  maskDiv.style.height = "100%";
  maskDiv.style.backgroundColor = "black";
  maskDiv.style.opacity = "0.6";
  maskDiv.style.position = "fixed";
  maskDiv.style.cursor = "pointer";
  maskDiv.setAttribute("onclick", "unInitalize();");
// 1.
  mapDiv.appendChild(maskDiv);
  
  var mapCover = document.createElement("div");
  mapCover.style.width = customWidth+'px';
  mapCover.style.height = customHeight+'px';
  mapCover.style.position = 'relative';
  var marginWidth = getWindowWidth() - customWidth;
  var marginHeight = getWindowHeight() - customHeight - 20;
  mapCover.style.top = marginHeight / 2 +'px';
  mapCover.style.left = marginWidth / 2 +'px';
// 2.
  mapDiv.appendChild(mapCover);
  
  var mapCanvas = document.createElement("div");
  mapCanvas.setAttribute("id", "map_canvas");
  mapCanvas.style.width = customWidth+'px';
  mapCanvas.style.height = customHeight+'px';
  mapCanvas.style.color = 'black';

  var closeLink = document.createElement("a");
  closeLink.style.padding = '0.25em 0.5em';
//       closeLink.style.display = 'block';
  closeLink.innerHTML = "Zavři";
  closeLink.setAttribute("href", "javascript:unInitalize();");
  var closeDiv = document.createElement("div");
  closeDiv.setAttribute("id", "CLOSE");
  closeDiv.style.backgroundColor = 'white';
  closeDiv.style.position = 'relative';
  closeDiv.style.width = customWidth+'px';
  closeDiv.style.textAlign = 'right';
  closeDiv.style.lineHeight = '30px';
  var span = document.createElement("span");
  span.style.position = 'absolute';
  span.style.left = '10px';
  span.style.color = 'black';
  span.innerHTML = title;
// 3.
  closeDiv.appendChild(span);
  closeDiv.appendChild(closeLink);
// 4.
  mapCover.appendChild(closeDiv);
// 5.
  mapCover.appendChild(mapCanvas);

  if (GBrowserIsCompatible() && mapCanvas != undefined) {
    var map = new GMap2(mapCanvas);
    geocoder = new GClientGeocoder();
    map.setMapType(G_NORMAL_MAP);
    var customUI = map.getDefaultUI();
    // Remove MapType.G_HYBRID_MAP
//         customUI.controls.scalecontrol = false;
    map.setUI(customUI);
    
    if (geocoder) {
      geocoder.getLatLng(
        address,
        function(point) {
          if (!point) {
            alert(address + " nenalezena");
          }
          else {
            map.setCenter(point, 13);
            var marker = new GMarker(point);
            map.addOverlay(marker);
//             marker.openInfoWindowHtml(address);
            map.setZoom(17);
          }
        }
      );
    }
    window.scrollTo(0,0);
  }
}
function unInitalize() {
  var mapDiv = document.getElementById("MAP");
  mapDiv.innerHTML = '';
}
function getWindowWidth() {
  var windowWidth = 0;
  if( typeof(window.innerWidth) == 'number' ) {
    //Non-IE
    windowWidth = window.innerWidth;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    windowWidth = document.documentElement.clientWidth;
  }
  else if( document.body && ( document.body.clientWidth) ) {
    //IE 4 compatible
    windowWidth = document.body.clientWidth;
  }
  return windowWidth;
}

function getWindowHeight() {
  var windowHeight = 0;
  if( typeof(window.innerWidth) == 'number' ) {
    //Non-IE
    windowHeight = window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowHeight = document.documentElement.clientHeight;
  }
  else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    windowHeight = document.body.clientHeight;
  }
  return windowHeight;
}

