// JavaScript Document

//<![CDATA[

var marker;
var point;
	var map = null;
    var geocoder = null;
	var directions;
	var dirdiv = null;

    function load() {
      if (GBrowserIsCompatible()) {
		  
        map = new GMap2(document.getElementById("map"));
        //dirdiv = document.getElementById("dir");
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
       // map.setCenter(new GLatLng(-33.48, 150.431), 8);
        geocoder = new GClientGeocoder();
		//directions = new GDirections(map,dirdiv);
		directions = new GDirections(map);
	  directions.load("from: Muswellbrook NSW Australia to: Mudgee NSW Australia to: Lithgow NSW Australia to: -33.82159,150.020513 to: Oberon NSW Australia to: Taralga NSW Australia to: Goulburn NSW Australia to: Collector NSW Australia to: Canberra NSW Australia");


		if(document.getElementById("mapTown").selectedIndex != 0){

			var t = setTimeout('showAddress(document.getElementById("mapTown")[document.getElementById("mapTown").selectedIndex].value,document.getElementById("mapTown")[document.getElementById("mapTown").selectedIndex].value,document.getElementById("mapTown")[document.getElementById("mapTown").selectedIndex].text,"")',2000);
			}
		if(document.getElementById("searchLoc").value != ""){
			showAddress(document.getElementById("searchLoc").value,document.getElementById("searchLoc").value,'','')
			}
		if(document.getElementById("searchBus").value != ""){
			ajaxFunction();
			document.getElementById('mapQuickSearchResults').style.visibility='visible';
			//document.getElementById("mapSearch").submit();
		}
      }
	  else{
		  alert("Browser is not compatible for Google Maps!")
	  }
    }

    function showAddress(town,address,bname,image,w,h,id,gltlng) {
      if (geocoder) {
		if (gltlng == ""){
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
				if(town != ""){
				showAddress('',town,bname,image,w,h,id,gltlng)
				}else{
				  alert(address + " not found");
				}
            } else {
              map.setCenter(point, 13);
              marker = new GMarker(point);
              map.addOverlay(marker);
			  if(town != ""){
				  if(image != ""){
					  marker.openInfoWindowHtml("<div style='text-align:center;'><a href='business.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>" + address +"</div>");
				  }else{
					  marker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='business.asp?ID="+id+"'>"+bname+"</a></b><br>" + address +"</div>");
				  }
			  }else{
				  if(image != ""){
					  marker.openInfoWindowHtml("<div style='text-align:center;'><a href='business.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>Address unavailable - " + address +"</div>");
				  }else{
					  marker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='business.asp?ID="+id+"'>"+bname+"</a></b><br>Address unavailable - " + address +"</div>");
				  }
				  }
            }
          }
        );
		}else{
			 var glat1;
			 var glng1;
			 if(gltlng != undefined){
				 for(var i=1; i<=gltlng.length; i++){
					 if(gltlng.substring(i-1,i) == ",")
						{
							glat1 = gltlng.substring(0,i-1);
							glng1 = gltlng.substring(i,gltlng.length);
						}
					 }
				 var point = new GLatLng(parseFloat(glat1),
								parseFloat(glng1));
				  map.setCenter(point, 13);
				  marker = new GMarker(point);
				  map.addOverlay(marker);
					  if(town != ""){
						  if(image != ""){
							  marker.openInfoWindowHtml("<div style='text-align:center;'><a href='business.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>" + address +"</div>");
						  }else{
							  marker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='business.asp?ID="+id+"'>"+bname+"</a></b><br>" + address +"</div>");
						  }
					  }else{
						  if(image != ""){
							  marker.openInfoWindowHtml("<div style='text-align:center;'><a href='business.asp?ID="+id+"'><img src='"+image+"' border='0' width='"+w+"' height='"+h+"' /><br><b>"+bname+"</b></a><br>Address unavailable - " + address +"</div>");
						  }else{
							  marker.openInfoWindowHtml("<div style='text-align:center;'><b><a href='business.asp?ID="+id+"'>"+bname+"</a></b><br>Address unavailable - " + address +"</div>");
						  }
					  }
			  }
		}
      }
    }
		
		//]]>

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function closeSearchWindow() {
	document.getElementById('mapQuickSearchResults').style.display='none'
}

function ajaxFunction()
  {  var xmlHttp;
  try
    {    // Firefox, Opera 8.0+, Safari    
	xmlHttp=new XMLHttpRequest();
	}
  catch (e)
    {    // Internet Explorer    
	try
      {      
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
	  }
		catch (e)
		  {      
		  try
			{        
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
			}
		  catch (e)
			{        
			alert("Your browser does not support AJAX!");
			return false;
			}
		}
	}
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        document.getElementById("searchresults").innerHTML=xmlHttp.responseText;
        }
      }
   	xmlHttp.open("GET","maps_google_searchresults.asp?name="+document.getElementById("searchBus").value+"",true);
    xmlHttp.send(null);
	}
		
	
function loadaddr(){
	if($("#searchBus").val() != ""){
		$('#mapQuickSearchResults').css('display','block');
		ajaxFunction();
			//showAddress($("#searchLoc").val()+' nsw australia',$("#searchBus").val()+' nsw australia',$("#searchBus").val(),'','','','','');
	}else{
		
		if($("#searchLoc").val() != ""){
			showAddress($("#searchLoc").val()+' nsw australia',$("#searchLoc").val()+' nsw australia','','','','','','');
		}
		
	}
}

$(document).ready(function(){
	load();
	var t;
	t = setTimeout(loadaddr,2000);
});
$(window).unload(function(){
	GUnload();
});

