// JavaScript Document
/*GLOBALS*/
	/*******************/
	var LANGUAGE; //?lang=
	var CITY; //?city=
	var COURT; //?court=
	var XMLFILE = "xml/csdfacilities_webmaster.xml";
	var PROFILEDIR = "profiles/";
	var IMAGEEXT = ".jpg";
	var PARKINGICON = "icons/acc_parking.jpg";
	var ENTRYICON = "icons/acc_entry.jpg";
	var WCICON = "icons/acc_wc.jpg";
	var ALSICON = "icons/acc_als.jpg";
	var DIRECTIONS = "dir.asp";
	var FOFLAGICON = "icons/foflag.jpg";

	var ACCESSEXT = ".pdf";
	
	var CITYSELECT=0;
	//var OFFICESELECT=0;
	
	var Global_side_bar_html;
    var Global_gmarkers;
    var Global_htmls;
    var Global_i;
	var Global_points;
	var Global_lasti;
	
	var iconONT = new GIcon();
		iconONT.image = "http://maps.google.com/mapfiles/kml/pal2/icon10.png";
		iconONT.shadow = "http://www.google.com/mapfiles/shadow50.png";
		iconONT.iconSize = new GSize(40, 40);
		iconONT.shadowSize = new GSize(30, 45);
		iconONT.iconAnchor = new GPoint(15, 15);
		iconONT.infoWindowAnchor = new GPoint(17, 2);
		
	/*functions*/
	/*******************/
	function createInfoMarker(point, info, icon, name,num) {
	   var marker = new GMarker(point, icon);
       GEvent.addListener(marker, "click",
          function() {
			 myclick(num);
          }
       );

//TO ADD FOR PRINTING

GEvent.addListener(marker, "infowindowopen", function() {
       	document.getElementById("printInfoBubble").innerHTML = info;
        });

//END OF TO ADD FOR PRINTING

	   Global_gmarkers[Global_i] = marker;
      Global_htmls[Global_i] = info;
	   Global_points[Global_i] = point;
	   Global_side_bar_html += '<a id="'+Global_i+'" onmouseover="map_listings_highlight('+Global_i+');" onmouseout="map_listings_unhighlight('+Global_i+');" href="javascript:myclick(' + Global_i + ')" class="listtag">' + name + '</a><br>';

       Global_i++;

	   return marker;
    }

	function myclick(i) {
        if (Global_lasti!=-1){
			document.getElementById(Global_lasti).style.color = "#666666";
			//alert("to grey");
		}
		//alert("not to grey");
		Global_gmarkers[i].openInfoWindowHtml(Global_htmls[i]);
		document.getElementById(i).style.color = "#0000FF";
		Global_lasti = i;
    }
	
	function map_listings_highlight(id){
			document.getElementById(id).style.color = "#0000FF";
	}
	
	function map_listings_unhighlight(id){
		if (id!=Global_lasti){
			document.getElementById(id).style.color = "#666666";
		}
	}

	  
