var mid_ = ""; // we capture city info for when we filter by activity/landmark
var rid_ = ""; // we capture city info for when we filter by activity/landmark  
var global_name = "";
var global_value = ""; 
    
// We also caputre user info the first time it's entered so that the user doesn't have to re-enter it for
// every activity he/she wants to join.
var client_name = "";
var client_email = "";
var client_phone = "";

/* Global vars for mouse click */
var global_pageX;
var global_pageY;

var baseIcon = new GIcon();
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.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);
	
var map;
var foundNameVar;

var is;




function collapse() {
	cities = document.getElementById('locationsarea');
	middle = document.getElementById('maparea');
	activities = document.getElementById('classarea');
	cities.style.visibility = "hidden";
	activities.style.visibility = "visible";
}

function expand() {
	cities = document.getElementById('locationsarea');
	middle = document.getElementById('maparea');
	activities = document.getElementById('classarea');
	cities.style.visibility = "visible";
	activities.style.visibility = "hidden";
}


function sizeMap() {
     
	var height = 0;
	var width = 0;
	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		height = window.innerHeight;
		width = window.innerWidth;
	} else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
		width = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		height = document.body.clientHeight;
		width = document.body.clientWidth;		
	}

	//
	var minHeight = 0;
	minHeight += document.getElementById("title").offsetHeight;
	minHeight += document.getElementById("titlebar").offsetHeight;
	minHeight += Math.max(document.getElementById("classarea").offsetHeight,document.getElementById("locationsarea").offsetHeight);

	var footer_id = document.getElementById("footer");
	var footerheight;

	footerheight = height-footer_id.offsetHeight-3;
	

	if (footerheight > minHeight) {
		height = height-footer_id.offsetHeight;
	}
	else if (footerheight > minHeight-footer_id.offsetHeight) {
		height = minHeight+3;
		footerheight = minHeight;		
	}
	else {
		footerheight = minHeight;
	}

	height = height - 100;  /* just to be on the safe side */			

	footer_id.style.top = footerheight + "px";

	mapid = document.getElementById("map");
	mapid.style.height = height + "px"; 

}



      

	function UpdateMap(name, value)
	{
	
		global_name = name;
		global_value = value; 
	
		foundNumberVar.style.color = "#330066";
		foundNumberVar.innerHTML = "Searching...";
		
		if(name == "rid") // display cities for region
		{
			map.clearOverlays();
			DisplayCities(value);
		}
		else if (name == "mid") // zoom into a city and display info
		{
			cities = document.getElementById('locationsarea');
			activities = document.getElementById('classarea');

			cities.style.visibility = "hidden";
			activities.style.visibility = "visible";		
		
			map.clearOverlays();
			DisplayCity(value);
			map.setMapType(G_NORMAL_MAP);
		}
		else if (name == "asid" || name == "atid")
		{
		
		
			if(name == "asid" && value == "4" && rid_ == "" & mid_ == "") // We only show all conferences at world view
			{
				map.clearOverlays();
				DisplayActivities("-2", "-2", value);

			}
			else
			{
				if (rid_ == "" & mid_ == "") // we're at world view
				{
					foundNumberVar.style.color = "#FF0000";
					foundNumberVar.innerHTML= "Please select a city first";
					openEntryLayer("Please select a city first");
					//UpdateMap("rid", ""); // we dont have to do anything
				}
				else if (rid_ != "")
				{
					foundNumberVar.style.color = "#FF0000";			
					foundNumberVar.innerHTML= "Please select a city first";
					openEntryLayer("Please select a city first");
					//UpdateMap("rid", rid_ ); we don't have to do anything 
				}
				else if (name == "asid") // show super types of activities
				{
					map.clearOverlays();
					DisplayActivities(mid_, "", value);
				}
				else if (name == "atid") // show types of activities
				{
					map.clearOverlays();
					DisplayActivities(mid_, value, "");
				
				}
				else // landmarks - should never happen
				{
					map.clearOverlays();
					DisplayLandmarks(mid_, value, "0");			
				}
			}
			
	
		}		
		else // name == "ltid" or conference
		{
		
		
			if (rid_ == "" & mid_ == "") // we're at world view
			{	
				map.clearOverlays();
				DisplayLandmarks("0", value, "0");	
			}
			else if (rid_ != "")
			{
		
				map.clearOverlays();
				DisplayLandmarks("0", value, rid_);	
			}
			else
			{
		
				map.clearOverlays();
				DisplayLandmarks(mid_, value, "0");	
			}		
		}


	}

	function delay(gap)
	{ 
		var then,now; then=new Date().getTime();
		now=then;
		while((now-then)<gap)
		{
			now=new Date().getTime();
		}
	}
	
	
	function ZoomToCity(mid)
	{
 		var path = "/dev/getmarkers.aspx?city=" + mid + GetRand();
		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
	        	if (request.readyState == 4) {
		          	var xmlDoc = request.responseXML;
				var cities = xmlDoc.documentElement.getElementsByTagName("city");
				
	          		for (var i = 0; i < cities.length; i++)
		          	{
		          		//map.centerAndZoom(new GPoint(parseFloat(new String((cities[i].getAttribute("mlon")))), parseFloat(new String((cities[i].getAttribute("mlat"))))), parseFloat(new String((cities[i].getAttribute("mzoom")))));
						map.setCenter(new GLatLng(parseFloat(new String((cities[i].getAttribute("mlat")))), parseFloat(new String((cities[i].getAttribute("mlon"))))), 17-parseFloat(new String((cities[i].getAttribute("mzoom")))));
						//map.zoomTo(parseFloat(new String((cities[i].getAttribute("mzoom")))));
						map.setZoom(17-parseFloat(new String((cities[i].getAttribute("mzoom")))));
						var cityname_id = document.getElementById("cityname");
						cityname_id.innerHTML = cities[i].getAttribute("met") + " - ";					
					
	          		}
			}
		}
		request.send(null);
	}
		
	function DisplayCity(mid)
	{
		mid_ = mid;
		rid_ = "";
		ZoomToCity(mid);
		DisplayLandmarks(mid, "", "0");		    
		DisplayActivities(mid, "", "");
	}

	function getCityName(mid) {
		var path = "/dev/getmarkers.aspx?city=" + mid + GetRand();
		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
	        	if (request.readyState == 4) {
		          	var xmlDoc = request.responseXML;
				var cities = xmlDoc.documentElement.getElementsByTagName("city");
				
	          		for (var i = 0; i < cities.length; i++)
		          	{
					var cityname_id = document.getElementById("cityname");
					cityname_id.innerHTML = cities[i].getAttribute("met") + " - ";					
	          		}
			}
		}
		request.send(null);		



	}


	function DisplayLandmarks(mid, ltid, rid)
	{
		//ZoomToCity(mid);

 		var path = "/dev/getmarkers.aspx?ltid=" + ltid + GetRand();
 		
 		if (mid == 0 && rid == 0)
 		{
 		
 		}
 		else if (mid == 0)
 		{
 			path += "&rid=" + rid;
 		}
 		else
 		{
 			path += "&mid=" + mid;
 		}
 		
		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
        	if (request.readyState == 4) {
			collapse();
	          	var xmlDoc = request.responseXML;
			var landmarks = xmlDoc.documentElement.getElementsByTagName("landmark");
				
				
	          	for (var i = 0; i < landmarks .length; i++)
	          	{
	          		var lat = new String((landmarks [i].getAttribute("lat")));
	          		var lon = new String((landmarks [i].getAttribute("lon")));
					var marker = GetMarker2(lat,lon, "l" + new String((landmarks [i].getAttribute("ltid"))), "", GetLandmarkHtml2(landmarks[i]));
					map.addOverlay(marker);
	          	
	          	}
	          	count = i;
			foundNumberVar.style.color = "#330066";
	          	if (foundNumberVar.innerHTML.indexOf("activit") > 0) {
	          		foundNumberVar.innerHTML += GetPhrase(count, "landmark") + " found. ";
	          	}
	          	else {
	          		foundNumberVar.innerHTML = GetPhrase(count, "landmark") + " found. ";
	          	}
	
	          	//foundNumberVar.innerHTML = foundNumberVar.innerHTML;
			}
		}
		request.send(null);
	
	
	
	}
	
	
	
	
	function DisplaySingleLandmark(lid)
	{
		//ZoomToCity(mid);

 		var path = "/dev/getsinglemarker.aspx?lid=" + lid + GetRand();
 		
 		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
        	if (request.readyState == 4) {
			collapse();
	          	var xmlDoc = request.responseXML;
			var landmarks = xmlDoc.documentElement.getElementsByTagName("landmark");
				
				
	          	for (var i = 0; i < landmarks .length; i++)
	          	{
	          		var lat = new String((landmarks [i].getAttribute("lat")));
	          		var lon = new String((landmarks [i].getAttribute("lon")));
					var marker = GetMarker2(lat,lon, "l" + new String((landmarks [i].getAttribute("ltid"))), "", GetLandmarkHtml2(landmarks[i]));
					map.addOverlay(marker);
					//map.centerAndZoom(new GPoint(lon, lat), 7);
					map.setCenter(new GLatLng(lat, lon), 17-7);
					marker.openInfoWindow(GetLandmarkHtml2(landmarks[i]));
					var cityname_id = document.getElementById("cityname");
					cityname_id.innerHTML = landmarks[i].getAttribute("cityname") + " - ";					


	          	
	          	}
	          	count = i;
			foundNumberVar.style.color = "#330066";
	          	if (foundNumberVar.innerHTML.indexOf("activit") > 0) {
	          		foundNumberVar.innerHTML += GetPhrase(count, "landmark") + " found. ";
	          	}
	          	else {
	          		foundNumberVar.innerHTML = GetPhrase(count, "landmark") + " found. ";
	          	}
	
	          	//foundNumberVar.innerHTML = foundNumberVar.innerHTML;
			}
		}
		request.send(null);
	
	
	
	}
	

	
	function JoinActivity(aid)
	{
	

		document.getElementById('joinform').joinbutton.disabled = true;
		document.getElementById('joinform').joinbutton.value = "Sending request...";
		client_name = document.getElementById('joinform').name.value;
		client_email = document.getElementById('joinform').email.value;
		client_phone = document.getElementById('joinform').phone.value;
		var comments = document.getElementById('joinform').comments.value;
		
		var path = "/dev/mailer.aspx?aid=" + aid + "&name=" + client_name+ "&email=" + client_email+ "&phone=" + client_phone + "&comments=" + comments;;
 		
		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
      	
		request.onreadystatechange = function() {
        	if (request.readyState == 4) {
	          	var xmlDoc = request.responseXML;
				var status = xmlDoc.documentElement.getElementsByTagName("status");
				var code = "";
				var message = ""
				
	          	for (var i = 0; i < status.length; i++)
	          	{
	          		code = new String((status[i].getAttribute("code")));
	          		message = new String((status[i].getAttribute("message")));	          	
	          	}
	          	
	          	if (code == "0")
	          	{
	          		window.alert(message);
	          		document.getElementById('joinform').joinbutton.disabled = false;
					document.getElementById('joinform').joinbutton.value = "Join";	          		
	          	}
	          	else
	          	{
	          		window.alert("Success: " + message);	
	          		map.closeInfoWindow();
	          	}
         	
			}
		}
		request.send(null);
	}
	
	function Join(aid, lat, lon)
	{

		bubblediv = document.createElement("div");
		bubblediv.className = "bubble_box";
		bubblediv.style.width = "250px";
		bubblediv.id = "bubble_box_id";
		
		var html = "";
		
		html += " <form action=\"javascript:JoinActivity("+aid+")\" id=\"joinform\">";
		html += "   <div id=\"join_total\">";
		html += "     <div id=\"join_identifiers\">";
		html += "       <div class=\"join_identifiers_item\">Name</div>";
		html += "       <div class=\"join_identifiers_item\">Email</div>";
		html += "       <div class=\"join_identifiers_item\">Phone</div>";
		html += "       <div class=\"join_identifiers_item2\">Comments</div>";
		html += "       <div class=\"join_identifiers_item\">&nbsp;</div>";
		html += "     </div>";	

		html += "     <div id=\"join_enterfields\">";
		html += "       <span class=\"join_enterfields_item\"><input class=\"inputclass\" type=\"text\" name=\"name\" id=\"name\" size=\"24\" value=\""+client_name+"\"></span>";
		html += "       <span class=\"join_enterfields_item\"><input class=\"inputclass\" type=\"text\" name=\"email\" id=\"email\" size=\"24\" value=\""+client_email+"\"></span>";	
		html += "       <span class=\"join_enterfields_item\"><input class=\"inputclass\" type=\"text\" name=\"phone\" id=\"phone\" size=\"24\" value=\""+client_phone+"\"></span>";
		html += "       <span class=\"join_enterfields_item2\"><textarea rows=\"3\" class=\"inputclass\" name=\"comments\" id=\"comments\" cols=\"26\"></textarea></span>";
		html += "       <span class=\"join_enterfields_item\"><input class=\"inputclass\" type=\"submit\" id=\"joinbutton\" value=\"Join\" name=\"joinbutton\" /></span>";
		html += "     </div>";
		html += "   </div>";              
		html += " </form> ";
		
		bubblediv.innerHTML = html;
		
		map.closeInfoWindow();
		//map.openInfoWindow(new GPoint(lon, lat), bubblediv, new GSize(0,-32));  
		map.openInfoWindow(new GLatLng(lat, lon), bubblediv, {pixelOffset:new GSize(0,-32)});  
		
	
	}
	
	function GetActivityHtml(actFields)
	{
	
	
		//TODO: process activity info correctly and create HTML depending on what fields are present
	
		var height = actFields.getAttribute("ht");
		var width = actFields.getAttribute("wt");
		var image = actFields.getAttribute("imagelink");
		var name = actFields.getAttribute("activitytypename");
		var startdate= NormalizeDate(actFields.getAttribute("startdate"));
		var enddate= NormalizeDate(actFields.getAttribute("enddate"));
		var starttime= actFields.getAttribute("starttime");
		var endtime= actFields.getAttribute("endtime");
		var fid= actFields.getAttribute("fid");
		var link= actFields.getAttribute("link");
		var aid= actFields.getAttribute("aid");		
		var lat= actFields.getAttribute("lat");
		var lon= actFields.getAttribute("lon");
		var joinv= actFields.getAttribute("joinv");	
		var contactname= actFields.getAttribute("contactname");		
		var phone= actFields.getAttribute("phone");		
		var address= actFields.getAttribute("address");	
		var addressv= actFields.getAttribute("addressv");			
		var email= actFields.getAttribute("email");												
	
		var preS = "<span class=\"bubble_heading\">";
		var preC = "<span class=\"bubble_content\">";
	
	
		var ht = parseInt(height);
		var wt = parseInt(width);
		
		if (ht < 100)
			ht = 100;
		
		wt += 200;
		
		var html =  "";


 		html += "<div class=\"bubble_box\" style=\"height: " + ht + "px;";
 		html += "width: "+ wt +"px\">";
      	if (image != "") 
      		html += "<img class=\"icon\" src=\"images/"+image+"\" alt=\""+name+"\" />";
      	if (link == "")
      		html += "<span class=\"bubble_title\">"+name+"</span>";
      	else
      		html += "<span class=\"bubble_title\"><a target=\"_blank\" href=\""+link+"\">"+name+"</a></span>";

      	html += "&nbsp;<img onMouseOver=\"javascript:this.style.cursor='hand';\" onClick=\"javascript:Zoom('" +lat+"', '"+lon+"')\" border=\"0\" src=\"/images/zoom.jpg\"><p>";

      	if (startdate != "")
      		html += preS  + "Start date </span>" + preC + startdate + "</span><br/>";
       	if (enddate != "")
      		html += preS + "End date</span> " + preC + enddate + "</span><br/>";
   		if (fid != "")
      		html += preS + "Frequency</span> " + preC + fid + "</span><br/>";
      	if (starttime != "")
      		html += preS + "Start time</span> " + preC + starttime + "</span><br/>";
       	if (endtime != "")
      		html += preS + "End time</span> " + preC + endtime + "</span><br/>";
       	if (address != "")
      		html += preS + "Address</span> " + preC + address + "<a href=\"http://www.google.com\" target=\"_blank\">Directions</a></span><br/>";    
      	else
      	{
      		if (addressv == "False")
      			html += preS + "Address</span> " + preC + "Blurred" + "</span><br/>";    
		}
       	if (email != "")
      		html += preS + "Email</span> " + preC + email + "</span><br/>";      		
       	if (contactname != "")
      		html += preS + "Contact person</span> " + preC + contactname + "</span><br/>";
       	if (phone != "")
      		html += preS + "Phone</span> " + preC + phone + "</span><br/>";      		
      		

      	if (joinv == "True")
      		html += "<br><center><img border=\"0\" src=\"/images/join.jpg\"><span class=\"bubble_join\"><a href=\"javascript:Join('"+aid+"', '"+lat+"', '"+lon+"')\">Join!</a></span></center>";
     	html += "</div>";


		return html;
	}


	function GetActivityHtml2(actFields)
	{
	
	
		//TODO: process activity info correctly and create HTML depending on what fields are present
		var aid = actFields.getAttribute("aid");
		var height = actFields.getAttribute("ht");
		var width = actFields.getAttribute("wt");
		var image = actFields.getAttribute("imagelink");
		var name = actFields.getAttribute("activitytypename");
		var activityname = actFields.getAttribute("activityname");	
		var startdate= NormalizeDate(actFields.getAttribute("startdate"));
		var enddate= NormalizeDate(actFields.getAttribute("enddate"));
		var starttime= actFields.getAttribute("starttime");
		var endtime= actFields.getAttribute("endtime");
		var fid= actFields.getAttribute("fid");
		var link= actFields.getAttribute("link");
		var aid= actFields.getAttribute("aid");		
		var lat= actFields.getAttribute("lat");
		var lon= actFields.getAttribute("lon");
		var joinv= actFields.getAttribute("joinv");	
		var contactname= actFields.getAttribute("contactname");		
		var phone= actFields.getAttribute("phone");		
		var address= actFields.getAttribute("address");	
		var addressv= actFields.getAttribute("addressv");			
		var email= actFields.getAttribute("email");												
		var prereq= actFields.getAttribute("prereq");	
		var language= actFields.getAttribute("language");
			
		var preS = "<span class=\"bubble_heading\">";
		var preC = "<span class=\"bubble_content\">";
	
	
		var ht = parseInt(height);
		var wt = parseInt(width);
		
		if (ht < 100)
			ht = 100;
		
		wt += 200;

		bubblediv = document.createElement("div");
		bubblediv.className = "bubble_box";
		bubblediv.style.width = wt + "px";
		bubblediv.id = "bubble_box_id";

		
		var html =  "<table><tr>";


 		if (image != "") { 
	      		html += "<td valign=\"top\" width=\""+parseInt(width)+"\"><img width=\""+parseInt(width)+"\" height=\""+parseInt(height)+"\"  class=\"icon3\" src=\"images/"+image+"\" alt=\""+name+"\" /></td>";
	      	}
	      	html += "<td valign=\"top\">";

		if (name == "Conference")
			name = activityname;	    
  	
		if (link == "") {
	      		html += "<span class=\"bubble_title\">"+name+"</span>";
	      	}
		else {
	      		html += "<span class=\"bubble_title\"><a target=\"_blank\" href=\""+link+"\">"+name+"</a></span>";
	      	}

		html += "&nbsp;<img onMouseOver=\"javascript:this.style.cursor='hand';\" onClick=\"javascript:Zoom('" +lat+"', '"+lon+"')\" border=\"0\" src=\"/images/zoom.jpg\"><p>";

		if (startdate != "") {
	      		html += preS  + "Start date </span>" + preC + startdate + "</span><br/>";
	      	}
		if (enddate != "") {
	      		html += preS + "End date</span> " + preC + enddate + "</span><br/>";
	      	}
   		if (fid != "") {
	      		html += preS + "Frequency</span> " + preC + fid + "</span><br/>";
	      	}
		if (starttime != "") {
	      		html += preS + "Start time</span> " + preC + starttime + "</span><br/>";
	      	}
		if (endtime != "") {
	      		html += preS + "End time</span> " + preC + endtime + "</span><br/>";
	      	}
		if (address != "") {
	      		html += preS + "Address</span> " + preC + address + " </span><br><a href=\"http://maps.google.com?q="+address+"\" target=\"_blank\">Directions</a><br/>";    
	      	}
		else {
      			if (addressv == "False") {
  				html += preS + "Address</span> " + preC + "Blurred" + "</span><br/>";    
  			}
		}
		if (email != "") {
	      		html += preS + "Email</span> " + preC + email + "</span><br/>";
	      	}
		if (contactname != "") {
	      		html += preS + "Contact person</span> " + preC + contactname + "</span><br/>";
	      	}
		if (phone != "") {
	      		html += preS + "Phone</span> " + preC + phone + "</span><br/>";      		
	      	}
		if (language != "") {
	      		html += preS + "Language</span> " + preC + language + "</span><br/>";      		
	      	}	      	
		if (prereq != "") {
	      		html += preS + "Notes</span> " + preC + prereq + "</span><br/>";      		
	      	}      		

		if (joinv == "True") {
	      		html += "<center><img border=\"0\" src=\"/images/join.jpg\"><span class=\"bubble_join\"><a href=\"javascript:Join('"+aid+"', '"+lat+"', '"+lon+"')\">Join!</a></span></center>";
	      	}

	    html += "<br><span class=\"bubble_join\"><a target=\"_blank\" href=\"/?t=a&v="+aid+"\">Direct link to this activity</a></span>";
		html += "</td></tr></table>";

		bubblediv.innerHTML = html;

		return bubblediv;
	}

	function NormalizeDate(dt)
	{
	
		var dt_array=dt.split(" ");
		return dt_array[0];

	
	
	}

	function Zoom(lat, lon)
	{
		map.closeInfoWindow();
		//map.centerAndZoom(new GPoint(lon,lat), 0);
		map.setCenter(new GLatLng(lat,lon),17-0);
		map.setMapType(G_SATELLITE_MAP);
	
	}
	function GetLandmarkHtml(landFields)
	{
	

		var height = landFields.getAttribute("ht");
		var width = landFields.getAttribute("wt");
		var image = landFields.getAttribute("imagelink");	
		var name = landFields.getAttribute("name");	
		var address = landFields.getAttribute("address");
		var email = landFields.getAttribute("email");
		var phone = landFields.getAttribute("phone");		
		var link = landFields.getAttribute("link");		
		var lat= landFields.getAttribute("lat");
		var lon= landFields.getAttribute("lon");				

		var preS = "<span class=\"bubble_heading\">";
		var preC = "<span class=\"bubble_content\">";

		var ht = parseInt(height);
		var wt = parseInt(width);
		
		if (ht < 100)
			ht = 100;
		
		wt += 150;
	
		var html = "";


 		html += "<div class=\"bubble_box\" style=\"height: " + ht + "px;";
 		html += "width: "+ wt +"px\">";
      	if (image != "") 
      		html += "<img class=\"icon\" src=\"images/"+image+"\" alt=\""+name+"\" />";
      	if (link == "")
      		html += "<span class=\"bubble_title\">"+name+"</span>";
      	else
      		html += "<span class=\"bubble_title\"><a target=\"_blank\" href=\""+link+"\">"+name+"</a></span>";
      	
      	html += "&nbsp;<img onMouseOver=\"javascript:this.style.cursor='hand';\" onClick=\"javascript:Zoom('" +lat+"', '"+lon+"')\" border=\"0\" src=\"/images/zoom.jpg\"><p>";

      	if (address != "")
      		html += preS  + "Address </span><br>" + preC + address + "</span><br/>";
      	if (phone != "")
      		html += preS  + "Phone </span>" + preC + phone + "</span><br/>";
      	if (email != "")
      		html += preS  + "Email </span>" + preC + email + "</span><br/>";


       	html += "</div>";

		
	

		return html;
	}


	function GetLandmarkHtml2(landFields)
	{
	
		var lid = landFields.getAttribute("lid");

		var height = landFields.getAttribute("ht");
		var width = landFields.getAttribute("wt");
		var image = landFields.getAttribute("imagelink");	
		var name = landFields.getAttribute("name");	
		var address = landFields.getAttribute("address");
		var addressv = landFields.getAttribute("addressv");		
		var email = landFields.getAttribute("email");
		var phone = landFields.getAttribute("phone");		
		var link = landFields.getAttribute("link");		
		var lat= landFields.getAttribute("lat");
		var lon= landFields.getAttribute("lon");				

		var preS = "<span class=\"bubble_heading\">";
		var preC = "<span class=\"bubble_content\">";

		var ht = parseInt(height);
		var wt = parseInt(width);
		
		if (ht < 100) {
			ht = 100;
		}
		
		wt += 200;

		bubblediv = document.createElement("div");
		bubblediv.className = "bubble_box";
		bubblediv.style.width = wt + "px";
		bubblediv.id = "bubble_box_id";

	
		var html = "<table><tr>";


	      	if (image != "") {
	      		html += "<td valign=\"top\" width=\""+parseInt(width)+"\"><img width=\""+parseInt(width)+"\" height=\""+parseInt(height)+"\"class=\"icon3\" src=\"images/"+image+"\" alt=\""+name+"\" /></td>";
	      	}

		html += "<td valign=\"top\">";

		if (link == "") {
	      		html += "<span class=\"bubble_title\">"+name+"</span>";
	      	}
	      	else {
	      		html += "<span class=\"bubble_title\"><a target=\"_blank\" href=\""+link+"\">"+name+"</a></span>";
	      	}
      	
		html += "&nbsp;<img onMouseOver=\"javascript:this.style.cursor='hand';\" onClick=\"javascript:Zoom('" +lat+"', '"+lon+"')\" border=\"0\" src=\"/images/zoom.jpg\"><p>"; 

		if (address != "") {
	      		html += preS  + "Address </span><br>" + preC + address + "</span> <br><a href=\"http://maps.google.com?q="+address+"\" target=\"_blank\">Directions</a><br/>";
		}
		else {
      			if (addressv == "False") {
  				html += preS + "Address</span> " + preC + "Blurred" + "</span><br/>";    
  			}
		}
		if (phone != "") {
      			html += preS  + "Phone </span>" + preC + phone + "</span><br/>";
		}
		if (email != "") {
      			html += preS  + "Email </span>" + preC + email + "</span><br/>";
		}

	    html += "<br><span class=\"bubble_join\"><a target=\"_blank\" href=\"/?t=l&v="+lid+"\">Direct link to this landmark</a></span>";
		html += "</td></tr></table>";

		bubblediv.innerHTML = html;		
	
		return bubblediv;
	}




	
	function GetPhrase(count, name)
	{
		if (name == "landmark")
		{
			if (count != 1)
				return count + " landmarks";
			else
				return count + " landmark";
		}
		else if (name == "activity")
		{
			if (count != 1)
				return count + " activities";
			else
				return count + " activity";		
		}
		else if (name == "city")
		{
			if (count != 1)
				return count + " cities";
			else
				return count + " city";		
		}		
		else
		{
			return "";
		}
	
	}
	
	function DisplayActivities(mid, atid, asid)
	{

		//ZoomToCity(mid);
 		var path = "/dev/getmarkers.aspx?mid=" + mid;
 		
 		if (getFutureActivityCheckbox())
 			path += "&future=1";
 		
 		if (atid != "")
 			path += "&atid=" + atid;
 			
 		if (asid != "")
 			path += "&asid=" + asid;
 		
 		
 		if (mid == "-2" && atid == "-2")
 			path = "/dev/getmarkers.aspx?asid=" + asid;
 		
 		
 		path += GetRand();

		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
        	if (request.readyState == 4) {

			collapse();				
	          	var xmlDoc = request.responseXML;
			var activities = xmlDoc.documentElement.getElementsByTagName("activity");

	          	for (var i = 0; i < activities.length; i++)
	          	{
	          		var lat = new String((activities[i].getAttribute("lat")));
	          		var lon = new String((activities[i].getAttribute("lon")));
				var marker = GetMarker2(lat,lon,"a" + new String((activities[i].getAttribute("atid"))), "", GetActivityHtml2(activities[i]));
				map.addOverlay(marker);
	          	
	          	}
	          	count = i;

			foundNumberVar.style.color = "#330066";				
	          	if (foundNumberVar.innerHTML.indexOf("landmark") > 0) {
	          		foundNumberVar.innerHTML += GetPhrase(count, "activity") + " found. ";
	          	}
	          	else {
	          		foundNumberVar.innerHTML = GetPhrase(count, "activity") + " found. ";
	          	}
	          	

			}
		}
		request.send(null);
	
	
	
	}
    
 

	
	function DisplaySingleActivity(aid)
	{

		//ZoomToCity(mid);
		
 		var path = "/dev/getsinglemarker.aspx?aid=" + aid;
 	 		
 		path += GetRand();

		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
        	if (request.readyState == 4) {

			collapse();				
	          	var xmlDoc = request.responseXML;
			var activities = xmlDoc.documentElement.getElementsByTagName("activity");

	          	for (var i = 0; i < activities.length; i++)
	          	{
	          		var lat = new String((activities[i].getAttribute("lat")));
	          		var lon = new String((activities[i].getAttribute("lon")));
				var marker = GetMarker2(lat,lon,"a" + new String((activities[i].getAttribute("atid"))), "", GetActivityHtml2(activities[i]));
				map.addOverlay(marker);
				//map.centerAndZoom(new GPoint(lon, lat), 7);
				map.setCenter(new GLatLng(lat,lon), 17-7);
				marker.openInfoWindow(GetActivityHtml2(activities[i]));
				var cityname_id = document.getElementById("cityname");
				cityname_id.innerHTML =activities[i].getAttribute("name") + " - ";					
				
				
				

	          	
	          	}
	          	count = i;

			foundNumberVar.style.color = "#330066";				
	          	if (foundNumberVar.innerHTML.indexOf("landmark") > 0) {
	          		foundNumberVar.innerHTML += GetPhrase(count, "activity") + " found. ";
	          	}
	          	else {
	          		foundNumberVar.innerHTML = GetPhrase(count, "activity") + " found. ";
	          	}
	          	

			}
		}
		request.send(null);
	
	
	
	}
    

     
	function GetMarker(lat, lon, pin, id, html) 
	{
		var icon = new GIcon(baseIcon);
		icon.image = "images/markers/"+pin+".png";
		
		var point 	= new GPoint(parseFloat(lon), parseFloat(lat))
		var marker 	= new GMarker(point, icon);
		
		if (pin == "m")
		{
			GEvent.addListener(marker, "click", function() {UpdateMap('mid', id);}); // should we have bubble or not for cities?
  		
  		}
  		else
  		{
	  		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
  		}
		return marker;
		
	}

	function GetMarker2(lat, lon, pin, id, html) 
	{
		var icon = new GIcon(baseIcon);
		icon.image = "images/markers/"+pin+".png";
		
		var point 	= new GPoint(parseFloat(lon), parseFloat(lat))
		var marker 	= new GMarker(point, icon);
		
		if (pin == "m")
		{
			GEvent.addListener(marker, "click", function() {UpdateMap('mid', id);}); // should we have bubble or not for cities?
  		
  		}
  		else
  		{
	  		GEvent.addListener(marker, "click", function() {marker.openInfoWindow(html);});
  		}
		return marker;
		
	}	
