//gmaps code
var iconNWSnew = new GIcon(); 
iconNWSnew.image = 'http://maps.google.com/mapfiles/kml/pal4/icon44.png';
iconNWSnew.shadow = 'http://maps.google.com/mapfiles/kml/pal4/icon44s.png';
iconNWSnew.iconSize = new GSize(32, 32);
iconNWSnew.shadowSize = new GSize(59,32);
iconNWSnew.iconAnchor = new GPoint(16, 32);
iconNWSnew.infoWindowAnchor = new GPoint(16, 1);

var iconDS = new GIcon(); 
iconDS.image = 'http://maps.google.com/mapfiles/ms/micons/red.png';
iconDS.shadow = 'http://maps.google.com/mapfiles/ms/micons/msmarker.shadow.png';
iconDS.iconSize = new GSize(32, 32);
iconDS.shadowSize = new GSize(59, 32);
iconDS.iconAnchor = new GPoint(16, 32);
iconDS.infoWindowAnchor = new GPoint(16, 1);

var iconBoth = new GIcon(); 
iconBoth.image = 'http://maps.google.com/mapfiles/kml/pal4/icon44.png';
iconBoth.shadow = 'http://maps.google.com/mapfiles/kml/pal4/icon44s.png';
iconBoth.iconSize = new GSize(32, 32);
iconBoth.shadowSize = new GSize(59, 32);
iconBoth.iconAnchor = new GPoint(16, 32);
iconBoth.infoWindowAnchor = new GPoint(16, 1);

var iconSchool = new GIcon(); 
iconSchool.image = 'http://maps.google.com/mapfiles/kml/pal2/icon10.png';
iconSchool.shadow = 'http://maps.google.com/mapfiles/kml/pal2/icon10s.png';
iconSchool.iconSize = new GSize(32, 32);
iconSchool.shadowSize = new GSize(59, 32);
iconSchool.iconAnchor = new GPoint(16, 32);
iconSchool.infoWindowAnchor = new GPoint(16, 1);