function getInfoString(court_id,name,address,city,province,postal_code,civil_scj,crim_y_ocj,crim_scj,crim_ocj,div_scj,enf_scj,fam_scj,fam_ocj,scc_scj,coa,mailing_court,cld_tel,cld_fax,ovss_tel,ovss_fax,ovss_tollfree,ovss_notes,acc_parking,acc_entry,acc_wc,acc_als,lat,lng,notes,access_tel,flsa,cja) {
	var htmlinfo = '<table width="375" border="0" cellspacing="0" cellpadding="0">';
    htmlinfo += '<tr><td width="160" align="left" valign="top"><img src="'+ PROFILEDIR + court_id + IMAGEEXT +'" height="100" style="border:1px solid black;" /></td>';
    htmlinfo += '<td width="233" align="left" valign="top"><span class="courtname">'+name+'</span><br />';
    htmlinfo += address + '<br />';
    htmlinfo += city + ' ' + province + ' ' + postal_code + '<br />';

	//accessibility icons
	if (acc_parking==1 || acc_entry==1 || acc_wc==1 || acc_als==1){
		htmlinfo += '<br>';//'<span class="barrierfree">'+gis_access+'</span><br>';
		htmlinfo += '<img src="'+ENTRYICON+'" height="30" title="'+gacc_entry+'" />&nbsp;&nbsp;&nbsp;<br>';
		htmlinfo += '<a href="'+ PROFILEDIR + court_id + '_access_' + LANGUAGE + ACCESSEXT +'" class="moreinfo" target="_blank">'+g_more+'</a><br> (<em>'+g_access_button+'</em>)<br>';
	}
	/*if (acc_parking==1){
		htmlinfo += '<img src="'+PARKINGICON+'" height="30" title="'+gacc_parking+'"/>&nbsp;&nbsp;&nbsp;';
	}
	if (acc_entry==1){
		htmlinfo += '<img src="'+ENTRYICON+'" height="30" title="'+gacc_entry+'" />&nbsp;&nbsp;&nbsp;';
	}
	if (acc_wc==1){
		htmlinfo += '<img src="'+WCICON+'" height="30" title="'+gacc_wc+'" />&nbsp;&nbsp;&nbsp;';
	}
	if (acc_als==1){
		htmlinfo += '<img src="'+ALSICON+'" height="30" title="'+gacc_als+'" />&nbsp;&nbsp;&nbsp;';
	}
	if (acc_parking==1 || acc_entry==1 || acc_wc==1 || acc_als==1){
		htmlinfo += '<a href="'+ PROFILEDIR + court_id + '_access_' + LANGUAGE + ACCESSEXT +'" class="moreinfo" target="">'+g_more+'</a><br>';
	}*/

	htmlinfo += '</td></tr><tr><td colspan="2" align="left"><br>';
	
	//recently added notes section
	if (notes!=""){
		htmlinfo += '<span style="color:red;">'+notes+'</span><br /><br />';
	}
	
	//recently added flsa, cja section
	if (flsa!="" || cja!=""){
		var temptable = '<table border="0" cellspacing="0" cellpadding="0"><tr><td align="left" valign="top" width="33px;"><img src="'+FOFLAGICON+'" />&nbsp;</td><td align="left" valign="top">';
		if (flsa!="" && cja!=""){
			temptable += '<span class="officename">'+gflsacja+'</span><br>';
		}
		else if (cja!=""){
			temptable += '<span class="officename">'+gcja+'</span><br>';	
		} else {
			temptable += '<span class="officename">'+gflsa+'</span><br>';
		}
		temptable += '<br></td></tr></table>';
	
	htmlinfo += temptable;
	}

	if (civil_scj!=""){
		htmlinfo += '<span class="officename">'+gcivil_scj+'</span> tel. ' + civil_scj + '<br />';
	}
	if (crim_y_ocj!=""){
		htmlinfo += '<span class="officename">'+gcrim_y_ocj+'</span> tel. ' + crim_y_ocj + '<br />';
	}
	if (crim_scj!=""){
		htmlinfo += '<span class="officename">'+gcrim_scj+'</span> tel. ' + crim_scj + '<br />';
	}
	if (crim_ocj!=""){
		htmlinfo += '<span class="officename">'+gcrim_ocj+'</span> tel. ' + crim_ocj + '<br />';
	}
	if (div_scj!=""){
		htmlinfo += '<span class="officename">'+gdiv_scj+'</span> tel. ' + div_scj + '<br />';
	}
	if (enf_scj!=""){
		htmlinfo += '<span class="officename">'+genf_scj+'</span> tel. ' + enf_scj + '<br />';
	}
	if (fam_scj!=""){
		htmlinfo += '<span class="officename">'+gfam_scj+'</span> tel. ' + fam_scj + '<br />';
	}
	if (fam_ocj!=""){
		htmlinfo += '<span class="officename">'+gfam_ocj+'</span> tel. ' + fam_ocj + '<br />';
	}
	if (scc_scj!=""){
		htmlinfo += '<span class="officename">'+gscc_scj+'</span> tel. ' + scc_scj + '<br />';
	}
	if (coa!=""){
		htmlinfo += '<span class="officename">'+gcoa+'</span> tel. ' + coa + '<br />';
	}
	
	var cldcheck = 0;
	if (cld_tel!=""){
		htmlinfo += '<span class="officename">'+gcld_tel+'</span> tel. ' + cld_tel + '<br />';
		cldcheck = 1;
	}
	if (cld_fax!=""){
		var spanstyle = "officename";
		if (cldcheck == 1){
			spanstyle = "officenamewhite";
		}
		htmlinfo += '<span class="'+spanstyle+'">'+gcld_fax+'</span> fax. ' + cld_fax + '<br />';
	}
	
	var ovsscheck = 0;
	if (ovss_tel!=""){
		htmlinfo += '<span class="officename">'+govss_tel+'</span> tel. ' + ovss_tel + '<br />';
		ovsscheck = 1;
	}
	if (ovss_fax!=""){
		var spanstyle;
		if (ovsscheck==1){ // ovss title was previously written, do no duplicate
			spanstyle = "officenamewhite";
		} else { // ovss title was not previously written, write the title, and change bit
			spanstyle = "officename";
			ovsscheck = 1;	
		}
		htmlinfo += '<span class="'+spanstyle+'">'+govss_fax+'</span> fax. ' + ovss_fax + '<br />';
			
	}
	if (ovss_tollfree!=""){
		var spanstyle;
		if (ovsscheck==1){ // ovss title was previously written, do no duplicate
			spanstyle = "officenamewhite";
		} else {// ovss title was not previously written, write the title
			spanstyle = "officename";
		}
		htmlinfo += '<span class="'+spanstyle+'">'+govss_tollfree+'</span> tollfree. ' + ovss_tollfree + '<br />';
	}
	
	if (access_tel!=""){
		htmlinfo += '<span class="officename">'+gaccess_tel+'</span> tel. ' + access_tel + '<br />';
	}
	
	htmlinfo += '<a href="'+DIRECTIONS+'?lat='+lat+'&lng='+lng+'&name='+name+'&address='+address+'&city='+city+'&province='+province+'&lang='+LANGUAGE+'" target="_blank">'+g_dir+'</a><br />';
	
	htmlinfo += '</td></tr></table><br /><br /><br />'; /* added three br so fits in bubble */

	return htmlinfo;

}


