
var apikey = "ABQIAAAAhC5cp5wuAFp4OOwNA4TQkxTcxVUjPBHVApAP93UmhRt1Svw2ghQGghh7dc-MS0TV9J7bQbrO_aFxrQ";
var apiurl = "http://maps.google.com/maps/geo?output=xml&key=" + apikey + "&q=";

var map = null;
var mgr = null;

function closeMap() {
  GUnload();
}

function initMap() {
  //var address = "3190 S Gilbert Rd Ste 5, Chandler, AZ";
  
  if (GBrowserIsCompatible()) {
    var pts = new Array();
    var markers = new Array();
    map = new GMap2(document.getElementById("map_canvas"));
    //geocoder = new GClientGeocoder();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		//showAddress(address);
		//map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    //map.setCenter(new GLatLng(33.264505, -111.847648), 12);

		for (var i = 0; i < 0; i++) {
		  pts[i] = new GLatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());
		  markers[i] = new GMarker(pts[i]);
	    //GEvent.addListener(marker, "click", popupMarker(marker, point.toUrlValue()));
		  map.addOverlay(markers[i]);
      GEvent.addListener(markers[i], "click", function() {
  		  pointLocation = this.getPoint().toUrlValue();
        this.openInfoWindowHtml('<em>' + pointLocation + '</em>');
      });
		}
	  getMarkers();
  }
}

function createMarker(index, latitude, longitude, titleName) {
	options = { title: titleName };
	marker = new GMarker(new GLatLng(latitude, longitude), options);
	//mgr.addMarker(marker);
	map.addOverlay(marker);
  GEvent.addListener(marker, "click", function() {
  	//pointLocation = this.getPoint().toUrlValue();
  	// html = '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';
    //this.openInfoWindowHtml("<img src='data/d/" + index + "/location.jpg' alt='' width='120' />"); //'<em>' + index + '</em>');
    this.openInfoWindowHtml("<a href='client.view.do?clientID=" + index + "'>More Information</a>"); //'<em>' + index + '</em>');
    coreGet('markerInfo', 'location.load.rpc?locationID=' + index, 'markerInfo');
  });
}

var handleSuccessMarkers = function(o){
  pendingRequest = false;
  div = document.getElementById('markerInfo');
	if (o.responseText !== undefined) {
		//div.innerHTML = o.responseText;
		if (o.responseText.indexOf(":") >= 0) {
			div.innerHTML = "";
			mySplitResult = o.responseText.split(";");
			mapCenter = mySplitResult[0].split(":");
      if (mapCenter.length >= 3) {
        //alert(mapCenter[0] + " : " + mapCenter[1] + " : " + mapCenter[2]);
        map.setCenter(new GLatLng(mapCenter[0], mapCenter[1]), parseInt(mapCenter[2]));
      }
      //mgr.clearMarkers();
      map.clearOverlays();
	    for (i = 1; i < mySplitResult.length; i++) {
				if (mySplitResult[i].length >= 4) {
					markerLoc = mySplitResult[i].split(":");
					createMarker(markerLoc[0], markerLoc[1], markerLoc[2], markerLoc[3]);
				}
			}
	  }
    coreGet('markerInfo', 'subarea.list.rpc', 'markerInfo');
	} else {
	  div.innerHTML = "<br/>Locations Load Process Failed.<br/>Please Reload and Try Again.<br/><br/><br/>";
	}
};

var handleFailureMarkers = function(o) {
  pendingRequest = false;
	div = document.getElementById('markerInfo');
	div.innerHTML = "<br/>Locations Load Process Failed.<br/>Please Reload and Try Again.<br/><br/><br/>";
};

var callbackMarkers =
{
  success:handleSuccessMarkers,
  failure:handleFailureMarkers,
  timeout: 20000
};

function getMarkers(subAreaID) {
  if (!pendingRequest) {
		setPending('markerInfo');
	  YAHOO.util.Connect.asyncRequest('GET', 'map.load.rpc?subAreaID=' + subAreaID, callbackMarkers);
	}
}

function clearLocation() {
  div = document.getElementById('markerInfo');
  div.innerHTML = "";
}

function saveLocation(token) {
  corePostToken('markerInfo', 'location.save.rpc', document.getElementById('startform'), false, token, 'getMarkers');
}

function deleteLocation(locationID, token) {
  coreGetToken('markerInfo', 'location.delete.rpc?addressID=' + locationID + '&token=' + token, 'markerInfo', token, 'getMarkers');
}

function saveSubArea(token) {
  //alert(map.getCenter() + " " + map.getZoom());
  document.getElementById('longitude').value = map.getCenter().lng();
  document.getElementById('latitude').value = map.getCenter().lat();
  document.getElementById('zoomLevel').value = map.getZoom();
  corePost('markerInfo', 'subarea.save.rpc', document.getElementById('updateform'), false);
  //corePostToken('markerInfo', 'subarea.save.rpc', document.getElementById('updateform'), false, token, 'getMarkers');
}

YAHOO.util.Event.addListener(window, "load", initMap);
YAHOO.util.Event.addListener(window, "unload", closeMap);

// http://maps.google.com/maps?f=q&hl=en&geocode=&q=3190+S+Gilbert+Rd+Ste+5,+Chandler,+AZ&sll=37.0625,-95.677068&sspn=46.092115,65.214844&ie=UTF8&z=12&iwloc=cent
