/*
These functions are use the Google Maps 2 API to write maps on the TorreySchoolhouse.com website.
By: Manuel Mejia
HTTP://www.soundsculpting.org
*/
	
// Creates a marker at the given point with the given number label
function addSchoolhouse (iconSize){
	//alert("running function : showSchoolhouse");
	if (!iconSize){var iconSize = 70};
	var tsh_loc = new GLatLng(38.3012, -111.4208);
	var tsh_icon = new GIcon();
	tsh_icon.image = "http://www.torreyschoolhouse.com/_templates/_images/tsh-schoolhouse-icon.png";
	tsh_icon.shadow = "";
	tsh_icon.iconSize = new GSize(iconSize, iconSize);
	tsh_icon.shadowSize = new GSize(0, 0);
	tsh_icon.iconAnchor = new GPoint(iconSize*.5, iconSize*.6);
	tsh_icon.infoWindowAnchor = new GPoint(0, 0);
	var tsh_marker = new GMarker(tsh_loc, tsh_icon, true);
	return tsh_marker;
};
//Toggles visibility of an overlay, and value of a button's label.
function toggleOverlay(map, overlayId, buttonId, buttonValue) {
	if(!buttonId.toggleVar){
			buttonId.toggleVar = 0;
	}
	if (buttonId.toggleVar == 1) {
		map.removeOverlay(overlayId);
		buttonId.toggleVar = 0;
		buttonId.value = "Show "+buttonValue;
		buttonId.className  = "toggle off";
	} else {
		map.addOverlay(overlayId);
		buttonId.toggleVar = 1;
		buttonId.className  = "toggle on";
		buttonId.value = "Hide "+buttonValue;
	};
};

//Functions that call the toggleOverlay function with certain parameters
function doToggleDining (){
	toggleOverlay(mainMap, torreyDining, toggleDining, 'Restaurants');
}
function doToggleDrives (){
	toggleOverlay(mainMap, torreyDrives, toggleDrives, 'Drives');
}
function doToggleDayhikes (){
	toggleOverlay(mainMap, torreyDayhikes, toggleDayhikes, 'Day Hikes');
}
function doToggleAttractions (){
	toggleOverlay(mainMap, torreyAttractions, toggleAttractions, 'Attractions');
}

// alters links with a given "rel" value to open in a given frame or window
function setChildLinksTarget (relValue, targetValue){
	var links = document.getElementsByTagName("a");
	for (i=0; i>links.length; i++){
		if (links[i].getAttribute("rel", relValue)){
			links[i].setAttribute("target", targetValue);
		}
	};
}

// adds a single marker to the map with parsed contact info in infoWindow.
// ADAPTED from go.utah.com
function createMarker (name, address, url, phone, latitude, longitude) {
	 //MM create point from LatLong or Address
		/* USAGE
		createMarker( 
		'name', //NAME
		'address', //ADDRESS
		'website', //WEBSITE
		'phone', //PHONE
		'lat', 'long' // Lat & Long
		),
		*/
	 //DEBUG alert("running function : showAddress");
	 var geocoder = new GClientGeocoder();
	 if(!latitude || !longitude){
	  //DEBUG alert(address + " using GPS");
	  var point = geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				alert(address + " not found");
				return;
			}}
		);
	  } else {
	  var point = new GLatLng(latitude, longitude);
	  }
	  //MM create marker at point
	  var marker = new GMarker(point);
	  //MM add info window to marker "onclick"
	  GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(
				'<span style="font: 14px arial">'
				+'<b>' + name + '</b><br/>' 
				+ address + '<br/>'
				+'<a href="../_templates/%27%20%2B%20url%20%2B%20%27">Website</a> <br/>'
				+'&bull; <b>Phone:</b> ' + phone + '</span>'
			)});
	  return marker;
};
