//[CDATA[
  
if (GBrowserIsCompatible())
{
	
// Global variables start here	
		
		var map; 
		var stateMarkers = new Array();
		var stateList = [];
		var collegeMarkers = new Array();
		var lat = 39.275006;
		var lng = -92.774769;
		var zoom = 4;
		// Sample custom marker code created with Google Map Custom Marker Maker
		// http://www.powerhut.co.uk/googlemaps/custom_markers.php
		var stateIcon = new GIcon();
		stateIcon.image = 'images/image.png';
		stateIcon.shadow = 'images/shadow.png';
		stateIcon.iconSize = new GSize(32,37);
		stateIcon.shadowSize = new GSize(51,37);
		stateIcon.iconAnchor = new GPoint(16,37);
		stateIcon.infoWindowAnchor = new GPoint(16,0);
		stateIcon.printImage = 'images/printImage.gif';
		stateIcon.mozPrintImage = 'images/mozPrintImage.gif';
		stateIcon.printShadow = 'images/printShadow.gif';
		stateIcon.transparent = 'images/transparent.png';
		stateIcon.imageMap = [27,3,28,4,28,5,28,6,28,7,28,8,28,9,28,10,28,11,28,12,28,13,28,14,28,15,28,16,28,17,28,18,28,19,28,20,28,21,28,22,28,23,28,24,28,25,28,26,28,27,27,28,21,29,20,30,19,31,18,32,17,33,16,34,15,34,14,33,13,32,12,31,11,30,10,29,4,28,3,27,3,26,3,25,3,24,3,23,3,22,3,21,3,20,3,19,3,18,3,17,3,16,3,15,3,14,3,13,3,12,3,11,3,10,3,9,3,8,3,7,3,6,3,5,3,4,4,3];
		
		var iconName = ["images/blue.png","images/green.png","images/yellow.png"]; //college icons		
		var displaymap = 0;
		var divisionmask = 7; // Bit pattern 0000 0000 0111     
		var typemask = 24;      // Bit pattern is 0000 0001 1000
		var popmask = 480;       // Bit pattern 0001 1110 0000 
		var curpopvalue = 0;
		var curtypevalue = 0;
		var curdivvalue = 0;
		var ziplatlng = null;
		var zipDist;
		var xmlhttp; 
		var visibleMarkers = 0; //number of individual college markers within current filter parameters
		var expiredays = 1;
		var cookiename = "stonewall";
		var divlist = ["","",""];
		var bounds;
		var distFilter = false; //distance filter state
		
		function defaultViewButton() {}

// To "subclass" the GControl, we set the prototype object to
// an instance of the GControl object

		defaultViewButton.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.

defaultViewButton.prototype.initialize = function(map) {
      
      var zoomOutDiv = document.createElement("div");
      this.setButtonStyle_(zoomOutDiv);
      zoomOutDiv.appendChild(document.createTextNode("Return to Default Map"));
      GEvent.addDomListener(zoomOutDiv, "click", function() {
        displaymap = 0;
        curpopvalue = 0;
        curtypevalue = 0;
        curdivvalue = 0;
        distFilter = false;
        document.filterform.population.options.selectedIndex = 0;
	 	  document.filterform.insttype.options.selectedIndex = 0;
   	  document.filterform.division.options.selectedIndex = 0;
        distZip = newZip = "";
        document.filterform.distCtrl.options.selectedIndex = 0;
		  document.filterform.zipCtrl.value = "";
		  map.setCenter(new GLatLng(39.275006,-92.774769),4);
		  updateMarkerInfo();
		  updateStateMarkersHTML();
		  updateMapList();
		  displayUpdate(); 
      });
      map.getContainer().appendChild(zoomOutDiv);
      return zoomOutDiv;
    }


// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.

defaultViewButton.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100, 7));
}

// Sets the proper CSS for the given button element.

defaultViewButton.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "underline";
  button.style.color = "#0000cc";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "10em";
  button.style.cursor = "pointer";
}

		
// end variable declarations    
//  Now check to see if we have set a cookie
//  If so retrieve the values and process the cookie data   
      
	if (document.cookie.length > 0) 
	{
		processCookieData();

	} 
	map = new GMap2(document.getElementById("map"));
	var mt = map.getMapTypes();	
	for (var i=0; i<mt.length; i++) 
	{
	   mt[i].getMinimumResolution = function() {return 4;}
	}
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
  	map.addControl(new defaultViewButton());
	map.setCenter(new GLatLng(lat,lng),zoom);
	GEvent.addListener(map,"zoomend",displayUpdate);
	GDownloadUrl("genxml7.php", init_Data);
}
else
	{
	map.innerHTML = "Your Browser is incompatible with this application";
	}
	