var customIcons = [];
customIcons["NOAA's National Weather Service"] = iconNWSnew;
customIcons["All"] = iconDS;
customIcons["Both"] = iconBoth;
customIcons["School"] = iconSchool;
   
    var map;

    function map_initialize() {
document.getElementById('loading_img').src='loading.gif';
        var strXMLData = 'js/NMFS_Tuna_Visits_2.xml';
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas")); 
        map.addControl(new GLargeMapControl());
//        map.addControl(new GOverviewMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(42.4419, -110.1419), 3);
        map.enableDoubleClickZoom();
        GDownloadUrl(strXMLData, function(data, responseCode)
        {
          // To ensure against HTTP errors that result in null or bad data,
         // always check status code is equal to 200 before processing the data
          if(responseCode == 200) 
         {
	    var xml = GXml.parse(data);			
  	  var gmapinfo = xml.documentElement.getElementsByTagName("ginfo");
      var strTitle = '<font face="Arial" size="2">' + gmapinfo.length + ' stores visited.</font>';
      document.getElementById('map_title').innerHTML = strTitle;
	  var fullURL = parent.document.URL;		  
	  var qs = fullURL.substring(fullURL.indexOf('?')+5, fullURL.length);
			
		var label;
		var visited;
		var collaborator;
		var city;
		var state;
		var store;
		var website;
		var news;
		var collabstore;
		var collabdate;

	  for (var i = 0; i < gmapinfo.length; i++)
            {
		if (gmapinfo[i].getAttribute("city") == city && gmapinfo[i].getAttribute("state") == state) {
			label = label + '<td>' + gmapinfo[i].getAttribute("label") + '</td>';
			store = store + '<td>' + gmapinfo[i].getAttribute("store") + '</td>';
			visited = visited + '<td>' + gmapinfo[i].getAttribute("visited") + '</td>';
		  if (gmapinfo[i].getAttribute("collaborator") == "NOAA's National Weather Service") {
		  	collabstore = gmapinfo[i].getAttribute("store");
		  	collabdate = gmapinfo[i].getAttribute("visited");
		  }
			if (gmapinfo[i].getAttribute("collaborator") == collaborator) {
		    collaborator = gmapinfo[i].getAttribute("collaborator")
		  }
		  else {
		  	collaborator = 'Both';
		  }
			city = gmapinfo[i].getAttribute("city");
			state = gmapinfo[i].getAttribute("state");
			website = website + gmapinfo[i].getAttribute("collaborator_website");
			news = news + gmapinfo[i].getAttribute("news");
		}
		else {
			collabstore = "";
			label = '<td>' + gmapinfo[i].getAttribute("label") + '</td>';
			store = '<td>' + gmapinfo[i].getAttribute("store") + '</td>';
			visited = '<td>' + gmapinfo[i].getAttribute("visited") + '</td>';
			collaborator = gmapinfo[i].getAttribute("collaborator");
			city = gmapinfo[i].getAttribute("city");
			state = gmapinfo[i].getAttribute("state");
			website = gmapinfo[i].getAttribute("collaborator_website");
			news = gmapinfo[i].getAttribute("news");
		  if (gmapinfo[i].getAttribute("collaborator") == "NOAA's National Weather Service") {
		  	collabstore = gmapinfo[i].getAttribute("store");
		  	collabdate = gmapinfo[i].getAttribute("visited");
		  }
		}
		var point = new GLatLng(parseFloat(gmapinfo[i].getAttribute("lat")),parseFloat(gmapinfo[i].getAttribute("lng")));
		var x = (i + 1);
		if (x < gmapinfo.length) {
		 if (gmapinfo[x].getAttribute("city") == gmapinfo[i].getAttribute("city") && gmapinfo[x].getAttribute("state") == gmapinfo[i].getAttribute("state")) {
	   }
	   else {
				label = '<tr id=\"infowin\"><td align="right" id=\"titlecell\">Name</td>' + label + '</tr>';
				store = '<tr id=\"storerow\"><td align="right" id=\"titlecell\">Store: </td>' + store + '</tr>';
				visited = '<tr id=\"daterow\"><td align="right" id=\"titlecell\">Visited: </td>' + visited + '</tr>';
				var marker = cMarker(point, label, collaborator, visited, city, state, store, website, news, collabstore, collabdate);
		    map.addOverlay(marker);
	   }
	  }
	  else {
				label = '<tr id=\"infowin\"><td align="right" id=\"titlecell\">Name: </td>' + label + '</tr>';
				store = '<tr id=\"storerow\"><td align="right" id=\"titlecell\">Store: </td>' + store + '</tr>';
				visited = '<tr id=\"daterow\"><td align="right" id=\"titlecell\">Visited: </td>' + visited + '</tr>';
				var marker = cMarker(point, label, collaborator, visited, city, state, store, website, news, collabstore, collabdate);
		    map.addOverlay(marker);
	  }
//                   var latlng = new GLatLng(parseFloat(gmapinfo[i].getAttribute("lat")),parseFloat(gmapinfo[i].getAttribute("lng")));
//                  map.addOverlay(new GMarker(latlng));			
	    }
            document.getElementById('loading_img').src='http://webapps.nwfsc.noaa.gov/Tuna/clear.gif';
      } else if(responseCode == -1) {
        alert("Data request timed out. Please try later.");
      } else { 
       alert("Request resulted in error. Check XML file is retrievable.");
      }
      });
     }
    } 

function cMarker(point, label, collaborator, visited, city, state, store, website, news, collabstore, collabdate) {
	var marker = new GMarker(point, customIcons[collaborator]);
	GEvent.addListener(marker, 'click', function() {
		if (website.length>0) {
	    marker.openInfoWindowTabsHtml( [new GInfoWindowTab("Store(s)", "<div id=\"infowin\"><b>" + city + ", " + state + "</b><br><br><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\">" + store  + visited + "</table></div>"), new GInfoWindowTab("Collaborator", "<div id=\"infowin\"><b>" + city + ", " + state + "</b><br>" + collabstore + "<br>" + collabdate + "<br><br>" + website + "<br>NOAA.gov Web Features " + news + "<br></div>")] );
    }
    else {
	    marker.openInfoWindowTabsHtml( [new GInfoWindowTab("Store(s)", "<div id=\"infowin\"><b>" + city + ", " + state + "</b><br><br><table cellspacing=\"1\" cellpadding=\"3\" border=\"0\">" + store  + visited + "</table></div>")] );
    }
	});
	return marker;
}




// Dean Edwards/Matthias Miller/John Resig

function map_init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
   if (_timer) clearInterval(_timer);

  // do stuff
  //writeMenus();
  map_initialize();
};

//var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", map_init, false);
}

///* for Internet Explorer */
///*@cc_on @*/
///*@if (@_win32)*/
//if (IE6) {
//  alert('IE6');
//  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
//  var script = document.getElementById("__ie_onload");
//  script.onreadystatechange = function() {
//    if (this.readyState == "complete") {
//      map_init(); // call the onload handler
//    }
//  }
//};
///*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      map_init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = map_init;

