// JScript File
//<![CDATA[

var map = new Object();
var communities;

var AUTO_ZOOM = 0;
var zoomLevel = 10;
var zoomThreshold = 30;

var destination;

var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(49,21);
baseIcon.iconAnchor=new GPoint(49>>1,21>>1);
baseIcon.shadowSize=new GSize(49,21);
baseIcon.infoWindowAnchor=new GPoint(15,0);

var houseIcon = new GIcon(baseIcon, "/images/googlemap/house.png", null, null);


function initialize() {
  if (GBrowserIsCompatible()) {
      map = new Object(); 
			getLocation();
  }
}
function getLocation() {
	$.getJSON("/home/location_json", null, function(data) { 
			destination = data;
			getMapData();
		});
}
function getMapData() {
	var point;
	var marker;
	
	$.getJSON("/home/communities_json", null, function(data) {
		
			communities = data;

			var mapDiv = document.getElementById("map");
			var mapSize = new GSize( 250, 291 );
			map = new GMap2(mapDiv);
			map.addControl(new DWHControl());
			map.addControl(new GMenuMapTypeControl());
			map.getContainer().style.overflow="hidden"; 

			// Add destination to map, but initialize setCenter first
			point = new GLatLng(destination.lat, destination.lng);
			map.setCenter(point, zoomLevel);
			map.setMapType(G_NORMAL_MAP);

			for (var x=0; x<communities.length; x++) {
				point = new GLatLng(parseFloat(communities[x].lat), parseFloat(communities[x].lng));
				marker = new GMarker(point, {icon: houseIcon, title: communities[x].title});

				addMarkerEvents(marker,communities[x]);
				map.addOverlay(marker);
			}
	});
}

function getMarkers(communities)
{
    var point;
    var marker;

    markers = new Array();
    markerNodes = new Array();

		// lets add our destination first
/*		
    point = new GLatLng(parseFloat(destination.lat), parseFloat(destination.lng));
    marker = new GMarker(point, {icon: houseIcon, title: destination.title});
    markers.push( marker );
    markerNodes.push( destination );
*/
		// add parking communities
    for (var i=0; i < communities.length; i++) 
    {
        point = new GLatLng(parseFloat(communities[i].lat), parseFloat(communities[i].lng));

				//filename = "/images/map/park-" + i + ".png";
				//parkIcon = new GIcon(baseIcon, filename, null, null);
	//alert(point);
        marker = new GMarker(point, {icon: houseIcon, title: communities[i].title});
        markers.push( marker );
        markerNodes.push( communities[i] );
    }
}

function loadListeners()
{
    for (i=0; i < markers.length; i++) {
        addMarkerEvents(markers[i], markerNodes[i]);
    }
}
function addMarkerEvents(marker, pointNodes)
{    
		GEvent.addListener(marker, 'click', function() { 
			//var content = "<table border='0' cellpadding='0' cellspacing='0' style='color:#000;'><tr><td class='contentDirections'><b>" + pointNodes.title + "</b></td></tr><tr><td><b>City:</b>&nbsp;" + pointNodes.city + "</td></tr><tr><td><b>State:</b>&nbsp;" + pointNodes.state + "</td></tr></table></div>";
			//map.openInfoWindowHtml( marker.getLatLng(), content, {pixelOffset: new		GSize(10,10)} );
			window.location = "/communities"
		} )
}
function resizeMap() 
{
    // Create new bounds object 
    var bounds = new GLatLngBounds();

    // Loop through the points, extending the bounds as necessary 
    for (var i=0; i<markers.length; i++) 
    {
        bounds.extend(markers[i].getPoint()); 
    }

    //Adjust the southwest point to take into account the controls
    var extendLat = bounds.getSouthWest().lat() - ((bounds.getNorthEast().lat() - bounds.getSouthWest().lat())*.25);
    var extendLng = bounds.getSouthWest().lng() + ((bounds.getNorthEast().lng() - bounds.getSouthWest().lng())*.3);
    bounds.extend(new GLatLng(extendLat, extendLng));

    // Find the centre of the new bounds 
    var lat = bounds.getSouthWest().lat() + ((bounds.getNorthEast().lat() - bounds.getSouthWest().lat()) / 2); 
    var lon = bounds.getSouthWest().lng() + ((bounds.getNorthEast().lng() - bounds.getSouthWest().lng()) / 2);

    // Get the bounds zoom level 
    AUTO_ZOOM = map.getBoundsZoomLevel(bounds);

    // The higher the zoom level the closer the zoom
    // if the AUTO_ZOOM level is greater than the default zoom level set the AUTO_ZOOM to the default zoom level
    if( AUTO_ZOOM > zoomLevel || AUTO_ZOOM == 0 ) AUTO_ZOOM = zoomLevel;
    // check to make sure zoomThreshold is 2 levels above the AUTO_ZOOM
    zoomThreshold = AUTO_ZOOM + 2;
    mapPoint = bounds.getCenter();

    // Change the map to the new bounds values 
    map.setCenter(mapPoint, AUTO_ZOOM);
    map.checkResize();
}


// Creates pan buttons on the map for zoom in/out, left, right, up, & down .
function DWHControl() { }
DWHControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to position properly.
DWHControl.prototype.initialize = function(map) {
	this.container = document.createElement("div");
	
	var size_of_direction_label = 25;
	var width = map.getSize().width;
	var middle_w = 0;
	if (width > 0) middle_w = width/2 - size_of_direction_label;

	var height = map.getSize().height;
	var middle_height = 0;
	if (height > 0) middle_height = height/2;
	
	this.build_map_control(middle_height - 15, width - 30, "/images/map/map_arrow_right.png", function() { map.panDirection(-1,0); });
	this.build_map_control(middle_height - 15, 10, "/images/map/map_arrow_left.png", function() { map.panDirection(1,0); });
	this.build_map_control(15, middle_w, "/images/map/map_arrow_up.png", function() { map.panDirection(0,1); });
	this.build_map_control(height - 30, middle_w, "/images/map/map_arrow_down.png", function() { map.panDirection(0,-1); });
	this.build_map_control(15, 10, "/images/map/zoom_in.png", function() { map.zoomIn(); });
	this.build_map_control(40, 10, "/images/map/zoom_out.png", function() { map.zoomOut(); });

	map.getContainer().appendChild(this.container);
	return this.container;
}

DWHControl.prototype.build_map_control = function(top, left, img_path, btn_function){
	var button_div = document.createElement("div");
	this.container.appendChild(button_div);
	button_div.innerHTML = '<img style="top:' + top + 'px;left:' + left + 'px;position:absolute;cursor:pointer;" src="' + img_path + '" class="map_control">';
	GEvent.addDomListener(button_div, "click", btn_function);
}


window.onload = initialize;
window.onunload=GUnload;
//]]>