function init_Data(data)
{
 var xml = GXml.parse(data);
 var stateXML = xml.documentElement.getElementsByTagName("state");
 setupStateMarkers(stateXML);
 var collegeXML = xml.documentElement.getElementsByTagName("college");
 setupCollegeMarkers(collegeXML);
 if (document.cookie.length>0) 
 {
 	updateMarkerInfo();
 }
 updateStateMarkersHTML();
 updateMapList();
 displayUpdate();
 }
   
function processCookieData()
{
	var cookieStart;
	var cookieEnd;
	var cookietext;
	cookieStart = document.cookie.indexOf(cookiename + "=");
	if (cookieStart!=-1) 
	{
		cookieStart += cookiename.length+1; 
		cookieEnd = document.cookie.indexOf(";",cookieStart);
		if (cookieEnd == -1) 
		{
			cookieEnd = document.cookie.length;
		}
	 	cookietext = document.cookie.substring(cookieStart,cookieEnd);
	 	crumbs = cookietext.split("|");
          	lat = parseFloat(crumbs[0]);
          	lng = parseFloat(crumbs[1]);
          	zoom = parseInt(crumbs[2]);
          	displaymap = parseInt(crumbs[3]);
          	curpopvalue = parseInt(crumbs[4]);
          	curtypevalue = parseInt(crumbs[5]);
          	curdivvalue = parseInt(crumbs[6]);
        document.filterform.view[parseInt(crumbs[10])].checked = true;
		document.filterform.population.options.selectedIndex = parseInt(crumbs[7]);
	 	document.filterform.insttype.options.selectedIndex = parseInt(crumbs[8]);
   	 	document.filterform.division.options.selectedIndex = parseInt(crumbs[9]);
   	 	if (crumbs.length > 11)
          	{
         		document.filterform.distCtrl.options.selectedIndex = parseInt(crumbs[11]);
          	 	zipDist = parseInt(crumbs[15]);
          	 	document.filterform.distzip.value = parseInt(crumbs[12]);
        	     	ziplatlng = new GLatLng(parseFloat(crumbs[13]),parseFloat(crumbs[14]));
        	     	distFilter = true;
          	}      	
         }
}  

function setupStateMarkers(statexml)
{
  	for (var i = 0; i < statexml.length; i++)
   	{
   		
   		var s = new obj_USState(statexml[i]);
			stateList[s.abbr] = s;
      } //end for statement
   for (var x in stateList)
	{
		var stateOptions = { icon:stateIcon,
						 			labelText:"",
						 			labelOffset: new GSize(-8,-32),
						 			labelClass: 'marker_label',
						 			title:stateList[x].name};
		var q = new LabeledMarker(stateList[x].center,stateOptions);
		map.addOverlay(q);
		q.hide();
		stateMarkers.push(q);
		stateList[x].markerNum = stateMarkers.length - 1;
	}
}  //end function 

function setupCollegeMarkers(collegexml) 
{
	var i,m,div;	
	
	for (i = 0; i<collegexml.length; i++)
	{
		m = createCollegeMarker(collegexml[i]);
		map.addOverlay(m);
		m.visible = true;
		visibleMarkers++;            
		m.hide();						
		collegeMarkers.push(m);
		stateList[m.st].collegeCnt++;
		stateList[m.st].collegeTotal++;				
		div = calcDivision(m.display);
		stateList[m.st].htmls[div] +=  '<li><a href = "profile.php?id=' + m.id + '">' + m.title + " </a></li>";
	} // end for loop
			 
}
 
function createCollegeMarker(marker)
{
	var point = new GLatLng(parseFloat(marker.getAttribute("lat")),
							parseFloat(marker.getAttribute("lng")));				
	var name = marker.getAttribute("name");
	var sortname = marker.getAttribute("sortname");
	var id = marker.getAttribute("id");
	var display = marker.getAttribute("display");
	var st = marker.getAttribute("state");
	var div = calcDivision(display);
	var html="";
	
   
	var myIcon = new GIcon(G_DEFAULT_ICON);
	myIcon.image = iconName[div - 1];
	var options = {icon:myIcon, title:sortname};
	var m = new GMarker(point,options);
	m.name = name;
	m.id = id;
	m.display = display;
	m.st = st;
	html = name + "<br />";
	html += "<a href = \"profile.php?id="+m.id+"\"> See Profile</a> <br>";
	GEvent.addListener(m, 'click', function()
	{
		m.openInfoWindowHtml(html);
   	});
      return m;
} // end function createMarker