function LoadMap() 
{

	expand();

	/* Allow mouse event handlers */
	is = new Is();
	var classarea_id = document.getElementById("classarea");
	if (classarea_id) {

		/* Set the event handler */
		classarea_id.onmousedown = getMouseCoords;
	}

	sizeMap();
	
	foundNumberVar = document.getElementById('foundNumber');
	foundNumberVar.innerHTML = "Searching...";
		

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	//map.centerAndZoom(new GPoint(global_lon, global_lat), global_zoom);
	map.setCenter(new GLatLng(global_lat,global_lon),17-global_zoom);

	if (displayCityQuery == "1" || displayCityQuery == "19") {
		UpdateMap("mid", displayCityQuery);
	}
	else if(type_of_loc != "" && value_of_loc != "")
	{
		if(type_of_loc == "l")
			DisplaySingleLandmark(value_of_loc);
		else
			DisplaySingleActivity(value_of_loc);
	
	}
	else {
		DisplayCities("");
	}
}

// to by-pass caching
function GetRand()
{
	return "&time=" + (new Date()).getTime();

}


	function DisplayCities(rid)  
	{  

		var cityname_id = document.getElementById("cityname");
		cityname_id.innerHTML = "World View - ";					

		
		rid_ = rid;
		mid_ = "";
 		var path = "/dev/getmarkers.aspx?rid=" + rid + GetRand();
		var request = GXmlHttp.create();
		var count = 0;
		request.open("GET", path, true);
 		request.onreadystatechange = function() {
        		if (request.readyState == 4) {
	          		var xmlDoc = request.responseXML;
				var regions = xmlDoc.documentElement.getElementsByTagName("region");
				var initial = false;
				if(rid != "") {
					initial = true;
				}
				else {// display all cities and center map over world
					//map.centerAndZoom(new GPoint(global_lon, global_lat), global_zoom);
					map.setCenter(new GLatLng(global_lat,global_lon),17-global_zoom);
				}
				
				
				var num_regions = 0;
				var original_region_name;
		          	for (var i = 0; i < regions.length; i++) {
					if(initial)
	          			{
						var temp_lon;
						var temp_lat;
						var temp_zoom;
	          				//map.centerAndZoom(new GPoint(parseFloat(new String((regions[i].getAttribute("rlon")))), parseFloat(new String((regions [i].getAttribute("rlat"))))), parseFloat(new String((regions [i].getAttribute("rzoom")))));
							map.setCenter(new GLatLng(parseFloat(new String((regions[i].getAttribute("rlat")))), parseFloat(new String((regions [i].getAttribute("rlon"))))), 17-parseFloat(new String((regions [i].getAttribute("rzoom")))));
	          				initial = false;
		          		}

					if (num_regions == 0) {
						original_region_name = regions[i].getAttribute("region");
						num_regions = 1;
					}
					else if (num_regions == 1) {
						if (original_region_name != regions[i].getAttribute("region")) {
							num_regions = 2;
						}

					}
					
		          		var lat = parseFloat(new String((regions[i].getAttribute("mlat"))));
					var lon = parseFloat(new String((regions[i].getAttribute("mlon"))));
	          			var mid = parseFloat(new String((regions[i].getAttribute("mid"))));
					var marker = GetMarker(lat,lon, "m", mid, "city");
					map.addOverlay(marker);
				}

				if (num_regions == 1) {
					cityname_id.innerHTML = regions[i-1].getAttribute("region") + " - ";
				}
	       		count = i;

				foundNumberVar.style.color = "#330066";
		          	foundNumberVar.innerHTML= "Found " + GetPhrase(count, "city") + ".";
	          	
			}
		}
		request.send(null);
		
		
	} 

