var geocoder = new GClientGeocoder();
var addresses = new Array();
var map;
var icon;

function setMapSize()
{
	var winHeight = window.innerHeight;
	if(!winHeight>0)
	{
		winHeight = document.documentElement.clientHeight;
	}
	if(!winHeight>0)
	{
		winHeight = document.body.clientHeight;
	}
	var newSize = winHeight-275;

	if(newSize<300)
	{
		newSize=300;
	}

	//if (window.ActiveXObject)//if IE
	//{
		//document.getElementById('result').height=newSize;
		//document.getElementById('mapdiv').height=newSize;
	//}
	//else
	{
		document.getElementById('result').style.height=newSize;
		document.getElementById('mapdiv').style.height=newSize;
	}
}
function setAddresses ()
{
	var query="";
	var i = 0;
	if ( document.getElementById('SunCheck').checked )
	{
		query+="weekdays[" + i + "]=Sunday&";
		i++;
	}
	if ( document.getElementById('MonCheck').checked )
	{
		query+="weekdays[" + i + "]=Monday&";
		i++;
	}
	if ( document.getElementById('TueCheck').checked )
	{
		query+="weekdays[" + i + "]=Tuesday&";
		i++;
	}
	if ( document.getElementById('WedCheck').checked )
	{
		query+="weekdays[" + i + "]=Wednesday&";
		i++;
	}
	if ( document.getElementById('ThuCheck').checked )
	{
		query+="weekdays[" + i + "]=Thursday&";
		i++;
	}
	if ( document.getElementById('FriCheck').checked )
	{
		query+="weekdays[" + i + "]=Friday&";
		i++;
	}
	if ( document.getElementById('SatCheck').checked )
	{
		query+="weekdays[" + i + "]=Saturday&";
		i++;
	}
	
	if ( document.getElementById('cityzip').value.length > 0 )
	{
		query+="cityzip=" + document.getElementById('cityzip').value + "&";
	}
/*	if ( document.getElementById('within').value.length > 0 )
	{
		query+="within=" + document.getElementById('within').value + "&";
	}
*/	document.getElementById('result').innerHTML="Searching ...";
	document.getElementById('errors').innerHTML="";

	makeRequest('meetingQuery.php?' + query);
}
function showAddresses(i,map) 
{
	if( i==0 )
	{
		map.clearOverlays();
	}
	if(i>=addresses.length){return;}
	geocoder.getLatLng
	(
		addresses[i][0],
		function(point)
		{
			if (!point)
			{
				//document.getElementById('errors').innerHTML+='<div><u>Could not plot address:</u><br/>' + addresses[i][1] + '</div>';
			}
			else
			{
				//map.setCenter(point, 13);
				var marker = new GMarker(point,icon);
				addresses[i][2]=marker;
				map.addOverlay(marker);
				GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(addresses[i][1]); 
						document.getElementById('result').scrollTop=document.getElementById('listingimg_' + i).offsetTop-300; } );
				GEvent.addListener(marker, "infowindowopen", 
					function() {
						document.getElementById('listing_' + i).className='listresult listingResultSelected';
					}
					)
				GEvent.addListener(marker, "infowindowclose", function() { document.getElementById('listing_' + i).className='listresult'; } );
				document.getElementById('listingimg_' + i).innerHTML= "<img src='/Images/naMapIcon.gif'/>";
			}
			showAddresses(i+1,map);
		}
		);
}
function showmap()
{
	if (GBrowserIsCompatible())
	{
		
		map = new GMap(document.getElementById("mapdiv"));
	}
	
	icon = new GIcon();
	icon.image = "http://wisconsinna.org/Images/naMapIcon.gif";
	icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	icon.iconSize = new GSize(20, 33);
	icon.shadowSize = new GSize(22, 20);
	icon.iconAnchor = new GPoint(6, 20);
	icon.infoWindowAnchor = new GPoint(5, 1);
	map.centerAndZoom(new GPoint(-89.350433,44.5), 10);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());	
}

/******************************
Basic AJAX functions
******************************/
function makeRequest(url)
{
	//alert(url);
	var httpRequest;
	
	if (window.XMLHttpRequest)
	{ // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType)
		{
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject)
	{ // IE
		try
		{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e)
		{
			try
			{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	
	if (!httpRequest)
	{
		alert('It appears your browser may not support the necessary technology.\n' +
			'Please try again and if you continue to receive this error,\n' +
			'use the Classic Meeting List link to the right or try upgrading your web browser.\n' +
			'If you have questions or problems, contact the Web Servants by clicking the link on the Contacts page.');
		return false;
	}
	httpRequest.onreadystatechange = function() { requestContents(httpRequest); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	
}

function requestContents(httpRequest)
{
	
	if (httpRequest.readyState == 4)
	{
		if (httpRequest.status == 200)
		{
			var responseTxt = httpRequest.responseText;
			var response;
			try //Internet Explorer
			{
				response=new ActiveXObject("Microsoft.XMLDOM");
				response.async="false";
				response.loadXML(responseTxt);
			}
			catch(e)
			{
				//alert(e.message);
				try //Firefox, Mozilla, Opera, etc.
				{
					var response = (new DOMParser()).parseFromString(responseTxt, "text/xml");
				}
				catch(e) {alert(e.message)}
			}
			var resultList = response.getElementsByTagName("result");
			var resultText="";
			addresses = new Array();
			
			for ( var i = 0 ; i < resultList.length ; i++ )
			{
				var address = resultList[i].getElementsByTagName("address")[0].childNodes[0].nodeValue;
				var label = resultList[i].getElementsByTagName("label")[0].childNodes[0].nodeValue;
				
				addresses[addresses.length]=[address,label];
				resultText += "<div id='listingimg_" + i + "' class='listingimg'></div><div class='listresult' onclick='gotoMarker(" + i + ");' id='listing_" + i + "'>"
				
				resultText += label + "</div>";
			}
			document.getElementById('result').innerHTML= 
				"<div>There are <strong>" + resultList.length + "</strong> meetings that match your search. Click on a listing below to view on the map.</div><hr />" +
				resultText;
			showAddresses(0,map);
			
		} else
		{
			document.getElementById('result').innerHTML="We're sorry. An error occurred while processing your meeting list search.";
		}
	}
	
}
function gotoMarker(i)
{
	if ( addresses[i].length ==3 )
	{
		var zoom = map.getZoom();
		if(zoom<13 || zoom>18)
		{
			zoom = 16;
		}
		map.setCenter(addresses[i][2].getLatLng(), zoom);
		addresses[i][2].openInfoWindowHtml(addresses[i][1]);
	}
	else
	{
		alert('The address for this meeting could not be plotted.');
	}
}
