// JavaScript Document

//============================================================================================
// lib_gmaps.js																				==
//============================================================================================
//																							==
// This library contains functions to manage a picture gallery with groups/categories.		==
//																							==
// DEPENDENCIES																				==
//																							==
// lib_ajax-essentials.js (ajax-related javascript)											==
// pdmarker.js (google maps api extensions)													==
//============================================================================================


//============================================================================================
// UNIVERSAL VARIABLE DECLARATIONS
//============================================================================================
var map ; // holder for the map object
var directions ; // holder for the directions object
var xmlhttp ; // holder for the ajax db requests
var pointGroupZoom = [] // holder for the point group zoom levels
	pointGroupZoom['airport'] = 8 ;
	pointGroupZoom['train'] = 9 ;
	pointGroupZoom['hotel'] = 10 ;
	pointGroupZoom['other'] = 10 ;
	

//============================================================================================
// DEFAULT POINT VARIABLES																	==
//============================================================================================
//																							==
// these variables are to create a central map point and provide information about this		==
// map point.  This point will always be displayed on the map unless you set the			==
// autoShowDefault variable to "false".														==
//============================================================================================

var autoShowDefault = true ; // set to false if you don't want this to show on the map automagically

var defaultPointAddy = "11 Town Square, Vacaville, CA" ; // address for the default point
var defaultPointLat = 38.356999 ; // lattitude for the default point
var defaultPointLong = -121.987777 ; // longitude for the default point
var defaultGLatLng = new GLatLng ( defaultPointLat , defaultPointLong ) ; // point object for the default point
var defaultPointLabel = "Town Square" // label for or name of the default point
var defaultPointDesc = "Town Square in Downtown Vacaville.  <br />Music, Festivals, Food, and more!" ; // description of the point
var defaultPointLinkTo = "/entertainment" ; // page to link to


//============================================================================================
// EVENT MANAGEMENT; APPLICATION OF FUNCTIONS TO EVENTS; PAGE INITIALIZATION
//============================================================================================

// initialize the gmaps interface
manageEvent ( window , "load" , gMapsInitialize ) ;
// kill it on page unload
manageEvent ( window , "unload" , GUnload ) ;
// attach other events to objects in the window
manageEvent ( window , "load" , function () {
	// attach address button behavior
	manageEvent ( aaElem ( "btnSubmit" ) , "click" , setDirections ) ;
	// attach drop-down box behavior
	manageEvent ( aaElem ( "selType" ) , "change" , getMapPoints ) ;
} ) ;


//============================================================================================
// function createMarker																	==
//============================================================================================
// DESCRIPTION																				==
// This function creates a new marker with a clickable window at a particular point.		==
//																							==
// PARAMETERS																				==
// point: a defined point consisting of a lattitude-longitude pair; requires the use of 	==
//		GLatLng function																	==
//============================================================================================

function createMarker ( point , label , htmlBody , linkto , dPoint ) {
	
	//alert ( "createMarker" );
	// Set up our GMarkerOptions object
	/*var marker = new GMarker ( point ) ;

	GEvent.addListener ( marker , "click" , function ( ) {
		marker.openInfoWindowHtml ( "Marker <b>" + letter + "</b>" ) ;
	} ) ;
	return marker;*/
	// set up the marker
	var marker = "" ;
	if ( dPoint == "d" ) {
		var blueIcon = new GIcon ( G_DEFAULT_ICON ) ;
		blueIcon.image = "http://maps.google.com/mapfiles/kml/pal3/icon23.png" ;
		blueIcon.iconSize = new GSize ( 32 , 32 ) ;
		blueIcon.shadowSize = new GSize ( 56 , 32 ) ;
		blueIcon.iconAnchor = new GPoint ( 10 , 20 ) ;
		marker = new PdMarker ( point , blueIcon ) ;
	} else {
		marker = new PdMarker ( point ) ;
	}
	
	// add the tooltip
	marker.setTooltip ( label ) ;
	// create the html
	var html = "<h1>" + label + "</h1><p>" + htmlBody + "</p><p>"
	// check to see if there's a link
	if ( linkto != "" ) {
		html = html + "<a href=\"" + linkto + "\" target=\"_self\">Entertainment Guide</a></p>" ;
	}
	// plug it in
	marker.setDetailWinHTML ( html ) ;
	// make it opaque
	marker.setOpacity ( 100 ) ;
	// give it back to the outside world
	return marker ;
}
// end function sendChange
//============================================================================================


//============================================================================================
// function gMapsInitialize																	==
//============================================================================================
// DESCRIPTION																				==
// This function initializes the gmaps application and places the center at the Publick		==
// House.																					==
//																							==
// PARAMETERS																				==
// there are no parameters																 	==
//============================================================================================
	
function gMapsInitialize () {
	// check to see if the browser is compatible
   	if ( GBrowserIsCompatible () ) {
		// if so, define the map and direction objects
		map = new GMap2 ( document.getElementById ( "map_canvas" ) ) ;
		directions = new GDirections ( map , document.getElementById ( "directions" ) ) ;
		
		// create the map and add controls to it Default Zoom
       	map.setCenter ( defaultGLatLng , 16 );
		map.addControl ( new GMapTypeControl () ) ;
		map.addControl ( new GSmallMapControl () ) ;
		map.addControl ( new GScaleControl () ) ;
		
		// check to see if we're displaying the default point
		if ( autoShowDefault ) {
			displayDefaultPoint () ;
		}
   	}
}
// end function gMapsInitialize () {
//============================================================================================


