/* This function is the meat of the map.  It creates the map
*  display and sets the default beginning viewpoint.
*/

// Global Variables
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map; 
var kmlLayer = null;
var currentLayer = null;
var loadBuilding = getQuerystring('b');
var camp = getQuerystring('camp');


// display interface stufff

		
		
$(document).bind('cbox_closed', function(){
		$('#hDiv').remove();
});

/*$(window).load(function() {
	$("#bottom, #thinControls").animate({'opacity':1},800);					
});*/


$(document).ready(function() {
	// Removed when directions menu removed
	/*$('#dmBtn').unbind().click(function(){
			toggDMenu();
							  });*/
	$('#bmBtn').unbind().click(function(){
			toggBList();
							  });
	
	
	
	initialize();
	
	
	$(".ui-button").click(function() {
		$(".ui-button").button("refresh");						   
	});

});


	
	
	
	
function toggBList() {
	
	$("#buildingsMenu").toggle(500);
}

// Removed when directions menu removed
/*function toggDMenu() {
	$("#directions").toggle(500);
}*/
	
function cleanCanvas() {
	$("#buildingsMenu").hide();
	//$("#directions").hide();
}
	
function showDButtons () {

	$("label[for*='checkDPark']").show();
	$("label[for*='checkInterest']").show();
	
	$("label[for*='checkVPark']").hide();
	$("label[for*='checkATM']").hide();
	$("label[for*='checkTransit']").hide();
}
	
function showNButtons() {

	$("label[for*='checkDPark']").hide();
	$("label[for*='checkInterest']").hide();
	
	$("label[for*='checkVPark']").show();
	$("label[for*='checkATM']").show();
	$("label[for*='checkTransit']").show();
}


function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

// The main function
function initialize() {
	document.getElementById("theForm").reset();
	// Starting point; Where the map defaults to
	var latlng = new google.maps.LatLng(43.9449069725596, -78.89595776796341);
	

	var myOptions = {
		zoom: 17,	// Default zoom level
		center: latlng,
		mapTypeControl: true,
   		mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, position:google.maps.ControlPosition.TOP_LEFT},
		navigationControl: true,
    	navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL, position: google.maps.ControlPosition.TOP_LEFT},

		mapTypeId: google.maps.MapTypeId.SATELLITE	// Default Map Type   //ROADMAP//SATELLITE//HYBRID//TERRAIN//
	};
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	 google.maps.event.addListener(map, 'zoom_changed', function() {
    	checkLabels();
     });

	

	// Initializes overlay(s) with the map (any overlays here appear onload)
	if (DetectIphoneOrIpod()) { initAllKml(); }

	
	// Initializes markers with the map (markers appear/do not appear dependant on binary value given in individual function)
	addAllMarkers();
	
	// Initializes markers for visitor and student/faculty parking
	//toggleParking();
	//toggleVParking();
	
	// Included to allow users to enter their own address in a text field
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(map);
	
	//populate the lists for start and destination
	makeBuildingList("north","start");  // first paramter is campus -- north || downtown // second parameter is id of select to be populated
	makeBuildingList("north","end");  // first paramter is campus -- north || downtown // second parameter is id of select to be populated

	// Instantiate an info window to hold step text for directions
	stepDisplay = new google.maps.InfoWindow();
	
	//use the info label class to put building names on the map for North Oshawa
	doMarkers(true);  // using false as the paramter will put building codes on the map, true will put full building names on 
	
	// and for downtown
	//dtMarkers();  // no paramters for this function
	
	
	// after timeout, this will call and clear a dummy direction request which solves the building cursor issue
	//var t=setTimeout("overrideKml()",100);
	
	overlayCheck();
	
	if (loadBuilding) {
		qsLoadBuilding(loadBuilding);
	}
	
	showNButtons ();
	

}

function qsLoadBuilding(loadBuilding) {
	if (camp=="n"){
		for (var i in names) {
		//check if it's a building
			if (names[i][4]==loadBuilding) makeInfoWindow(i,'n');
			cleanCanvas();
		}
	}
	else if (camp=="d"){
		for (var i in dtAdds) {
		//check if it's a building
			if (dtAdds[i][6]==loadBuilding) makeInfoWindow(i,'d');
			cleanCanvas();
		}
	}
}
