//<![CDATA[
var icon = new GIcon();
icon.image = "http://www.utah.com/art/maps/green-marker.png";
icon.shadow = "http://www.utah.com/art/maps/marker-background.png";
icon.iconSize = new GSize(20, 20);
icon.shadowSize = new GSize(20, 20);
icon.iconAnchor = new GPoint(10, 19);
icon.infoWindowAnchor = new GPoint(10, 5);

var featuredicon = new GIcon(icon);
featuredicon.image = 'http://www.utah.com/art/maps/red-marker.png';
	
function load(clat, clong, zlevel) {
  if(clat == 0 || clong == 0){ return }
  if (GBrowserIsCompatible()) {				
var map = new GMap2(document.getElementById("gmap"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var geocoder = new GClientGeocoder();
map.setCenter(new GLatLng(clat, clong), zlevel);
var boundaryPoints = map.getBounds();

// Add event listener for movend or dragend or both. When the user quits moving the map, then make the ajax call to get
// properties within the bounds of the map
GEvent.addListener(map, "zoomend", function(){ boundaryPoints = map.getBounds(); getProperties(boundaryPoints); });
GEvent.addListener(map, "dragend", function(){ boundaryPoints = map.getBounds(); getProperties(boundaryPoints); });

function importanceOrder (marker,b) {
  return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

// Creates a marker at the given point with the given number label
function createMarker(point, name, html, preference) {
  if(preference == '1'){
    var marker = new GMarker(point, { icon: featuredicon, title: name, zIndexProcess:importanceOrder});
	marker.importance = 2;
  } else {
    var marker = new GMarker(point, { icon: icon, title: name });
  }
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml( html, { maxWidth: 250 } );
  });
  return marker;
}			

function addMarker(id, name, address, url, video_url, phone, latitude, longitude, preference) {
  var video_html = '';
  var website_html = '';
  var phone_html = '';
  if(video_url){ video_html = '<br /><a href="/utah_logs/thing_link_log.php?log=' + video_url + '||' + id + '||video_url||' + window.location.href + '" target="_blank">Video</a> '; }
  if(url){
  	website_html = (video_html ? ' &bull; ' : '<br />') + '<a href="/utah_logs/thing_link_log.php?log=' + url + '||' + id + '||web_address||' + window.location.href + '" target="_blank">Website</a>'
  }
  if(phone){
  	phone_html = '<br /><div id="gmapphonenumber" style="display: inline;"><a href="javascript: void(0)" onclick="showPhoneNumber(' + id + ', \'gmapphonenumber\');return false;">Click for phone number</a></div>';
  }
  var html = '<div style="font: 14px arial; padding: 5px;"><b>' + name + '</b><br />' + address + phone_html + video_html + website_html  + '<br /><a href="/campground/amenities' + id + '.htm" onclick="show_amenities(' + id + '); return false;" target="_blank">More information</a></div>';
  if(latitude && longitude){ 
  	point = new GLatLng(latitude, longitude);
	if(point != null){
		map.addOverlay(createMarker(point, name, html, preference));
	}
  } 
}

// Show just the properties in the the list
function parseDelimited(delimited_data){
	//alert(map);
	var data = delimited_data.split("\n");
	marker_ids = data[0].replace(/^\s+|\s+$/g,"").split('|');
	marker_names = data[1].replace(/^\s+|\s+$/g,"").split('|');
	addresses = data[2].replace(/^\s+|\s+$/g,"").split('|');
	urls = data[3].replace(/^\s+|\s+$/g,"").split('|');
	video_urls = data[4].replace(/^\s+|\s+$/g,"").split('|');
	ph_numbers = data[5].replace(/^\s+|\s+$/g,"").split('|');
	latitudes = data[6].replace(/^\s+|\s+$/g,"").split('|');
	longitudes = data[7].replace(/^\s+|\s+$/g,"").split('|');
	preferences = data[8].replace(/^\s+|\s+$/g,"").split('|');
	// Add address layers to the map.
	if(addresses){
		map.clearOverlays();
		for(var x = 0; x < marker_names.length; x++){
			addMarker(marker_ids[x], marker_names[x], addresses[x], urls[x], video_urls[x], ph_numbers[x], latitudes[x], longitudes[x], preferences[x]);
		}
	}
}

// get lodging addresses to mark
function getProperties(bounds){	
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var respText, respCode, timestamp, cdate;
	var ucdate = new Date();
	var timestamp = ucdate.getSeconds();
	var downloadURL = '/database/getCampgroundList.php?nec='+northEast+'&swc='+southWest+'&time=' + timestamp;
	//document.write(downloadURL);
	GDownloadUrl(downloadURL, function(respText, respCode){ 
		//var troubleshoot = document.getElementById("troubleshoot");
		//troubleshoot.innerHTML = 'URL: ' + downloadURL + '<br />Response Code: ' + respCode + ' Response: ' + respText.replace("\n", '<br />'); 
		if(respCode = '200'){
			parseDelimited(respText);
		}
	})
}

//if(init_data){
	//alert('load data: ' + init_data);
	//parseDelimited(init_data);
	//init_data = '';
//}

getProperties(boundaryPoints);



  }
}
//]]>
