
var gmarkers = [];
var gicons = [];
var map;
var text = "";
var cmap;
var zoomla;
var zoomlb;

var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(32,32);
baseIcon.shadowSize=new GSize(56,32);
baseIcon.iconAnchor=new GPoint(16,32);
baseIcon.infoWindowAnchor=new GPoint(16,0);
gicons["church"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon11.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon11s.png");
gicons["gas"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon29.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon29s.png");
gicons["hotel"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
gicons["bank"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon58.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon58s.png");
gicons["adm"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon10.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon10s.png");
gicons["other"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon13.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon13s.png");
gicons["shop"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon26.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon26s.png");
gicons["hospital"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon46.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon46s.png");
gicons["hospital_vet"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/vet.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon46s.png");
gicons["sight"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon46.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_mon"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/modernmonument.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_statue"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/statue.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_villa"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/villa-tourism.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_ruins"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/ancienttempleruin.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_cemetary"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/cemetary.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_beach"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/beach.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_fountain"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/fountain.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_museum"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/museum-archeological.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_wheel"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/ferriswheel.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_lake"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/lake.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sight_train"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/steamtrain.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_skicross"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/crosscountryskiing.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_skimount"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/skiing.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_sail"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/sailboat-sport.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_skateice"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/iceskating.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_skateboard"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/skateboarding.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["sport_tennis"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/tennis2.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["photo"] = new GIcon(baseIcon, "http://google-maps-icons.googlecode.com/files/photo.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon46s.png");
gicons["camp"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon53.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon53s.png");
gicons["place"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon56.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon56s.png");
gicons["service"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon29.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon29s.png");
gicons["eat"] = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon27.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon27s.png");

//
function gup( name )
{
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var tmpURL = window.location.href;
  var results = regex.exec( tmpURL );
  if( results == null )
    return "";
  else
    return results[1];
}

//
var zl = gup('zl');
var lol = gup('lo');
var lal = gup('la');
var cat = gup('cat');
var xdf = gup('xd');
if ( xdf.length < 2 ) { xdf = '01'; }
var xmlf = 'trk_gmap_data-' + xdf + '.xml';
if ( cat.length < 2 ) { cat = 'sight'; }

//
function createMarker(point, name, html, category, icontype) {
  var marker = new GMarker(point, {icon:gicons[icontype], title:name});  
// === Store the category and name info as a marker properties ===
  marker.mycategory = category;                                 
  marker.myname = name;

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });

  gmarkers.push(marker);
  return marker;
}

function createStreet(pts, colour, width, html) {

  var street = new GPolyline(pts,colour,width);

  GEvent.addListener(street, "click", function() {
    map.openInfoWindow(pts[0],html);
//    street.openInfoWindowHtml(html);
  });

  return street;
}


// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show(category) {
  for (var i=0; i<gmarkers.length; i++) {
     if (gmarkers[i].mycategory == category) {
        gmarkers[i].show();
     }
 }
 // == check the checkbox ==
 document.getElementById(category+"box").checked = true;
}

// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide(category) {
  for (var i=0; i<gmarkers.length; i++) {
    if (gmarkers[i].mycategory == category) {
      gmarkers[i].hide();
    }
  }
  // == clear the checkbox ==
  document.getElementById(category+"box").checked = false;
  // == close the info window, in case its open on a marker that we just hid
  map.closeInfoWindow();
}

//
function myclick(i) {
  GEvent.trigger(gmarkers[i],"click");
}

// == rebuilds the sidebar to match the markers currently displayed ==
function makeSidebar() {
  text = "";
  for (var i=0; i<gmarkers.length; i++) {
    if (!gmarkers[i].isHidden()) {
      text += '- <a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '</a><br>';
    }
  }
  text = '<p>' + text + '</p>';
  document.getElementById("side_bar").innerHTML = text;
}

// == a checkbox has been clicked ==
function boxclick(box,category) {
  if (box.checked) {
    show(category);
  } else {
    hide(category);
  }
  // == rebuild the side bar
  makeSidebar();
}

//
function createMap() {
 map = new GMap2(document.getElementById("map"));

 GEvent.addListener(map, "moveend", function() {
   cmap = map.getCenter();
   zoomla = map.getZoom();
   var latLngStr = '(' + cmap.y + ', ' + cmap.x + ') zoom=' + zoomla;
   document.getElementById("message").innerHTML = latLngStr;
//   var clink = '<a href="trk_gmap.php?xd=' + xdf + '&lo=' + cmap.y + '&la=' + cmap.x + '&zl=' + zoomla + '">Ссылка на текущую карту</a>';
   var clink = '<a href="?xd=' + xdf + '&lo=' + cmap.y + '&la=' + cmap.x + '&zl=' + zoomla + '">Ссылка на текущую карту</a>';
   document.getElementById("clink").innerHTML = clink;
 });
 
 GEvent.addListener(map, "zoom", function() {
   zoomlb = map.getZoom();
   var zoomlStr = '(' + zoomlb + ')';
   document.getElementById("zoom").innerHTML = zoomlStr;
 });

 map.addControl(new GSmallMapControl());
 map.addControl(new GMapTypeControl());
 map.addControl(new GOverviewMapControl(new GSize(120,100)));

 GDownloadUrl(xmlf, function(data, responseCode) {
  var xml = GXml.parse(data);
//
  var area = xml.documentElement.getElementsByTagName("area");
  if ( zl == "" ) { zl = area[0].getAttribute("zl"); }
  if ( lol == "" ) { lol = area[0].getAttribute("lol"); }
  if ( lal == "" ) { lal = area[0].getAttribute("lal"); }
  map.setCenter(new GLatLng(lol, lal), parseInt(zl));  
//  
  var markers = xml.documentElement.getElementsByTagName("info");
  for (var i = 0; i < markers.length; i++) {
//       var category = markers[i].getAttribute("topic");
       var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                            parseFloat(markers[i].getAttribute("lng")));
//       var icontype = markers[i].getAttribute("icon");
       var marker = createMarker(point, 
			    markers[i].getAttribute("name"),
			    markers[i].getAttribute("html"),
			    markers[i].getAttribute("topic"),
			    markers[i].getAttribute("icon"));
       map.addOverlay(marker);
  }

// ========= Now process the polylines ===========
 var streets = xml.documentElement.getElementsByTagName("street");
 if (streets.length > 0) {
// read each line
  for (var a = 0; a < streets.length; a++) {
// get any line attributes
     var colour = streets[a].getAttribute("colour");
     var width  = parseFloat(streets[a].getAttribute("width"));
     var html = streets[a].getAttribute("html");
// read each point on that line
     var nodes = streets[a].getElementsByTagName("point");
     var pts = [];
     for (var b = 0; b < nodes.length; b++) {
        pts[b] = new GLatLng(parseFloat(nodes[b].getAttribute("lat")), 
                             parseFloat(nodes[b].getAttribute("lng")));
     }
     var street = createStreet(pts, colour, width, html);
     map.addOverlay(street);
//     map.addOverlay(new GPolyline(pts,colour,width));
  }
 }
// ================================================

  // == show or hide the categories initially ==
  if ( cat == 'camp' ) { show("camp"); } else { hide("camp"); }
  if ( cat == 'place' ) { show("place"); } else { hide("place"); }
  if ( cat == 'sight' ) { show("sight"); } else { hide("sight"); }
  if ( cat == 'service' ) { show("service"); } else { hide("service"); } 
  if ( cat == 'state' ) { show("state"); } else { hide("state"); } 
  if ( cat == 'pano' ) { show("pano");  } else { hide("pano"); } 
  if ( cat == 'sport' ) { show("sport"); } else { hide("sport"); }
  if ( cat == 'eat' ) { show("eat"); } else { hide("eat"); }
  // == create the initial sidebar ==
  makeSidebar();  
 });

}

function onLoad() {
 createMap();
}
