var map;
var state_save = "";
var centerLatitude = 41.869;
var centerLongitude = -71.514;
var startZoom = 16;

var small_icons = false;

var baseIcon = new GIcon();
var baseIconSmall = new GIcon();

baseIcon.shadow = "http://www.scaredycatfilms.com/benchmarks/icons/icons.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);
baseIcon.infoShadowAnchor = new GPoint(18, 25);

baseIconSmall.shadow = "http://www.scaredycatfilms.com/benchmarks/icons/icons_sm.png";
baseIconSmall.iconSize = new GSize(12, 20);
baseIconSmall.iconAnchor = new GPoint(9, 20);
baseIconSmall.infoWindowAnchor = new GPoint(9, 2);
baseIconSmall.infoShadowAnchor = new GPoint(18, 25);
	
// Scaled Green Icon - Marks in Good Condition, and Scaled
var scaleGIcon = new GIcon(baseIcon);
scaleGIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/icongs.png";
var scaleGIconSM = new GIcon(baseIconSmall);
scaleGIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/green.png";
	
// Adjusted Green Icon - Marks in Good Condition, and Adjusted
var adjGIcon = new GIcon(baseIcon);
adjGIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconga.png";
var adjGIconSM = new GIcon(baseIconSmall);
adjGIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/greena.png";

// Scaled Red Icon - Marks in UNK Condition, and Scaled
var scaleRIcon = new GIcon(baseIcon);
scaleRIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconrs.png";
var scaleRIconSM = new GIcon(baseIconSmall);
scaleRIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/red.png";

// Adjusted Red Icon - Marks in UNK Condition, and Adjusted
var adjRIcon = new GIcon(baseIcon);
adjRIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconra.png";
var adjRIconSM = new GIcon(baseIconSmall);
adjRIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/reda.png";

// Monument Whie Icon - Marks in MONUMENTED Condition, and Scaled
var scaleWIcon = new GIcon(baseIcon);
scaleWIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconws.png";
var scaleWIconSM = new GIcon(baseIconSmall);
scaleWIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/white.png";

// Monument Whie Icon - Marks in MONUMENTED Condition, and Adjusted
var adjWIcon = new GIcon(baseIcon);
adjWIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconwa.png";
var adjWIconSM = new GIcon(baseIconSmall);
adjWIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/whitea.png";

// Cluster
var clustIcon = new GIcon(baseIcon);
clustIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconc.png";
var clustIconSM = new GIcon(baseIconSmall);
clustIconSM.image = "http://www.scaredycatfilms.com/benchmarks/icons/yellow.png";

// Not used at this time.
var destIcon = new GIcon(baseIcon);
destIcon.image = "http://www.scaredycatfilms.com/benchmarks/icons/iconr.png";

function updateMarkers(state,db,old_center,old_zoom) {

	// Save current State.
	state_save = state;
    // Check and see if it has been a significant move
    var new_center = map.getCenter();
	var new_zoom = map.getZoom();
	if ( (Math.abs((new_center.lat()-old_center.lat())) > 0.003) ||
	     (Math.abs((new_center.lng()-old_center.lng())) > 0.003) ||
		 (old_zoom != new_zoom) ) {
		 // Refresh, we have moved!
         //alert("Outside Center N,O" + new_center + "@" + old_center);
		 
	//remove the existing points
	map.clearOverlays();
	//create the boundary for the data
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var getVars = 'ne=' + northEast.toUrlValue()
	+ '&sw=' + southWest.toUrlValue() + '&state_id=' + state
	//log the URL for testing
	//GLog.writeUrl('server.php?'+getVars);
	//retrieve the points using Ajax
	//alert("Point1");
	//alert(state);
	var request = GXmlHttp.create();
	request.open('GET', 'server.php?'+getVars, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var jscript = request.responseText;
			//alert(jscript);
			var points;
			eval(jscript);
			//create each point from the list
			var counter = 0;
			for (i in points) {
  				var point = new GLatLng(points[i].lat,points[i].lng);
				// Check if it is a cluster mark.
				if (points[i].cluster > 0)
				{
				    // If so, call Cluster marker maker.
					var marker = createClusterMarker(point, points[i].cluster);
				}
				else
				{
	   			    var marker = createMarker(point,points[i].name,points[i].pid,points[i].adjscale,points[i].ldate,points[i].lcond,points[i].lfinder,points[i].ngslink, points[i].cluster);
				}
				map.addOverlay(marker);
				counter++;
			}
			//alert("i value is: " + counter);
			//document.form1.mark_count.value = counter;
			// Display Info based on Cluster or not.
			if (points[i].cluster > 0)
			{
			    counter = points[i].cluster * 100;
			    document.getElementById("message").innerHTML = "There are over " + counter.toString() + " Benchmarks represented by the markers being displayed at this zoom level.";
			}
			else
			{
			    document.getElementById("message").innerHTML = "There are " + counter.toString() + " Benchmarks available for viewing at this zoom level.";
			}
		}
	}

	request.send(null);

    }   /* End of if inside, outside center */
	else
	{
	    // Do nothing, haven't moved far enough
	    alert("Inside Center");
	}
}

