// JavaScript Document

window.onload = loadit;
window.onunload = unloadit;
var map;

function loadit() {
	//alert("loadit");
	var urlarg = "";
	//alert("location.search=" + location.search);
	if( location.search.substring(0,1) == "?" ) { // check for an argument passed with the url
			urlarg = location.search.substring(1);
			}
	//alert("urlarg=" + urlarg);

	if( maptype168 != "map" )
		creatmap(maploc168, "client", maptype168, urlarg );
	else
		creatmap(maploc168, maptype168, "null", urlarg );
		}

function unloadit() {
	GUnload();
	}
	
function creatmap( curLoc, mapType, clientName, URLarg ) {
	
	// curLoc is location of the map to be displayed (actual subdirectory name or "colorado")
	// mapType is indicates a map page ("map.html") or client page with map ("lyons/stonemountainlodge/index.html")
	// curLoc:	colorado	boulder		estes-park		stonemountainlodge
	// mapType:	map			map			map				client
	
	if (GBrowserIsCompatible()) {
		//alert(curLoc);
		//alert(mapType);

		// 
		// create the map object and set the map controls for the map to be displayed
		//
		
		var map = new GMap2(document.getElementById("map"));
		
		if( mapType == "map") {
			// add all controls for main maps
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GScaleControl());
			map.addControl(new GOverviewMapControl());
			} // else no controls for (mapType == "client")

		//
		// determine and set the map center and zoom for the map we are to display
		//

		// Define the Map Centers, (and locations for city markers)
		var estesPoint = new GLatLng(40.376007, -105.523639);
		var boulderPoint = new GLatLng(40.021009, -105.263454);
		var coloradoSpringsPoint = new GLatLng(38.835863, -104.822509);
		var manitouSpringsPoint = new GLatLng(38.857455, -104.914584);
		var coloradoPoint = new GLatLng(39.41, -105.323);

		// Define map center for client, (may not be the same as the location for client marker)
		var smlPoint = new GLatLng(40.246581, -105.285330);
		var smlZoom = 8;

		// set map center to the location to be displayed and the appropriate zoom 
		if( mapType == "map" )
			{
			if( curLoc == "estes-park" ) { // to display estes park map
				// center for estes park in general
				map.setCenter(estesPoint, 12); 
				}
			else if( curLoc == "boulder" ) { // to display boulder map
				// center for boulder in general
				map.setCenter(boulderPoint, 11); 
				}		
			else if( curLoc == "colorado-springs" ) { // to display boulder map
				// center for boulder in general
				map.setCenter(coloradoSpringsPoint, 11); 
				}		
			else if( curLoc == "manitou-springs" ) { // to display boulder map
				// center for boulder in general
				map.setCenter(manitouSpringsPoint, 13); 
				}		
			else { // assume (curLoc == "colorado") to display colorado map
				var tmpPoint = coloradoPoint;
				var tmpZoom = 6;
				if( URLarg ) // if client name is passed via url
					{ // we want to center around a client location so get center and zoom for client
					if( URLarg == "stonemountainlodge" ) {
						tmpPoint = smlPoint;
						tmpZoom = smlZoom;
						}
					}
				// else set center and zoom for colorado in general
				map.setCenter(tmpPoint, tmpZoom); 
				}
			}
		else { // assume (mapType == "client")
			if( clientName == "stonemountainlodge" ) { // to display client map on client page
				map.setCenter(smlPoint, smlZoom);
				}
			}
			
		//	if( !refreshmap && location.search == "?sml" ) {
				// center and zoom for SML
		//		map.setCenter(smlPoint, 9); // to display client map on a map page
		//		}
			
		//
		// Now create the markers for the map to be displayed	
		//

		// Get the icons we want
		var baseIcon = new GIcon();
          baseIcon.iconSize=new GSize(32,32);
          baseIcon.shadowSize=new GSize(56,32);
          baseIcon.iconAnchor=new GPoint(16,32);
          baseIcon.infoWindowAnchor=new GPoint(16,0);

		var lodgeIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal2/icon28.png", null, "http://maps.google.com/mapfiles/kml/pal2/icon28s.png");
		var cityIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon43.png", null, "http://maps.google.com/mapfiles/kml/pal3/icon43s.png");
		//attractionIcon = G_DEFAULT_ICON;
		var attractionIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon49.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon49s.png");

		// create the Marker Manager
		var mm = new GMarkerManager(map);
		var marker;
		
		//
		// create client markers for lodges
		//
		var title;
//		if( mapType == "client" ) { // only create the marker for the one client lodge
//			}
//		else if( mapType == "map") { // create markers for all clients for curLoc (IF SHOW CLIENTS IS CHECKED) TBD
			// for now display all clients
//			}

		// NOTE: above if else if is blank since we only have one client - so do in-line for now
		//???		var point = new GLatLng(40.2370, -105.2877); // exact location for SML?
		title = "Stone Mountain Lodge and Cabins";
//// BREAK UP THE PATH
		marker = createClientMarker(smlPoint, curLoc, "colorado", mapType, lodgeIcon, title, "lyons/stonemountainlodge/index");			
		mm.addMarker(marker,8,17); // define marker and zoom for client
		//
 		// create markers for Cities and Towns
		// Note: City marker locations are the same as where city maps are centered
		//
		// Boulder
		title = "Boulder";
		marker = createClickMarker(boulderPoint, curLoc, "boulder", mapType, cityIcon, title, "index");
		mm.addMarker(marker,6,17);
 
		// Colorado Springs
		title = "Colorado Springs";
		marker = createClickMarker(coloradoSpringsPoint, curLoc, "colorado-springs", mapType, cityIcon, title, "index");
		mm.addMarker(marker,6,17);

		// Manitou Springs
		title = "Manitou Springs";
		marker = createClickMarker(manitouSpringsPoint, curLoc, "manitou-springs", mapType, cityIcon, title, "index");
		mm.addMarker(marker,7,17);

		// Estes Park
		title = "Estes Park";
		marker = createClickMarker(estesPoint, curLoc, "estes-park", mapType, cityIcon, title, "index");
		mm.addMarker(marker,6,17);

		//
 		// create markers for Attractions
		//
		// Rocky Mountain National Park
		var attractionPoint = new GLatLng(40.404395, -105.591820);
		title = "Rocky Mountain National Park";
		marker = createClickMarker(attractionPoint, curLoc, "estes-park", mapType, attractionIcon, title, "../rocky-mountain-national-park/index");
		mm.addMarker(marker,8,17);

		// University of Colorado
		attractionPoint = new GLatLng(40.006169, -105.272080);
		title = "University of Colorado at Boulder";
		marker = createClickMarker(attractionPoint, curLoc, "boulder", mapType, attractionIcon, title, "things-to-do");
		mm.addMarker(marker,10,17);

		// Pearl Street
		attractionPoint = new GLatLng(40.018010, -105.279332);
		title = "Pearl Street Mall";
		marker = createClickMarker(attractionPoint, curLoc, "boulder", mapType, attractionIcon, title, "shopping");
		mm.addMarker(marker,10,17);

		mm.refresh();
		
		}  // display a warning if the browser was not compatible
    else {
      	alert("Sorry, Google Maps is not compatible with this browser");
    	}
    }
	
