/////////// GLOBAL VARIABLES   /////////////////////////

//holder for direction step markers
var markerArray= new Array;

// array for north campus labels
ibLabel=new Array;

var uLabel=new Array;
var dLabel=new Array;
var sLabel=new Array;
var pLabel=new Array;
var rLabel=new Array;
var oLabel=new Array;




// array for downtown campus labels
dtLabel=new Array;


/// array of markers for entrances
var entArray;

// array of markers for wheelchair accessible entrances
var accArray;

// will hold direction display results further along
var directionsDisplay;

// Initialize Directions Object 
var directionsService = new google.maps.DirectionsService();

//
var infowindow="";

///////// variables for pathfinding algorithm /////
var inf=10000; 
var undef=-1;


//////////////////////////////////////////// PATHFINDING SECTION ////////////////////////////////////////////////////


// x will be substituted into the adjacency table to indicate a connection which cannot be made
var x=inf;

// variable which will store path information
var myPath=0;






////////////////// this section builds the adjacency table using lat/lng values///////////////////////////////////////////////////

// m will hold the distances between each point on the map
var m=new Array;

// m will have the same length as names, since they both store the same points
for (i=0; i<names.length; i++) {
	
	// create the new 2nd level array
	m[i]=new Array;
	
	// assign x (huge value) to everything for now
	// wherever x is assigned, no connection can be made
	for (b=0; b<names.length; b++) {
		m[i][b]=x;
	}
	
	
	// for every node this node can connect to, overwrite x with the actual distance to that node
	for (q=0;q<names[i][1].length; q++) {
		
		// put aside the node we are measuring for this iteration
		var connector=names[i][1][q];
		
		//calculate separation between two nodes to get distance
		if (names[connector]) {
			var distanceY=Math.abs(names[i][2]-names[connector][2]);
			var distanceX=Math.abs(names[i][3]-names[connector][3]);
			var total= Math.sqrt(distanceX*distanceX+distanceY*distanceY);
		}
		
		// assign distance to table
		m[i][connector]=total;
	}
	

}
/*// for testing
for (var j in m) {
	document.write(m[j]+"<br/>");
}
*/
////////////////////////////////////////////////////////////////////////////////

function weight(a,b){ 
	return m[a][b] 
}

function findPath(V,s,d){ 
var myLine= new Array;
var P = new Array(V) ;
var i ;
var done = new Array(V) ;
var pred = new Array(V) ;
for(i=0; i<V; i++){ 
P[i] = inf;
pred[i]=undef;
done[i]=false; } 

P[s]=0; 
var v; 

for(v=0; v<V; v++){ 
var minDist = inf, closest = -1 
	for (i=0; i<V; i++){ 
		if(!done[i]){ 
			if(P[i] <= minDist){ 
				minDist = P[i]; 
				closest = i; 
			} 
		} 
	} 
	done[closest] = true; 
	
	for (i=0; i<V; i++){ 
		if (!done[i]){ 
		var w = weight(closest, i); 
			if (P[closest]+w < P[i]){ 
				P[i] = P[closest] + w; 
				pred[i] = closest; 
			} 
	} 
} 
} 

//export our results for display

i=d;
if (P[i] < inf) { 

	var thePath = names[i][0]; 
	var v = i; 
	var lat;
	var lng;
	
	while (v>=0){ 
		//add LatLng objects to myLine array
		lat=names[v][2];
		lng=names[v][3];
		
		
		myLine[myLine.length]= new google.maps.LatLng(lat,lng);
		
		v = pred[v];
		if (v>=0) {
			thePath = names[v][0] + '->' + thePath; 
		}
	} 
	

	
	////// draw path on map//////////////////
	if (myPath!=0) {
		
		myPath.setMap(null);
	}
	
	 myPath = new google.maps.Polyline({
    	path: myLine,
		clickable:false,
   	 	strokeColor: "#000",
    	strokeOpacity: 1,
    	strokeWeight:6
  	});

  	myPath.setMap(map);
	//////////////////////////////////////////
	
	
} else { 
	alert("no path") 
} 

	
} 


////////////////////////////////////////// END PATHFINDING SECTION ///////////////////////////////




///////////////// OTHER FUNCTIONS /////////////////////

// function for sorting by number
function sortNumber(a,b)
{
return a - b;
}

