var minmagnitude = 0;         // CDA 2 - Feb-01-2009 - Changing CheckResize - Check *1 to 3*
//var url = "one.xml";
//var url = "20.xml";
var url = "library.xml";
var latitude = 23;
var longitude = 15;
var zoom = 15;
var type = "map";
var map;
var timeOut = 150;
var i=0;
var items;
var M0count = 0;
var M1count = 0; var M2count = 0; var M3count = 0; 
var M4count = 0; var M5count = 0; var M6count = 0;
var whiteicon; var blueicon; var orangeicon; var greenicon; var purpleicon; 
var blackicon;

//Detect IE5.5+
var isMSIE = false;
var isOpera = false;
var MSIEversion=0;
if (navigator.appVersion.indexOf("MSIE")!=-1) {
  temp=navigator.appVersion.split("MSIE");
  MSIEversion=parseFloat(temp[1]);
}
if (MSIEversion>=5.5) { //NON IE browser will return 0
  isMSIE = true;
}
if (navigator.userAgent.indexOf("Opera")!=-1) {
  isOpera = true;
  isMSIE = false;
}

// Centers the map on the given coordinates and chooses either
// satellite, hybrid or map type
function recenter(latitude, longitude, zoom, type) {
  map.centerAndZoom(new GPoint(latitude, longitude), zoom);

// CDA: Same as below

//  if (type == 'sat') {
//    map.setMapType(G_SATELLITE_TYPE);
//  } else if (type == 'hybrid') {
//    map.setMapType(G_HYBRID_TYPE);
//  } else {
//    map.setMapType(G_MAP_TYPE);
//  }

  if (type == 'sat') {
    map.setMapType(G_SATELLITE_MAP);
  } else if (type == 'hybrid') {
    map.setMapType(G_HYBRID_MAP);
  } else {
    map.setMapType(G_NORMAL_MAP);
  }


}

// Creates a marker whose info window displays the given text
function createMarker(point, magnitude, link, title, date, location) {
   switch (magnitude) {
   case "0":
        var marker = new GMarker(point,blackicon);
	M0count++;
        document.getElementById('M0').innerHTML = M0count;
	break
   case "1":
        var marker = new GMarker(point,purpleicon);
	M1count++;
        document.getElementById('M1').innerHTML = M1count;
	break
   case "2":
        var marker = new GMarker(point,orangeicon);
	M2count++;
        document.getElementById('M2').innerHTML = M2count;
	break
   case "3":
        var marker = new GMarker(point,greenicon);
	M3count++;
        document.getElementById('M3').innerHTML = M3count;
	break
   case "4":
        var marker = new GMarker(point,blueicon);
	M4count++;
        document.getElementById('M4').innerHTML = M4count;
	break
   case "5":
        var marker = new GMarker(point,whiteicon);
	M5count++;
        document.getElementById('M5').innerHTML = M5count;
	break
   default:
        var marker = new GMarker(point);
	M6count++;
        document.getElementById('M6').innerHTML = M6count;
   }

//MTcount = M0count + M1count + M2count + M3count + M4count + M5count + M6count;
//document.getElementById('MT').innerHTML = MTcount;

// With no Book Towns as part of destructions
MTcount = M1count + M2count + M3count + M4count + M5count + M6count;
document.getElementById('MT').innerHTML = MTcount;


// With "Did you Feel It?"
// var html = '<span id="marker"> <p><a href="'+ link +'" onclick="window.open(this.href,\'newwin\'); return false;">' + title + '</a><br>' + date + '</p><p><a href="http://pasadena.wr.usgs.gov/shake/ous/html/unknown_form.html" onclick="window.open(this.href,\'newwin\'); return false;">Did You Feel It?</a></p></span>';
// GEvent.addListener(marker, "click", function() {
// marker.openInfoWindowHtml(html);
// });

// Without "Did you Feel It?" and without link
// var html = '<span id="marker"> <p><a href="'+ link +'" onclick="window.open(this.href,\'newwin\'); return false;">' + title + '</a><br>' + location + '<br>' + date + '</p><p><i>Full description and references in the book</i>.</p></span>';
// GEvent.addListener(marker, "click", function() {
// marker.openInfoWindowHtml(html);
// });

// var html = '<span id="marker"> <p>' + title + '<br>' + location + '<br>' + date + '</p><p><i>Full description and references in the book</i>.</p></span>';

// This might work
// var html = '<span id="marker"> <p>' + "(Key: " + MTcount + ") - " + title + '<br>' + location + '<br>' + date + '</p><p><i>Full description and references in the book</i>.</p></span>';

// Check count
 var html = '<span id="marker"> <p>' +  title + '<br>' + location + '<br>' + date + '</p><p>' + "(Key: " + MTcount + ")" + '</p><p><i>Full description and references in the book</i>.</p></span>';

   GEvent.addListener(marker, "click", function() {
   marker.openInfoWindowHtml(html);
   });



   return marker;
}

