/******************************************************************************
******************************************************************************
// INITIALIZATION & GLOBALS MODULE  //////////////////////////////////////////
// Copyright 2007-2008 Collaborative Bike Map Project
// This is your mind on progamming.
******************************************************************************
******************************************************************************/

// Globals ********************/
var startZoom = 13;
var Redmarker;

var Map;                   // GMap2 object
var RouteList;                         // linked list (type = LinkedList)
var SelectedRoute = null;              // currently selected route
var OpenRoute = null;                  // currently open route (for editing)
var SelectedSegment = null;
var SelectedPoint = null;
var SegmentMemory = null;
// var NoGetList = null;      // list of routes to not download when new panels requested (eventually use closure instead)
var Iconset = null
var ButtonStates = null; 
var MapEv = null;
var CurrMode = null;
var HereYet = false;
var Site = null;
var Layout = null;
var XmlWindow = null;
// var ThereYet = false;

/* Constants *******************/
// Modes...
var MODE_VIEW = 1;              // Note: "Const" not supported by IE
var MODE_NEW = 2;
var MODE_RTE_SELECTED = 3;      // needed?
var MODE_APPEND = 4;
var MODE_INSERT_AFTER = 5;
var MODE_INSERT_BEFORE = 6;

// Version of this site...
var SITE_VIEWER = 1;              
var SITE_EDITOR = 2;

// Layouts...
var LAYOUT_EDITOR = 0;     // default
var LAYOUT_LONG = 1;
var LAYOUT_SHORT = 2;
var LAYOUT_DOCKED = 3;

// Display filter modes...
var FILTER_STANDARD = 1;
var FILTER_STANDARD_SW = 2;
var FILTER_GOODROADS = 3;
var FILTER_GOODROADS_SW = 4;
var FILTER_PATHS = 5;
var FILTER_ALL = 6;
var FilterMode = FILTER_ALL;

// Widths...
var POLYLINE_WIDTH_SELECTED = 7;
var POLYLINE_WIDTH_DEFAULT = 5;
var POLYLINE_WIDTH_INACTIVE = 4;
// Opacities...
var POLYLINE_OPACITY_SELECTED = 0.6;
var POLYLINE_OPACITY_DEFAULT = 0.5;
var POLYLINE_OPACITY_INACTIVE = 0.5;
var BUTTON_OPACITY_INACTIVE_TIMES_100 = 20;

// Misc...
var CENTER_LAT = 39.0196176;
var CENTER_LNG = -77.146318;

// Situations passed to determineChecks()
var SIT_RTE_FINISH = 1;
var SIT_SPLIT_SEG = 3;
var SIT_SEG_CLICK = 5;
var SIT_SEG_NAV = 7;
var SIT_PNT_CLICK = 9;
var SIT_PNT_NAV = 11;
var SIT_PNT_DEL = 13;
var SIT_PNT_APP = 15;
var SIT_PNT_INS_BEFORE = 16;
var SIT_PNT_INS_AFTER = 17;
var SIT_PNT_NEW = 18;

// Zoom levels...
var ZOOM_MAX = 19;
var ZOOM_MIN = 5;

/*
MODES... 
1. Base - Add a route or click on a route (route not selected yet)
2. Base with selection - Add a route, click on another route, edit or delete selected route 
3. Open route - Route is open, segment(s) and point(s) are selected.  Submodes:
   a. Append point - Append points to open route.
   b. Insert point before - Insert point before selected point (not allowed if multiple points selected)
   c. Insert point after - Insert point after selected point (not allowed if multiple points selected)
SAVE STATES...
1. Buffered changes to existing route
2. Buffered changes to new route - not finished yet (may save as route in progress?)
3. Finished route, ready for save
4. Deleted route, ready for save
5. Buffered changes to multiple routes (new routes, deletions, edits)
NOTES...
- User will be prompted whether to save changes when route is finished
- Only intersections require changes to multiple routes at the same time
- Opening a route requires that it not be opened by another user.  Not sure how to handle.
  Maybe allow route checkin/out.  
*/


/************************************/
/* init() - Run upon page opening   */
/************************************/
function init()
{
	var idx;
	var glatlng;
	
	// Set site & layout global
	Site = SITE_EDITOR;
	Layout = LAYOUT_EDITOR;

	//debugger;
	if (GBrowserIsCompatible()) {	

		// Set up global objects
		ButtonStates = new ButtonInfo();
		MapEv = new MapEvents();

		// Set layout
		setLayout();
		// Set div dimensions based on window size
        // handleResize(); 

		// Draw map
        Map = new GMap2(document.getElementById("mapdiv"));

        Map.setCenter(new GLatLng (CENTER_LAT, CENTER_LNG), startZoom);
		Map.addControl(new GLargeMapControl());
		Map.addControl(new GMapTypeControl());
		Map.addControl(new GScaleControl());
		
		// Create custom marker icons
		createCustomMarkers();                  
		
		// Setup of Routemaker
		if (Site == SITE_EDITOR) {
			// Initialize into MODE_VIEW... (may override below)
			enterViewMode ();

			// Define map click event handler
			MapEv.setHandler (Map, 'GMap2', 'click', []);
			
		
			/*
			GEvent.addListener (Map, 'click', function(overlay, glatlng) {
				//debugger;
				if (overlay == null) {      // ignore case where overlay clicked on (overlay not null)
											// Google Maps API seems somewhat fluid - may change in the future
					mapClick(overlay, glatlng);
				}
			});
			*/
			
			// Set some other events
			setFieldEvents();

			// Initialize fields
			FldInfo.configFields_NoRoute();

			// Then set mode to MODE_NEW (as if New Route button were pressed...
			routeAdd ();			
			
		}   // end if Site = SITE_EDITOR
	}	

	openPrompt (
		"<p style=\"font-size:14px\">" + 
		"Click on the map to start a new route. This is a prototype and no routes will be saved. " +
		"</p>" + 
		"<p>See <a href=\"javascript:openHelp()\">online help</a> for a " + 
		"description of BikeZip functions.  The BikeZip project page is " +
		"at <a href=\"http://www.BikeMapProject.org\" target=\"_blank\">www.BikeMapProject.org</a>." + 
		"<p align='center'><button type='button' onclick='closePrompt()' name='yesbutton' style='width:60px'>OK</button></p>"  
	);   

	/* openPrompt (
		"<p style=\"font-size:14px\">Welcome to BikeZip!  This is a prototype and no routes will be saved.  " + 
		"Click anywhere on the map to begin a route.</p>" +
		"<p>BikeZip's goal is to let cyclists upload their knowledge of every street and path to create a " +
		"bike map that\'s massive, accurate and current.  The result will be a complete road and trail map from a " +
		"bicyclist\'s perspective, which could be viewed through BikeZip or used to support bike mapping services  " +
		"such as Google or Ride the City.  " + 
		"<p align='center'><button type='button' onclick='closePrompt()' name='yesbutton' style='width:60px'>OK</button></p>"  
	);   */ 
	

	// Test code...
	// MapEv.setHandler (Map, 'GMap2', 'moveend', []);
	MapEv.setHandler (Map, 'GMap2', 'zoomend', []);

		
	// Test AJAX
	// getServerXML();
}

function abc() 
{
	handleResize();
}


// alert ("Welcome to the BikeZip Mapmaker!  Not all features are turned on in this prototype.  To start, click on the 'New Route' button and then click on the map!");

/************************************/
/* Set window event procs           */
/************************************/
window.onload = init;
window.onunload = GUnload;
window.onresize = abc;