function obj_USState(xmlnode)
{
	this.center = new GLatLng(parseFloat(xmlnode.getAttribute("lat")),
                                parseFloat(xmlnode.getAttribute("lng")));
	this.abbr = xmlnode.getAttribute("abbr");
	this.name = xmlnode.getAttribute("name");
	this.markerNum = 0;
	this.collegeCnt = 0;
	this.collegeTotal = 0;
	this.htmls = ['','','',''];
}


function updateStateMarkersHTML()
{
	var labels = ["Summary","DI","DII","DIII"];
	var html;
	
	
	for (var x in stateList)
	{
		var num = stateList[x].collegeCnt;
		var tabs = [];
		var tab0Txt = "Total number of teams in " + stateList[x].name + ":" + stateList[x].collegeTotal;
		tab0Txt += "<br> Number meeting your current criteria: " + stateList[x].collegeCnt;
		tabs.push(new GInfoWindowTab(labels[0], tab0Txt));  
		if ( num > 0)
		{
			var z = stateList[x].markerNum;	
			for (var i = 1; i < 4; i++)
			{
				if (stateList[x].htmls[i].length > 0)
				{
					html = '<DIV class="info_window"><ol>' + stateList[x].htmls[i] + "</ol></DIV>"; 
					tabs.push(new GInfoWindowTab(labels[i],html));
				}
			}
		
			stateMarkers[z].bindInfoWindowTabsHtml(tabs);
			stateMarkers[z].visible = true;
			stateMarkers[z].setLabelText(stateList[x].abbr + "<br />" + stateList[x].collegeCnt);
		}
	}
}

function displayUpdate()
{
    if (document.filterform.view[0].checked)
        chgView("Map");
    else
        chgView("List");
    map.closeInfoWindow();   
    var curZoom = map.getZoom();
        //var newCenter = bounds.getCenter();
        //curZoom = map.getZoom();
	    //If the current zoom is less than 7 AND there are more than 10 college markers
	    // then show the state markers

    if (curZoom < 7 && visibleMarkers > 10)  
    {
	    document.getElementById("maplegend").style.display = 'none';			
	    for (var i = 0; i < collegeMarkers.length; i++)
	    {
		    collegeMarkers[i].hide();
	    }
	    for (var x in stateList)
	    {
		    if (stateList[x].collegeCnt > 0)				
		    {				
		    	stateMarkers[stateList[x].markerNum].show();
		    }
		    else
		    {
		    	stateMarkers[stateList[x].markerNum].hide();
		    }
	    }
    } else
    {
	    document.getElementById("maplegend").style.display = 'block';         
           for ( var i = 0; i < collegeMarkers.length; i++)
	    {
		    if (collegeMarkers[i].visible == true)
		    	collegeMarkers[i].show();
	        else
		       	collegeMarkers[i].hide();
	    }
	    for (i = 0; i < stateMarkers.length; i++)
	    {
		    stateMarkers[i].hide();
	    }
    }
}

function selectChange(value,which)
{


        var newflag=0;
        savevalue = "cur"+which+"value";
        if (window[savevalue] >= 0)
      	{
        		displaymap ^= window[savevalue]; //clear the current pop value 
   		}
        displaymap |= value;
        
        window[savevalue] = value;  // Save the current setting so we can clear it on a change
        updateMarkerInfo();
        updateStateMarkersHTML();
        updateMapList();
        displayUpdate();
}

