var gSchool =null;
var gMap = null;
var school_dict = {};
function onload_tab_schools() {
	     if (gMap!= null){
			 gMap.checkResize();
			 return;
		 } 
		 
         gSchool = new GIcon();
         gSchool.iconSize = new GSize(24, 26);
         gSchool.iconAnchor = new GPoint(12, 26);
         gSchool.infoWindowAnchor = new GPoint(12, 2);
         gSchool.image = "http://www.fizber.com/static/images/drivescore/school.png";		 
         gMap = new GMap(document.getElementById("schools_gmap"));
         gMap.addControl(new GSmallMapControl());
         gMap.addControl(new GMapTypeControl());
         geocoder = new GClientGeocoder();
		 
		 if (zoom_city){
		   		if (zoom_city["iscoord"] == 1){
				   //alert(1);	
		           var point = new GLatLng(zoom_city["lat"],zoom_city["lng"]);
				   gMap.setCenter(point, 10);
				}
				 else if(state){
					if (state["iscoord"] == 1){
					   var point = new GLatLng(state["lat"],state["lng"]);
					   gMap.setCenter(point, 6);
					}
					else
						 gMap.setCenter(new GLatLng(37.4419, -105.1419), 3);
				 }
		 }
		 else 
		     if(state){
		   		if (state["iscoord"] == 1){
		           var point = new GLatLng(state["lat"],state["lng"]);
				   gMap.setCenter(point, 6);
				}
				else
				     gMap.setCenter(new GLatLng(37.4419, -105.1419), 3);
			 }
			  showSchoolsMarkersPage(0);
 }
 
var ajax_get_schools = false;
function ajax_getShools(){
      if (ajax_get_schools)
	      return;
	  ajax_get_schools = true;	
       //alert("tut");	
      parent.ajaxData('/ajax_get_schools.html?state_id='+state_id+'&city='+city,{},'ajax_viewSchools(req.responseJS,req.responseText)');
     
}

function ajax_viewSchools(hash, mes){
       ///alert("tut2");	
	   if (mes=="error")
	       return;	   
	   var ht = ""
	   if (hash){
	       schools = hash["schools"];
	   }
	   viewSchools(hash, mes);
//	   viewSchoolsMap(schools);
}

function viewSchoolsMap(schools){
	     var c = schools.length;
		 for (var i=0; i<c; i++){
		    //alert(schools[i]["iscoord"]);
			//alert(schools[i]["lat"] + ":" +schools[i]["lng"]);
		 }
		 
	}
function showSchoolsMarkersPage(page){
         gMap.clearOverlays();

		 for (var i=page*16; i<schools.length && i<(page+1)*16; i++){
			 school = schools[i];
            if (school["iscoord"] == 1){
                var point = new GLatLng(school["lat"],
                              school["lng"]);
				marker = markerSchool2(school,point,i)
				gMap.addOverlay(marker);
			}
			else {
				school["i"] = i;
				school_dict[school["street"] + " " + school["city"]+  ", " + school["state"]] = school;
				if (geocoder) {
                    geocoder.getLocations(
                    school["street"] + " " + school["city"]+  ", " + school["state"],
					   function(response){
						 if (!response || response.Status.code != 200) {
							 //alert(response.name + "not found");
      					  } else {
							//alert(response.name + " find");
							place = response.Placemark[0];
                            point = new GLatLng(place.Point.coordinates[1],
                                               place.Point.coordinates[0]);
    					    //alert(school_dict[response.name]);
							setSchoolCoord(school_dict[response.name]["id"],point.lat(),point.lng());
							
				            marker = markerSchool2(school_dict[response.name],point,school_dict[response.name]["i"]);
                            gMap.addOverlay(marker);
					   }
					   }
				   )
				}
			}
		 }  
}


function setSchoolCoord(id,lat,lng){
	////alert("setHouseCoord");
	//parent.ajaxData("/ajax_gmaps_set_house_coord.html",{id:id,lat:lat,lng:lng},"view_response('',req.responseJS,req.responseText)");
    //alert(document.domain);
	$.get("http://"+document.domain+"/ajax_gmaps_set_schools_coord.html",{id:id,lat:lat,lng:lng},"");

}


function view_response(data,hash,mess){
    //alert(mess);  

}

function markerSchool2(school,point,i){
	var icon = new GIcon(gSchool);
    var marker = new GMarker(point,icon); 
	var htmltext = "<div style='font:75% Verdana, Arial, Helvetica, sans-serif; color:#101010;width:260px' >" + school["school_name"] + "<br/>"
	htmltext += "<b>Address: </b>"
	htmltext += "&nbsp;" + school["street"] +  "<br/>"
	htmltext += "<b>City, State: </b>" + school["city"] + ' ' + school["state"] + "<br/>"
	htmltext += "<b>Students: </b>"
	htmltext += "&nbsp;" + school["total_students"] + "<br />"
	htmltext += "</div>"
    GEvent.addListener(marker, "click", function() {  marker.openInfoWindowHtml(htmltext); viewSchoolInfo(i)} );
	return marker;
}