function checkLabels() {
	var zLevel=map.getZoom();
	

	
	if (zLevel<16) {
		
		clearMarkers(null);
		
		
		$('.markerCB').attr('checked',false);		
		$('.markerCB').button('refresh');
		
		for (i in accArray) {
      		accArray[i].setMap(null);
    	}
		
		for (i in entArray) {
			//remove each marker
      		entArray[i].setMap(null);
    	}
		
		clearOverlays("emergency");
		emergencyArray = 1;
		
		clearOverlays("vparking");
		visitorParkingArray = 1;
		
		clearOverlays("elevator");
		elevatorArray = 1;
		
		/*
		clearOverlays("dining");
		diningArray = 1;
		
		clearOverlays("shopping");
		shoppingArray = 1;
		*/
		
		clearNMark();
	
	} else if (zLevel<17) {
		doMarkers(false);
		dtMarkers();
		$('#markCode').attr('checked',true);
		$('#markCode').button('refresh');
	}
	else doMarkers(true);
}



function makeInfoWindow(i, c) {
	
	
	// set hover styles
	var	hover="this.style.color='#0082d1', this.style.cursor='pointer', this.style.textDecoration='underline'";
		 	
	// wrap the building code in a span to make it clickable and add hover effects
	var labelText = "<a onClick=\"doColor('" + i +"','" + c + "');\">More Information</a>";
	
	
	//close any open info windows
	if (infowindow!="") {
		
		infowindow.close();
		infowindow="";
	}
	
	
	// if north oshawa....
	if (c=="n") {
		
		// ignore parking lots
		if (names[i][4]!="P") {
			
			// set the content of info window
			var cString= "<div style='text-align:left; padding-left:10px; '><div style='margin:0 0 5px; color:#003580; font-size:90%;'>" + names[i][0] + "</div><div style=\"float:left; width:160px; \"> " + names[i][7] + "</div><div style=\"float:left;  width:160px; font-size:90%;\"> " +  labelText  +  "</div><div style=\"clear:both;\"></div></div>";
			
			// initialize options
			var pos= new google.maps.LatLng(names[i][2],names[i][3]);
			infowindow = new google.maps.InfoWindow({
    			content: cString
				,position: pos
				,zIndex:99
				
			});
			//open it
			infowindow.open(map);
			
			
		}
	// same deal for downtown
	} else {
		
var cString= "<div style='text-align:left; padding-left:10px; '><div style='margin:0 0 5px; color:#003580; font-size:90%;'>" + dtAdds[i][0] + "</div><div style=\"float:left; width:160px; \"> " + dtAdds[i][4] + "</div><div style=\"float:left;  width:160px; font-size:90%;\"> " +  labelText  +  "</div><div style=\"clear:both;\"></div></div>";
			var pos= new google.maps.LatLng(dtAdds[i][2],dtAdds[i][3]);
			infowindow = new google.maps.InfoWindow({
    			content: cString
				,position: pos
				,zIndex:99});
			infowindow.open(map);
			
	}
	
	$('#iWinLabel').click(function() {
				doColor(i,c);				   
			}).hover(function() {
				$(this).css({"color":"#0082d1","cursor":"pointer","textDecoration":"underline"}),
				$(this).css({"color":"#000"})
			});
}



// this function gets called when user requests directions
function directMe() {
	
	//get start and destination from dropdown lists
	var from=document.getElementById("start").value;
	
	var to = document.getElementById("end").value;
	
	//check to see if the request is for downtown locations
	if (isNaN(from)){
		
		//if so use the google directions object
		gDirects(from,to);
	
	// if not....
	} else {
	
		/////////  stuff for panning to directions ///////////////////
	
		// array of 2 lats and 2 lngs for boundaries
	
		var lats=new Array;
		var lngs=new Array;
	
		lats[0]=names[from][2];
		lats[1]=names[to][2];
	
		lngs[0]=names[from][3];
		lngs[1]=names[to][3];
	
		// sort the arrays so lowest value is at top
		lats.sort(sortNumber);
		lngs.sort(sortNumber);
	
		// southwest lat is the higher number
		var swLat=lats.shift();
	
		// ne lat is the higher number (now the only one left in the array)
		var neLat=lats.shift();
	
		// same goes for longitudes
		var swLng=lngs.shift();
		var neLng=lngs.shift();
	
		// create LatLng objects for bounds
		var swBound = new google.maps.LatLng(swLat,swLng);
		var neBound = new google.maps.LatLng(neLat,neLng);
	
		//create bounds object
		var bndBox=new google.maps.LatLngBounds(swBound,neBound);
	
	
		//make sure start and end are not the same
		if (from==to) {
			alert("Pick two different points!");
		} else {
			//finally pan to bounds
			map.fitBounds(bndBox);
			
			// and now call the pathfinder function
			findPath(m.length, from, to);
		}
	}
}

