var gmarkers = [];
var htmls = [];

function ltmap() {
//<![CDATA[

    if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventualkly be placed in the side_bar
      var side_bar_html = "";
    
      // arrays to hold copies of the markers and html used by the side_bar
      // because the function closure trick doesnt work there
      
      var i = 0;
      
      // === Create an associative array of GIcons() ===
      var gicons = [];
      
      var baseIcon = new GIcon();  
  
       baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";  
       baseIcon.iconSize = new GSize(32, 32);  
       baseIcon.shadowSize = new GSize(37, 34);  
       baseIcon.iconAnchor = new GPoint(9, 34);  
       baseIcon.infoWindowAnchor = new GPoint(9, 2);  
       baseIcon.infoShadowAnchor = new GPoint(18, 25);  
    
       
       // var icon = new GIcon(baseIcon);  
       // icon.image = "/images/mapicon_yellow.png";  
       
       var iconYellow = new GIcon(baseIcon);  
       iconYellow.image = "/images/mapicon_yellow.png";  
       
       var iconBlue = new GIcon(baseIcon);  
       iconBlue.image = "/images/mapicon_blue.png";  
       
       var iconRed = new GIcon(baseIcon);  
       iconRed.image = "/images/mapicon_red.png";  
       
       
       gicons["yellow"] = iconYellow;
       gicons["blue"] = iconBlue;
       gicons["red"] = iconRed;
      
      
      
      // gicons["yellow"] = gicons["yellow"] = new GIcon(G_DEFAULT_ICON);
      // gicons["blue"] = new GIcon(G_DEFAULT_ICON, "/images/mapicon_blue.png");
      // gicons["red"]  = new GIcon(G_DEFAULT_ICON, "/images/mapicon_red.png");
      
      


      // A function to create the marker and set up the event window
      function createMarker(point,name,html,icontype) {
        // === create a marker with the requested icon ===
        var marker = new GMarker(point, gicons[icontype]);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        htmls[i] = html;
        // add a line to the side_bar html
        // side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '<\/a><br>';
        i++;
        return marker;
      }


      // This function picks up the click and opens the corresponding info window
      
      // lat="43.216934" lng="27.872528"
      // 43.214000,27.893000
      
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng( 43.216934,27.872528), 14);


      // Read the data from example5.xml
      var request = GXmlHttp.create();
      request.open("GET", "/js_css/map_points_bg.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
          for (var i = 0; i < markers.length; i++) {
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            var html = "<div style=\"font-weight:bold; color:navy; text-decoration:underline;\">"+ markers[i].getAttribute("html1") + "</div>" + markers[i].getAttribute("html2") +"<br />" + markers[i].getAttribute("html3");
            var label = markers[i].getAttribute("label");
            // === read the icontype attribute ===
            var icontype = markers[i].getAttribute("icontype");
            // === create the marker, passing the icontype string ===
            var marker = createMarker(point,label,html,icontype);
            map.addOverlay(marker);
          }
          // put the assembled side_bar_html contents into the side_bar div
          // document.getElementById("side_bar").innerHTML = side_bar_html;
        }
      }
      request.send(null);
    }

    else {
      alert("Съжаляваме но Google Maps API не е съвместимо с този браузър");
    }
    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/   
    // http://econym.googlepages.com/index.htm

    //]]>
}
function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }