var weather;
var map;
var myMarkers = new Array();
function openEdit(name) {
  window.open('/atlas2/jsp/uedit.jsp?map=' + name, 'edit', 'width=750,height=700,menubar=no,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes');
}

function createMap(path) {
    //path = queryString("map");
    name = path;
    var request = GXmlHttp.create();
    var d = new Date(); 
    var t = d.getTime();
    
    request.open("GET", "/data2/" + name + ".xml?t=" + t, true);
    request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
      var mapNode = xmlDoc.documentElement;
      var mapLat = parseFloat(mapNode.getAttribute("lat"));
      var mapLon = parseFloat(mapNode.getAttribute("lon"));
      var zoom = parseInt(mapNode.getAttribute("zoom"));

      document.title = name;
      var e = document.getElementById("map");
      e.innerHTML = "";

      var mapHeight = parseInt(mapNode.getAttribute("h"));
      if (mapNode.getAttribute("enableugc") == "1") {
        var e2 = document.getElementById("contrib");
        e2.style.textAlign = "right";
        e2.style.fontSize = "16px";
//        e2.style.paddingBottom = "2px";
//        e2.innerHTML = "<img src='/atlas2/images/tatlas.gif' border='0'/>&nbsp;<a href='javascript:openEdit(\"" + name + "\");'>Contribute to This Map</a>";
e2.innerHTML = "<a href='javascript:openEdit(\"" + name + "\");'><img src='/atlas2/images/contributefinal4.gif' width='170' height='20' border='0'/></a>";
        mapHeight -= 22;
      }

      e.style.width = mapNode.getAttribute("w") + "px";
      e.style.height = mapHeight + "px";
      document.getElementById("fm").style.width = mapNode.getAttribute("w") + "px";
      document.getElementById("contrib").style.width = mapNode.getAttribute("w") + "px";
      if (document.getElementById("sb") != null)
      document.getElementById("sb").style.width=mapNode.getAttribute("w") + "px";
      map = new GMap2(e);

      if (mapNode.getAttribute("control") == "large") {
        map.addControl(new GLargeMapControl());
      } else if (mapNode.getAttribute("control") == "small") {
        map.addControl(new GSmallMapControl());
      }

      if (mapNode.getAttribute("types") == "on") {
        map.addControl(new GMapTypeControl());
      }

      var p = new GLatLng(mapLat, mapLon);
      map.setCenter(p, zoom);
      var mt = mapNode.getAttribute("mapType");
      if (mt != null) {
      if (mt == G_NORMAL_MAP.getName()) {
        map.setMapType(G_NORMAL_MAP);
      } else if (mt == G_SATELLITE_MAP.getName()) {
        map.setMapType(G_SATELLITE_MAP);
      } else if (mt == G_HYBRID_MAP.getName()) {
        map.setMapType(G_HYBRID_MAP);
      } else {
        map.setMapType(G_NORMAL_MAP);
      }
      }

      var tooltips = mapNode.getAttribute("tooltips");
      var enableInfo = mapNode.getAttribute("enableinfo");
      var infoWidth = mapNode.getAttribute("infoWidth");
      var infoHeight = mapNode.getAttribute("infoHeight");
      var mapCre = mapNode.getAttribute("cre");
      weather = parseInt(mapNode.getAttribute("showweather"));
      if (weather == null) weather = 0;
      
      var markers = mapNode.getElementsByTagName("marker");
      for (var m=0; m < markers.length; m++) {
          var icon = markers[m].getAttribute("icon");
          var markerid = markers[m].getAttribute("id");
          var lat = parseFloat(markers[m].getAttribute("lat"));
          var lon = parseFloat(markers[m].getAttribute("lon"));
          var ttl = GXml.value(markers[m].getElementsByTagName("ttl")[0]);
          var localResult = GXml.value(markers[m].getElementsByTagName("localResult")[0]);
          if (localResult == null || localResult == "null") localResult = "";
          var cre = markers[m].getAttribute("creator");
          var cre_url = markers[m].getAttribute("creator_url");
          if (cre_url != null && cre_url != "") {
            var regex = /^http:\/\//;
            if (!regex.test(cre_url)) {
              cre_url = "http://" + cre_url;
            }
          }
          var cre_date = markers[m].getAttribute("cre_date");
          var byl = "";
          if (cre != null && cre != '' && cre != mapCre) {
            byl = "<div style='text-align:right'>";
            if (cre_url != '') byl += "<a href='" + cre_url + "' target='_new'>";
            byl += cre;
            if (cre_url != '') byl += "</a>";
            byl += "<br/>";
            byl += cre_date;
            byl += "</div>";
          }


        
          var txt = "<div style='overflow: auto; font-family: sans-serif; width: " + infoWidth + "px;height: " + infoHeight + "px;'>";
          txt += GXml.value(markers[m].getElementsByTagName("summary")[0]);
          txt += "</div>";
          txt += byl;

          var p = new GLatLng(lat, lon);
          var mrkr = createMarker(p, ttl, txt, tooltips, enableInfo, icon, localResult, lat, lon, weather, markerid, infoWidth);
          myMarkers.push(mrkr);
          map.addOverlay(mrkr);
      }
      var gkh = new GKeyboardHandler(map, window);
      //var gam = new GAdsManager(map, "ca-pub-6243110931517898");
      //gam.enable();

	//try {
	var options;
        var adWidth = parseInt(mapNode.getAttribute("w"));
	if (adWidth > 727) {
		options = {
			'format'   : google.ads.LEADERBOARD,
			'ad_slot'  : '7393806228' 
		};
	} else if (adWidth < 728 && adWidth > 467) {
		options = {
			'format'   : google.ads.BANNER,
			'ad_slot'  : '7607267034' 
		};
	} else {
		options = {
			'format'   : google.ads.HALF_BANNER,
			'ad_slot'  : '8645329921' 
		};
	}
	if (document.getElementById("adblock") != null) {
       ad = new google.ads.MapAd(map, 'pub-6243110931517898', 'adblock', options);
	}
		var overlay_options = {
			'ad_channel': '7064822842'
		};
	if (!name.match(/odesk/) && !name.match(/News-Democrat/i)) {
		var insetAd = new google.ads.MapInsetAd(map, 'pub-6243110931517898', overlay_options);
	}

	//} catch (err) {}
    }
    }
    request.send(null);
}
/*
	function init() {
		var overlay_options = {
			'ad_channel': '7064822842'
		};
		var insetAd = new google.ads.MapInsetAd(map, 'pub-6243110931517898', overlay_options);
	}
*/

