var loadResult = null;
var map;
var markers = new Array();
var qt=String.fromCharCode(34);
var myMarkers;
var centerLatitude=39.99;
var centerLongitude=-75.16;
var startZoom= 12;
var deselectCurrent= function(){};  //a null function for now
var theMapCenter = new GLatLng(centerLatitude, centerLongitude), startZoom;
var type;
var allTypes = { 'All Health Centers':[] };


//
function getMarkers(){
GDownloadUrl("data3.xml", function(data, responseCode) {
  			var xml = GXml.parse(data);

			myMarkers = xml.documentElement.getElementsByTagName("marker");
         for (var i =0; i < myMarkers.length; i++) {
			markers[i] = {head:null,
				lat:null,
			   lng:null,
			   inst:null,
			   hctr:null,
			   addr:null,
			   addr2:null,
			   csz:null,
			   phone:null,
			   lnk:null,
			   icon:null}; 
		markers[i].head=myMarkers[i].getAttribute("head");
		markers[i].lat=myMarkers[i].getAttribute("lat");
		markers[i].lng=myMarkers[i].getAttribute("lng");
		markers[i].inst=myMarkers[i].getAttribute("inst");
		markers[i].hctr=myMarkers[i].getAttribute("hctr");
		markers[i].addr=myMarkers[i].getAttribute("addr");
		markers[i].addr2=myMarkers[i].getAttribute("addr2");
		markers[i].csz=myMarkers[i].getAttribute("csz");
		markers[i].phone=myMarkers[i].getAttribute("phone");
		markers[i].lnk=myMarkers[i].getAttribute("lnk");
		markers[i].icon=myMarkers[i].getAttribute("icon");
		
		if ( markers[i].head =="yes"){
			initializeHeading( markers[i]);
			}
		else{
			initializePoint( markers[i]);
			}
		
	allTypes[markers[i].inst] = true;
	}
for(type in allTypes) {
		initializeSortTab(type);
	}

	
});

}


//This is the all new code from the book

function initializePoint(pointData) {
	var point = new GLatLng(pointData.lat, pointData.lng);
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));
	var lbl = null;
	var visible = false;
	listItemLink.href = "#";
	
	
	var address = pointData.addr+","+pointData.csz;
				var address2=pointData.addr2;
				var thelink=pointData.lnk;
				if (address2!=""){
				var street=pointData.addr + "<br>"+pointData.addr2 + "<br>";
				} //**if
				else{
				var street=pointData.addr + "<br>";
				} //**else
				//put the html for the sidebar list items
		listItemLink.innerHTML = '<strong>' + pointData.hctr+ ' </strong><span>' + street  +pointData.csz + '</span>';
		
				
				if (thelink != ""){
				var thesite="<br><a href='"+thelink+"' target='_blank'>Click here for this Health Center's website</a>";
				}  //**if
				else{
				var thesite="";
				} //**else
				//set the lable to display when marker is selected
				lbl = ("<p style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:12px;'><b>"+pointData.inst+"<br>"+pointData.hctr+"</b>"
				+ "<br>" + street
				+ pointData.csz + "<br>" + pointData.phone
				+ thesite+"</p>"
				);
				
				var thisIcon =  pointData.icon;
			switch (thisIcon)
				{
				case "GREEN":
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#00ff00"});
				break;
				
				case "YELLOW":
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ffff00"});
				break;
				case "RED":
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ff0000"});
				break;
				case "BLUE":
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#0000ff"});
				break;
				default:
				//if here, then thisIcon is probably a color--check for a # at the start
				if (thisIcon.charAt(0) == "#"){
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: thisIcon });
				} //**if
				else{
					//make it red if itis not a hex color
				theIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ff0000"});

				} //**else
				}  //**switch
				
	var marker = new GMarker(point, {icon:theIcon});
	var focusPoint = function() {
		deselectCurrent();
		listItem.className = 'current';
		deselectCurrent = function() { listItem.className = ''; }
		marker.openInfoWindowHtml(lbl);
		map.panTo(point);
		return false;
	} //**function focuspoint
	GEvent.addListener(marker, 'click', focusPoint);	
	listItemLink.onclick = focusPoint;
	
	pointData.show = function() {
		if (!visible){
			document.getElementById('sidebar-list').appendChild(listItem);
			map.addOverlay(marker);
			visible = true;
		} //**if
	} //**function
	pointData.hide = function() {
		if (visible) {
			document.getElementById('sidebar-list').removeChild(listItem);
			map.removeOverlay(marker);
			visible = false;
		} //**If
		
	}; //**function

	pointData.show();
} //**function initializePoint end