function useSmallIcons(sm_icon_flag) {
	small_icons = sm_icon_flag;

	//updateMap
	updateMarkers(state_save,1,(new GLatLng('0.0','0.0')),1);
}

function createMarker(point,name,PID,adj_scale,last_date,last_cond,last_finder,ngs_link, counter) {
    // Create the icon depending on Condition, and Scale/Adjusted.
	// Monumented? (last_cond == "MONUMENTED") || (last_cond == "MONUMENTED      ")
    if ((last_cond == "GOOD") || (last_cond == "GOOD            ")){
	    // Icon will be Green
		if (adj_scale == "ADJUSTED") {
			if (small_icons)
				var marker = new GMarker(point,adjGIconSM);
			else
				var marker = new GMarker(point,adjGIcon);
		}
		else {
			if (small_icons)
				var marker = new GMarker(point,scaleGIconSM);
			else
				var marker = new GMarker(point,scaleGIcon);
		}
	}
	else if ((last_cond == "MONUMENTED") || (last_cond == "MONUMENTED      ")){
	    // Icon will be White
		if (adj_scale == "ADJUSTED") {
			if (small_icons)
				var marker = new GMarker(point,adjWIconSM);
			else
				var marker = new GMarker(point,adjWIcon);
		}
		else {
			if (small_icons)
				var marker = new GMarker(point,scaleWIconSM);
			else
				var marker = new GMarker(point,scaleWIcon);
		}
	}
	else {
	    // Icon will be Red - for unknown Condition
		if (adj_scale == "ADJUSTED") {
			if (small_icons)
				var marker = new GMarker(point,adjRIconSM);
			else
				var marker = new GMarker(point,adjRIcon);
		}
		else {
			if (small_icons)
				var marker = new GMarker(point,scaleRIconSM);
			else
				var marker = new GMarker(point,scaleRIcon);
		}
	}

	var pt_loc = marker.getPoint();
 	GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml("<font size='2' face='Arial, Helvetica, sans-serif'>" + "<B>" + PID + ": </B>" + name + "<BR><B>Location is: </B>" 
		     + adj_scale + "<BR>" + "<B>Last Activity: </B>" + last_date + ", " + last_cond +
			 ", <B>By</B> " + last_finder + "<BR><B>NGS Data Sheet: </B>" + 
			 "<A HREF='" + ngs_link + "' target=blank>Link</A>" + 
			 ", <B>Geocache Page: </B>" + "<A HREF='http://www.geocaching.com/mark/details.aspx?PID=" + PID + "' target=blank>Link</A>" +
			 "</font>",{suppressMapPan:true});
	});   /* End of addListener */
    return marker;
}

function createClusterMarker(point, counter) {
    // Use Cluster Icon
	if (small_icons)
		var marker = new GMarker(point,clustIconSM);
	else
		var marker = new GMarker(point,clustIcon);

	var pt_loc = marker.getPoint();
 	GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml("<font size='2' face='Arial, Helvetica, sans-serif'>" + "<B>" + 
		"This icon represents a cluster <br>" + "of " + counter + " Benchmarks</B><BR>" + 
		"Zoom in to View More Markers in the Area." + 
		"</font>", {suppressMapPan:true});
	});   /* End of addListener */
    return marker;
}


function showAddress(address)
{
    if (geocoder)
	{

        geocoder.getLatLng(address, function(point) {
            if (!point)
			{
              alert(address + " not found");
            } else {
              map.setCenter(point, 11);
              //var marker = new GMarker(point);
              //map.addOverlay(marker);
              //marker.openInfoWindowHtml(address);
            }
        });
    }   // End of if... geocoder
}   // End of function showAddress.

/*
Copyright [2009] [David Eschmann and ScaredyCatFilms.com]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
