// JavaScript Document
var map = null;
var geocoder = null;
var calendar = null;
var windowHeight = 900;


function load() {
	//alert("load");
  if (GBrowserIsCompatible()) {
	mapDiv = document.getElementById("map");
	map = new GMap2(mapDiv);

	map.addControl(new GSmallMapControl(),
					new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(15, 40)));
	map.addControl(new GMapTypeControl(),
					new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(15, 15)));
	map.setCenter(new GLatLng( 43.059763929921644, -87.8834581375122), 13);
	geocoder = new GClientGeocoder();
	
	//map.setMapType(G_HYBRID_TYPE);

	//getMarkers();
	  
	  GEvent.addListener(map, "click", function(overlay, point) {
		  if (overlay){	// marker clicked
			  overlay.openInfoWindowHtml(overlay.infowindow);	// open InfoWindow
		  } else if (point) {	// background clicked
			  
		  }
	  });
	
  }
  calendarResize();
  window.onresize = function(){calendarResize();}
  //document.recalc();
}

									

function calendarResize(){
	//alert("calendarResize");
	var titlebar = document.getElementById('titlebar');
	var gallery	= document.getElementById('gallery');
	calendar = document.getElementById('calendar'); // set globally
	var footer = document.getElementById('footer');
	mapDiv = document.getElementById("map"); //set globally and onLoad
	var marginOffest = 35; //seven margins of 5 pixels each
	
	var elementHeight = 0;//titlebar.offsetHeight + gallery.offsetHeight + footer.offsetHeight; // + 35;
	
	if(titlebar){
		elementHeight += titlebar.offsetHeight;}
	if(gallery){
		elementHeight += gallery.offsetHeight;}
	if(footer){
		elementHeight += footer.offsetHeight;}
	//calendar.innerHTML = "begin cal resize";
	
	var cHeight = "0px";
	//cHeight = calendar.style.height;
	//calendar.innerHTML = 	"windowheight:: " + windowHeight + "px" + "<br/>" +	"calendarheight:: " + cHeight + "<br/>";
	
	if(window.innerHeight){
		windowHeight = window.innerHeight;
		}
	else if(document.documentElement && document.documentElement.clientHeight){
		//alert("ie-strict");
		windowHeight = document.documentElement.clientHeight;
		}
	else if(document.body){
		//alert("ie");
		windowHeight = document.body.clientHeight;
	}
		
	calendar.style.height = (windowHeight - elementHeight) +"px";
	mapDiv.style.height = (windowHeight - elementHeight) + "px";
	cHeight = calendar.style.height;
	//calendar.innerHTML = "windowheight:: " + windowHeight + "px" + "<br/>" +	"calendarheight:: " + cHeight + "<br/>";
}
																			
function createMarker(point, nstring) {
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("Marker #<b>" + nstring + "</b>");
  });
  return marker;
}

function showAddress(address) {
	if(document.mapsearch.address){
		document.mapsearch.address.value = address;	
		startSearch();
		}

	if (geocoder) {
	geocoder.getLatLng(
	  address,
	  function(point) {
		if (!point) {
		  //alert(address + " not found");
			failedSearch();
		} else {
		  sucessSearch();
		  map.setCenter(point, 13);
		  var myMarker = createMarker(point, address);
		  map.clearOverlays();
		  map.addOverlay(myMarker);
		  //myMarker.openInfoWindowHtml("<b>" + address + "</b>");
		}
	  }
	);
  }
}

function startSearch(){
	document.mapsearch.address.style.backgournd = "#AAAAFF";
	showDiv("searching");//document.getElementById("searching").style.visibility = "visible";
	hideDiv("notfound");//document.getElementById("notfound").style.visibility = "hidden";

}

function failedSearch(){
	document.mapsearch.address.style.backgournd = "#99FFFF";
	hideDiv("searching");//document.getElementById("searching").style.visibility = "hidden";
	showDiv("notfound");//document.getElementById("notfound").style.visibility = "visible";

}

function sucessSearch(){
	document.mapsearch.address.style.backgournd = "#FFFFFF";
	hideDiv("searching");//document.getElementById("searching").style.visibility = "hidden";
	showDiv("notfound");
	
}

function getRefToDiv(divID,oDoc) {
  if( document.getElementById ) {
    return document.getElementById(divID); }
  if( document.all ) {
    return document.all[divID]; }
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      //repeatedly run through all child layers
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        //on success, return that layer, else return nothing
        y = getRefToDiv(divID,oDoc.layers[x].document); }
    return y; } }
  return false;
}

function showDiv(divID_as_a_string) {
  //get a reference as above ...
  myReference = getRefToDiv(divID_as_a_string);
  if( !myReference ) {
    //window.alert('Nothing works in this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ) {
    //DOM & proprietary DOM
    myReference.style.visibility = 'visible';
  } else {
    //layers syntax
    myReference.visibility = 'show';
  }
}

function hideDiv(divID_as_a_string) {
  //get a reference as above ...
  myReference = getRefToDiv(divID_as_a_string);
  if( !myReference ) {
    //window.alert('Nothing works in this browser');
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ) {
    //DOM & proprietary DOM
    myReference.style.visibility = 'hidden';
  } else {
    //layers syntax
    myReference.visibility = 'hide';
  }
}

function getMarkers()
{
	var urlstr="getLocations.XML.php";
	var request = GXmlHttp.create();
	request.open('GET', urlstr , true);	// request XML from PHP with AJAX call
	request.onreadystatechange = function () {
		if (request.readyState == 4) {
			//alert(request.responseText);
			var xmlDoc = request.responseXML;
			locations = xmlDoc.documentElement.getElementsByTagName("loc");
			var markers = [];
			if (locations.length){
				for (var i = 0; i < locations.length; i++) { // cycle thru locations
					markers[i] = new GMarker(
										new GLatLng(
											locations[i].getAttribute("latitude"),
											locations[i].getAttribute("longitude")
											)
										);
					// Add attributes to the marker so we can poll them later.
					// When clicked, an overlay will have these properties.
					arryLocName = locations[i].getElementsByTagName("name");
					markers[i].infowindow = "This is "+ arryLocName[0].nodeValue;
					
					// Useful things to store on a marker (Not needed for this example, could be removed)
					// Tells you what index in the markers[] array an overlay is
					markers[i].markerindex = i;
					// Store the location_id of the location the marker represents.
					// Very useful to know the true id of a marker, you could then make
					// AJAX calls to the database to update the information if you had it's location_id
					markers[i].db_id = locations[i].getAttribute("loc-id");
					
					map.addOverlay(markers[i]);
					
				}//for
			}//if
		}//if
	}//function
	request.send(null);
}//function