function markerSchool(school,i){
  if (school["iscoord"] == 1)
     var point = new GLatLng(school["lat"],
                              school["lng"]);
  else{
    //var 
      //marker["distance"] = "N/A"
      return null;
    }
  var icon = new GIcon(gSchool);
  var marker = new GMarker(point,icon);
  var htmltext = "<div style='width:260px' >" + school["school_dicr"] + "<br/>"
   htmltext += "<b>Address: </b>"
   htmltext += "&nbsp;" + school["street"] +  "<br/>"
   htmltext += "<b>City, State: </b>" + school["city"] + ' ' + school["state_code"] + "<br/>"
   htmltext += "<b>Students: </b>"
   htmltext += "&nbsp;" + school["total_students"] + "<br />"
   htmltext += "</div>"
   //////alert(htmltext);
   GEvent.addListener(marker, "click", function() {  marker.openInfoWindowHtml(htmltext); viewSchoolInfo(i)} );
   htmltext = '<div style="font:75% Verdana, Arial, Helvetica, sans-serif; color:#101010;">' + htmltext + '</div>'
   marker["cont"] = htmltext;
   if (point_house)
       marker["distance"] = distance2(point_house,point) + " Mi"
   else
       marker["distance"] = "N/A"
   return marker;
}


function viewSchools(hash, mes){
       if (mes=="error")
	       return;	   
	   var ht = ""
	   
	   if (hash){
	     ht += "<b style='color:#3E7095;' > Schools in " + hash["loc"] + " </b><br />"
         ht +=  " <div style='width:355px; font-size:85%;margin-top:4px;'>"
		 ht +=  "   <div style='float:left;overflow:hidden;width:236px;height:16px;margin-top:3px;color:#3E7095;'> School Name </div>" 
		 ht +=  "   <div style='float:left;width:59px;color:#245D85;'> Distance </div>" 
		 ht +=  "   <div style='float:left;width:55px;color:#245D85;'> Students </div>"    
		 ht +=  "	</div>"
		 ht +=  "<div class='clear'> </div>"
	     if(!hash.state){
		   schools = hash["schools"];
		   //document.getElementById('ggmapiframe_tab_schools').contentWindow.schowSchools(schools);
		   
		   for (var i=0; i<hash["schools"].length; i++){
		      if (i%2!=0)
                ht +=  " <div style='width:355px; background:#EAF8FF;font-size:85%;padding: 1px 0; cursor:pointer; cursor:hand; '  onmouseout='{this.style.background=\"#EAF8FF\"}' onmouseover='{this.style.background=\"#ffc\"}'>"
		      else
			    ht +=  " <div style='width:355px;font-size:85%; cursor:pointer; cursor:hand; ' onmouseout='{this.style.background=\"#FFFFFF\"}' onmouseover='{this.style.background=\"#ffc\"}' >"
			  ht +=  "   <div style='float:left;overflow:hidden;width:245px;height:16px;padding: 1px 0;'> <span style='' onclick='document.getElementById(\"ggmapiframe_tab_schools\").contentWindow.viewSchoolInfo("+ i +");return false;'>" + hash["schools"][i]["school_dicr"] + "</span></div>" 
			  var marker = markerSchool(schools[i],i);
			  schools[i]["marker"] = marker;
			  if (marker){
			     gMap.addOverlay(marker);
  			     ht +=  "   <div style='float:left;width:55px;text-align:left;text-align:right;'>" + marker["distance"] +" </div>" 
			  }
			  else
 			     ht +=  "   <div style='float:left;width:55px;text-align:left;text-align:right;'>" + "N/A" +" </div>" 
			  ht +=  "   <div style='float:left;width:55px;text-align:left;text-align:right;'>" + hash["schools"][i]["total_students"] +" </div>" 
    		  ht +="<div class='clear'> </div>"
			  ht +=  "	</div>"
		  }
		}
		ht +="<div class='clear'> </div>"
        ht +="<div>&nbsp</div>"    
		
	 }
     viewSchoolInfo(0);
	 //alert(schools[0]["id"]);
	 parent.document.getElementById('schools_info').innerHTML = ht;
	 
   }	