function createClickMarker(point, curLoc, newLoc, maptype, icon, title, file) {
	var marker = new GMarker(point, { title: title, icon: icon });

	// add the marker where when clicked yo go to the specified file in the appropriate newLoc directory
	GEvent.addListener(marker, "click", function() {gotoPage(file, curLoc, newLoc, maptype);} );	

	return marker;
	}

function gotoPage(file, curLoc, newLoc, maptype) {
	//
	// This function is called when a city or attraction marker is clicked. It's arguments pre-set what to display.
	// Contruct the appropriate relative path knowing the levels of the curLoc and newLoc directories, 
	// then display the page.
	//

	if(maptype == "client"){ 
		// we're coming from a client directory and going to a map in a location subdirectory
		urltext = "../../" + newLoc + "/map.html";
		}
	else { // maptype == map
		if(curLoc == "colorado"){
			// either a map or attraction - we're going to a subdirectory to display a map
				urltext = newLoc + "/map.html";
			}
		else if( curLoc == newLoc ){	
			// we're staying in same directory, so we're alreading in a subdirectory - so display the "info" file
			urltext = file + ".html";
			}
		else{ // different areas - so we need to display the map in the new area
				// we're going to another subdirectory
				urltext = "../" + newLoc + "/map.html";
			}
		}
	// open in a new window
	//window.open(urltext,"_blank");

	// display page in same window (replace page)
	window.open(urltext,"_self");

	//reloadPage();
	}

function createClientMarker(point, curLoc, newLoc, maptype, icon, title, file) {
	var marker = new GMarker(point, { title: title, icon: icon });

	// add the marker where when clicked yo go to the specified file in the appropriate newLoc directory
	GEvent.addListener(marker, "click", function() {gotoClientPage(file, curLoc, newLoc, maptype);} );	

	return marker;
	}

function gotoClientPage(file, curLoc, newLoc, maptype) {
	//
	// This function is called when a client marker is clicked. It's arguments pre-set what to display.
	// Contruct the appropriate relative path knowing the levels of the curLoc and newLoc directories,
	// then display the page.
	//
//alert( "maptype = " + maptype);
//alert( "curLoc = " + curLoc);
//alert( "newLoc = " + newLoc);
//alert( "file = " + file);
	if(maptype == "client"){ // This client marker is on the map that is on this clients page
		// we're coming from a client directory and going to the main map this client is primarily located on
		// HERE's WHERE WE PASS THE ARGS TO DISPLAY THE CLIENT ALONE ON THE GENERAL MAP, SO WE ADD IT THE THE URL
		if(newLoc == "colorado"){ // this client is displayed on the Colorado map (top level)
			// go up to root directory to find general colorado map
			urltext = "../../" + "map.html?stonemountainlodge";
			}
		else { 
			// go to clients directory
			urltext = "../../" + newLoc + ".html";
			}
		}
	else { // maptype == map - This client marker is on some general area map 
		// SO - we are to go the the clients page: file is "lyons/stonemountainlodge/index"
		if(curLoc == "colorado"){ // marker is on Colorado map
			// we're going into a subdirectory from the root
			urltext = file + ".html";
			}
		else { // marker is on an area map (not Colorado)
			// curLoc is in some subdirectory
			// we're going into another subdirectory
			urltext = "../" + file + ".html";
			}
		}
	// put page in same window (replace page)
	window.open(urltext,"_self");

	//reloadPage();
	}

//// -------- not sure if anything is used below ----------------????

function createInfoMarker(point, text, icon, title) {
	var marker = new GMarker(point, { title: title, icon: icon });
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(text, {maxWidth: 300}) ;} );
	return marker;
	}


function refreshMap(txt) {
	creatmap(txt, "null", true);
	//	This is how to display a new page: window.open("http://www.w3schools.com","_self")
	//reloadPage()
	}