function updateMarkerInfo()
{
	var displayflag = calcDisplayFlag();
	var i,x,z,show,div,s;
	var newZoom;
	
	
	// Clear out any existing html for the info window and the sidebar lists
	for (i=0;i<3;i++)
		divlist[i]="";
	for (x in stateList)
	{
		stateList[x].collegeCnt = 0;
		for (z=0;z<4;z++)
		{	
			stateList[x].htmls[z] = "";
		}
	}	
	visibleMarkers = 0;
   //bounds = new GLatLngBounds();
	
   for (i=0; i<collegeMarkers.length; i++)
   {
		show = true; //default is to show all markers
		div = calcDivision(collegeMarkers[i].display);
		s=collegeMarkers[i].st;
		 	
				
		if ((collegeMarkers[i].display & displayflag) != collegeMarkers[i].display)
      	{
            show = false;
         }
		if (distFilter)
      	{
			  	var distance = ziplatlng.distanceFrom(collegeMarkers[i].getLatLng())*0.00062137;
   	      if (distance > zipDist )
      		{
        			show = false;
       		}
       }
       if (show)
       	{
       		collegeMarkers[i].visible = true;
       		stateList[s].collegeCnt++;
       		visibleMarkers++;
       		divlist[div - 1] += '<li><a href = "profile.php?id=' + collegeMarkers[i].id + '">' + collegeMarkers[i].getTitle() + " </a></li>";
       		stateList[s].htmls[div] += '<li><a href = "profile.php?id=' + collegeMarkers[i].id + '">' + collegeMarkers[i].getTitle() + " </a></li>";
       		//bounds.extend(collegeMarkers[i].getLatLng());
       	}
       	
       if (!show)
       	{
       		collegeMarkers[i].visible = false;
       	}
   }
}

function calcDisplayFlag()
{
	var displayflag = 0; // A temporary variable to do internal calculation to the displaymap
   
        /* First calculate the mask for the type (Public or Private)
           The if statement checks to see if one of the check boxes is set.  If no boxes are checked the
			bitwise AND of the bits 4 and 5 of current displaymap will return 0.  If neither check box 
			is checked, it's the same as if both are checked so we do a bitwise OR of those bits to set
			them.   Conversely if the conditional is false one (or both) of the boxes is checked.  To
			determine which box is set we and the current displaymap with the mask, which should return an
		    8, a 16,or a 24.  A bitwise OR of this with the display flag set the appropriate bits.
       
	
  */
	(displaymap & typemask) == 0 ? displayflag |= typemask : displayflag |= (displaymap & typemask);

   /*  See above for a more detailed description, but we are doing the same thing with the lowest 3 bits of
		the displaymap
   */
	(displaymap & divisionmask) == 0 ? displayflag |= divisionmask : displayflag |= (displaymap & divisionmask);
   /*  See above for a more detailed description, but we aredoing the same thing with the bits 6-9 of the 
       displaymap
   */
	(displaymap & popmask) == 0 ? displayflag |= popmask : displayflag |= (displaymap & popmask);   
	return displayflag;
}

function calcDivision(display)
{
   var div = display & 7;
	if (div < 3) 
		div = div;
	else
	  div = div/2 + 1;
	return div;
}

function checkZipCode(newzip)
{
var regZipCode = /^([0-9]){5}$/;

// First check the value and make sure 1) that the entry is 5 characters long

if (newzip.length == 0)
	{
	  return 1;
	}	
if (newzip.length < 5)
	{
	    return 2; 
	}
//Now check and be sure all the characters are numbers

if (!regZipCode.test(newzip))
	{
	return 3;
	}
return 0;
}


function zipGeocoded()
{
	if (xmlhttp.readyState == 4)
	{
		// 4 = "loaded"
		if (xmlhttp.status == 200)
			{// 200 = OK
			var xml = xmlhttp.responseXML.documentElement;
			if (xml.getElementsByTagName("zip").length > 0)
			{	
				var lat = xml.getElementsByTagName("lat")[0].childNodes[0].nodeValue;
				var lng = xml.getElementsByTagName("lng")[0].childNodes[0].nodeValue;
   			ziplatlng = new GLatLng(parseFloat(lat),parseFloat(lng));
   			distFilter = true;
   			updateMarkerInfo();
   			updateStateMarkersHTML();
   			updateMapList();
   			displayUpdate();
   		}
		}
		else
		{
				alert("XML Status returned " + xmlhttp.status + " " + xml);
		}
	}
}
	
function setCookie() 
{
     var cookietxt = cookiename+"="+map.getCenter().lat()+"|"+map.getCenter().lng()+"|"+map.getZoom()+"|";
     //Save the current settings for Divsion, population and type filters
     cookietxt += displaymap + '|' + curpopvalue + "|" + curtypevalue + "|" + curdivvalue;  
     var popidx = document.filterform.population.options.selectedIndex;
     var dividx = document.filterform.division.options.selectedIndex;
     var typeidx = document.filterform.insttype.options.selectedIndex;
  	  cookietxt += "|" + popidx + "|" + typeidx + "|" + dividx;

     // Save whether the view is map or list
      cookietxt += "|" + (document.filterform.view[0].checked==true?0:1);
  	  // Save the setting for the zip code distance filter if it is active
            
  	  if (distFilter)
  	  	{
  	  	cookietxt += "|" + document.filterform.distCtrl.options.selectedIndex;
  	  	cookietxt += "|" + document.filterform.distzip.value;
  	  	cookietxt += "|" + ziplatlng.lat() + "|" + ziplatlng.lng() + "|" + zipDist;
  	  	}
    
  if (expiredays)
  {
     var exdate=new Date();
     exdate.setDate(exdate.getDate()+ expiredays);
     cookietxt += ";expires="+exdate.toGMTString();
  }
  
// == write the cookie ==
  document.cookie=cookietxt;

 // == Call GUnload() on exit ==
  GUnload();
} 

