var clickedPixel;
var isThereAMarker = false;
var markers = new Array();
var color = "#d23333";
var options = {};
options.maxVertices = 2;


var icon = new GIcon();
icon.image = "http://maps.google.com/mapfiles/kml/pal3/icon59.png";
icon.size = new GSize(16,16);
icon.iconAnchor = new GPoint(8,9);
icon.infoWindowAnchor = new GPoint(7,7);

var opt;
opt = { };
opt.icon = icon;
opt.draggable = false;
opt.clickable = true;
opt.dragCrossMove = true;

function loadMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(parseFloat(45.425), parseFloat(-122.79)), 13);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.clearOverlays();

		
		/* 
		GEvent.addListener(map, "dblclick", function (a, point) {
			if (!isThereAMarker) {

				var marker = new GMarker(point);
				map.addOverlay(marker);
				marker.enableDragging();
				marker.openInfoWindowHtml("<strong>Great!</strong> Next, tell me about this location in the comments box below.<br /><br /><em>(Didn't mean to put this marker on the map? No problem, <a href='javascript:undo();'>just undo.</a>)</em>");
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml("<strong>Great!</strong> Next, tell me about this location in the comments box below.<br /><br /><em>(Didn't mean to put this marker on the map? No problem, <a href='javascript:undo();'>just undo.</a>)</em>");
				});
				
				markers[0] = marker;
				updateFields();
			}
		});
		*/
		
		addMarkers();
		addRoute();
	}
}
function updateFields() {
	isThereAMarker = true;
	document.getElementById("ctl00_ContentPlaceHolder1_problem").disabled = false;
	document.getElementById("ctl00_ContentPlaceHolder1_name").disabled = false;
	document.getElementById("ctl00_ContentPlaceHolder1_email_address").disabled = false;
	document.getElementById("ctl00_ContentPlaceHolder1_phone").disabled = false;
	document.getElementById("ctl00_ContentPlaceHolder1_problem").innerHTML = "";
	document.getElementById("ctl00_ContentPlaceHolder1_problem").focus();
	document.getElementById("ctl00_ContentPlaceHolder1_problem").style.border = "2px solid yellow";
	
	var line = markers[0];
	document.getElementById("ctl00_ContentPlaceHolder1_lat").value = line.getVertex(0).lat();
	document.getElementById("ctl00_ContentPlaceHolder1_lng").value = line.getVertex(0).lng();
	
	document.getElementById("ctl00_ContentPlaceHolder1_lat2").value = line.getVertex(1).lat();
	document.getElementById("ctl00_ContentPlaceHolder1_lng2").value = line.getVertex(1).lng();
}
function addRoute() {
	var studyArea = new GPolyline([new GLatLng(45.3962929592532, -122.7492078734882 ), new GLatLng(45.39635742961763, -122.7472458488375 ), new GLatLng(45.40198682203058, -122.7448979192778 ), new GLatLng(45.40638672676666, -122.7441994350646 ), new GLatLng(45.41486697983436, -122.7440021134777 ), new GLatLng(45.41937634256682, -122.7440934024843 ), new GLatLng(45.42506809854123, -122.7441883760379 ), new GLatLng(45.43234402806442, -122.7441247588774 ), new GLatLng(45.43713177862627, -122.7442173310611 ), new GLatLng(45.44289978924982, -122.7437497299681 ), new GLatLng(45.44460224577302, -122.7437479239344 ), new GLatLng(45.44471181336671, -122.7453331038044 ), new GLatLng(45.445775934847, -122.7447723347817 ), new GLatLng(45.44705714747919, -122.744740534604 ), new GLatLng(45.44697508596676, -122.7435853728723 ), new GLatLng(45.45493774541199, -122.7435953792776 ), new GLatLng(45.45484895748704, -122.7488822722896 ), new GLatLng(45.45388255114138, -122.7499885983972 ), new GLatLng(45.45367097745425, -122.7511397144537 ), new GLatLng(45.45448440914741, -122.7532050544994 ), new GLatLng(45.45487875542636, -122.7549430385538 ), new GLatLng(45.45500361953312, -122.7673004042638 ), new GLatLng(45.4549843993598, -122.7708419988697 ), new GLatLng(45.45329980524362, -122.7740192652471 ), new GLatLng(45.45481635930172, -122.7788101815874 ), new GLatLng(45.45589698097022, -122.7801940788852 ), new GLatLng(45.45688005216779, -122.7824343381192 ), new GLatLng(45.45399120077193, -122.7838621052542 ), new GLatLng(45.45230764989123, -122.7838610435524 ), new GLatLng(45.44995898682885, -122.7857780486405 ), new GLatLng(45.44848884193909, -122.7874524314308 ), new GLatLng(45.44591351597843, -122.7933111867378 ), new GLatLng(45.44419680712414, -122.8003322069981 ), new GLatLng(45.44374515868676, -122.8053801920741 ), new GLatLng(45.44296752739647, -122.8103753491781 ), new GLatLng(45.44107506906573, -122.8168424999566 ), new GLatLng(45.43913409000705, -122.818296203803 ), new GLatLng(45.430961290161, -122.8257694704764 ), new GLatLng(45.42821103726322, -122.8331248410862 ), new GLatLng(45.42686780772341, -122.834497302634 ), new GLatLng(45.42639052174301, -122.836316976964 ), new GLatLng(45.42665784874163, -122.8464864129049 ), new GLatLng(45.41128749738947, -122.8466417159107 ), new GLatLng(45.41131273976523, -122.831258799434 ), new GLatLng(45.40171312430985, -122.8311208800859 ), new GLatLng(45.40218711967355, -122.8264010837565 ), new GLatLng(45.40294553481015, -122.8226805672291 ), new GLatLng(45.40347498748827, -122.8183719202869 ), new GLatLng(45.40438032095833, -122.8101260669148 ), new GLatLng(45.40633162619569, -122.8068948464102 ), new GLatLng(45.40769200410851, -122.8047817211358 ), new GLatLng(45.41133621750048, -122.796071248668 ), new GLatLng(45.41127617380089, -122.7943237635258 ), new GLatLng(45.41004941380318, -122.7946959367488 ), new GLatLng(45.40411289105555, -122.7968123709467 ), new GLatLng(45.40153472052892, -122.798451594628 ), new GLatLng(45.39394025458492, -122.7990414714082 ), new GLatLng(45.39683126917407, -122.7927510959039 ), new GLatLng(45.39835959277922, -122.7856394724689 ), new GLatLng(45.39862179319226, -122.7800220614982 ), new GLatLng(45.39725346559723, -122.7753024497802 ), new GLatLng(45.39467616782791, -122.7742534489195 ), new GLatLng(45.39331142337254, -122.7740326746936 ), new GLatLng(45.393110431302, -122.7721754345161 ), new GLatLng(45.39483423049705, -122.7695366203319 ), new GLatLng(45.39499057884412, -122.7681861438686 ), new GLatLng(45.39341229100678, -122.7682603041648 ), new GLatLng(45.39267899980305, -122.7669928894831 ), new GLatLng(45.39262434476095, -122.764443670401 ), new GLatLng(45.39640099867836, -122.764336984633 ), new GLatLng(45.39839827317575, -122.7627645876085 ), new GLatLng(45.40060515353866, -122.7584212946601 ), new GLatLng(45.40275651284009, -122.7538550169044 ), new GLatLng(45.40191270410796, -122.752287287302 ), new GLatLng(45.40065401761012, -122.754006671521 ), new GLatLng(45.40070623553058, -122.7528852656889 ), new GLatLng(45.39913118214916, -122.7531865533482 ), new GLatLng(45.39897365278145, -122.7523645296824 ), new GLatLng(45.39808219477249, -122.7525914683244 ), new GLatLng(45.39761096016601, -122.7537867380478 ), new GLatLng(45.39614422164535, -122.7540862977853 ), new GLatLng(45.3962929592532, -122.7492078734882 )], "#c50014", 5, .7);
map.addOverlay(studyArea);	
GEvent.addListener(studyArea, "click", function(point) {
		map.openInfoWindowHtml(point, "This is the study area boundary for the Tigard Neighborhood Trails Plan.<br /><br />Please place all comments inside of this boundary.");
});
}
function undo() {
	if (markers[0]) { map.removeOverlay(markers[0]); }
	map.closeInfoWindow();
	isThereAMarker = false;
	document.getElementById("ctl00_ContentPlaceHolder1_problem").disabled = true;
	document.getElementById("ctl00_ContentPlaceHolder1_name").disabled = true;
	document.getElementById("ctl00_ContentPlaceHolder1_email_address").disabled = true;
	document.getElementById("ctl00_ContentPlaceHolder1_problem").innerHTML = "Please select a location on the map";
	document.getElementById("ctl00_ContentPlaceHolder1_name").value = "Your name";
	document.getElementById("ctl00_ContentPlaceHolder1_email_address").value = "Your email address";
	document.getElementById("ctl00_ContentPlaceHolder1_problem").style.border = "1px solid #ccc";
	
	document.getElementById("ctl00_ContentPlaceHolder1_lat").value = "";
	document.getElementById("ctl00_ContentPlaceHolder1_lng").value = "";
	document.getElementById("ctl00_ContentPlaceHolder1_lat2").value = "";
	document.getElementById("ctl00_ContentPlaceHolder1_lng2").value = "";
	document.getElementById("ctl00_ContentPlaceHolder1_phone").disabled = true;
	document.getElementById("ctl00_ContentPlaceHolder1_phone").value = "Your phone number";
}
function popup() {
	if (document.getElementById("ctl00_ContentPlaceHolder1_lng").value != "") {
		
		//create elements
		var backgroundDiv = document.createElement("div");
		var messageDiv = document.createElement("div");
	
		//add css ids
		backgroundDiv.setAttribute("id", "backgroundpopup");
		messageDiv.setAttribute("id", "messagepopup");

		//change background color
		backgroundDiv.style.backgroundColor = "#000000";
	
		//append elements
		document.body.appendChild(backgroundDiv);
		document.body.appendChild(messageDiv);
	
		//make visible, resize to windoww
		messageDiv.style.display = "none";
		backgroundDiv.style.display = "block";
		backgroundDiv.style.height = document.body.clientHeight + 500 + "px";
		backgroundDiv.style.width = document.body.clientWidth + "px";
	
		//add message
		messageDiv.innerHTML = "<h1>Thank you for your feedback.</h1><p>We are continually trying to improve the city's infrastructure and livability, and you have helped further our goal. Thank you for taking the time to submit your feedback.</p><p><em><a href='javascript:popdown();'>Submit more? &raquo;</a></em></p>";
	
		//make message visible
		Effect.SlideDown(document.getElementById("messagepopup"));
	
		window.scrollTo(0,0);
	}
}
function startLine() {
  var line = new GPolyline([], color);
  startDrawing(line, "Line", function() {
    var cell = this;
    var len = line.getLength();
    cell.innerHTML = (Math.round(len / 10) / 100) + "km";
  }, color);
}
function startDrawing(poly, name, onUpdate, color) {
  map.addOverlay(poly);
  poly.enableDrawing(options);
  poly.enableEditing({onEvent: "mouseover"});
  poly.disableEditing({onEvent: "mouseout"});
  markers[0] = poly;
  GEvent.addListener(poly, "endline", function(point) {
	map.openInfoWindowHtml(point, "<strong>Great!</strong> Next, tell me about this location in the comments box below. To adjust your line, hover over the line and drag the white squares that appear at either end.<br /><br /><em>(Didn't mean to put this marker on the map? No problem, <a href='javascript:undo();'>just undo.</a>)</em>");
	updateFields();
	isThereAMarker = true;
  });
  GEvent.addListener(poly, "click", function(point) {
	map.openInfoWindowHtml(point, "<strong>Great!</strong> Next, tell me about this location in the comments box below. To adjust your line, hover over the line and drag the white squares that appear at either end.<br /><br /><em>(Didn't mean to put this marker on the map? No problem, <a href='javascript:undo();'>just undo.</a>)</em>");
  });
}
function popdown() {
	Effect.SlideUp(document.getElementById("messagepopup"));
	Effect.Fade(document.getElementById("backgroundpopup"));
	
	setTimeout("$('backgroundpopup').remove();", 1500);
	setTimeout("$('messagepopup').remove();", 1500);
	
	setTimeout("location.href='Default.aspx'", 1750);
}
function isDefault(elem, text) {
	if (elem.value == text) {
		elem.value = "";
	} else if (elem.value == "") {
		elem.value = text;
	}
}