
	function retrieveComputedStyle(element,styleProperty){
		var computedStyle=null;
		if(typeof element.currentStyle != "undefined"){
			computedStyle=element.currentStyle;
		}else{
			computedStyle=document.defaultView.getComputedStyle(element,null);
		}
		return computedStyle[styleProperty];
	}

	var geo=new GClientGeocoder();
	var reasons=[];
	var side_bar_html="";
	var gmarkers=[];
	var htmls=[];
	var i=0;
	// arrays to hold variants of the info window html with get direction forms open
	var to_htmls=[];
	var from_htmls=[];

	// ====== Array for decoding the failure codes ======
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";


	function place(lat,lng){
		var point=new GLatLng(lat,lng);
		map.setCenter(point,14);
		var marker=createDirectionMarker(point,'','');
		map.addOverlay(marker);
	}
	function createDirectionMarker(point,name,html){
        var marker=new GMarker(point);

        // The info window version with the "to here" form open
        to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
           '<br>Start address:<form action="javascript:getDirections()">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
           '"/>';
        // The info window version with the "to here" form open
        from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
           '<br>End address:<form action="javascript:getDirections()">' +
           '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
           '"/>';
        // The inactive version of the direction info
        html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';

        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        htmls[i] = html;
        // add a line to the side_bar html
        side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        i++;
        return marker;
      }
	// ===== request the directions =====
      function getDirections() {
		document.getElementById('searchres').innerHTML='';
        var saddr = document.getElementById("saddr").value
        var daddr = document.getElementById("daddr").value
        gdir.load("from: "+saddr+" to: "+daddr);
      }


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }

	  // === create a GDirections Object ===


	// === catch Directions errors ===
      GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        }

        alert("Failed to obtain directions, "+reason);
      });

	function mapexpand(itemID){
			if(navigator.appVersion.indexOf("MSIE")!=-1){
				if(document.getElementById(itemID).style.display=='block'){
					document.getElementById(itemID).style.display='none';
				}else{
					document.getElementById(itemID).style.display='block';
				}
			}else{
				if(document.getElementById(itemID).style.display=='table-row'){
					document.getElementById(itemID).style.display='none';
				}else{
					document.getElementById(itemID).style.display='table-row';
				}
			}
		}

	function showAddress(address){
		if(address=='Address, or location ...' || address=='Type an address, or location ...'){ address=''; }

		address=address.replace(/Australia/gi,'');
		address=address+' Australia';

		searchcount++;
		pastsearch[searchcount]="showAddress('"+address+"')";
		window.location.hash='search:'+searchcount;
		geo.getLocations(address,function(result){
			map.clearOverlays();
			if(result.Status.code == G_GEO_SUCCESS) {
				// ===== If there was more than one result, "ask did you mean" on them all =====
				if (result.Placemark.length > 1) {
					document.getElementById("searchres").innerHTML = "<h3>Help Us Out ...</h3><p>We found more than one location for <b>"+address+"</b>.  Did you mean:</p><ul style=\"list-style:none;\">";
					// Loop through the results
					for (var i=0; i<result.Placemark.length; i++) {
						var p = result.Placemark[i].Point.coordinates;
						document.getElementById("searchres").innerHTML += "<li>"+(i+1)+": <a href=\"javascript:showAddress('" +result.Placemark[i].address+"')\">"+ result.Placemark[i].address+"</a></li>";
					}
					document.getElementById("searchres").innerHTML += "</ul><p>Simply click the address that best matches what you are looking for.</p><p>If you can't find the right address in the list above, please check the details and conduct another search.  If possible, provide more details to your search query.</p>";
				}
				// ===== If there was a single marker =====
				else {
					document.getElementById("searchres").innerHTML = "";
					var p = result.Placemark[0].Point.coordinates;
					place(p[1],p[0]);
					findSurrounding(p[1],p[0]);
				}
			}
			// ====== Decode the error status ======
			else {
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				}
				alert('Could not find "'+address+ '" ' + reason);
			}
		});
	}
	function searchJobs(pID,address){
		if(address=='Where ...'){
			alert('Please specify an address/location.');
		}else{
			professionID=pID;
			showAddress(address);
		}
	}

	function addaddress(){
		var address=document.getElementById('number').value+' '+
			document.getElementById('street').value+' '+
			document.getElementById('streettype').value+', '+
			document.getElementById('suburb').value+', '+
			document.getElementById('state').value+', '+
			document.getElementById('postcode').value+', '+
			document.getElementById('country').value;
		showAddress(address);
	}

	function createRequestObject(){
		var request_o;
		var browser=navigator.appName;
		if(browser=="Microsoft Internet Explorer"){
			request_o=new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			request_o=new XMLHttpRequest();
		}
		return request_o;
	}

	function createMarker(point,index,color){
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
		var icon = new GIcon(baseIcon);
		icon.image = "/img/map/marker" + letter + "_"+color+".png";
		var marker = new GMarker(point, icon);

		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml("Marker <b>" + letter + "<\/b>");
		});
		return marker;
	}

	function findSurrounding(lat,long){
    	var nearbyoutput='';

		if(professionID>0){

		}else{
			professionID='';
		}

		/* alert('mapsearchv1.php?action=getlocal&lat='+lat+'&long='+long+'&professionID='+professionID+'&jobTypeID=&state='); */

		var http=createRequestObject();
		http.open('get','/job/mapsearchv1.php?action=getlocal&lat='+lat+'&long='+long+'&professionID='+professionID+'&jobTypeID=&state=');
		http.onreadystatechange=function(){
	    	if(http.readyState==1){
		    	document.getElementById('searchres').innerHTML='<table style="height:100%;width:100%;"><tr><td align="center" style="color:#92a5c4;font:12pt Arial;"><img src="/images/loading/j4cloadingv1.gif" /><br /><br />LOADING ...</td></tr></table>';
	    	}
	    	if(http.readyState==4){
				var baseIcon=new GIcon();
				baseIcon.shadow="http://www.google.com/mapfiles/shadow50.png";
				baseIcon.iconSize=new GSize(20, 34);
				baseIcon.shadowSize=new GSize(37, 34);
				baseIcon.iconAnchor=new GPoint(9, 34);
				baseIcon.infoWindowAnchor=new GPoint(9, 2);
				baseIcon.infoShadowAnchor=new GPoint(18, 25);

		    	var jobs=new Array();
		    	eval(http.responseText);

				var nearby=document.getElementById('searchres');

				if(jobs.length>0){
			    	nearbyoutput='<table cellspacing="0">';
			    	var markerArray=new Array();
			    	var samedist='';
			    	for(var i=0;i<jobs.length;i++){
				    	if(jobs[i][8]==1){
					    	if(samedist!=''){
						    	samedist='';
						    	nearbyoutput+='</table></td></tr>';
					    	}
				    		eval("var point=new GLatLng("+jobs[i][1]+","+jobs[i][2]+")");
					    	var letter=String.fromCharCode("A".charCodeAt(0)+i);
							var icon=new GIcon(baseIcon);
							icon.image="/images/map/marker"+letter+"_blue.png";

							var html="<table width='300'><tr valign='top'><td><\/td><td><font style='font:bold 10pt Arial;'>"+jobs[i][3]+":<\/font><br \/><br \/><table style='background:rgb(255,204,132);width:100%;'><tr valign='top'><td width='100'><b>Role Type:</b></td><td>"+jobs[i][6]+"</td></tr><tr valign='top'><td><b>Location:</b></td><td>"+jobs[i][7]+"</td></tr></table><table style='border-bottom:rgb(150,150,150) 1px solid;width:100%;'><tr><td><b>Job Overview:</b></td></tr></table>"+jobs[i][5]+"<br \/><br \/><\/td><\/tr><tr><td colspan='2' style='background:rgb(255,204,132);'><b>Options:<\/b><br \/><table><tr><td><img src='/images/icons/pages/page_white.png' /></td><td><a href='"+jobs[i][4]+"'>View This Job<\/a></td></tr><tr><td><img src='/images/icons/email_edit.png' /></td><td><a href='/job/apply.php?id="+jobs[i][10]+"'>Apply For This Job<\/a></td></tr><tr><td><img src='/images/icons/find.png' /></td><td><a href='"+jobs[i][9]+"'>Find Similar<\/a></td></tr></table><\/td><\/tr><\/table><br>Directions: <b>To here</b><br>Please enter starting address:<form action='javascript:getDirections()'><input type='text' SIZE=40 MAXLENGTH=40 name='saddr' id='saddr' value='' /><br><INPUT value='Get Directions' TYPE='SUBMIT'><input type='hidden' id='daddr' value='"+jobs[i][3]+"@"+ jobs[i][1] + "," + jobs[i][2] +"' />";

							eval("marker["+i+"]=new GMarker(point,icon);");
							eval("GEvent.addListener(marker["+i+"],'click',function(){marker["+i+"].openInfoWindowHtml(\""+html+"\");});");
							eval("map.addOverlay(marker["+i+"]);");

							distance='';
					        nearbyoutput+='<tr id="job:'+i+'" valign="top" onmouseover="this.style.background=\'rgb(190,200,255)\';" onmouseout="this.style.background=\'transparent\';">'+
					        	'<td width="50"><img alt="" src="'+icon.image+'" \/><\/td>'+
					        	'<td><a href="'+jobs[i][4]+'"><b>'+jobs[i][3]+'</b><\/a><br \/><font class="subhead" style="font-size:8pt;">'+jobs[i][6]+'</font><br \/><b>'+jobs[i][7]+'</b><br \/><i>Distance: '+jobs[i][0]+' km</i><br />'+
					        	'<a href="javascript:void(map.setCenter(new GLatLng('+jobs[i][1]+','+jobs[i][2]+'),15));">Show On Map</a><br \/><br \/>'+
					        	'<\/td><\/tr>';
						}else{
					        if(samedist==jobs[i][0]){
						        distance='';
					        	nearbyoutput+='<tr valign="top" onmouseover="this.style.background=\'rgb(190,200,255)\';" onmouseout="this.style.background=\'transparent\';">'+
					        		'<td><a href="'+jobs[i][4]+'"><b>'+jobs[i][3]+'</b><\/a><br \/><font class="subhead" style="font-size:8pt;">'+jobs[i][6]+'</font><br \/><b>'+jobs[i][7]+'</b><br \/><i>Distance: '+jobs[i][0]+' km</i><br \/><br \/><\/td><\/tr>';
					        }else{
						        if(samedist!=''){
						    		nearbyoutput+='</table></td></tr>';
					    		}
						        samedist=jobs[i][0];
						    	eval("var point=new GLatLng("+jobs[i][1]+","+jobs[i][2]+")");
						    	var letter=String.fromCharCode("A".charCodeAt(0)+i);
								var icon=new GIcon(baseIcon);
								icon.image="/images/map/marker"+letter+"_green.png";

								eval("marker["+i+"]=new GMarker(point,icon);");
								eval("GEvent.addListener(marker["+i+"],'click',function(){marker"+i+".openInfoWindowHtml('<table width=\"300\"><tr valign=\"top\"><td><\/td><td><b>There are "+jobs[i][8]+" jobs advertised in this region:<\/b><br \/><br \/>"+jobs[i][7]+"<br \/><\/td><\/tr><tr><td colspan=\"2\"><b>Options:<\/b><br \/><a href=\""+jobs[i][9]+"\">View These Jobs<\/a><br \/><\/td><\/tr><\/table>');})");
								eval("map.addOverlay(marker["+i+"]);");

								distance='';
						        nearbyoutput+='<tr valign="top" onmouseover="this.style.background=\'rgb(190,200,255)\';" onmouseout="this.style.background=\'rgb(255,204,132)\';" style="background:rgb(255,204,132);">'+
						        	'<td colspan="2">'+
						        	'<div style="background:url(/images/btn_bg.gif);background-repeat:repeat-x;color:rgb(255,255,255);height:25px;font:bold 10pt Arial;padding-left:5px;">'+jobs[i][8]+' Jobs In '+jobs[i][7]+'</div>'+
						        	'<table>'+
						        		'<tr valign="top">'+
						        			'<td><img alt="" src="'+icon.image+'" \/></td>'+
						        			'<td><i>Distance: '+jobs[i][0]+' km</i><br \/>'+jobs[i][8]+' jobs have been listed for this location.  For further information, please choose one of the following:<br \/>'+
						        				'<table><tr><td><img src="/images/icons/page_find.png" /></td><a href="'+jobs[i][9]+'">Search These Listings<\/a></td></tr><tr><td><img src="/images/nav/plus.gif" /></td><td><a href="javascript:void(mapexpand(\'dist:'+(jobs[i][0].replace('.',''))+'sub\'));">Show These Listings</a></td></tr></table>'+
						        				'<br \/>'+
						        				'<br \/>'+
						        			'</td>'+
						        		'</tr>'+
						        	'</table>'+
						        	'</td></tr>'+
						        	'<tr id="dist:'+(jobs[i][0].replace('.',''))+'sub" style="display:none;"><td colspan="2" style="background:rgb(255,204,132);">'+
						        		'<table cellspacing="0" style="width:100%;">'+
						        			'<tr valign="top" onmouseover="this.style.background=\'rgb(190,200,255)\';" onmouseout="this.style.background=\'transparent\';">'+
					        					'<td><a href="'+jobs[i][4]+'"><b>'+jobs[i][3]+'</b><\/a><br \/><font class="subhead" style="font-size:8pt;">'+jobs[i][6]+'</font><br \/><b>'+jobs[i][7]+'</b><br \/><i>Distance: '+jobs[i][0]+' km</i><br \/><br \/><\/td>'+
					        				'<\/tr>';
				        	}
				        }
			    	}
			    	nearbyoutput+='<\/table>';
				}else{
					nearby='Found Nothing!!';
				}
				document.getElementById('searchres').innerHTML=nearbyoutput;
			}
		}
		http.send(null);
	}