function viewSchoolInfo(i){
   sh = schools[i];
   try{
	   if (sh.marker){
	   	   sh.marker.openInfoWindowHtml(sh.marker.htmltext); 
	   }
   } catch(e){}
   
   ht = "<table>"
   ht += "<tr valign='top'>"
   ht += "<td>"
   ht += "  <table width='360px;' class='tbl3'>"
   ht += "  <th colspan='2'> "+ sh["school_name"] + " </th>"
   ht += "    <tr>"
   ht += "     <td align='right'>Address: </td>"
   ht += "     <td>" + sh["street"] + "</td>"
   ht += "   </tr>"
   ht += "   <tr>"
   ht += "     <td align='right'>City, State: </td>"
   ht += "     <td>" + sh["city"] + " (" +sh["state"]+ "), "+ sh["zip"]+ "</td>"
   ht += "   </tr>"
   if (sh["school_dicr"]){
       ht += "<tr>"
       ht += "  <td align='right'>District Name: </td>"
       ht += "  <td>" + sh["school_dicr"]+ "</td>"
       ht += "</tr>"
   }
   if (sh["phone"]&&sh["phone"]!="None"){
	   ht += "<tr>"
       ht += "  <td align='right'>Phone: </td>"
       ht += "  <td>" + sh["phone"]+ "</td>"
       ht += "</tr>"
   }
   if (sh["total_students"]&&sh["total_students"]!="None"){
       ht += "<tr>"
       ht += "  <td align='right'>Students: </td>"
       ht += "  <td>" + sh["total_students"] + " </td>"
       ht += "</tr>"
   }
   if (sh["teachers"]&&sh["teachers"]!="None"){
       ht += "<tr>"
       ht += "  <td align='right'>Teachers: </td>"
       ht += "  <td>" + sh["teachers"] + "</td>"
       ht += "</tr>"
    }
    if (sh["students_vs_teachers"]&&sh["students_vs_teachers"]!="None"){
       ht += "<tr>"
       ht += "   <td align='right'>Student/Teacher<br/>"
       ht += "                      Ratio: </td>"
       ht += "   <td>" + sh["students_vs_teachers"] + "</td>"
       ht += "</tr>"
    }
    if (sh["grades"]&&sh["grades"]!="None"){
       ht += "<tr>"
       ht += "    <td align='right'>Grade: </td>"
       ht += "    <td>" + sh["grades"]+ "</td>"
       ht += "</tr>"
    }
    if (sh["score"]&&sh["score"]!="None"){
       ht += "<tr>"
       ht += "    <td align='right'>Score: </td>"
       ht += "    <td>" + sh["score"]+ "/10 </td>"
       ht += "</tr>"
    }
    if (sh["school_name_url"]){
       ht += "<tr>"
       ht += "    <td align='right'>Url: </td>"
       ht += "    <td><a href='http://schools.fizber.com/" + sh["school_name_url"] + "'>" + sh["school_name"] + "</a></td>"
       ht += "</tr>"
    }	
	
    ht += "</table>"
    ht += "</td>"
    ht += "<td></td>"
    ht += "<td>"
	if (sh["male"] && sh["male"]!="None" && sh["female"]&& sh["female"]!="None"){
	    ht += "  <table width='360px;' class='tbl3' >"
	    ht += "   <tr>"
	    ht += "      <th colspan='3'> <b>Enrollment by Gender </b> </th>"
	    ht += "   </tr>"
	    ht += " <tr>"
	    ht += " <td colspan='3'><img src='"+ sh["link_chart_gender"] +"'/></td>"
	    ht += "</tr>"
	    ht += "<tr valign='top'>"
	    ht += " <td></td>"
	    ht += " <td> Male </td>"
	    ht += " <td> Female </td>"
	    ht += "</tr>"
	    ht += "<tr valign='top'>"
	    ht += "  <td> Students </td>"
	    ht += "  <td>"+ sh["male"] +"</td>"
	    ht += "  <td>" +sh["female"]+"</td>"
	    ht += "</tr>"
	    ht += "</table>"
	}
    ht += "</td>"
    ht +=  "</tr>"
    ht += " <tr valign='top'>"
    ht += "<td>"
	
	if ((sh["others"]&&sh["others"]!="0"&&sh["others"]!="None") ||(sh["asian"]&&sh["asian"]!="0"&&sh["asian"]!="None") || (sh["black"]&&sh["black"]!="0"&&sh["black"]!="None") || (sh["white"]&&sh["white"]!="0"&&sh["white"]!="None" ) || (sh["hispanic"]&&sh["hispanic"]!="0"&&sh["hispanic"]!="None")){
    ht += "   <table width='360px' class='tbl3'  >"
    ht += "     <tr>"
    ht += "     <th colspan='5'> <b>Enrollment by Race/Ethnicity: </b> </th>"
    ht += "</tr>"
    ht += "<tr>"
    ht += "<td colspan='5'><img src='"+ sh["link_chart_race"] +"'/></td>"
    ht += "</tr>"
    ht += "<tr align='center'>"
    ht += "<td></td>"
    if (sh["amer_ind_alaskan"]){
        ht += "<td> Amer Ind/<br/>"
        ht += "      Alaskan </td>"
    } 
    if (sh["asian"]){
        ht += "<td> Asian </td>"
	}
    if (sh["black"]){
        ht += "<td> Black </td>"
    }
    if (sh["hispanic"]){
        ht += "<td> Hispanic </td>"
    }
    if (sh["white"]){
        ht += "<td> White </td>"
	}
	if (sh["others"]){
        ht += "<td> Others </td>"
	}
	ht += "</tr>"
    ht += "<tr align='center'>"
    ht += "       <td> Students </td>"
    if (sh["amer_ind_alaskan"]){
        ht += " <td>" + sh["amer_ind_alaskan"] +" </td>"
	}
    if (sh["asian"]){
        ht += " <td>" +  sh["asian"] + " </td>"
	}
    if (sh["black"]){
        ht += "<td>" + sh["black"]+ "</td>"
    }
    if (sh["hispanic"]){
        ht += "<td>" +  sh["hispanic"]+"</td>"
    }
    if (sh["white"]){
        ht += "<td>" + sh["white"] + "</td>"
	}
    if (sh["others"]){
        ht += "<td>"+  sh["others"] + "</td>"
    }
	ht += "</tr>"
    ht += "</table>"
	}
    ht += "</td>"
    ht += "<td ></td>"
    ht += "<td>"
	if ( sh["total_students"]&&sh["total_students"]!="None"&& sh["teachers"]&& sh["teachers"]!="None" ){
	    ht += "  <table width='360px;' class='tbl3' >"
	    ht += "   <tr>"
	    ht += "     <th colspan='3'> <b> Students and Teachers </b> </th>"
	    ht += "   </tr>"
	    ht += "   <tr>"
	    ht += "       <td colspan='3'><img src='"+ sh["link_chart_students"] +"'/></td>"
	    ht += "   </tr>"
	    ht += "   <tr valign='top'>"
	    ht += "     <td></td>"
	    ht += "     <td> Students </td>"
	    ht += "     <td> Teachers </td>"
	    ht += "   </tr>"
	    ht += "   <tr valign='top'>"
	    ht += "     <td> Students </td>"
		if (sh["total_students"])
	       ht += "     <td>" +  sh["total_students"]+"</td>";
		else   
	       ht += "     <td> N/A </td>";
	    ht += "     <td>" + sh["teachers"] + "</td>"
	    ht += "   </tr>"
	    ht += " </table>"
	}
    ht += "</td>"
    ht += "</tr>"
    ht += "</table>"

    document.getElementById('school_info_data').innerHTML = ht;
 
   
   
}