function createMarker(point, ttl, txt, tooltips, enableInfo, icon, localResult, lat, lon, weather, markerid,infoWidth) {
  var html = "";
  if (localResult == "") {
  html = "<div style='margin-top: 10px; font-family: sans-serif; font-weight: bold;'>" + ttl + "</div><p>" + txt + "</p>";
  } else {
  html = "<div style='margin-top: 10px; margin-bottom: 5px; border: 1px solid gray; padding: 2px;'>" + localResult + "</div>" + txt;
  }
  if (weather == 1) {
    html += "<div style='height: 160px; width: " + infoWidth + "px;'>";
    html += "<div style='text-align:left;' id='w-" + markerid + "'><b>Loading Live Weather Forecast...</b></div>";
    html += "</div>";
  }

  html = html.replace(/&lt;/, "<");
  html = html.replace(/&gt;/, ">");
  var marker;

  var gicon;
  if (icon != null) {
  if (icon == "" || icon == "undefined") {
    icon = "marker_large";
  }

  if (icon != "marker_large") {
    var p = iconprops[icon];
    gicon = new GIcon();

    gicon.image = p.img_url;
    gicon.shadow = p.shd_url;
    gicon.iconSize = new GSize(parseInt(p.width), parseInt(p.height));
    gicon.shadowSize = new GSize(parseInt(p.shd_w), parseInt(p.shd_h));
    gicon.iconAnchor = new GPoint(parseInt(p.anc_x), parseInt(p.anc_y));
    gicon.infoWindowAnchor = new GPoint(parseInt(p.wanc_x), parseInt(p.wanc_y));    
  }
  }

  if (tooltips == "on") 
    if (icon != null) 
      marker = new GMarker(point, {title: ttl, icon: gicon});
    else
      marker = new GMarker(point, {title: ttl});
  else
    if (icon != null)
      marker = new GMarker(point, {icon: gicon});
    else
      marker = new GMarker(point);

  if (enableInfo == "click") {
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
      if (weather == 1) {
        getWeather(lat, lon, markerid);
      }
    });
  } else if (enableInfo == "mouse") {
    GEvent.addListener(marker, "mouseover", function() {
      marker.openInfoWindowHtml(html);
      if (weather == 1) {
        getWeather(lat, lon, markerid);
      }
    });
  }
marker.html = html;
marker.lat = lat;
marker.lon = lon;
marker.markerid = markerid;
return marker;
}

function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; }
}

function queryString(key){
var page = new PageQuery(window.location.search);
return unescape(page.getValue(key));
}
function displayItem(key){
if(queryString(key)=='false')
{
document.write("you didn't enter a ?name=value querystring item.");
}else{
document.write(queryString(key));
}
}
function show(idx) {
   var marker = myMarkers[idx];
   marker.openInfoWindowHtml(marker.html);
   if (weather == 1) {
      getWeather(marker.lat, marker.lon, marker.markerid);
   }
}
