var iconBlue = new GIcon(); 
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

var iconRed = new GIcon(); 
iconRed.image = 'http://www.google.com/mapfiles/markerB.png';
iconRed.shadow = 'http://www.google.com/mapfiles/shadow50.png';
iconRed.iconSize = new GSize(20, 34);
iconRed.shadowSize = new GSize(37, 34);
iconRed.iconAnchor = new GPoint(6, 20);
iconRed.infoWindowAnchor = new GPoint(5, 1);
  
var iconYellow = new GIcon(); 
iconYellow.image = 'http://labs.google.com/ridefinder/images/mm_20_yellow.png';
iconYellow.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconYellow.iconSize = new GSize(12, 20);
iconYellow.shadowSize = new GSize(22, 20);
iconYellow.iconAnchor = new GPoint(6, 20);
iconYellow.infoWindowAnchor = new GPoint(5, 1);
  
var iconGreen = new GIcon(); 
iconGreen.image = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
iconGreen.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconGreen.iconSize = new GSize(12, 20);
iconGreen.shadowSize = new GSize(22, 20);
iconGreen.iconAnchor = new GPoint(6, 20);
iconGreen.infoWindowAnchor = new GPoint(5, 1);

var iconBlack = new GIcon(); 
iconBlack.image = 'http://labs.google.com/ridefinder/images/mm_20_black.png';
iconBlack.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlack.iconSize = new GSize(12, 20);
iconBlack.shadowSize = new GSize(22, 20);
iconBlack.iconAnchor = new GPoint(6, 20);
iconBlack.infoWindowAnchor = new GPoint(5, 1);

var iconWhite = new GIcon(); 
iconWhite.image = 'http://labs.google.com/ridefinder/images/mm_20_white.png';
iconWhite.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconWhite.iconSize = new GSize(12, 20);
iconWhite.shadowSize = new GSize(22, 20);
iconWhite.iconAnchor = new GPoint(6, 20);
iconWhite.infoWindowAnchor = new GPoint(5, 1);

var iconGrey = new GIcon(); 
iconGrey.image = 'http://labs.google.com/ridefinder/images/mm_20_grey.png';
iconGrey.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconGrey.iconSize = new GSize(12, 20);
iconGrey.shadowSize = new GSize(22, 20);
iconGrey.iconAnchor = new GPoint(6, 20);
iconGrey.infoWindowAnchor = new GPoint(5, 1);
 
var iconPurple = new GIcon(); 
iconPurple.image = 'http://labs.google.com/ridefinder/images/mm_20_purple.png';
iconPurple.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconPurple.iconSize = new GSize(12, 20);
iconPurple.shadowSize = new GSize(22, 20);
iconPurple.iconAnchor = new GPoint(6, 20);
iconPurple.infoWindowAnchor = new GPoint(5, 1);
 
var customIcons = [];


function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.enableDoubleClickZoom();
    map.setCenter(new GLatLng(53.12318673492632,18.03), 13);

    GDownloadUrl("biura_map.php", function(data) {
      var xml = GXml.parse(data);
      var markers = xml.documentElement.getElementsByTagName("marker");
      for (var i = 0; i < markers.length; i++) {
        var title = markers[i].getAttribute("title");
        var pic = markers[i].getAttribute("pic");
        var city = markers[i].getAttribute("city");
        var street = markers[i].getAttribute("street");
        var zip = markers[i].getAttribute("zip");
        var tel = markers[i].getAttribute("tel");
        var channel = markers[i].getAttribute("channel");
        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), 
        parseFloat(markers[i].getAttribute("lng")));
        var marker = createMarker(point, title, city, street, zip, tel, pic, channel);
        map.addOverlay(marker);
        
        if(channel.indexOf("1")!=-1){
          customIcons[channel] = iconRed;
        }
        if(channel.indexOf("4")!=-1){
          customIcons[channel] = iconGreen;
        }
        if(channel.indexOf("3")!=-1){
          customIcons[channel] = iconRed;
        }
        if(channel.indexOf("5")!=-1){
          customIcons[channel] = iconYellow;
        }
        if(channel.indexOf("6")!=-1){
          customIcons[channel] = iconBlack;
        }
        if(channel.indexOf("7")!=-1){
          customIcons[channel] = iconWhite;
        }
        if(channel.indexOf("9")!=-1){
          customIcons[channel] = iconGrey;
        }
        if(channel.indexOf("10")!=-1){
          customIcons[channel] = iconBlack;
        }
        if(channel.indexOf("13")!=-1){
          customIcons[channel] = iconPurple;
        }           
      }
    });
    }
  }
  
function createMarker(point, title, city, street, zip, tel, pic, channel) {
  var marker = new GMarker(point, customIcons[channel]);  
  var ctitle = title.replace(/\n/g,"<br>");
  var ctel = tel.replace(/\n/g,"<br>");
  var html = '<b>' + ctitle + '</b><br/>' + street + '<br/>' + zip + ' ' + city + '<br/>' + ctel + '<br/>';
  GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
  });
  return marker;
}