function viewSchoolPage(page){
	 showSchoolsMarkersPage(page);
	viewSchoolInfo(16*(page-1));
	
	ht = ""
	for (var i=(page-1)*16; i<schools.length && i < page*16; i++){
	if (i%2) {	
	ht +=  "  <div style='width:305px; background:#EAF8FF;font-size:85%;padding: 1px 0; cursor:pointer; cursor:hand; '  onmouseout='{this.style.background=\"#EAF8FF\"}' onmouseover='{this.style.background=\"#ffc\"}'>"
	}
    else {
    ht += "<div style='width:305px;font-size:85%; cursor:pointer; cursor:hand; ' onmouseout='{this.style.background=\"#FFFFFF\"}' onmouseover='{this.style.background=\"#ffc\"}' > "
	}
	
    ht +=  "<div style='float:left;overflow:hidden;width:245px;height:16px;padding: 1px 0;'> <span style='' onclick='viewSchoolInfo("+i+");return false;'>" + schools[i]["school_name"]+"</span></div>"
    ht +=  "<div style='float:left;width:55px;text-align:left;text-align:right;'>"
	if (schools[i]["total_students"]) 
	   ht += schools[i]["total_students"]
	else 
	   ht += "N/A"
   ht += "</div>"
   ht += "<div class='clear'> </div>"
   ht +=  "</div>"
	}

document.getElementById('list_schools').innerHTML = ht;
ht = ""
 for (var i=1; i<(schools.length+15)/16 ; i++){
     if (i==page){
		 ht += " <strong>" + i +"</strong>" 
	 } else {
	     ht += "<a href='#' onclick='viewSchoolPage("+ i +");return false;'>"+ i +"</a>"
	 }
  }
document.getElementById('paging2').innerHTML = ht;

	
}


function showCitiesDiv(){
	if ($('#list_cities').is(':hidden')){
		$('#list_cities').show();
		$('#list_states').hide();		
	} else {
		$('#list_cities').hide();
	}
}

function showStatesDiv(){
	if ($('#list_states').is(':hidden')){
		$('#list_states').show();
		$('#list_cities').hide();		
	} else {
		$('#list_states').hide();
	}
}
