var map		// this is our map

var customMap;	// ISU map overlay

var defaultCenterPoint = new GLatLng(DEFAULT_CENTER.y, DEFAULT_CENTER.x);//Default center point - different from default point cordinates

var markerArray = new Array();	// array that stores all of our markers

var markerDataArray = new Array();	// array that stores the marker's string and html its info window

var markerEnabledArray = new Array();	// array that stores each marker's status
										// true if marker is currently enabled
var clickable = new Array();	// true if layer's locations can be clicked on map without layer turned on

var layersChecked = new Array();	// true if layer is currently enabled

curInfoWindowI = -1;	// layer number of current marker that has an info window open
curInfoWindowJ = -1;	// location number of current marker that has an info window open

var currentPopUp = "none"; //tracks status of pop-up html window

var printMap = true; //Prevents functions not needed from running for printMap.shtml

var mapLoad = true; //flag for determining if the map is initially loading

// getMapURLParameter
// extracts data after the "?" from the URL
// currentSelection is the current marker with an info window open
// latLng is the set of current map coordinates
// currentZoom is the last map zoom level
// layers is an array of numbers, either 0 or 1, for the status of each respective layer
//                 0 means layer is off
//                 1 means layer is on
// returns an object of the currentSelection and layers array
function getMapURLParameter() {

	var dataFromURL = window.location.href;

	//Checks for and reads query string
	if(dataFromURL.indexOf("?") > -1)
	{
		var rtnQS;
		var currentSelection = "none";
		var latLng = defaultCenterPoint;
		var currentZoom = 17;
		var mapType = "campus";
		var layers = "none";
		var mapSec = "main";

		rtnQS =  getQS("s", dataFromURL);
		if (rtnQS)
			currentSelection = rtnQS;
			
		rtnQS =  getQS("c", dataFromURL);
		if (rtnQS) 
		{
			var currentLocationSplit = rtnQS.split(',');
			latLng = new GLatLng(currentLocationSplit[0], currentLocationSplit[1]);
		}
		
		rtnQS =  getQS("z", dataFromURL);
		if (rtnQS)
			currentZoom = rtnQS*1;
		
		rtnQS =  getQS("t", dataFromURL);
		if (rtnQS)
			mapType = rtnQS;

		tnQS =  getQS("m", dataFromURL);
		if (rtnQS)
			mapSec = rtnQS;
			
		rtnQS =  getQS("l", dataFromURL);
		if (rtnQS)
			layers = rtnQS.split(',');;

	}
	else
	{
		currentSelection="none";
		latLng = defaultCenterPoint;
		currentZoom=DEFAULT_ZOOM;
		layers = "none";
		mapType = "campus";
	}
	
	return {currentSelection : currentSelection, latLng : latLng, currentZoom : currentZoom, layers : layers, mapType: mapType, mapSec : mapSec};
}

function getQS(qs, specificQS)
{
	qs += "=";
	var dataFromURL = window.location.href;

	if (specificQS)
	{
		dataFromURL = specificQS;
	}

	if (dataFromURL.indexOf("?")>-1)
	{
		dataFromURL = dataFromURL.substring(dataFromURL.indexOf("?")+1);
	
		var i = dataFromURL.indexOf(qs);

		if (i>-1)
		{
			dataFromURL = dataFromURL.substring(i + qs.length);
			//alert(dataFromURL);
			if (dataFromURL.indexOf("&") > -1)
			{
				return dataFromURL.substring(0,dataFromURL.indexOf("&"));
			}
			else
			{
				return dataFromURL;
			}
			
		}
		else
			return null;
	}
	else
		return null;
}

var urlData = getMapURLParameter();		// this is the object where we store our data received from another page