function onLoad() {
  if (!GBrowserIsCompatible()) {
    var err = document.getElementById("errmsg");
    err.style.visibility = "visible";
  } else {
  getParameters();
  var baseicon = new GIcon();
//baseicon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
  baseicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  baseicon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  baseicon.iconSize = new GSize(20, 35); // new GSize(12, 20);
  baseicon.shadowSize = new GSize(22, 20);
  baseicon.iconAnchor = new GPoint(6, 20);
  baseicon.infoWindowAnchor = new GPoint(5, 1);

  blackicon = new GIcon(baseicon);
//blackicon.image = "http://labs.google.com/ridefinder/images/mm_20_black.png";
  blackicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  blackicon.iconSize = new GSize(30, 30); // CDA - Nov-25-2011

  purpleicon = new GIcon(baseicon);
//purpleicon.image = "http://labs.google.com/ridefinder/images/mm_20_purple.png";
  purpleicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  purpleicon.iconSize = new GSize(30, 12); // CDA - Nov-25-2011

  orangeicon = new GIcon(baseicon);
//orangeicon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
  orangeicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  orangeicon.iconSize = new GSize(12, 30); // CDA - Nov-25-2011

  whiteicon = new GIcon(baseicon);
//whiteicon.image = "http://labs.google.com/ridefinder/images/mm_20_white.png";
  whiteicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  whiteicon.iconSize = new GSize(12, 20); // CDA - Nov-25-2011

  greenicon = new GIcon(baseicon);
//greenicon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
  greenicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  greenicon.iconSize = new GSize(12, 12); // CDA - Nov-25-2011

  blueicon = new GIcon(baseicon);
//blueicon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
  blueicon.image = "http://www.google.com/mapfiles/marker.png"; // CDA - Nov-25-2011
  blueicon.iconSize = new GSize(20, 20); // CDA - Nov-25-2011


  map = new GMap(document.getElementById("map"));           // CDA: Nov-23-2008
//map = new GMap2(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GScaleControl());
  map.centerAndZoom(new GPoint(latitude, longitude), zoom);  // CDA: Nov-23-2008
//map.centerAndZoom(new GLatLng(latitude, longitude), zoom);

// Changed on Nov-02-2008

//  if (type == 'sat') {
//    map.setMapType(G_SATELLITE_TYPE);
//  } else if (type == 'hybrid') {
//    map.setMapType(G_HYBRID_TYPE);
//  } else {
//    map.setMapType(G_MAP_TYPE);
//  }

// CDA: one site (http://groups.google.com/group/Google-Maps-API/browse_thread/thread/eec00441339c0a52) said:

// I had the same problem since this morning but changed the following:
//
// G_SATELLITE_TYPE changed to: G_SATELLITE_MAP
// G_MAP_TYPE change to: G_NORMAL_MAP
// G_HYBRID_TYPE changed to: G_HYBRID_MAP 


  if (type == 'sat') {
    map.setMapType(G_SATELLITE_MAP);
  } else if (type == 'hybrid') {
    map.setMapType(G_HYBRID_MAP);
  } else {
    map.setMapType(G_NORMAL_MAP);
  }

  var request = GXmlHttp.create();
  request.open("GET", url, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
      items = xmlDoc.documentElement.getElementsByTagName("item");
      window.setTimeout(mapOne,timeOut);
    }
  }
  request.send(null);


// *1* From here ...
//  var center = map.getCenterLatLng();
//  document.getElementById("latitude").innerHTML = center.y;
//  document.getElementById("longitude").innerHTML = center.x;
//
//  GEvent.addListener(map, "moveend", function() {
//    var center = map.getCenterLatLng();
//    document.getElementById("latitude").innerHTML = center.y;
//    document.getElementById("longitude").innerHTML = center.x;
//  }
// *1* ... to here, it works

// *2* This is added, from here ...
 // Monitor the window resize event and let the 'map' know when it occurs
    if (window.attachEvent) { 
      window.attachEvent("onresize", function() {map.checkResize()} );
    } else {
      window.addEventListener("resize", function() {map.checkResize()} , false);
    }

    if (document.getElementById("latitude")) {
      var center = map.getCenter();
      document.getElementById("latitude").innerHTML = center.y;
      document.getElementById("longitude").innerHTML = center.x;

      GEvent.addListener(map, "moveend", function() {
        var center = map.getCenter();
        document.getElementById("latitude").innerHTML = center.y;
        document.getElementById("longitude").innerHTML = center.x;
      });

// *2* ... to here

    }
  }  // end of 'else'
}  // end of 'onload'