function updateMapList()
{
	var divisions = ["I","II","III"];
	 var newhtml = "<table><tr>";	
	//var curel;
					
	for (var y = 0; y < 3; y++)
		{
//			var el = "division"+divisions[y];	
               		
				if (divlist[y].length > 0)
			    {
			        newhtml +='<td><h3> Division ' + divisions[y]+ '</h3><ul  id= "list' + divisions[y] + '">' + divlist[y] + '</ul></td>';		
				}	
        }
        newhtml += '</tr></table>';
        document.getElementById("maplist").innerHTML=newhtml;
}

function chgView(which)
{
    document.getElementById("maplist").style.visibility="hidden";
    document.getElementById("map").style.visibility="hidden";
    if (which=="Map")
    {
        document.getElementById("listcontentcolumn").style.display="none";
        document.getElementById("mapcontentcolumn").style.display="block";
    }else   
    {
        document.getElementById("mapcontentcolumn").style.display="none";
        document.getElementById("listcontentcolumn").style.display="block";
    }
    document.getElementById("maplist").style.visibility="visible";
    document.getElementById("map").style.visibility="visible";
}
    
function chgListDisplay(which)
{

	var maxheight;
	var ct = 0;	
	for (var y = 0; y < 3; y++)
		{					
			if (divlist[y].length > 0) ct++;
		}
  // maxheight = document.getElementById("listcontentcolumn").offsetHeight/ct - 20 * ct;
  // maxheight = maxheight + "px";
	var dv = document.getElementById("list"+which);
		
	if (dv.style.display == "none" || dv.style.display =="")
	{
		dv.style.display = "block";
		dv.style.height ="auto";
	} 
	else
	{
		dv.style.display = "none";
	}
  swapImage("D"+which+"png");
}
  
function swapImage(what)
{
	originalSrc = "http://" + document.location.host + "/recruiting/images/laxbulletgrey.png";
	curSrc = document.getElementById(what).src;
	if (curSrc == originalSrc)
	{
		document.getElementById(what).src = "http://" + document.location.host +"/recruiting/images/laxbulletgrey90.png" ;
	}
	else
	{
		document.getElementById(what).src = originalSrc;
	}
}

function zipFilterClick(which)
{

	if (which == "Clear")
	{
	distFilter = false;
	distZip = newZip = "";
	document.filterform.distCtrl.options.selectedIndex = 0;
	document.filterform.zipCtrl.value = "";
	updateMarkerInfo();
	updateStateMarkersHTML();
	updateMapList();
	displayUpdate(); 
	}
	else
	{
		//First be sure a the distance field is set
		distFilter = false;
		ziplatlng=null;
  		if (document.filterform.distCtrl.options.selectedIndex == 0)
		{
  			alert("You must set a distance");
  			return;
		}
		else
		{
  			zipDist = document.filterform.distCtrl.options[document.filterform.distCtrl.selectedIndex].value;
		}
	
		//Check zip code and if valid try to geocode it
		var newZip = document.filterform.distzip.value;
		switch(checkZipCode(newZip))
  		{
  			case 1: alert ("You must enter a Zip Code");
  				  return;
  	   	case 2,3: alert ("Zip Code must be 5 digits.");
  	   			 return;
  	   	case 0:  xmlhttp = null;
					var url = "getZipGeocode.php?z=" + newZip;
					if (window.XMLHttpRequest)
  					{
  						// code for all new browsers
  						xmlhttp=new XMLHttpRequest();
  					}
					else if (window.ActiveXObject)
  					{//  IE5 and IE6
  						xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  					}
					if (xmlhttp!=null)
  					{
  						xmlhttp.onreadystatechange = zipGeocoded;
  						xmlhttp.open("GET",url,true);
  						xmlhttp.send(null);
  					}
					else
  					{
  						alert("You need to update your browser use this page.");
  					}
		}
	}
}
//]]>