//============================================================================================
// function displayDefaultPoint																==
//============================================================================================
// DESCRIPTION																				==
// This function displays the default map point and defines its tooltip and info			==
// window.																					==
//																							==
// PARAMETERS																				==
// there are no parameters																 	==
//============================================================================================

function displayDefaultPoint () {
	point = new GLatLng ( defaultPointLat , defaultPointLong ) ;
	map.addOverlay ( createMarker ( point , defaultPointLabel , defaultPointDesc , defaultPointLinkTo , "d" ) ) ;
}


//============================================================================================
// function setDirections																	==
//============================================================================================
// DESCRIPTION																				==
// This function pulls the address, city, and state from the form, creates point-to-point	==
// driving directions, and creates a driving directions list.								==
//																							==
// PARAMETERS																				==
// evnt: the event which triggered the function												==
//============================================================================================
	
function setDirections ( evnt ) {
	// cancel default click behavior
	evnt = evnt ? evnt : window.event;
	cancelEvent ( evnt ) ;
	// get the form values
	var city = aaElem ( "city" ).value ;
	var state = aaElem ( "state" ).value ;
	var address = aaElem ( "address" ).value ;
	// check to see that we have at least the city and state
	if ( ( city == null || city == "" ) || ( state == null || state == "" ) ) {
		// if not, throw an alert
		alert ( "Please enter at least a city and state to get directions" ) ;
	} else {
		// otherwise, create a query string
		var addressString = address + ', ' + city + ', ' + state ;
		// submit it to google
		directions.load ( "from " + addressString + " to " + defaultPointAddy ) ;
		// plug it into the directions div
		document.getElementById ( "directions" ).style.display= "block" ;
	}
}
// end function setDirections
//============================================================================================


//============================================================================================
// function getMapPoints																	==
//============================================================================================
// DESCRIPTION																				==
// This function takes the drop-down box value and submits it to the database to get the	==
// map points associated with that value													==
//																							==
// PARAMETERS																				==
// there are no parameters																	==
//============================================================================================

function getMapPoints ( ) {
	//alert ( "getMapPoints" ) ;
	// get the value of the drop-down box
	if ( this.value != "" ) {
		var pointGroup = this.value ;
		// create the parameters
		var params = "id=" + pointGroup + "&tbl=tblGMapPoints&field=gmapType" ;
		// start preparing the xmlhttp object
		if ( !xmlhttp ) xmlhttp = getXmlHttpRequest(  );
		var url = "common/groupRecord.php";
		xmlhttp.open ( "POST" , url , true);
		xmlhttp.onreadystatechange = function () {
			displayPoints ( ) ;
		} ;
		xmlhttp.setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
		xmlhttp.send ( params ) ;
	}
}
// end function getMapPoints
//============================================================================================


//============================================================================================
// function displayPoints																	==
//============================================================================================
// DESCRIPTION																				==
// This function takes the results from the maps database call and creates a point from 	==
// each record																				==
//																							==
// PARAMETERS																				==
// there are no parameters																	==
//============================================================================================

function displayPoints ( ) {
		
	// check to see if we're ready to fill
	if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
		// check to see if this is a null recordset
		if ( xmlhttp.responseText == "null recordset" ) {
			alert ( "There are no points of that type in the database" ) ;
		} else {
			// remove all existing map points
			// get the first marker
			var marker = map.getFirstMarker ( ) ;
			// loop through all the markers, removing them
			while ( marker != null ) {
				marker.remove ( ) ;	
				marker = map.getFirstMarker ( ) ;
			}
			
			// create an object for the db results
			var fieldsObj = eval ( "( " + xmlhttp.responseText + " )" ) ;
			
			// shortcut to fields
			var fields = fieldsObj.fields ;
		
			// get the size of the array
			var fieldLength = fields.length ;
				
			// set the default point
			if ( autoShowDefault ) {
				displayDefaultPoint () ;
			}
		
			// loop through and create a map point for each item
			for ( var i = 0 ; i < fieldLength ; i ++ ) {
			
				// pull the variables
				var desc = fields[i].gmapDesc ;
				var lat = fields[i].gmapLat ;
				var linkto = fields[i].gmapLink ;
				var long = fields[i].gmapLong ;
				var name = fields[i].gmapName ;
				var type = fields[i].gmapType ;
				
				// set the point
				var point = new GLatLng ( lat , long ) ;
				
				// call the function which creates map points
				map.addOverlay ( createMarker ( point , name , desc , linkto , '' ) ) ;
				
				// extend the boundaries to include the point
				//GBounds.extend ( point ) ;
		
			}
			// change the zoom level
			map.zoomToMarkers ( 8 ) ;
			//map.setZoom ( pointGroupZoom[type] ) ;
		}
	}
}
// end function displayPoints
//============================================================================================


//============================================================================================
// function getDistanceFromCenter															==
//============================================================================================
// DESCRIPTION																				==
// This function analyzes an array of points to see if they are all within an existing	 	==
// defined boundary.  If so, then nothing happens to the existing boundary.  If not, the	==
//																							==
// PARAMETERS																				==
// there are no parameters																	==
//============================================================================================