
var directions; //Stores url string for getting directions

function buildTabsHtml(layerNum, locNum)
{
	//Get name of marker
	//Looks for the bold tags
	//Must follow template for html pop-up info to work properly
	var markerLocation = markerDataArray[layerNum][locNum].name;
	//markerLocation = markerLocation.substring(markerLocation.indexOf("<h2>") + 4, markerLocation.indexOf("</h2>"));

	//Create Tabbed Pop-Up
	
	var directionsTab = "Start address:<br /><input type='text' SIZE=40 MAXLENGTH=40 name='saddr' id='saddr' value='' /><br>";


	//Build Directions tab
	if (markerDataArray[layerNum][locNum].noParkOpts) //Exclude parking location options
	{
		directionsTab += "<br /><div id='button' class='buttonBig' onclick='getDirections(" + layerNum + ", " + locNum + ")'>Get Directions</div>";
	}
	else//Add parking location options 
	{
		directionsTab += "To:<br /><select id='location' name='location'><option value='location'>" + markerLocation + "</option><option value='visitor'>Visitor Parking</option><option value='pay'>Pay Lot or Garage</option><option value='faculty'>Faculty Lot</option><option value='commuter'>Commuter Lot</option><option value='student'>Guranteed Student Lot</option><option value='meters'>Meter</option></select><br />";
		directionsTab += "<br/><div id='button' class='buttonBig' onclick='getClosestParking(" + layerNum + ", " + locNum + ")'>Get Directions</div>";
	}

	//Build htmls array
	var htmls = [
		"<div style='min-width:'+htmls.length*88+'px;'>" + markerDataArray[layerNum][locNum].html + "</div>",
		directionsTab
	];
	
	var tab1="Information";

	if(markerDataArray[layerNum][locNum].tabTitle)
		tab1=markerDataArray[layerNum][locNum].tabTitle;

	//Tab Labels Array
	var labels = [tab1,"Directions"];

	//Add Events tab
	if (markerDataArray[layerNum][locNum].evtHtml)
	{
		htmls.push(markerDataArray[layerNum][locNum].evtHtml);
		labels.push("Events");
	}
	else if (!markerDataArray[layerNum][locNum].noEvtTab)
	{
		for (var i in eventLoc)
		{
			if (markerLocation==eventLoc[i])
			{
				var limit=0;
				var eventTab = "<div style='clear:both; overflow:auto; max-height:155px; margin-right:10px; max-width:325px'>";

				for (var j in locEventData[2])
				{
					if (markerLocation==locEventData[2][j])
					{
						limit++;
						eventTab +=
							"<div id='title'><a target='_blank' href='" + locEventData[0][j] + "'>" + locEventData[1][j] + "</a></div>" +
							"<div style='margin:0 0 5px 0' id='description'>" + locEventData[3][j] + "</div>";	
					}
					if (limit==3)
					{
						break;
					}
				}
				
				eventTab += "</div>";
				htmls.push(eventTab);
				labels.push("Events");
				break;
			}
		}
	}

	//Tabs array
	var tabs = [];
	for (var i=0; i<htmls.length; i++) {
		tabs.push(
			new GInfoWindowTab(
				labels[i],
				'<div class="tabbody" style="margin:0; padding:0">' + htmls[i] + '</div>'
				)
			);
	}

	return tabs;
}

//Displays the directions redirection and info modal box
//Used for markers that selecting nearest parking doesn't make sense (ex. Parking and Emergency Phone Markers)
function getDirections(layerNum, locNum)
{
	var html = markerDataArray[layerNum][locNum].name;
	//html = html.substring(html.indexOf("<h2>") + 4, html.indexOf("</h2>")); //Get title of marker
	html = html.replace("#",""); //Emergency phone titles have '#', removed because they don't play nice with Google Map directions

	//Dynamically writes info to modal box
	document.getElementById('otherParking').style.display='none';
	document.getElementById('proxLoc').innerHTML= "directly";
	document.getElementById('parkingLoc').innerHTML= "";
	document.getElementById('selectionLoc').innerHTML=html;

	showModal('wrapModal');

	//Creates Google maps redirection url
	directions = "http://maps.google.com/maps?saddr=" + document.getElementById('saddr').value + "&daddr=" + html + "@" + markerArray[layerNum][locNum].getLatLng();
}

//Finds closest Parking to selected marker
//Display directions redirection and info modal box to nearest parking or directly to marker location
function getClosestParking(layerNum, locNum)
{
	
	var html;
	var selection = document.getElementById("location").value;

	var selectedLoc = markerDataArray[layerNum][locNum].html;
	var newLoc = "";
	var proxLoc = "to the closest";

	if (selection!="location")
	{
		var x1 = markerArray[layerNum][locNum].getLatLng().x;
		var y1 = markerArray[layerNum][locNum].getLatLng().y;
		
		var curMin = 999999999;	// current mininum distance from click location to marker
		var locNum = 0;	// layer number of marker with smallest distance from click location

		switch(selection){
			case "visitor":
				layerNum = PARKING_VISITOR;
				newLoc="Visitor Parking";
				break;
			case "pay":
				layerNum = PARKING_PAY_LOTS_GARAGES_NUM;
				newLoc="Pay Lot or Garage";
				break;
			case "faculty":
				layerNum = PARKING_FACULTY_LOTS;
				newLoc="Faculty Lot";
				break;
			case "commuter":
				layerNum = PARKING_COMMUTER_LOTS;
				newLoc="Commuter Lot";
				break;
			case "student":
				layerNum = PARKING_GUARANTEED_STUDENT_LOTS;
				newLoc="Student Lot";
				break;
			case "meters":
				layerNum = PARKING_METERS;
				newLoc="Meter";
				break;
			default: layerNum = PARKING_VISITOR; newLoc="Visitor Parking";
		}
	
		//check the distance between every marker and the click location
		//find the least distance using the distance formula

		for(var i = 0; i < markerArray[layerNum].length; i++)
		{
			if (markerArray[layerNum][i]!==0)
			{		
				var x2 = markerArray[layerNum][i].getLatLng().x;
				var y2 = markerArray[layerNum][i].getLatLng().y;

				if(Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) < curMin)
				{
					curMin = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
					locNum = i;
				}
			}
		}
	}
	else
	{
		proxLoc="directly";
	}

	html = markerDataArray[layerNum][locNum].html;

	//Dynamically writes info to modal box
	document.getElementById('proxLoc').innerHTML= proxLoc;
	if (newLoc!="")
	{
		document.getElementById('parkingLoc').innerHTML= newLoc + "&nbsp;";
	}
	document.getElementById('selectionLoc').innerHTML=selectedLoc.substring(selectedLoc.indexOf("<h2>") + 4, selectedLoc.indexOf("</h2>"));
	document.getElementById('otherParking').style.display='block';

	showModal('wrapModal');

	//Creates Google maps redirection url
	directions = "http://maps.google.com/maps?saddr=" + document.getElementById('saddr').value + "&daddr=" + html.substring(html.indexOf("<h2>") + 4, html.indexOf("</h2>")) + "@(" + markerArray[layerNum][locNum].getLatLng().y.toString().substring(0,7) + "," + markerArray[layerNum][locNum].getLatLng().x.toString().substring(0,8) + ")";

}