//function for getting and displaying directions for downtown campus
// uses google road directions rather than custom algorithm
// start and end are taken form user selections
function gDirects(start, end) {
	
  //clear any existing directions		
  clearDirections();
  
  
  
  
  //get directions if start and end positions are not the same
  if (start!=end){
  	
 	 
	// set options for directions request
	var request = {
   	 	origin:start, 
    	destination:end,
    	travelMode: google.maps.DirectionsTravelMode.WALKING
  	};
	
	//perform the request
 	directionsService.route(request, function(result, status) {
    	
		//display results if we get a valid response from google
		if (status == google.maps.DirectionsStatus.OK) {
     		directionsDisplay.setDirections(result);
	  		
			
			// this function call puts a marke ron the map at each step
			// used only if user requests directions back to north oshawa campus
			if (start=="2000 Simcoe Street North, Oshawa, Ontario" || end=="2000 Simcoe Street North, Oshawa, Ontario") {
				showSteps(result);
			}
		}
  	});
	
	//update the map with directions
  	directionsDisplay.setMap(map);
  }
}

//this function displays the steps for directions
// directionResult is th information we get back from google
function showSteps(directionResult) {
  // For each step, place a marker, and add the text to the marker's
  // info window. Also attach the marker to an array so we
  // can keep track of it and remove it when calculating new
  // routes.
  var myRoute = directionResult.routes[0].legs[0];

  for (var i = 0; i < myRoute.steps.length; i++) {
      var marker = new google.maps.Marker({
        position: myRoute.steps[i].start_point, 
        map: map
      });
      attachInstructionText(marker, myRoute.steps[i].instructions);
      markerArray[i] = marker;
  }
}

//put the instruction text into marker pop ups
// marker is the icon to be modified
//text is the text to be attached

function attachInstructionText(marker, text) {
  google.maps.event.addListener(marker, 'click', function() {
    stepDisplay.setContent(text);
	
    stepDisplay.open(map, marker);
  });
}


//function for populating drop down location lists
// campus= north || downtown, dropList= id of select to be populated

function makeBuildingList(campus,dropList) {
	
	// if north osh cmapus...
	if (campus=="north") {
		//clear the select
		document.getElementById(dropList).innerHTML="";
		var u=new Array;
		var d=new Array;
		var s=new Array;
		var p=new Array;
		var r=new Array;
		var o=new Array;
		
		var text;
		var opt;
	
		// for each point
		for (i=0; i<names.length; i++) {
			//check if it's a building
			if (names[i][0]!="") {
				// create the option
				if (names[i][8]=="u") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					u.push(opt);
				}
				
				if (names[i][8]=="d") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					d.push(opt);
				}
				
				if (names[i][8]=="s") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					s.push(opt);
				}
				
				if (names[i][8]=="p") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					p.push(opt);
				}
				
				if (names[i][8]=="r") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					r.push(opt);
				}
				
				if (names[i][8]=="o") {
					opt=document.createElement("OPTION");
					opt.innerHTML=names[i][0] + " - " + names[i][4];
					opt.value=i;
					o.push(opt);
				}
				
			}
		}
		
		//append all option tags to respective lists
		for  (z in u) {
			document.getElementById(dropList).appendChild(u[z]);
		}
		
		for  (z in d) {
			document.getElementById(dropList).appendChild(d[z]);
		}
		
		for  (z in s) {
			document.getElementById(dropList).appendChild(s[z]);
		}
		
		for  (z in p) {
			document.getElementById(dropList).appendChild(p[z]);
		}
		
		for  (z in r) {
			document.getElementById(dropList).appendChild(r[z]);
		}
		
		for  (z in o) {
			document.getElementById(dropList).appendChild(o[z]);
		}
		
		
	
	
		

	
	//if it's downtown........
	} else {
		var list;
		//clear the select
		document.getElementById(dropList).innerHTML="";
		
		// for each building
		for (var i in dtAdds) {
			// create the option
			var myOption=document.createElement("OPTION");
				
			//set it's text
			myOption.innerHTML=dtAdds[i][0];
				
			//set it's value
			myOption.value=dtAdds[i][2]+","+dtAdds[i][3];
				
			//append it to select
			document.getElementById(dropList).appendChild(myOption);
		}
		
		
	
	}
	
	// call function to build dropdown menu
	makeBldAccordion(campus);	
	
}

