
// hashtable that contains a list of all the markers we've inserted so far
// be sure not to add a duplicate marker!
// markers are inserted using the course hash as the index (as a string)
var	markerList = {};
var markerCount = 0;

var latitude = 0, longitude = 0;

// these are set depending on the latitude. The values represent
// the number of statute miles per degree or latitude or longitude, respectively.
var latIncrement = 52.1739;
var longIncrement = 60.0000;

var distances = Array(10,20,30,50,75,100);
var searchRadius = 10;
var	searchLong = searchRadius * longIncrement, searchLat = searchRadius * latIncrement;
var city, state;
var cityState = "Hillsboro, OR";

var map;
var	marker;
var pngAlpha;

function trim(inputString)
{
	if (typeof inputString != "string")
	{
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ")
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	return retValue;
 }

var letters="abcdefghijklmnopqrstuvwxyz -";
var LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ -";

function isAlpha(c)
{
	// is c a String or a character?
	if(c.length>1)
	{
		for(j=0;j<c.length;j++)
		{
			// call isAlpha recursively for each character
			alpha=isAlpha(c.substring(j,j+1));
			if(!alpha) return alpha;
			}
			return alpha;
		}
		else
		{
			// if c is alpha return true
			if(letters.indexOf(c)>=0 || LETTERS.indexOf(c)>=0) return true;
			return false;
		}
}

function set_results(theString)
{
	document.getElementById("results").innerHTML += theString;
}

function set_message(theString)
{
	document.getElementById("messages").innerHTML += theString + "<br>";
}

function deg2rad(deg)
{
	var conv_factor = (2.0 * Math.PI)/360.0;
	return(deg * conv_factor);
}

function compute(latvalue) // Compute lengths of degrees
{
	var lat, m1, m2, m3, m4, p1, p2, p3, latlen, longlen;
	var latmeters, latfeet, latsm, longmeters, longfeet, longsm;
	
	// Convert latitude to radians
	lat = deg2rad(latvalue);

	// Set up "Constants"
	m1 = 111132.92;		// latitude calculation term 1
	m2 = -559.82;		// latitude calculation term 2
	m3 = 1.175;			// latitude calculation term 3
	m4 = -0.0023;		// latitude calculation term 4
	p1 = 111412.84;		// longitude calculation term 1
	p2 = -93.5;			// longitude calculation term 2
	p3 = 0.118;			// longitude calculation term 3

	// Calculate the length of a degree of latitude and longitude in meters
	latlen = m1 + (m2 * Math.cos(2 * lat)) + (m3 * Math.cos(4 * lat)) + (m4 * Math.cos(6 * lat));
	longlen = (p1 * Math.cos(lat)) + (p2 * Math.cos(3 * lat)) + (p3 * Math.cos(5 * lat));
	
	// Place values in output fields
	latmeters = Math.round(latlen);
	latfeet = Math.round(latlen / 12 * 39.370079);
	latsm = latfeet / 5280;
	latnm = latsm / 1.15077945;
	longmeters = Math.round(longlen);
	longfeet = Math.round(longlen / 12 * 39.370079);
	longsm = longfeet / 5280;
	longnm = longsm / 1.15077945;
	latIncrement = latsm;
	longincrement = longsm;
}

// called to split the city and state off
//

function build_city_and_state()
{
	var	myArray;
	
	myArray = cityState.split(",");
	alert;
	if (myArray.length == 2)
	{
		city = trim(myArray[0]);
		state = trim(myArray[1]);
	}
	else
	{
		city = "1";
		state = "1";
	}
}

function replace_html(text)
{
	var	t2;
	
	t2 = text.replace('<','&lt;');
	t2 = t2.replace('>', '&gt;');
	return t2;
}

// called when we enter a new city. Get the name of the city
// and the nearby markers
function get_city_and_markers()
{
	var theURL;
	
	theURL = build_city_and_marker_request(city, state);

	var	myRequest;

	myRequest = GXmlHttp.create();
	myRequest.open("GET", theURL, true);
	myRequest.onreadystatechange = function()
	{
		if (myRequest.readyState == 4)
		{
			// parse the XML
			// move the map
			// get markers, if any
			
			var	xmlDoc;
			var	locations;
			var myCity, myState, remLong, remLat;
			
			xmlDoc = myRequest.responseXML;
			
			if (xmlDoc != null)
			{
				locations = xmlDoc.documentElement.getElementsByTagName("location");
				
				myCity = locations[0].getAttribute("city");
				if (myCity.charAt(0) == '0')
				{
					alert("I couldn't find the city you entered! Please make sure the city is spelled correctly, and try again.");
				}
				else
				{
					clearMap();
					myState = locations[0].getAttribute("state");
					
					remLong = parseFloat(locations[0].getAttribute("long"));
					remLat = parseFloat(locations[0].getAttribute("lat"));
	
	
					compute(remLat);
					myPoint = new GPoint(remLong, remLat);
					map.centerAndZoom(myPoint, 6);
//								set_message("Server: myCity=" + myCity + " myState=" + myState + " long=" + remLong + " lat=" + remLat);
				}
			}
		}
	};
	myRequest.send(null);
}

function clickMarker(markerID)
{
	var aMarker;
	
	aMarker = markerList[markerID];
	aMarker.openInfoWindowHtml(aMarker.info_url);
}

// called when the map is moved. We just get the nearby markers,
// using the current longitude and latitude
function get_markers()
{
	var theURL;
	var	myRequest;
	
	theURL = build_marker_request(latitude, longitude, searchLat, searchLong);

	myRequest = GXmlHttp.create();
	myRequest.open("GET", theURL, true);
	myRequest.onreadystatechange = function()
	{
		if (myRequest.readyState == 4)
		{
			// parse the XML
			// get markers, if any
			var	xmlDoc;
			var	courses;
			
			xmlDoc = myRequest.responseXML;
			if (xmlDoc != null)
			{
				var cName, cAddress, cCity, cState, cZip, cPhone, cRate, cLong, cLat, cURL, myMarker;
				var	aCourse, courseName, toolTip;
				var markersToAdd = [];
				
				courses = xmlDoc.documentElement.getElementsByTagName("course");
				
				for (var i = 0; i < courses.length; i++)
				{
					cID = courses[i].getAttribute("id");
					aCourse = markerList[cID];
					if (aCourse == null)
					{
						var c = courses[i];
						cName = c.getAttribute("name");
						cAddress = c.getAttribute("address");
						cCity = c.getAttribute("city");
						cState = c.getAttribute("state");
						cZip = c.getAttribute("zip");
						cPhone = c.getAttribute("phone");
						cRate = c.getAttribute("rate");
						cLong = c.getAttribute("long");
						cLat = c.getAttribute("lat");
						cURL = c.getAttribute("url");
						cNumHoles = c.getAttribute("num_holes");
						cRange = c.getAttribute("range");
						
						if (cRange == 'N') cRange = 'No';
						else cRange = 'Yes';
						
						if (cURL.length > 1)
						{
							newcURL = '<div class="course"> <a href="http://' + cURL + '" target="_new">'+ cName + '</a></div>';
						}
						else
						{
							newcURL = '<div class="course">' + cName + '</div>';
						}
						newcURL += '<div class="info">Holes: ' + cNumHoles + ', Range: ' + cRange + '</div>';
						newcURL += '<div class="info">Phone: ' + cPhone + '</div>';
						cURL = newcURL;
						
						markerCount++;
						toolTip = cName + "<br>" + "Holes: " + cNumHoles + ", Range: " + cRange + "<br>Phone: " + cPhone;
						myMarker = make_new_gx_marker(parseFloat(cLong), parseFloat(cLat), cURL, toolTip, markerCount);
						markerList[cID] = myMarker;
						
						markersToAdd.push(myMarker);

						courseName = '<div class="cName"><a href="javascript:void(0)" onclick="clickMarker(' + cID + ')">' + cName + '</a></div>';
						if (pngAlpha)
						{
							set_results('<div class="aResult">');
							set_results('<span class="image" onclick="clickMarker(' + cID + ')" style="DISPLAY: inline-block; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'icons/icon' + markerCount + '.png\', sizingMethod=\'scale\');width: 24px; height: 38px"></span>');
							set_results(courseName);
						}
						else
						{
							set_results('<div class="aResult"><span class="image"><img src=\"icons/icon' + markerCount + '.png\" onclick="clickMarker(' + cID + ')" /></span>' + courseName);
						}
						set_results('<div class="cAddr">' + cAddress + '</div>');
						set_results('<div class="cState">' + cCity + ', ' + cState + ' ' + cZip + '</div>');
						set_results('<div class="cInfo">Holes: ' + cNumHoles + ', Range: ' + cRange + '</div>');
						set_results('<div class="cInfo">' +  cPhone + '</div>');
						set_results('</div>');
					}
				}
				map.addOverlays(markersToAdd);
			}
		}
	};
	myRequest.send(null);
}

function convert_to_seconds(myLocation)
{
	var	isNegative;
	var	degrees, minutes, minutes2, seconds, total;
	
	myLocation = parseFloat(myLocation);
	isNegative = (myLocation < 0);
	if (isNegative)
	{
		myLocation *= -1;
	}
	degrees = Math.floor(myLocation);
	minutes = myLocation - degrees;
	degrees *= 3600;
	minutes *= 60;
	minutes2 = Math.floor(minutes);
	seconds = minutes - minutes2;
	minutes2 *= 60;
	seconds *= 60;
	total = degrees + minutes2 + seconds;
	
	total = Math.floor(total);
	
	if (isNegative)
	{
		total *= -1;
	}
	return total;
}


// NOTE2: do we really want to do this?
// maybe we shouldn't do dynamic map loading
// until we know everything works?


// this is called when the map stops moving.
// at this point we get our search radius
// query our database given our current location
// and add a bunch of markers (if necessary)
// we need to track the markers so we don't add them again

function map_move_ended()
{
	var center, zoomLevel, latLngStr;
	var	isMarker;

	center = map.getCenterLatLng();
	zoomLevel = map.getZoomLevel();
	if (!latitude)
	{
		clear_results();
	}

	latitude = center.y;
	longitude = center.x;
	
	compute(latitude);
	latitude = convert_to_seconds(latitude);
	longitude = convert_to_seconds(longitude);
	
	// at this point, we should run a query and get some marker data?
	// it's asynchronous, so we have another function do the hard work
	
	get_markers();
}

function make_new_marker(longitude, latitude, icon_to_user, info_url)
{
	var	myPoint;
	var	myMarker;
	
	myPoint = new GPoint(longitude, latitude);
	if (icon_to_user != null)
	{
		myMarker = new GMarker(myPoint, icon_to_user);
	}
	else
	{
		myMarker = new GMarker(myPoint);
	}
	
	//info_url = "<iframe src=\"" + info_url + "\" />";
	
	// anonymous function is here
	GEvent.addListener(myMarker, "click", function() {myMarker.openInfoWindowHtml(info_url)});
	
	return myMarker;
}

var protoIcon = null;

function make_new_gx_marker(longitude, latitude, info_url, toolTip, index)
{
	var	myPoint;
	var	myMarker;

	if (protoIcon == null)
	{
		var aMarker = new GMarker();

		protoIcon = aMarker.icon;
		protoIcon.iconSize = new GSize(24,38);
		protoIcon.shadow ="icons/shadow50.png";
	}
	myIcon = new GIcon(protoIcon);
	if (index > 99)
	{
		index = '00';
	}
	myIcon.image = "icons/icon" + index + ".png";
	myPoint = new GPoint(longitude, latitude);
	myMarker = new GxMarker(myPoint, myIcon);
	
	if ((typeof toolTip != "undefined") && (toolTip.length > 0))
	{
		myMarker.setTooltip(toolTip);
	}
	
	//info_url = "<iframe src=\"" + info_url + "\" />";
	
	// anonymous function is here
	myMarker.info_url = info_url;
	GEvent.addListener(myMarker, "click", function() {myMarker.openInfoWindowHtml(info_url)});
	
	return myMarker;
}

function select_radius(list)
{
	searchRadius =distances[list.selectedIndex];
	searchLong = searchRadius * longIncrement;
	searchLat = searchRadius * latIncrement;
}

function clear_results()
{
	document.getElementById("results").innerHTML = "&nbsp;<BR>";
}

function clearMap()
{
	markerList = {}
	markerCount = 0;
	map.clearOverlays();
	clear_results();
}

// called when the submit button is whacked.
function set_location_function(myForm)
{
	cityState = document.forms["set_location_form"].elements["location_to_find"].value;
	if (cityState.length > 1)
	{
		select_radius(document.forms["set_location_form"].elements["search_radius"]);
		build_city_and_state();
		if ((state.length > 2) || (state.length < 1))
		{
			alert("The state must be in two-letter form: CA, not California.");
		}
		else
		{
			if (isAlpha(city) && isAlpha(state))
			{
				get_city_and_markers();
			}
			else
			{
				alert("The city and state must be letters and separated with a comma! No numbers or other characters allowed. Example: Seattle, WA, not Seattle, Washington");
			}
		}
	}
	else
	{
		alert("Please enter a city and state, in the form 'City, State'. 'Seattle, WA',  not 'Seattle, Washington'.");
	}
	return false;
}

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase(); 

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1); 
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1); 
   this.isIcab        = (ua.indexOf('icab') != -1); 
   this.isAol         = (ua.indexOf('aol') != -1); 
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirebird    = (ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   
   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);
   
   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );
   
   // browser version
   this.versionMinor = parseFloat(navigator.appVersion); 
   
   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }
   
   this.versionMajor = parseInt(this.versionMinor); 
   
   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);
   
   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);
   
   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);
   
   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);
   
   this.isIE4xMac = (this.isIE4x && this.isMac);
}

function load_func()
{
	var browser = new BrowserDetect();

	// if IE5.5+ on Win32, then display PNGs with AlphaImageLoader
	if ((browser.isIE55 || browser.isIE6up) && browser.isWin32)
	{
			pngAlpha = true;
	// else, if the browser can display PNGs normally, then do that
	}
	map = new GMap(document.getElementById("map"));
	
	GMap.prototype.addOverlays =function(a)
	{
		var b=this;
		
		for (i=0; i<a.length; i++)
		{
			try
			{
				this.overlays.push(a[i]);
				a[i].initialize(this);
				a[i].redraw(true);
			}
			catch(ex)
			{
				alert('err: ' + i + ', ' + ex.toString());
			}
		}
		this.reOrderOverlays();
	}

	map.addControl(new GLargeMapControl());
	
	map.centerAndZoom(new GPoint(-122.9055, 45.52687), 6);
	
	// map_move_ended();
	
	GEvent.addListener(map, "moveend", map_move_ended);
	marker = make_new_marker(-122.9055, 45.52687, null, 'http://www.veloso.org/');
	
	// weird bug if there isn't a marker. So add and remove one.
	map.addOverlay(marker);
	map.clearOverlays();

	document.forms.set_location_form.location_to_find.focus();
}

