// Import Google Maps API Javascript v2 Library
var publicTransitTo;
var publicTransitFrom;

var _PHILLY_LATLNG_;


var office_markers_html = "";
var gmarkers = [];
var htmls = [];
// arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];
var IDs = [];


// A function to create the marker and set up the event window
function createMarker(point,name,html,img,link,id) {
	var marker = new GMarker(point);
	
	IDs.push(id);
	var cycleNum = IDs.length;
	
	var i = gmarkers.length;

	
	var listingNumber = id ;
	// The info window version with the "to here" form open
	to_htmls[id] = "<div class=\"infoBox\"><a href=\"http://aptsny-dev.mics.me/apartments/" + listingNumber + "\">" + html + '</a><br><p>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + id + ')">From here<\/a>' +
	'<br /></p><br/><form action="javascript:getDirections()">Start address:' +
	'<input type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
	'<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
	'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" /><br> &nbsp; ' +
	'Public Transit <input type="checkbox" name="publictransit" id="publictransit" ' +
	'onClick="if (this.checked){' + 
	'if (document.getElementById(\'walk\').checked || document.getElementById(\'highways\').checked) {' +
		'document.getElementById(\'walk\').checked = false;' +
		'document.getElementById(\'highways\').checked = false;' +
		'}' +
	'document.getElementById(\'walk\').disabled=true;' +
	'document.getElementById(\'highways\').disabled=true;' +
		'}' +
	'else {' + 
		'document.getElementById(\'walk\').disabled=false;' +
		'document.getElementById(\'highways\').disabled=false;' +
	'}" value=ON />' +

	'<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
	'"/></form></div>';
	// The info window version with the "from here" form open
	from_htmls[id] = '<div class=\"infoBox\">' + html + '<br><p>Directions: <a href="javascript:tohere(' + id + ')">To here</a> - <b>From here</b>' +
	'<br /></p><br/><form action="javascript:getDirections()">End address:' +
	'<input type="text" SIZE=30 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
	'<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
	'Walk <input type="checkbox" name="walk" id="walk" /> &nbsp; Avoid Highways <input type="checkbox" name="highways" id="highways" /><br> &nbsp; ' +
	'Public Transit <input type="checkbox" name="publictransit" id="publictransit" ' +
	'onClick="if (this.checked){' + 
	'if (document.getElementById(\'walk\').checked || document.getElementById(\'highways\').checked) {' +
		'document.getElementById(\'walk\').checked = false;' +
		'document.getElementById(\'highways\').checked = false;' +
	'}' +
	'document.getElementById(\'walk\').disabled=true;' +
	'document.getElementById(\'highways\').disabled=true;' +
	'}' +
	'else {' + 
		'document.getElementById(\'walk\').disabled=false;' +
		'document.getElementById(\'highways\').disabled=false;' +
	'}" value=ON />' +
 
	'<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
	'"/></form></div>';
	// The inactive version of the direction info
	html ='<div class=\"infoBox\">' + html + '<p><a href="javascript:prevclick(' + (cycleNum - 1) + ')">Prev</a> <a href="javascript:nextclick(' + (cycleNum - 1) + ')">Next</a>' +
	'<br /><br /><div class=\"infoInfo\"><p><span>Rent: </span>' + 
	rent + '<br /><span>Rooms: </span>'+ rooms + '<br /><span>Bathrooms: </span>' + bathrooms + '<br /><span>Availability: </span>' + availability + 
	'<br /></div>' + link + '<img src="' + 
	img + '" alt="photo" class="infothumb"></a>' + 
	'<p id=\"infoFooter\">Directions: <a href="javascript:tohere('+ id +')">To here<\/a> - <a href="javascript:fromhere('+ id +')">From here<\/a></p>' +
	'</div>';

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});
	
	
	// save the info we need to use later for the office_markers
	gmarkers[id] = marker;
	htmls[id] = html;
	
	// add a line to the office_markers html
	office_markers_html += '<a href="javascript:markerClick(' + id + ')">' + name + '<\/a><br>';
	return marker;
}

// ===== request the directions =====
function getDirections() {
	// ==== set the start and end locations ====
	var saddr = document.getElementById("saddr").value;
	var daddr = document.getElementById("daddr").value;
	

	
	if(document.getElementById('publictransit').checked) // ===== redirect to google transit page =====
	{/*
		if(_DIRECTION_ == "to the office")
		{
			//set the end to the office
			if(daddr == _PHILLY_LATLNG_)
				daddr = "1333+Race+Street%2C+Philadelphia%2C+PA+19107-1585" ;
			else
				daddr = "117+N.+Olive+Street,+Media,+PA+19063" 
		}
		else
		{
			// set start to the office
			if(saddr == _PHILLY_LATLNG_)
				saddr = "1333+Race+Street%2C+Philadelphia%2C+PA+19107-1585" ;
			else
				saddr = "117+N.+Olive+Street,+Media,+PA+19063";				
		}
	*/
		// redirect to transit URL using saddr and daddr
		var gMapUrl = "http://www.maps.google.com/maps?f=d&source=s_d&dirflg=r&saddr=" + saddr + "&daddr=" + daddr;			
		window.location = gMapUrl;
	}
	else // ===== get directions and display locally =====
	{
		// ==== Set up the walk and avoid highways options ====
		var opts = {} ;
		if (document.getElementById("walk").checked) {
			opts.travelMode = G_TRAVEL_MODE_WALKING;
		}
		if (document.getElementById("highways").checked) {
			opts.avoidHighways = true;
		}
	
		gdir.load("from: "+saddr+" to: "+daddr, opts);
	}

}


// This function picks up the click and opens the corresponding info window
function markerClick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function prevclick(i)
{
	if (gmarkers[IDs[i-1]])
		markerClick(IDs[i-1]);
}

function nextclick(i)
{
	if (gmarkers[IDs[i+1]])
		markerClick(IDs[i+1]);
}

// functions that open the directions forms
function tohere(i) {
	_DIRECTION_ = "to the office";
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
	//publicTransitTo = gmarkers[i]. ;
	//publicTransitFrom;
	
}
function fromhere(i) {
	_DIRECTION_ = "from the office";
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

/*
function createMap(centerLat, centerLng, divId){
	/* divId - string; div id where map is to be placed
	 * returns GMap2 object
	 *
	var map = new GMap2(document.getElementById(divId));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(centerLat, centerLng), 9);

	return map;
}

function createDirections(map, divId){
	/* divId - string, div id where directions are to be placed
	 * returns GDirections Object
	 *
	var gdir = new GDirections(map, document.getElementById(divId));
	
	// === Array for decoding the failure codes ===
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	
	// === catch Directions errors ===
	GEvent.addListener(gdir, "error", function() {
		var code = gdir.getStatus().code;
		var reason="Code "+code;
		if (reasons[code]) {
			reason = reasons[code]
		} 
	
		alert("Failed to obtain directions, "+reason);
	});
	
	return gdir;
}
*/	

	// This Javascript is based on code provided by the
	// Community Church Javascript Team
	// http://www.bisphamchurch.org.uk/   
	// http://econym.org.uk/gmap/

	//]]>