// checkBoxUpdate
// based on the layers array from urlData:
//        markers will be turned on if the respective array location is a 1
// any checkbox that is checked, the layer will be turned on
function displayMarkers() 
{
	if (urlData.layers!="none")
	{
		for(var i = 0; i < urlData.layers.length; i++)
		{
			var chkBox = leftNav[LEFT_NAV_INFO][i][0]; //Collect root check box name
			var chkAll = chkBox.substring(0,3) + 'All' + chkBox.substring(3); //Create id for all chkAll
			var chkLength = leftNav[LEFT_NAV_INFO][i][1];
			var layerNum = leftNav[LEFT_NAV_INFO][i][2];

			var numOfChk="";

			var urlLayerData = urlData.layers[i];
			
			if (urlLayerData.indexOf("a") > -1) //If all check boxes in layer are checked
			{
				numOfChk="a";
			
			}
			else if (urlLayerData.indexOf("n") > -1) //If no check boxes are checked
			{
				numOfChk="n";
			
			}
			else //If some check boxes are checked - saves 0 for not checked, 1 for checked
			{
				numOfChk = urlLayerData.substring(urlLayerData.indexOf("s") + 1);
				numOfChk = convertLayerString(numOfChk, 1, chkLength).toString();
			}
			

			//Run functions to place markers on map
			if (numOfChk=="a")
			{
				/*var chkBox = leftNav[LEFT_NAV_INFO][i][0];
				var chkAll = chkBox.substring(0,3) + 'All' + chkBox.substring(3);*/
				layersChecked[layerNum] = true;
				updateNavLeft(chkAll);
			}
			else if (numOfChk!="n")
			{
				var subChk = numOfChk.split('');
				
				for (x in subChk)
				{
					if (subChk[x]==1)
					{
						markerEnabledArray[layerNum][x] = 1;
						updateNavLeftSub(chkBox + x)
					}
				}	
			}		
		}
	}
}

function showLocation()
{
	for(var i = 0; i < markerDataArray.length; i++)
	{
		for(var j = 0; j < markerDataArray[i].length; j++)
		{
			if(urlData.currentSelection == markerDataArray[i][j].name || urlData.currentSelection == markerDataArray[i][j].abrv)
			{
				if(markerEnabledArray[i][j]==0)	// if marker is disabled, then enable just the marker
				{
					enableMarker(i, j);
				}
			}
		}
	}
}

function moreUpdate()
{
	var rtnQS =  getQS("a");
	if (rtnQS)
	{
		selEventArray = convertLayerString(rtnQS, 1).substring(1).split("");
		showHideLayers();
	}
}

//show the marker
function enableMarker(layerNum, locNum)
{
	map.addOverlay(markerArray[layerNum][locNum]);
	markerArray[layerNum][locNum].clickable=false;
}

function load()
{
	if (GBrowserIsCompatible())
	{		
		map = new GMap2(document.getElementById("map"));
			
		initTileOverlay(); //creates custom tile layer - mapTileLayer.js		

		//Add Controls, Map Type, Scale
		var ControlPosition = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(280,10));
		map.addControl(new GScaleControl(), ControlPosition);	// scale bar
		var navIn = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
		map.addControl(new GLargeMapControl(), navIn);
		
		zoom(); // Calls function to restrict the zoom level
		map.enableScrollWheelZoom();

		//Set map type based on query string
		if (urlData.mapType=="campus")
			map.setMapType(customMap);
		else if (urlData.mapType=="sat")
			map.setMapType(G_SATELLITE_MAP);
		else if (urlData.mapType=="hybrid")
			map.setMapType(G_HYBRID_MAP);
		else
			map.setMapType(customMap);

		map.setCenter(urlData.latLng, urlData.currentZoom); //center map based on query string or default

		new GKeyboardHandler(map); //Adds keyboard controls

		map.removeMapType(G_NORMAL_MAP);
		
		//Loads all the initial overlays (ex. default marker)
		//Secondary maps typically have more overlays
		//Found in mapCampus.js, specialMap.js, etc.
		loadInitOverlay();	
		
		displayMarkers(); //Read layer information and check appropriate boxes and add markers

		if (urlData.currentSelection != "returnNone" && urlData.currentSelection != "none")
		{
			showLocation();	 //Display marker and pop-up of current selection
		}

		moreUpdate() //Display more button info
		
		//Display Accessibility Legend if zoom is higher than 16
		if (map.getZoom()>16 && map.getCurrentMapType() == customMap)
			showLegend("accLegend");
		else
			hideLegend("accLegend");
		
		loadListeners(); //Add map Listeners

		mapLoad = false;

		document.getElementById('mapLoad').style.display='none'; //Hides loading screen
	}
	else
	{
		window.location.href = "/accessibility/browsercompatibility.shtml";
	}
	
}

//Get LayerNum based on Checkbox Name
function getLayerNum(chkBox)
{
	if (chkBox.length>6)
	{
		chkBox = chkBox.substring(0,6);
	}

	for (i=0;i<leftNav[LEFT_NAV_INFO].length;i++)
	{
		if (chkBox == leftNav[LEFT_NAV_INFO][i][0].substring(0,6))
		{
			return leftNav[LEFT_NAV_INFO][i][2];
		}
	}
}


