var map;

var default_x = 50;
var default_y = 14;
var default_z = 9;

var url = "http://istavitel.cz/cs/pomocne-objekty/pomocne-skripty/logo-na-mape/?logoid=";
var firm = "http://istavitel.cz/pomocne-objekty/pomocne-skripty/firma-na-mape/?firmid=";
var branch = "http://istavitel.cz/pomocne-objekty/pomocne-skripty/firma-na-mape/?branchid=";

function initAjax(id,type)
  {

    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    
    if(!type) request = firm + id;
    if(type==1) request = branch + id;
    
    http.open('GET', request);
    
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
        
            content = http.responseText;      
            document.getElementById('booble').innerHTML = content;
            
        }
    }
    
    http.send(null);
  
  }

function init()
  {

  if (GBrowserIsCompatible())
    {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(default_x,default_y), default_z);
    map.clearOverlays();
    }
  
  }

function initCoordinates(x,y,z)
  {

  if (GBrowserIsCompatible())
    {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.setCenter(new GLatLng(x,y),z);
    map.clearOverlays();
    }
  
    GEvent.addListener(map, "moveend", function() {
      updateCoordinates();      
      document.getElementById('region').selectedIndex=0;
      document.getElementById('city').selectedIndex=0;
      });

    updateCoordinates();
  
  }

function updateCoordinates()
  {

      var center = map.getCenter();   
      
      bounds = map.getBounds();
      
      southWest = bounds.getSouthWest();
      northEast = bounds.getNorthEast();
      zoomLevel = map.getBoundsZoomLevel(bounds)
      
      document.getElementById('sw').value = southWest;
      document.getElementById('ne').value = northEast;
      document.getElementById('zl').value = zoomLevel;
      document.getElementById('ct').value = center;
  
  }
 
function createIcon(logo)
  {
  
    var icon = new GIcon(G_DEFAULT_ICON);
    
    img = url + logo;
    
    icon.image = img;
    icon.mozPrintImage = img;
    icon.printImage = img;
    
    icon.iconSize = new GSize(48, 64);        
    icon.iconAnchor = new GPoint(26, 60);        
    icon.shadowSize = new GSize(0, 0);
    icon.imageMap = [0,0,48,0,48,48,0,48,0,0];

    return icon;
  
  } 

function addInfoIcon(x,y,id,logo,type)
  {

  point = new GLatLng(x,y);
  icon = createIcon(logo);

  var marker = new GMarker(point, {icon: icon});

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml('<div id="booble" style="width:500px;height:300px"></div>');
    initAjax(id,type);
    });
  
  map.addOverlay(marker);

  }

function addDragIcon(x,y,logo)
  {

  point = new GLatLng(x,y);
  icon = createIcon(logo);
  
  var marker = new GMarker(point, {icon: icon, draggable: true});

  map.setCenter(point);

  GEvent.addListener(marker, "dragend", function() {
    point = marker.getPoint();
    map.panTo(point);
    document.getElementById('coordinate-x').value = point.lat();
    document.getElementById('coordinate-y').value = point.lng();
    });
  
  map.addOverlay(marker);
  
  }

function addEmptyIcon(x,y,logo)
  {

  point = new GLatLng(x,y);
  icon = createIcon(logo);
  
  marker = new GMarker(point, {icon: icon});  
  
  map.setCenter(point);
  map.addOverlay(marker);
  
  }

function addLinkIcon(x,y,link,logo)
  {

  point = new GLatLng(x,y);
  icon = createIcon(logo);
  
  marker = new GMarker(point, {icon: icon});

  GEvent.addListener(marker, "click", function() {
    window.location.href = link;
    });

  
  map.setCenter(point);
  map.addOverlay(marker);
  
  }