function getValueByTagName(node, tag) {
  var a = node[i].getElementsByTagName(tag)[0];
  if (a) {
    if (GXml.value(a)) {
      return(GXml.value(a));
    } else {
      alert('value of " + tag + " is null (empty?)');
    }
  } else {
  alert ('node[i] is null: tag=' + tag);
  }
}

function mapOne() {
  if ( i < items.length ) {
    var max=Math.min(i+10, items.length);
    while (i < max) {
      if (isMSIE ) {
        var point = new GPoint(parseFloat(getValueByTagName(items,'geo:long')),
                               parseFloat(getValueByTagName(items,'geo:lat')));
      } else {
        var point = new GPoint(parseFloat(getValueByTagName(items,'long')),
                               parseFloat(getValueByTagName(items,'lat')));
      }
      var title = getValueByTagName(items,'title');
      var link = getValueByTagName(items,'link');
      var date = getValueByTagName(items,'description');
      var location = getValueByTagName(items,'location');
      if (isMSIE ) {
        var magnitude = getValueByTagName(items,'dc:subject');
      } else {
        var magnitude = getValueByTagName(items,'subject');
      }
      if (magnitude >= minmagnitude) {
        var marker = createMarker(point, magnitude, link, title, date, location); //CDA
        map.addOverlay(marker);
      }
      i++;
    }
    if (document.getElementById('loadingMessage')) {
      document.getElementById('loadingMessage').innerHTML= "Loading " + max + " of " + items.length ;
    }
    window.setTimeout(mapOne,timeOut);
  } else {
    document.getElementById('loading').style.visibility='hidden';
  }

  // *3* This part was moved to the 'onload' procedure. From here ...
  // Monitor the window resize event and let the map know when it occurs
//  if (window.attachEvent) { 
//    window.attachEvent("onresize", function() {map.CheckResize()} );
//  } else {
//    window.addEventListener("resize", function() {map.CheckResize()} , false);
//  }
//   *3* ... up to here
}

function linkToThisPage() {
  var curURL = new String(location.href);
  var baseURL = curURL.split('?')[0];
  var curzoom = map.getZoomLevel();
  var center = map.getCenterLatLng();
  var curlat = center.x;
  var curlon = center.y;

// CDA: Same as above

//  if (map.getCurrentMapType() == G_SATELLITE_TYPE) {
//    var curtype = 'sat';
//  } else if (map.getCurrentMapType() == G_HYBRID_TYPE) {
//    var curtype = 'hybrid';
//  } else {
//    var curtype = 'map';
//  }

  if (map.getCurrentMapType() == G_SATELLITE_MAP) {
    var curtype = 'sat';
  } else if (map.getCurrentMapType() == G_HYBRID_MAP) {
    var curtype = 'hybrid';
  } else {
    var curtype = 'map';
  }



  var url = baseURL + "?lat=" + curlat + "&lon=" + curlon + "&type=" + curtype + "&zoom=" + curzoom;
  location.href=url;
}

function getParameters() {
  var url = new String(location.href);
  if (url.indexOf('?') > -1) {
    var queryString = url.split('?');
    if (queryString[1].length > 0) {
      var paramString = queryString[1];
      var params = paramString.split("&");
      for (var i=0; i<= params.length-1; i++){
        var curParam= params[i].split("=");
        if (curParam[1].length > 0) {
	  var param=curParam[0].toLowerCase();
          switch (param) {
            case "lon":
              var curlon = curParam[1];
	      if ((curlon >=-180 ) && (curlon <= 180)) {
                longitude = curParam[1];
	      }
              break;
            case "lat":
              var curlat = curParam[1];
	      if ((curlat >=-180 ) && (curlat <= 180)) {
                latitude = curlat;
	      }
              break;
            case "zoom":
              var curzoom = curParam[1];
	      if ((curzoom >=1 ) && (curzoom <= 18)) {
	        zoom = curzoom;
	      }
              break;
            case "type":
              if ( curParam[1].toLowerCase() == 'sat' ) {
                type = "sat";
              } else if ( curParam[1].toLowerCase() == 'hybrid' ) {
                type = "hybrid";
              } else {
                type = "map";
              }
              break;
          }
	}
      }
    }
  }
}