function closeEntryLayer() {
	entryLayer = document.getElementById('entrylayer');
	entryLayerContent = document.getElementById('entrylayer_content');		   
	entryLayer.style.visibility = "hidden";
	entryLayer.style.backgroundColor = "#E3E4AD";
	entryLayerContent.innerHTML = "";
}	

function openEntryLayer(str) {
	entryLayer = document.getElementById('entrylayer');
	entryLayerContent = document.getElementById('entrylayer_content');

	/* Get Mouse Click Location */
	/* bubbleX = global_pageX-50;*/
	bubbleX = 10;
	bubbleY = global_pageY-10;

	/*First erase the old HTML in the layer */
	entryLayerContent.innerHTML = "";
	entryLayer.style.width = "200px";
	entryLayer.style.top = bubbleY + "px";
	entryLayer.style.left = bubbleX + "px";
	entryLayerContent.style.width = "185px";

	/* Make the new HTML for the entry layer */
	newHTML = "";

	newHTML += "<div style=\"color: #FF0000; \">";
	newHTML += "<p>";
	newHTML += str;
	newHTML += "</p>";
	newHTML += "</div>";

	/* Make the Entry layer Visible */
	entryLayerContent.innerHTML = newHTML;	
	entryLayer.style.visibility = "visible";


}

// Event Handler
function getMouseCoords(e) {

	if (is.ie) {

		global_pageX = window.event.clientX+document.documentElement.scrollLeft;
		global_pageY = window.event.clientY+document.documentElement.scrollTop;

	}
	else {
		global_pageX = e.pageX;
		global_pageY = e.pageY;

	}
}

// CheckBoxCheck
function getFutureActivityCheckbox() {
	var myForm = document.getElementById("optionsForm");
	if (myForm) {
		return(myForm.onlyFuture.checked);
	}
	else {
		return false;
	}
}

function checkBoxChanged() {

	// only update if user is viewing activities
	if (!(global_name == "ltid" || global_name == ""))
		UpdateMap(global_name, global_value);

}