function initializeSortTab(type) {
	var listItem = document.createElement('li');
	var listItemLink = listItem.appendChild(document.createElement('a'));

	listItemLink.href = "#";
	listItemLink.innerHTML = type;
	listItemLink.onclick = function() {
		changeBodyClass('standby', 'loading');

		 for (var i =0; i < markers.length; i++) {
			if (markers[i].inst == type || 'All Health Centers' == type)
				markers[i].show();
			else
				markers[i].hide();	
		}

		changeBodyClass('loading', 'standby');

		return false;
	}

	document.getElementById('filters').appendChild(listItem);
}

function windowHeight() {
	// Standard browsers (Mozilla, Safari, etc.)
	if (self.innerHeight)
		return self.innerHeight;
	// IE 6
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	// IE 5
	if (document.body)
		return document.body.clientHeight;
	// Just in case.
	return 0;
}

function changeSize(){
//first, get the current map center in case we moved it manually
theMapCenter = map.getCenter();
//resize the elements
handleResize();
//have the map notice the resize
map.checkResize();
//pan tot he center we had
map.panTo(theMapCenter);

}

function handleResize() {
	var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 125;
	document.getElementById('map').style.height = height + 'px';
	document.getElementById('sidebar').style.height = height + 'px';


}

function changeBodyClass(from, to) {
	document.body.className = document.body.className.replace(from, to);
	return false;
}

function init() {
	
	
	document.getElementById('button-sidebar-hide').onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };
	document.getElementById('button-sidebar-show').onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };
	handleResize();
	
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.setCenter(theMapCenter, startZoom);
	getMarkers();
	
	
	
	changeBodyClass('loading', 'standby');
	
}





//end NEw code from book


function putMarker (map,thispoint,thisaddress,thislbl,thisIcon){
var geocoder= new GClientGeocoder();
if (thispoint=="NaN,NaN"){ 
geocoder.getLatLng(thisaddress, function(point){
												if (!point) {
             													alert(thisaddress + " not found");
           													} 
												else {
	 													map.addOverlay(createMarker(point,thislbl,thisIcon));
														alert(thisaddress+"= "+point);
													}
	 											});
						}
		
		if (thispoint != "NaN,NaN"){
		//alert ("not null="+thispoint);
		map.addOverlay(createMarker(thispoint,thislbl,thisIcon));
		}
}

function initializeHeading(heading){
		var visible = false;
	
	//first, make a list element
	var listItem=document.createElement('li');
	//now a heading 5 element to contain the name
	var listItemHeading=listItem.appendChild(document.createElement('h6'));
	//Put the Institution iformation into it
	listItemHeading.innerHTML=heading.inst;

	//make it the rigght color
	listItem.style.backgroundColor=heading.icon;
	//now put hte hide/show function in
	heading.show = function() {
		if (!visible){
			document.getElementById('sidebar-list').appendChild(listItem);
			visible = true;
		} //**if
	} //**function
	heading.hide = function() {
		if (visible) {
			document.getElementById('sidebar-list').removeChild(listItem);
			visible = false;
		} //**If
		
	}; //**function
	heading.show();
} //**function initializeHeading end
		
																	
	
	
	

   
    


  	window.onresize = changeSize;
	window.onload = init;
 
