var map;
var Icon1 = [];
var Icon2 = [];
var Icon3 = [];
var icons = [];
var marker = [];
var newMarker = [];
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var icontype;
var pIcon;
var nurseries = false;
var primary = false;
var secondary = false;

function schoolsMap(){
//	alert('set map');
    if (GBrowserIsCompatible()) {

	function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }
	  
      map = new GMap2(document.getElementById("googleMap2"));
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.setCenter(new GLatLng(aLat, aLong), 14);
//	alert('map set');
} else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function checkSchools(){
	if(document.getElementById('Nurseries').checked==true){
		nurseries = true;
	}else{
		nurseries = false;
	}
	if(document.getElementById('Primary').checked==true){
		primary = true;
	}else{
		primary = false;
	}
	if(document.getElementById('Secondary').checked==true){
		secondary = true;
	}else{
		secondary = false;
	}
	loadSchoolMarkers();
}

function loadSchoolMarkers(){
	
	map.clearOverlays();
	
	//alert('firing');
	
	//Add Property Marker to Map
	pIcon = new GIcon();
	  pIcon.image = "images/bg_marker.png";
	  pIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	  pIcon.iconSize = new GSize(34, 46);
	  pIcon.shadowSize = new GSize(37, 34);
	  pIcon.iconAnchor = new GPoint(9, 34);
	  pIcon.infoWindowAnchor = new GPoint(9, 2);
	  pIcon.infoShadowAnchor = new GPoint(18, 25);
	  pIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
	  pIcon.printImage = "images/bg_marker.png";
	  pIcon.mozPrintImage = "images/bg_marker.png";
	  pIcon.title = "Property Details";
		  
		var PropPoint = new GLatLng(PropPointlat,PropPointlng);
		PropertyMarker = createMarker(PropPoint,'Your property','<strong>Property Name</strong>',pIcon,i);
	 	PropertyMarker = new GMarker(PropPoint,pIcon);
//		alert('marker');
        GEvent.addListener(PropertyMarker, "click", function() {
          PropertyMarker.openInfoWindowHtml(html);
        });

   		map.addOverlay(PropertyMarker);

      // Create some custom icons

      // This icon uses the same shape as the default Google marker
      // So we can use its details for everything except the image
	  function makeIcons(i){

		  Icon1[i] = new GIcon();
		  Icon1[i].image = "images/marker_nursery.gif";
		  Icon1[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon1[i].iconSize = new GSize(34, 46);
		  Icon1[i].shadowSize = new GSize(37, 34);
		  Icon1[i].iconAnchor = new GPoint(9, 34);
		  Icon1[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon1[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon1[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon1[i].printImage = "images/marker_nursery.gif";
		  Icon1[i].mozPrintImage = "images/marker_nursery.gif";
		  Icon1[i].title = "Nursery";
	
		  Icon2[i] = new GIcon();
		  Icon2[i].image = "images/marker_primary.png";
		  Icon2[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon2[i].iconSize = new GSize(34, 46);
		  Icon2[i].shadowSize = new GSize(37, 34);
		  Icon2[i].iconAnchor = new GPoint(9, 34);
		  Icon2[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon2[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon2[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon2[i].printImage = "images/marker_primary.png";
		  Icon2[i].mozPrintImage = "images/marker_primary.png";
		  Icon2[i].title = "Primary";
		  
		  Icon3[i] = new GIcon();
		  Icon3[i].image = "images/marker_secondary.png";
		  Icon3[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
		  Icon3[i].iconSize = new GSize(34, 46);
		  Icon3[i].shadowSize = new GSize(37, 34);
		  Icon3[i].iconAnchor = new GPoint(9, 34);
		  Icon3[i].infoWindowAnchor = new GPoint(9, 2);
		  Icon3[i].infoShadowAnchor = new GPoint(18, 25);
		  Icon3[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
		  Icon3[i].printImage = "images/marker_secondary.png";
		  Icon3[i].mozPrintImage = "images/marker_secondary.png";
		  Icon3[i].title = "Secondary";
		
		// An array of GIcons, to make the selection easier 
		//icons[0][i] = Icon1[i];
	  	//icons[1][i] = Icon2[i];
		//alert('making Icon'+i);
	  }

      // the icon information is passed to this function
      function createMarker(point,name,html,icontype,i) {
		//alert('creating marker'+i);
       // marker = new GMarker(point,icons[icontype][i]);
	   marker[i] = new GMarker(point,icontype);
//		alert('marker');
        GEvent.addListener(marker[i], "click", function() {
          marker[i].openInfoWindowHtml(html);
        });
//		alert('click added');
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
		gmarkers[i].title = "Property Details";
        htmls[i] = html;
        // add a line to the side_bar html
        side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        
        return marker[i];
      }
	  
//	alert('load markers');
	// get variables for php xml
	//var Town = document.getElementById('Town').value;
	//var refineTypeBuy = document.getElementById('refineTypeBuy').checked;
	//var GlobalPropType = document.getElementById('GlobalPropType').value;
	//var Bedrooms = document.getElementById('Bedrooms').value;
	//var MinPrice = document.getElementById('MinPrice').value;
	//var MaxPrice = document.getElementById('MaxPrice').value;
	
//	alert(Town);
//	alert(refineTypeBuy);
//	alert(GlobalPropType);
//	alert(Bedrooms);
//	alert(MinPrice);
//	alert(MaxPrice);
	
	
//	var stringArgs = "";
//	
//	if(refineTypeBuy == true){
//		var RentalPeriod = '1';
//	}else{
//		var RentalPeriod = '4';
//	}
//	
////	alert('set vars 1');
	stringArgs = "";
//	
	stringArgs = stringArgs + '&lat='+PropPointlat+'&lng='+PropPointlng;
	if(nurseries){ stringArgs = stringArgs + '&nurseries=Y';}
	if(primary){ stringArgs = stringArgs + '&primary=Y';}
	if(secondary){ stringArgs = stringArgs + '&secondary=Y';}
	
	
//	alert(stringArgs);
	
      // Read the data from custom.xml
      var request = GXmlHttp.create();
      request.open("GET", "phpScripts/xml_schools_map.php?args"+stringArgs, true);
	  //request.open("GET", "xml_schools_map.php?args", true);
	//  alert('requesting');
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
	//	alert('getting xml');
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	//	  alert(markers.length);
          for (var i = 0; i < markers.length; i++) {
			// alert(i);
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
			//var lat = 53.391109;
            //var lng = -2.847776;
            var point = new GLatLng(lat,lng);
			var html = [];
			var name = markers[i].getAttribute("name");
			var address = markers[i].getAttribute("address");
			var average_ks3 = markers[i].getAttribute("average_ks3");
			var primary_url = markers[i].getAttribute("primary_url");
			var phase = markers[i].getAttribute("phase");
			var pupils_ks3 = markers[i].getAttribute("pupils_ks3");
			var postcode = markers[i].getAttribute("postcode");
			var average_primary = markers[i].getAttribute("average_primary");
			var age_range = markers[i].getAttribute("age_range");
			var max_age = markers[i].getAttribute("max_age");
			var min_age = markers[i].getAttribute("min_age");
			var pupils_primary = markers[i].getAttribute("pupils_primary");
			var secondary_url = markers[i].getAttribute("secondary_url");
			var special = markers[i].getAttribute("special");
			var pupils_secondary = markers[i].getAttribute("pupils_secondary");
			var post16_url = markers[i].getAttribute("post16_url");
			var ofsted_url = markers[i].getAttribute("ofsted_url");
			var ks3_url = markers[i].getAttribute("ks3_url");
			var pupils_post16 = markers[i].getAttribute("pupils_post16");
			var average_secondary = markers[i].getAttribute("average_secondary");
			var average_post16 = markers[i].getAttribute("average_post16");
			var schoolID = markers[i].getAttribute("schoolID");
			html[i] = '<div style="width:300px; height:240px; overflow:auto; background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000; padding:5px;" >';
			html[i] += '<div style="width:250px; margin:5px 0 5px 0; float:left;">'+phase+'<br /><strong>'+name+'</strong><br /><br />';
			html[i] += ''+address+'<br />'+postcode+'<br /><br />';
			html[i] += '<strong>Age Range :</strong>'+age_range+'<br />';
			html[i] += '<strong>Max Age Admitted :</strong>'+max_age+'<br />';
			html[i] += '<strong>Min Age Admitted :</strong>'+min_age+'<br />';
			html[i] += '<strong>No. Pupils (Primary):</strong>'+pupils_primary+'<br />';
			html[i] += '<strong>No. of Key Stage 3 Pupils :</strong>'+pupils_ks3+'<br />';
			html[i] += '<strong>No. Pupils (Secondary):</strong>'+pupils_secondary+'<br />';
			html[i] += '<strong>No. Pupils (GCE):</strong>'+pupils_post16+'<br />';
			html[i] += '<br /><strong style="color:red;">Performance </strong><br />';
			html[i] += '<strong>Primary Score :</strong>'+average_primary+'<br />';
			html[i] += '<strong>GCSE Score :</strong>'+average_secondary+'<br />';
			html[i] += '<strong>GCE Score :</strong>'+average_post16+'<br />';
			html[i] += '<br /><strong style="color:red;">Official Reports </strong><br />';
			html[i] += '<strong><a href="http://'+ks3_url+'" target="_blank">Key Stage 3 Performance</a></strong><br />';
			html[i] += '<strong><a href="http://'+secondary_url+'" target="_blank">GCSE Performance</a></strong><br />';
			html[i] += '<strong><a href="http://'+post16_url+'" target="_blank">GCE Performance</a></strong><br />';
			html[i] += '<strong><a href="http://'+ofsted_url+'" target="_blank">OFSTED Report</a></strong><br />';

			html[i] += '</div>';
			html[i] += '</div>';
            // create the marker
//			alert('\npoint\n'+point+'\nlabel\n'+label+'\nhtml\n'+html[i]+'\nicontype\n'+icontype);
			makeIcons(i);
			if(phase=='Nursery'){
            	icontype = Icon1[i];
			}if(phase=='Primary'){
				icontype = Icon2[i];
			}if(phase=='Secondary'){
				icontype = Icon3[i];
			}
			var label = name;
			//alert('set icon'+i);
            newMarker[i] = createMarker(point,label,html[i],icontype,i);
            map.addOverlay(newMarker[i]);
          }
        }
      }
      request.send(null);
}