function load(lang,city_id,office_id,court_id){ 
	Global_side_bar_html = "";
    Global_gmarkers = [];
    Global_htmls = [];
    Global_i = 0;
	Global_points = [];
	Global_lasti = -1;
	
	if (GBrowserIsCompatible()) {
        
		
		var xhr; 
		try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
		catch (e) 
		{
			try {   xhr = new ActiveXObject('Microsoft.XMLHTTP');    }
			catch (e2) 
			{
			  try {  xhr = new XMLHttpRequest();     }
			  catch (e3) {  xhr = false;   }
			}
		 }
	  
		xhr.onreadystatechange  = function()
		{ 
			 if(xhr.readyState == 4)
			 {
				if(xhr.status  == 200) {
					var doc = xhr.responseXML;   // Assign the XML file to a var
					var courts = doc.getElementsByTagName('court');
					var index = -1;
					var LAT;
					var LNG;
					
					//BEGIN LOAD SEQUENCE LOGIC
					
					//
					//1) get specific court information if the city_id variable is not "-1"
					//
					
					if (court_id!=-1){ //if the court_id variable was specified in the URL, get the city to which this single court belongs
						for (var i = 0; i <= courts.length-1; i++) { 	
							if(courts[i].getAttribute("court_id")==court_id){//for this single court...
								city_id = courts[i].getAttribute("city_id"); //find its city
								LAT = courts[i].getAttribute("lat"); //record this court's location
								LNG = courts[i].getAttribute("lng");							
								break; //break for loop as court has been found
							 } //end if
						 }//end for
					}//end if
					
					
					//
					//2) load the city select and use city_id as the selected city
					//
					
					if (CITYSELECT==0){ 
						var opts = [];
						var vals = [];
						var tempval;
						var tempopt;
						for (var i=0;i<=courts.length-1;i++){
							tempval = courts[i].getAttribute("city_id");
							tempopt = courts[i].getAttribute("city");
							if (!valueExists(vals,tempval)){
								vals[vals.length] = tempval;
								opts[opts.length] = tempopt;
							}
						}
						sortOptions(opts,vals);
						writeSelect("city_select",opts,vals,city_id);
						CITYSELECT = 1; //flag this so the select is not written again
					}

					//
					//3) get all offices for this city_id if office_id is "0"
					//
					
					if (office_id==0){
						//re-init global array
						//resetGlobalArray();
						var temparray = [];
						temparray[0] = 0;
						for (var i=0;i<=courts.length-1;i++){
							if(courts[i].getAttribute("city_id")==city_id){// for this court...
								recordOffices(temparray,courts[i].getAttribute("civil_scj"),courts[i].getAttribute("crim_y_ocj"),courts[i].getAttribute("crim_scj"),courts[i].getAttribute("crim_ocj"),courts[i].getAttribute("div_scj"),courts[i].getAttribute("enf_scj"),courts[i].getAttribute("fam_scj"),courts[i].getAttribute("fam_ocj"),courts[i].getAttribute("scc_scj"),courts[i].getAttribute("coa"));
							}//end if 
						}//end for
						
						var tempvals = getUniqueValues(temparray);
						sortArray(tempvals);
						var tempopts = getOfficeOptions(LANGUAGE,tempvals);
						
						clearSelectOptions("office_select");
						writeSelect("office_select",tempopts,tempvals,tempvals[0]);
					
					} //end if office_id==0
					
					//
					//4) get all courts for this city_id and office_id
					//
					
					for (var i = 0; i <= courts.length-1; i++) { 	
						if(courts[i].getAttribute("city_id")==city_id){// for this city...
							if(checkOffice(office_id,courts[i].getAttribute("civil_scj"),courts[i].getAttribute("crim_y_ocj"),courts[i].getAttribute("crim_scj"),courts[i].getAttribute("crim_ocj"),courts[i].getAttribute("div_scj"),courts[i].getAttribute("enf_scj"),courts[i].getAttribute("fam_scj"),courts[i].getAttribute("fam_ocj"),courts[i].getAttribute("scc_scj"),courts[i].getAttribute("coa"))){
								
								var point = new GLatLng(parseFloat(courts[i].getAttribute("lat")), parseFloat(courts[i].getAttribute("lng")));
								
								var tempaddress;
								var tempcourtname;
								var tempnotes;
								if (LANGUAGE=="fr"){
									tempaddress = courts[i].getAttribute("address_fr");
									tempcourtname = courts[i].getAttribute("name_fr");
									tempnotes = courts[i].getAttribute("notes_fr");
								} else {
									tempaddress = courts[i].getAttribute("address");
									tempcourtname = courts[i].getAttribute("name");
									tempnotes = courts[i].getAttribute("notes_en");
								}
								
								var htmlinfo = getInfoString(courts[i].getAttribute("court_id"),tempcourtname,tempaddress,courts[i].getAttribute("city"),"ON",courts[i].getAttribute("postal_code"),courts[i].getAttribute("civil_scj"),courts[i].getAttribute("crim_y_ocj"),courts[i].getAttribute("crim_scj"),courts[i].getAttribute("crim_ocj"),courts[i].getAttribute("div_scj"),courts[i].getAttribute("enf_scj"),courts[i].getAttribute("fam_scj"),courts[i].getAttribute("fam_ocj"),courts[i].getAttribute("scc_scj"),courts[i].getAttribute("coa"),courts[i].getAttribute("mailing_court"),courts[i].getAttribute("cld_tel"),courts[i].getAttribute("cld_fax"),courts[i].getAttribute("ovss_tel"),courts[i].getAttribute("ovss_fax"),courts[i].getAttribute("ovss_tollfree"),courts[i].getAttribute("ovss_notes"),courts[i].getAttribute("acc_parking"),courts[i].getAttribute("acc_entry"),courts[i].getAttribute("acc_wc"),courts[i].getAttribute("acc_als"),courts[i].getAttribute("lat"),courts[i].getAttribute("lng"),tempnotes,courts[i].getAttribute("access_tel"),courts[i].getAttribute("flsa"),courts[i].getAttribute("cja"));
								var marker; 
								
								if (courts[i].getAttribute("court_id")==court_id){index = Global_i;}
								
								//create an info marker for this court
								//this function also writes these objects for global use
								createInfoMarker(point,htmlinfo,iconONT,tempaddress,Global_i);
								
							}//end if
						 } //end if
					 }//end for
					 
					// 
					//4b) create the map
					//
					
					var map = new GMap2(document.getElementById("map"));
					map.setCenter(new GLatLng(43.7626330666747, -79.4081497192382), 11);
					map.addControl(new GLargeMapControl());
					//map.addControl(new GMapTypeControl());
					var bounds = new GLatLngBounds();
					
					var numcourts = Global_gmarkers.length;
					
					for (var i=0;i<=numcourts-1;i++){
						map.addOverlay(Global_gmarkers[i]);
						bounds.extend(Global_points[i]);
					}
					
					//
					//5) write elements to list box
					//
					
					document.getElementById("listbox").innerHTML = Global_side_bar_html;
					
					//
					//6) zoom to courts, or zoom to individual court if court_id is not "-1"
					// or if a single court exists for a city, launch its html bubble via myclick
					//
					
					if (index!=-1){
						map.setCenter(new GLatLng(LAT,LNG), 16);
						myclick(index);
					} else {
					  map.setZoom(map.getBoundsZoomLevel(bounds));
					  map.setCenter(bounds.getCenter());
					  if (Global_gmarkers.length==1){
					  	myclick(0);
						//map_listings_highlight(0);
					  } //end if 
					} //end if
				
				  } else { 
					 alert("Error code " + xhr.status);
				  } //end if ==200
			 } //end if ==4
		} //end function()
	
	   xhr.open("GET", XMLFILE,  true); 
	   xhr.send(null); 
	} //end if GBrowserCompat 
}// end load()