// since parking has no content, we just move view to that spot
function panToPark(i) {
  map.panTo(new google.maps.LatLng(names[i][2],names[i][3]));
  map.setZoom(18);
  doMarkers(true);
}

// function for building and changing building accordion list
function makeBldAccordion(c){
	var cat;
	
	//clear all tabs
	document.getElementById("uoitList").innerHTML="";
	document.getElementById("durhamList").innerHTML="";
	document.getElementById("sharedList").innerHTML="";
	document.getElementById("parkList").innerHTML="";
	document.getElementById("resList").innerHTML="";
	document.getElementById("outList").innerHTML="";
	if (c=="north") {
		

		for (var i in names) {
			//check if it's a building
			if (names[i][0]!="" && names[i][4]!="P") {
				var campus="n";
		 		
				//set hover styles 
		 		var hover="this.style.color='#0082d1'; this.style.cursor='pointer'";
		 		
				// get catgeory
				cat=names[i][8];
				
				
				// add a link for each building to appropriate tabs 
				switch (cat) {
					case "u":
						document.getElementById("uoitList").innerHTML += "<a href='#' style='color:black; text-decoration:none;' onclick=\"makeInfoWindow(" + i + ",'" + campus + "'), cleanCanvas()\" class=\"iLabel\" onmouseover=\"" + hover + "\" onmouseout=\"this.style.color='black'\" >" + names[i][0] + "</a><br/>";
					break;
					case "d":
						document.getElementById("durhamList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"makeInfoWindow(" + i + ",'" + campus + "'),  cleanCanvas()\" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\" >" + names[i][0] + "</a><br/>";
					break;
					case "s":
						document.getElementById("sharedList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"makeInfoWindow(" + i + ",'" + campus + "'),  cleanCanvas()\" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\" >" + names[i][0] + "</a><br/>";
					break;
					
					case "r":
						document.getElementById("resList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"makeInfoWindow(" + i + ",'" + campus + "'),  cleanCanvas()\" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\" >" + names[i][0] + "</a><br/>";
					break;
					
					case "o":
						document.getElementById("outList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"makeInfoWindow(" + i + ",'" + campus + "'),  cleanCanvas()\" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\" >" + names[i][0] + "</a><br/>";
					break;
					
					
					
				
				}
				
			}
			
			// parking is a special case since it doesn't have text/image content
			if (names[i][4]=="P") {
				hover="this.style.color='#0082d1'; this.style.cursor='pointer'";
				
				document.getElementById("parkList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"panToPark("+i+"), cleanCanvas() \" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\"  >" + names[i][0] + "</a><br/>";
		}
	
		
		}

		
	} else {
		
		
		// smae deal as above but for downtown
		for (var i in dtAdds) {
			//check if it's a building
			if (dtAdds[i][2]!=inf) {
				var campus="d";
		 		var hover="this.style.color='#0082d1'; this.style.cursor='pointer'";
	
				document.getElementById("uoitList").innerHTML+= "<a href='#' style=\"color:black; text-decoration:none;\" onclick=\"makeInfoWindow(" + i + ",'" + campus + "'),  cleanCanvas()\" class=\"iLabel\" onmouseover=\""+hover+"\" onmouseout=\"this.style.color='black'\" >" + dtAdds[i][0] + "</a><br/>";
				
				
			}
		}
	}
	
	//make sure the first tab (UOIT) starts opened
	$("#buildingsMenu").accordion("activate" , 0);
	
}



	

// function for adding or removing downtown markers

function dtMarkers() {
	// for each downtown building....
	
	
	
	if (dtLabel.length<1){
	
	for (var i = 0; i < dtAdds.length; i++) {
		
		// make sure not to add a marker for north osh campus
		if (dtAdds[i][2]!=inf) {
			
			var campus="d";
			//set text as building name
			var dtText="<span id='dl"+i+"' style=\"width:100%; display:block\">" + dtAdds[i][0] + "</span>";
			
		
			// get latitude and longitude for this bulding
			var ibLat = dtAdds[i][2];
	   		var ibLng = dtAdds[i][3];
  		
		 	//create lat long object	
		 	var pos = new google.maps.LatLng(ibLat,ibLng);
  		 
		 	//set label options
		 	var myOptions = {
			 content: dtText
			 
			,boxStyle: {
			   border: "1px solid #fff"
			   ,background:"#eaeaea"
			  ,textAlign: "center"
			  ,fontSize: "9px"
			  ,width:"80px"
			  ,fontFamily:"Arial, Helvetica, Sans-serif"
			  
			 }
			,disableAutoPan: true
			,pixelOffset: new google.maps.Size(-40, 0) 		
			,position: pos
			,closeBoxURL: ""
			,isHidden: false
			,pane: "floatPane"
			,zIndex:5
			,enableEventPropagation: true
			
			
			};
		
			//create label object
			dtLabel[i] = new InfoBox(myOptions);

		
			/*if (document.getElementById("checkUOIT").checked) {
				dtLabel[i].open(map); 
			}*/
		
		
  		}
			dtLabel[i].open(map); 
	
	}
	


	   			
		
	
	
	}
	
	 
		
		
		
	    // ensure proper display state of buttons
		$('#markClear').attr('disabled',false);
		$('#markCode').attr('disabled',false);
		$('#markName').attr('disabled',true);
		
		
		$("#labelChanger").buttonset('refresh');
		
 		setTimeout("attachDtHandlers()",1000);
}

function attachDtHandlers() {
	
					for (var z in dtLabel) {
		
		
						if ($('#dl'+z)) {
    					$('#dl'+z).unbind().click(function() {
						
						doColor(this.id.substr(2),'d');				   
						}).hover(
							function() {
								$(this).css({"color":"#0082D1","cursor":"pointer"});
							},
							function() {
								$(this).css({"color":"#000"});
							}
						);
						}

					}
	
}

function attachNHandlers() {
	
					for (var z in names) {
						if (names[z][4]!="P") {
    					$('#nl'+z).unbind().click(function() {
						
							doColor(this.id.substr(2),'n');				   
						});
						
						$('#nl'+z).hover(
							function() {
								$(this).css({"color":"#0082D1","cursor":"pointer"});
							}
							,
							function() {
								$(this).css({"color":"#000"});
							}
						);
						}

					}
	
}

function clearMarkers(set) {
	
	if (set==null) {
		
		// ensure proper display state of buttons
		document.getElementById("markClear").checked=true;
		
		
		$('#markClear').attr('disabled',true);
		$('#markCode').attr('disabled',false);
		$('#markName').attr('disabled',false);
		
		
		$("#labelChanger").buttonset('refresh');
		
		//loop through and clear north campus labels
		for (l in uLabel) {
			uLabel[l].close();
		
	 	 }
	  	uLabel=new Array;
		
		for (l in dLabel) {
			dLabel[l].close();
		
	 	 }
	  	dLabel=new Array;
		
		for (l in sLabel) {
			sLabel[l].close();
		
	 	 }
	  	sLabel=new Array;
		
		for (l in pLabel) {
			pLabel[l].close();
		
	 	 }
	  	pLabel=new Array;
		
		for (l in rLabel) {
			rLabel[l].close();
		
	 	 }
	  	rLabel=new Array;
		
		for (l in oLabel) {
			oLabel[l].close();
		
	 	 }
	  	oLabel=new Array;
		
		
	 	 //loop through and clear downtown labels
	 	 for (l in dtLabel) {
			dtLabel[l].close();
		
	 	}
	 	 dtLabel=new Array;
	
	} else {
		
		switch (set) {
			case "u":
				for (l in uLabel) {
					uLabel[l].close();
		
	 	 		}
	  			uLabel=new Array;
			break;
			case "d":
				for (l in dLabel) {
					dLabel[l].close();
		
	 	 		}
	  			dLabel=new Array;
			break;
			case "s":
				for (l in sLabel) {
					sLabel[l].close();
		
	 	 		}
	  			sLabel=new Array;
			break;
			case "r":
				for (l in rLabel) {
					rLabel[l].close();
		
	 	 		}
	  			rLabel=new Array;
			break;
			case "p":
				for (l in pLabel) {
					pLabel[l].close();
		
	 	 		}
	  			pLabel=new Array;
			break;
			case "o":
				for (l in oLabel) {
					oLabel[l].close();
		
	 	 		}
	  			oLabel=new Array;
			break;
		
			case "dt":
				for (l in dtLabel) {
					dtLabel[l].close();
		
	 			}
	 	 		dtLabel=new Array;
			break;
		
		}
	
	
	
	}
	
	
	  
}

function markVis(clear, cat){
	if (clear) {
		switch (cat) {
			case "u":
				for (l in uLabel) {
					uLabel[l].hide();
		
	 	 		}
	  			
			break;
			case "d":
				for (l in dLabel) {
					dLabel[l].hide();
		
	 	 		}
	  			
			break;
			case "s":
				for (l in sLabel) {
					sLabel[l].hide();
		
	 	 		}
	  			
			break;
			case "r":
				for (l in rLabel) {
					rLabel[l].hide();
		
	 	 		}
	  			rLabel=new Array;
			break;
			case "p":
				for (l in pLabel) {
					pLabel[l].hide();
		
	 	 		}
	  		
			break;
			case "o":
				for (l in oLabel) {
					oLabel[l].hide();
		
	 	 		}
	  			
			break;
		
			case "dt":
				for (l in dtLabel) {
					dtLabel[l].hide();
		
	 			}
	 	 		
			break;
		}
	} else {
		switch (cat) {
			case "u":
			
				for (l in uLabel) {
					uLabel[l].show();
		
	 	 		}
	  			
			break;
			case "d":
				for (l in dLabel) {
					dLabel[l].show();
		
	 	 		}
	  			
			break;
			case "s":
				for (l in sLabel) {
					sLabel[l].show();
		
	 	 		}
	  			
			break;
			case "r":
				for (l in rLabel) {
					rLabel[l].show();
		
	 	 		}
	  			
			break;
			case "p":
				for (l in pLabel) {
					pLabel[l].show();
		
	 	 		}
	  		
			break;
			case "o":
				for (l in oLabel) {
					oLabel[l].show();
		
	 	 		}
	  			
			break;
		
			case "dt":
				for (l in dtLabel) {
					dtLabel[l].show();
		
	 			}
	 	 		
			break;
		}
	}
}

// function which gets called to create colorbox
function doColor (i,c) {
	
	
	//close info winodow to avoid graphic glitches in certain browsers
	if (infowindow!="") {
		infowindow.close();
		infowindow="";
	}
	
	//fetch info from appropriate arrays and asign to generic variables
	var code;
	var title;
	var descrip;
	var images=new Array;
	
	if (c=="n") {
		title=names[i][0];
		code=names[i][4];
		descrip=names[i][9];
		for (var z in names[i][10]) {
			images[z]=names[i][10][z];
		}
	} else if (c=="d") {
		
		title=dtAdds[i][0];
		code=dtAdds[i][6];
		descrip=dtAdds[i][7];
		for (var z in dtAdds[i][8]) {
			images[z]=dtAdds[i][8][z];
		}
	}
			
	//create hidden container	
	$('body').append("<div style='display:none;' id='hDiv'></div>");
	
	//add content div
	$("#hDiv").append("<div id='cbHolder' ></div>")

	//add inner wrapper (kind of redundat, but here just in case)
	$("#cbHolder").append("<div id='cbInter'  class='cBox'></div>");
	
	//add image wrapper div
	$("#cbInter").append("<div class='imgHolder'></div>");
	
	//loop through images and add them to image holder
	for (var z in images) {
		$(".imgHolder").append("<img src='images/cb_content/"+images[z]+"' alt='"+title+"' width='400' height='500'/>");
	}

	//add div for text
	$("#cbInter").append("<div class='textHolder'></div>");
	
	
	//add text
	$(".textHolder").append("<p class='cHead'>"+title+"</p>"+descrip);
	
	
	// call cycler function on images to create slideshow
	$('.imgHolder').cycle({fx: 'fade' });
	
	// call colorbox on newly created content
	$.fn.colorbox({inline:true, href:"#cbHolder", innerWidth:750, innerHeight:500});
	
	//an event listener, defined in initialize.js, will remove the markup when colorbox closes
	
}



/// function for putting label text markers for north osh on map
// if full is true, names will be put on map.  if false, building codes
function doMarkers(full) {
		
		clearMarkers(null);
		
	  // for each building
	  for (var i = 0; i < names.length; i++) {
	  
	  //make sure it's a building and not a point
	  if (names[i][4]!="") {
		  
		  var cat=names[i][8];
		 
		 //get the name or code
		 
		 var labelText;
		 var myWidth;
		 var offset;
		 
		 var campus="n";
		 
		 var hover="";
		 
		 var cf="";
		 
		 
		 //dont add hover effect for parking lots
		 if (names[i][4]!="P") {
			 hover="this.style.color='#0082d1'; this.style.cursor='pointer'";
			 cf="doColor('"+i+"','"+campus+"')";
		 }
		 
		 // check if we're using full name or building code
		 if (full) {
			 
			 //ensure proper button state
			 $('#markClear').attr('disabled',false);
			 $('#markCode').attr('disabled',false);
			 $('#markName').attr('disabled',true);
			 
			 document.getElementById("markName").checked="checked";
			 
			 $('#labelChanger').buttonset('refresh');
			 
			 
			 // set text as building name...
			 // wrap the building name in a span to make it clickable and add hover effects
			labelText =  "<span style=\"width:100%; display:block\" id='nl"+i+"' class='iLabel'   >" + names[i][0] + "</span>";
			 
			 // set label width and offset to fit name
			 myWidth="70px";
			 offset=-35;
		 
		 } else {
			  //ensure proper button state
			 $('#markClear').attr('disabled',false);
			 $('#markCode').attr('disabled',true);
			 $('#markName').attr('disabled',false);
			 
			 document.getElementById("markCode").checked="checked";
			 $('#labelChanger').buttonset('refresh');
			 
			 
			 
			 
			 //get the building code
			 var innerText=names[i][4];
			 
			 var hover="";
		 
		 
		 	//dont add hover effect for parking lots
			 if (names[i][4]!="P") {
			 	hover="this.style.color='#0082d1'; this.style.cursor='pointer'";
				cf="doColor('"+i+"','"+campus+"')";
		 	}
			 
			 
		 	
			// wrap the building code in a span to make it clickable and add hover effects
			labelText = "<span style=\"width:100%; display:block\" id='nl"+i+"' class='iLabel'   >" + innerText + "</span>";
			
			//set label width and offset to fit building code
			myWidth="25px";
			offset=-12;
			
		 }
		
		 
		 // get lat and long
		 var ibLat = names[i][2];
		 var ibLng = names[i][3];
  		
		 //create lat long object	
		 var pos = new google.maps.LatLng(ibLat,ibLng);
  		 
		 //set label options
		 var myOptions = {
			 content: labelText
			 
			,boxStyle: {
			   border: "1px solid #fff"
			   ,background:"#eaeaea"
			  ,textAlign: "center"
			  ,fontSize: "9px"
			  ,width:myWidth
			  ,fontFamily:"Arial, Helvetica, Sans-serif"
			 
			  
			 }
			,disableAutoPan: true
			,pixelOffset: new google.maps.Size(offset, 0) 	
			,position: pos
			,closeBoxURL: ""
			,isHidden: false
			,pane: "floatPane"
			
			,zIndex:5
			,enableEventPropagation: true
			
			
		};
		
		dLabel[i] = new InfoBox(myOptions);	
		dLabel[i].open(map); 
		/*switch (cat) {
			case "u":
				//create label object
				
				
				uLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkUOIT").checked) {
					
					uLabel[i].open(map); 
				}
				
			break;
			case "d":
				//create label object
				dLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkDC").checked) {
					dLabel[i].open(map); 
				}
			break;
			case "s":
				//create label object
				sLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkShared").checked) {
					sLabel[i].open(map); 
				}
			break;
			case "r":
				//create label object
				rLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkResidences").checked) {
					rLabel[i].open(map); 
				}
			break;
			case "p":
				//create label object
				pLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkParking").checked) {
					pLabel[i].open(map); 
				}
			break;
			case "o":
				//create label object
				oLabel[i] = new InfoBox(myOptions);		
		
				if (document.getElementById("checkRecreation").checked) {
					oLabel[i].open(map);
				}
			break;
		}*/
	  
	  }
 }
 
 	setTimeout("attachNHandlers()",1000);
 	doMark();


}






//move view between campuses
function panCampus(c) {
	
	if (c=="n") {
		showNButtons();
		var pos=new google.maps.LatLng(43.9449069725596, -78.89595776796341);
	} else {
		showDButtons();
		var pos=new google.maps.LatLng(43.897401474010834, -78.86035144329071);
	}
	

	
	
	
  // clear any google road directions
  clearDirections();
	
  //clear any custom path lines	
  if (myPath!=0) {
		
		myPath.setMap(null);
  }
  
  map.setMapTypeId(google.maps.MapTypeId.SATELLITE);
  
  //pan map using the latitude and longitude number variables and zoom to default
  map.panTo(pos);
  map.setZoom(17);
  
  if (c=="n") {
	  makeBuildingList("north","start");
	  makeBuildingList("north","end");
	  clearMarkers(null);
	  doMarkers(false);
	  
	  //ensure proper button states
	 $("#markCode").attr('checked',true);
 	 $("#markCode").button("refresh");
	  
  } else {
	  makeBuildingList("downtown","start");
	  makeBuildingList("downtown","end");
	  clearMarkers(null);
	  dtMarkers();
	  
	   //ensure proper button states
	  $("#markName").attr('checked',true);
 	  $("#markName").button("refresh");
  }
  

}

//get  google road directions off the screen
function clearDirections() {
	//remove the actual direction line
	directionsDisplay.setMap(null);
	
	//loop through direction markers and remove them

	for (var i = 0; i < markerArray.length; i++) {
		markerArray[i].setMap(null);
	}
	stepDisplay.close();
}

function doMark() {
	var myLatLng= new google.maps.LatLng(43.94774771516745, -78.89120757579803);
	 var marker = new google.maps.Marker({
      position: myLatLng, 
      map: map, 
	  icon:"images/trans.png",
      title:"What's up budday!"
  });   
}

//function for adding or removing accessibility markers
function toggAcc() {
	
	// check to see if markers are already set
	if (accArray) {
		// remove each marker
    	for (i in accArray) {
      		accArray[i].setMap(null);
    	}
		//clear the array
		accArray=null;
    } else {
	
		// reset the array
		accArray=new Array;
		
		//for each node...
		for (var i = 0; i < names.length; i++) {
	  
	 		 //check to see if it's an accesible entrance
	 		 if (names[i][5]) {
		 
				// get lat and long
				var ibLat = names[i][2];
		 		var ibLng = names[i][3];
				
				// create lat lng object
		 		var pos=new google.maps.LatLng(ibLat,ibLng);
		 
		 		// create marker
		 		var marker = new google.maps.Marker({
					clickable:false,
    				position: pos,
    				map: map,
					icon: './images/access.jpg',
					zIndex:97
  				});
				
				// add marker to end of array
 				accArray.push(marker);
			 
			}
		}
	}
	
}


// function for adding or removing entrance markers
function toggEnt() {
	
	
	//check to see if entrance markers are already up
	if (entArray) {
    	for (i in entArray) {
			//remove each marker
      		entArray[i].setMap(null);
    	}
		//clear the array
		entArray=null;
    } else {
	
	
		// reset array
		entArray=new Array;
		
		for (var i = 0; i < names.length; i++) {
	  
	 		 //make sure it's an entrance
	 		 if (names[i][6]) {
		 
				// get lat and long
				 var ibLat = names[i][2];
		 		var ibLng = names[i][3];
		 
		 		//create lat lng object
		 		var pos=new google.maps.LatLng(ibLat,ibLng);
		 
		 		// create marker
		 		var marker = new google.maps.Marker({
    				clickable:false,
					position: pos,
    				map: map,
					icon: './images/door.png',
					zIndex:96
  				});
				
				//add marker to end of array
 				entArray.push(marker);
			 }

  		
		
	  
		}
	}
}

// An all-around inelegant hack which makes use of a bug to disable the pointer cursor for kml overlays
/*var oK=0;
function overrideKml(){
	if (oK==0){
	findPath(m.length, 0, 0);
	myPath.setMap(map);
	oK=1;
	}else{
	}
}*/
