	//Global variables
    var geocoder;
	var map; 

	//Creates the custom icons with anchor, shadow etc. Uses icon from Google lab
    var iconBlue = new GIcon(); 
    iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(12, 20);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconyellow = new GIcon(); 
    iconyellow.image = 'http://labs.google.com/ridefinder/images/mm_20_yellow.png';
    iconyellow.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconyellow.iconSize = new GSize(12, 20);
    iconyellow.shadowSize = new GSize(22, 20);
    iconyellow.iconAnchor = new GPoint(6, 20);
    iconyellow.infoWindowAnchor = new GPoint(5, 1);
	
	 var iconred = new GIcon(); 
    iconred.image = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconred.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconred.iconSize = new GSize(12, 20);
    iconred.shadowSize = new GSize(22, 20);
    iconred.iconAnchor = new GPoint(6, 20);
    iconred.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["No"] = iconBlue;
    customIcons["Yes"] = iconyellow;
	customIcons["Touch"] = iconred;
	
	function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
        map = new GMap2(document.getElementById('map'));
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GLargeMapControl());    
		map.addControl(new GMapTypeControl());  
		map.addControl(new GScaleControl());
		//map.addControl(new GOverviewMapControl()); 
		//map.enableScrollWheelZoom();
        map.setCenter(new GLatLng(-32, 147), 5);
		
		///hide controls
	  	map.hideControls();
	  	GEvent.addListener(map, "mouseover", function(){map.showControls();});
	  	GEvent.addListener(map, "mouseout", function(){map.hideControls();});
      }
    }
	
	//function for entering address. Searches Google geoCoder for Lat and Long
	function searchLocations() {
		var state =  document.getElementById('stateSelect').value; 
  		var address = document.getElementById('addressInput').value +" "+ state + ' australia';
		var countryCode = "AU";
		geocoder.setBaseCountryCode(countryCode);
		geocoder.getLatLng(address, function(latlng) {
    		if (!latlng) {
     	 		alert(address + ' not found');
    		} else {
      			searchLocationsNear(latlng);
    		}
	  	});
	}
	
	function showLocations(){	
      	GDownloadUrl("http://www.dpi.nsw.gov.au/system-resources/google-map-components/public-site/fishing-licence/sql_showall_locations", function(data) {
			var xml = GXml.parse(data);
			var markers = xml.documentElement.getElementsByTagName("marker");
			map.clearOverlays();
			var sidebar = document.getElementById('sidebar');
			sidebar.innerHTML = '';
			for (var i = 0; i < markers.length; i++) {   
				var name = markers[i].getAttribute("name");    
				var address = markers[i].getAttribute("address");
				var phone = markers[i].getAttribute("phone");     
				var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),                            
										parseFloat(markers[i].getAttribute("lng")));
				var latdir = markers[i].getAttribute("lat");
				var lngdir = markers[i].getAttribute("lng");
				var dayonly = markers[i].getAttribute("dayonly");
				var active = markers[i].getAttribute("active");
				var comments = markers[i].getAttribute("comments");
				var marker = createMarker(point, name, address, phone, dayonly, comments, latdir, lngdir);  
				var sidebarEntry = createSidebarEntry(marker, name, address, 0, dayonly);
				
				if (active=="Yes"){   
					map.addOverlay(marker);  
					sidebar.appendChild(sidebarEntry);
				}  
			}
			map.setCenter(new GLatLng(-32, 147), 5);
        });
	}
	
	function searchLocationsNear(center) {
	 	var radius = document.getElementById('radiusSelect').value;
     	//Calls the php file with lat,lng and radius
	 	var searchUrl = "http://www.dpi.nsw.gov.au/system-resources/google-map-components/public-site/fishing-licence/sql_search_locations?lat=" + center.lat() + "&lng=" + center.lng() + "&radius=" + radius;

		GDownloadUrl(searchUrl, function(data) {
       	var xml = GXml.parse(data);
       	var markers = xml.documentElement.getElementsByTagName('marker');
		//var localhost = xml.documentElement; 
		//var markers=localhost.getElementsByTagName('marker'); 
		
       	map.clearOverlays();
		
   		var sidebar = document.getElementById('sidebar');
       	sidebar.innerHTML = '';
	   	var sidebarTotal = createResultTotal(markers);  
		sidebar.appendChild(sidebarTotal);
		//alert ();
       	if (markers.length == 0) {
        	sidebar.innerHTML = 'Sorry there are no Licence Agents in your area. Try searching a wider radius or in a different location. <br><br>The fishing fee can be purchased online by <a href="https://pnpnet.qvalent.com/NSWFish">CLICKING HERE</a> or by calling 1300 369 365.';
    	    map.setCenter(new GLatLng(-32, 147), 5);
         	return;
       	}
	   
       var bounds = new GLatLngBounds();
       for (var i = 0; i < markers.length; i++) {
	   		var name = markers[i].getAttribute('name');
        	var address = markers[i].getAttribute('address');
			var phone = markers[i].getAttribute("phone"); 
        	var distance = parseFloat(markers[i].getAttribute('distance'));
        	var point = new GLatLng(parseFloat(markers[i].getAttribute('lat')),
                                 parseFloat(markers[i].getAttribute('lng')));
			var latdir = markers[i].getAttribute("lat");
			var lngdir = markers[i].getAttribute("lng");					 
			var dayonly = markers[i].getAttribute("dayonly");
			var active = markers[i].getAttribute("active");
			var comments = markers[i].getAttribute("comments");
			var marker = createMarker(point, name, address, phone, dayonly, comments, latdir, lngdir);
			var sidebarEntry = createSidebarEntry(marker, name, address, distance, dayonly);   
			if (active=="Yes"){	
				map.addOverlay(marker);
				sidebar.appendChild(sidebarEntry);
			}	
       bounds.extend(point);
       }
      map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
     });
   }
   
    function createResultTotal(markers){
	  var resultlength = 0;
	  var div = document.createElement('div');
	  div.style.fontSize = '8pt';
	  div.style.backgroundColor = '#FFEFBF';
	  div.style.marginBottom = '5px';
	  for (var i = 0; i < markers.length; i++) {
	  	var active = markers[i].getAttribute("active");
	  	if (active=="Yes"){	resultlength++;}
	  }
	  if (resultlength == 0) {
	  	div.style.fontSize = '8pt';
	  	div.style.backgroundColor = '#FFFFFF';
         var html ='Sorry there are no Licence Agents in your area. Try searching a wider radius or in a different location. <br><br>The fishing fee can be purchased online by <a href="https://pnpnet.qvalent.com/NSWFish">CLICKING HERE</a> or by calling 1300 369 365.';
	  } else if (resultlength == 1) {
	  	var html = '<strong>' + resultlength + ' </strong> Licence Agent in your area:';
	  }
	  else {
	   		var html = '<strong>' + resultlength + ' </strong> Licence Agents in your area:';
	   } 
	   div.innerHTML = html;
	   return div;
	}
   
   function createSidebarEntry(marker, name, address, distance, dayonly) {
	  var div = document.createElement('div');
	  if (dayonly=='No'){
	  	var html = '<div style="color:#4D4DFF"><strong>' + name + ' <br>(' + distance.toFixed(1) + ' Km)</strong></div>' + address;
	  } else if (dayonly=='Yes'){
	   	var html = '<div style="color:#F9C700"><strong>' + name + ' <br>(' + distance.toFixed(1) + ' Km)</strong></div>' + address;
	  } else {
   	  	var html = '<div style="color:#FF3544"><strong>' + name + ' <br>(' + distance.toFixed(1) + ' Km)</strong></div>' + address;
	  }
	  div.innerHTML = html;
	  div.style.cursor = 'pointer';
	  div.style.marginBottom = '5px';
	  GEvent.addDomListener(div, 'click', function() {GEvent.trigger(marker, 'click');});
	  GEvent.addDomListener(div, 'mouseover', function() {div.style.backgroundColor = '#eee';});
	  GEvent.addDomListener(div, 'mouseout', function() {div.style.backgroundColor = '#fff';});
	  return div;
	}

    
	//Creates the markers out of the data passed from the php file and MySQL DB
    function createMarker(point, name, address, phone, dayonly, comments, latdir, lngdir) {
      var marker = new GMarker(point, customIcons[dayonly]);
      var html = "<div class=highlight>" + name + "</div>" + address + "<br/>" + "Ph: " + phone  + "<br/>" + comments + "<br/><a href='http://maps.google.com?daddr=" + latdir + "," + lngdir + "'>Get driving directions</a>";
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
      });
      return marker;
    }
	
		
	function StopIt(anObj)
        {
         	anObj.Stop();
        }
	
	function changeDiv(the_div,the_change) { 
        var the_style = getStyleObject(the_div); 
        if (the_style != false) { 
            the_style.display = the_change; 
        } 
    } 

    function setVisibility() { 
        changeDiv("searchbox","block"); 
        changeDiv("mapcontainer","block");
		changeDiv("agentlistjava","block");
		changeDiv("showagents","block");
		changeDiv("legend","block");
		changeDiv("instructions","block");		
    } 

    function getStyleObject(objectId) { 
        if (document.getElementById && document.getElementById(objectId)) { 
            return document.getElementById(objectId).style; 
        } else if (document.all && document.all(objectId)) { 
            return document.all(objectId).style; 
        } else { 
            return false; 
        } 
    } 
