/* Original by DL, October 2007. Edited by AT, November 2009 */

function mapUnload() { 
	GUnload();
	//reset global variables
	linksDIV = null;
	ajax = null;
	gmarkers = null;
	ghtml = null;
	theNewsBox = null;
	theNewsBoxUL = null;
	theNewsHeading = null;
	newsLI = null;
	newslink = null;
	button = null;
	infodiv = null;
	showNews = null;
	parseNews = null;
	

}

if ( navigator.appVersion.indexOf("MSIE 6")>=0 ) {

	browser = "ie6";

} else {

	browser = "decent";
	
}

window.onload = function() {
	
	
	/*linksDIV = document.createElement("div");
	linksDIV.setAttribute("id", "text-links-div");
	linksDIV.className="text-links-div";
	linksHeader = document.createElement("h2");
	linksHeaderText = document.createTextNode("Birkbeck buildings");
	linksHeader.appendChild(linksHeaderText);
	linksDIV.appendChild(linksHeader);	
	document.getElementById("finding-Birkbeck").appendChild(linksDIV);
	document.getElementById("text-links-div").appendChild(textLinksUL);
	*/
	showMap();		
	//alert(document.getElementById("text-links-div").innerHTML);
}

window.onunload = mapUnload;



var ajax = new Ajax();



// buildings outside Birkbeck -- we will need to filter on these later
/*var restrictedIds = new Array('bm',
							  'mal01',
							  'ucck',
							  'senate-house',
							  
							  'ucl',
							  'ulu',
							  'wat',
							  'ucl',
							  'ioe',
							  'soas');
*/
/*if (browser=='ie6') {
*/	
	var restrictedIds = new Array(
						  'malt01', //Torrington Sq entrance
						  // 'mal01', //Malet St entrance
						  'clore', 
						  'mal02', 
						  'rus26',
						  'rus30',
						  'gow1016',
						  'gow4',
						  'gor50-51',
						  'tor32',
						  'ucls',
						  'gor29',
						  'tav32',
						  'gor3947',
						  'bed7',
						  'em23-29',
						  'braindev',
						  'egmont',
						  // Non-BBK locations 
						  'ich',
						  'soas1',
						  'soas2',
						  'lshtm1',
						  'lshtm2',
						  'lse',
						  'bm',
						  'ucl01',
						  'ucl02',
						  'ucl03',
						  'ucl04',
						  'ucl05',
						  'ucl06',
						  'ucl07',
						  'ucl08',
						  'ucl09',
						  'ucl10',
						  'sop',
						  'stewart',
						  'hult',
						  'friends',
						  'wmin1',
						  'wmin2',
						  'senate-house',
						  'ulu',
						  'wat',
						  'ucck',
						  'ucl',
						  'ioe',
						  'soas');
	
//}


// build the text links by creating html on the fly via createMarker -- store in these arrays
var gmarkers = new Array();
var ghtml = new Array();
var sgmarkers = new Array();
var sghtml = new Array();
var gcoords = new Array();

	
	


function buildNews(title, thelink, description, theTitle) {

	var theNewsBox = document.getElementById("newsbox");
	
	if (!document.getElementById("newslisting")) {
		var theNewsBoxID = theNewsBox.getAttribute("id");
		var button = makeCloseButton(theNewsBoxID);
		var newsBoxUL = document.createElement("ul");
		newsBoxUL.setAttribute("id", "newslisting");
		newsBoxUL.className = "newslisting";
		var theNewsHeading = document.createElement("h2");
		var theNewsHeadingText = document.createTextNode("News from the " + theTitle);
		theNewsHeading.appendChild(theNewsHeadingText);
		
		
	} else {
		
		var newsBoxUL = document.getElementById("newslisting");	
		
	}

	var newsLI = document.createElement("li");
	var newslink = document.createElement("a");
	newslink.setAttribute("href", thelink);
	newslink.setAttribute("title", "Read this story on the " + theTitle + " website");
	newslink.className = "news-story";
	var linktext = document.createTextNode(title);
	newslink.appendChild(linktext);
	newsLI.appendChild(newslink);
	newsBoxUL.appendChild(newsLI);
	if (button)
		theNewsBox.appendChild(button);
	if (theNewsHeading) 
		theNewsBox.appendChild(theNewsHeading);	
	theNewsBox.appendChild(newsBoxUL);
	newsBoxUL.className = "bgimg";
	
	
	
}


function noNews(theTitle) {
	
	theNewsBox = document.getElementById('newsbox');
	var theNewsBoxID = theNewsBox.getAttribute("id");
	var button = makeCloseButton(theNewsBoxID);
	nonewsp = document.createElement("p");
	nonewsHeader = document.createElement("h2");
	nonewsHeadingText = document.createTextNode("News from the " + theTitle);
	nonewsHeader.appendChild(nonewsHeadingText);
	theNewsBox.appendChild(button);
	theNewsBox.appendChild(nonewsHeader);
	nonewsText = document.createTextNode("There are no current news stories");
	nonewsp.appendChild(nonewsText);
	theNewsBox.appendChild(nonewsp);
	
	
	
}


var parseNews = function(str, str2) {

var deptName = str2;

try {
		var newsBox = document.getElementById("newsbox");
		newsBox.parentNode.removeChild(newsBox);
		var theNewsBox = document.createElement("div");
		theNewsBox.setAttribute("id", "newsbox");
		

} catch(e) {
		
	var theNewsBox = document.createElement("div");
	theNewsBox.setAttribute("id", "newsbox");
	
		
			
}



document.getElementById("finding-Birkbeck").appendChild(theNewsBox);
var title = str.getElementsByTagName('title');
var theTitle = title[0].firstChild.nodeValue;
var rss = str.getElementsByTagName('rss');
var channel = str.getElementsByTagName('channel');
var items = str.getElementsByTagName('item');

if ( items.length > 0 ) {
	
	var maxLength = 5;
	if (items.length <= maxLength) {
		theLength = items.length;	
	} else {
		theLength = maxLength;	
	}

	for (i=0; i < theLength; i++) {
		//alert(items[i].childNodes.length);
		// IE thinks there are 3 nodes; FF thinks there are 7
		if (items[i].childNodes.length == 3) {
		
			try {
				// get title, link and description
				var title = items[i].childNodes[0].firstChild.nodeValue;
				var thelink = items[i].childNodes[1].firstChild.nodeValue;
				var description = items[i].childNodes[2].nodeValue;
			} 
			
			catch (error) {
			
				// error!
			
			}
			
			
		
		} else if (items[i].childNodes.length == 7 ) {
		
			var title = items[i].childNodes[1].firstChild.nodeValue;
			var thelink = items[i].childNodes[3].firstChild.nodeValue;
			var description = items[i].childNodes[5].firstChild.nodeValue;
		
			
		}

	// build the news story
	buildNews(title, thelink, description, theTitle);
	}  
}

else {
	
	noNews(theTitle);
}

} 
	



var showNews = function(str, str2) {

var deptName = str2;

try {

	newsBox = document.getElementById("newsbox");
	newsBox.className = "news-show";
	newsBox.parentNode.removeChild(newsBox);
	theNewsBox = document.createElement("div");
	theNewsBox.setAttribute("id", "newsbox");
	theNewsBoxMsg = document.createElement("p");
	theNewsBoxMsgText = document.createTextNode("Loading news please wait");
	theNewsBoxMsg.appendChild(theNewsBoxMsgText);
	theNewsBox.appendChild(theNewsBoxMsg);
	document.getElementById("finding-Birkbeck").appendChild(theNewsBox);
	
	
} catch(e) {
	//alert(e);
	// do nothing

}

url = "http://" + str;

now = new Date();

proxyURL = "http://betula.ccs.bbk.ac.uk:7373/main/proxy?xml=" + url + "&amp;?date=" + now; 

ajax.doGet(proxyURL, parseNews, 'xml');

}

function manageDeptsInfo(address) {
	
		try { 

			var theNewsBox = document.getElementById("newsbox");
			document.getElementById("finding-Birkbeck").removeChild(theNewsBox);
			newsBox = document.createElement("div");
			newsBox.setAttribute("id", "newsbox");
	
		} catch(e) {
			newsBox = document.createElement("div");
			newsBox.setAttribute("id", "newsbox");
			// nothing to do
		
		}
		try {
			infodiv = document.getElementById("infodesc");
			infodiv.parentNode.removeChild(infodiv);
		} catch(e) {
			// nothing	
		}
		var recreateDiv = document.createElement("div");
		recreateDiv.setAttribute("id", "infodesc");
		recreateDiv.className = "depts";
	
		
		/*recreateDiv.appendChild(newsBox);*/
		//document.getElementById("body").appendChild(recreateDiv);
		document.getElementById("map").appendChild(recreateDiv);
		
}


function closeWin(divToClose) {
	
	try {
		
		var theDiv = document.getElementById(divToClose);
		//document.getElementById("body").removeChild(infodesc);
		theParentID = theDiv.parentNode.getAttribute("id");
		//alert(theDiv.parentNode.childNodes.length);
		document.getElementById(theParentID).removeChild(theDiv);
		// resize map
		
		if (divToClose == 'infodesc') {
			try {
				theNewsBox = document.getElementById("newsbox");
				document.getElementById("finding-Birkbeck").removeChild(theNewsBox);
				
			} catch(e) {
				// do nothing
				document.getElementById("map").className="map";
			}
			
		
		}
		document.location.href="#body";
		setCenter(map);
	} catch(e) {
		
		//alert(e);
	}
	
}

// fade effect on background
function setBackground(divID) {
	
	theDiv = document.getElementById(divID);
	theDiv.className="depts-show";
	//theDiv.setAttribute("style", "filter:alpha(opacity=80)");
	//theDiv.setAttribute("style","background-color:#F6F797;-moz-opacity:0.8");
	
	
	
}

function makeCloseButton(divID) {
	
	var button = document.createElement("div");
	button.className="button";
	buttonImg = document.createElement("img");
	buttonImg.setAttribute("src", "close.gif");
	buttonImg.setAttribute("width", "14");
	buttonImg.setAttribute("height", "13");
	buttonImg.setAttribute("alt", "Close this information window");
	buttonImg.setAttribute("border", "0");
	buttonLink = document.createElement("a");
	buttonLink.setAttribute("href", "javascript:closeWin('" + divID + "');");
	buttonLink.setAttribute("title", "Close this information window");
	buttonLink.appendChild(buttonImg);
	button.appendChild(buttonLink);
	return button
	
	
}

function showDepts(str, address) {
	// 

	
	document.getElementById("map").className="map-info-panel";
	gmap = getMap();
	
	
	
	newsBox = document.getElementById("newsbox");
	

	if ( document.getElementById("infodesc")) {
		;
		manageDeptsInfo(address);
	} else {
		
		infodiv = document.createElement("div");
		infodiv.setAttribute("id", "infodesc");
		
		br = document.createElement("br");
		infodiv.appendChild(br);
		infodiv.appendChild(br);
		//document.getElementById("body").appendChild(infodiv);
		document.getElementById("map").appendChild(infodiv);
		gmap.checkResize();
		
		manageDeptsInfo(address);
		
	}
	
	
	
	try {
		infoDiv = document.getElementById("infodesc");
		var infoDivID = infoDiv.getAttribute("id");
		closeButton = makeCloseButton(infoDivID);
		infoDiv.appendChild(closeButton);
		infoHeading = document.createElement("h2");
		infoHeadingText = document.createTextNode("Departments in " + address);
		infoHeading.appendChild(infoHeadingText);
		infoDiv.appendChild(infoHeading);
		//infoDiv.className="depts-show";
		setBackground(infoDivID);
		document.location.href="#body";
	} 
	
	catch(e) {
		
		
		
	}
	


fileName = str + ".xml";

//alert(infodiv);



GDownloadUrl(fileName, function(data, responseCode) {
	
	var xml = GXml.parse(data);	
	
	var getDepts = xml.documentElement.getElementsByTagName('department');
	
	var getCoords  = xml.documentElement.getElementsByTagName("coords");
	
	var mlat = getCoords[0].getAttribute("lat");

	var mlng = getCoords[0].getAttribute("lng");
		
	var mlatlng = new GLatLng(mlat, mlng);
	
	
	gmap.setCenter(mlatlng);
	
	var nodeArray = new Array();
	var eachDept = new Array();
	// feed department values into the nodeArray array
	for ( f=0; f < getDepts.length; f++) {
	
		dept = getDepts[f];
		// get each department tag
		for (g = 0; g < dept.childNodes.length; g++) {
			
			typeofnode = dept.childNodes[g].nodeName;
			// filter out whitespace nodes (FF, Opera etc.)
			if ( typeofnode != "#text" )  {
					// make a string out of all the departments: loc, website, newsfeed
					// we'll split this into an array later		
					
					var theNodeValue = dept.childNodes[g].firstChild.nodeValue;
					
					if ((dept.childNodes[g].nodeName == "website")) {
										 
						try {
								var restrictions = dept.childNodes[g].getAttribute("restrictions");
						} catch(e) {
							
								//var restrictions = "none";	
						}
										 
						eachDept = eachDept + theNodeValue + "," + restrictions + ",";
						
					} else {
					
						eachDept = eachDept + theNodeValue + ",";
						
					}
					
									
			}
		} // end getting child nodes of each <department> tag
		
		// convert eachDept into an array			
		
		var deptsInfo = eachDept.split(",");
		eachDept = '';
		
		// create HTML elements to hold information				
		var listdiv = document.createElement("ul");
		
		for (x=0; x < deptsInfo.length; x++) {
	
			if (x % 4 == 0 ) {				
					/*alert("x%3 is true and I'm at " + deptsInfo[x]
					+ "\n" + deptsInfo[x+1]
					+ "\n" + deptsInfo[x+2] );*/ 
				if (deptsInfo[x+2]) {		
					var innerdiv = document.createElement("div");
					// Get the department name in <name>
					var name = deptsInfo[x];
					// Get the department website in <website>
					var website = deptsInfo[x+1];
					// Get the department newsfeed in <newsfeed>
					var restrictions = deptsInfo[x+2];
					var newsfeed = deptsInfo[x+3];
					// make a link with name as link text and website as the url
					weblink = document.createElement("a");
					weblink.className = 'dept-website';
					weblink.setAttribute("title", "Link to " + name + " website");
					weblink.setAttribute("href", "http://" + website);
					if (restrictions != "null") 
						weblink.setAttribute("title", restrictions);					
					var newsfeedlink; // Is there a newsfeed? If yes, create a link to see latest news
					if ((newsfeed != "none") && (restrictions =="null")) {
						newsfeedlink = document.createElement("a");
						newsfeedlink.setAttribute("href", 'javascript:showNews("' + newsfeed + '",  "' + name + '");');
						newsfeedlink.setAttribute("title", "Latest news from " + name);
						newsfeedtext = document.createTextNode("Latest news");
						newsfeedlink.className = "newsfeed";
						newsfeedlink.appendChild(newsfeedtext);
								
					} else {
						newsfeedlink = document.createTextNode(" ");
								
					}
					// Create the HTML
					weblink.appendChild(document.createTextNode(name));
					divcontent = document.createElement("li");
					divcontent.className = "dept-info";
					divcontent.appendChild(weblink);
					// only display separator if nodeType is an element (i.e. <a>)
					if (newsfeedlink.nodeType == "1")  {
							divcontent.appendChild(document.createTextNode(" | "));
					} else {
							divcontent.appendChild(document.createTextNode(" "));	
					}
					divcontent.appendChild(newsfeedlink);
					
					// Append the HTML to the #infodesc div
					appendHTML(divcontent);
								
				} // end if
												
			} // end modulus check

		} // end loop for each department's child nodes			
	} // end loop for each department 
	
}) // end GDownload call


}

function appendHTML(html) {

	if ( !document.getElementById("dept-listing")) {
	
		var deptListing = document.createElement("ul");
		deptListing.setAttribute("id", "dept-listing");
		var list = document.getElementById("infodesc").appendChild(deptListing);
		document.getElementById("infodesc").appendChild(list);
		document.getElementById("dept-listing").appendChild(html)
	
	} else {
	
		document.getElementById("dept-listing").appendChild(html);
	
	}

}


function createLineBreak() {

	lineBreak = document.createElement("br");
	return lineBreak;

}

var m = 0;
var textLinksUL = document.createElement("ul");


// create UL for stations and tubes

//var tubeTextUL = document.createElement("ul");
//tubeTextUL.setAttribute("id", "tube-links");

//textLinksUL.setAttribute("id", "text-links");

// open info  window
function createMarker(map, mkrMgr, lat, lng, tab1Text, address, minZoom, zl ) {
	
	
	GEvent.addListener(map, "zoomend", function(oldZ,newZ) { 
			var zoomLevel = newZ; });
	
	try {
		zoomLevel = zoomLevel
	} catch(e) {
		zoomLevel = zl;	
	}

	// What happens when you click on a link...
	
	
	
	var point = new GLatLng(lat, lng);
	var marker = new GMarker(point);
	
	// build text links
	// get current zoom level


	gmarkers[m] = marker;
	ghtml[m] = tab1Text;
	latlng = new Array();
	latlng[0] = lat;
	latlng[1] = lng;
	gcoords[m] = latlng;
	
	this.resetCentre = function(mnumber) {
		mnumber = parseFloat(mnumber);
		lat_to_ctr = parseFloat(gcoords[mnumber][0]);
		lng_to_ctr = parseFloat(gcoords[mnumber][1]);
		newctr = new GLatLng(lat_to_ctr, lng_to_ctr);
		map.setCenter(newctr);
	}
	
/*	
	m ++;
	// if not a station or a tube link
	var textLinksLI = document.createElement("li");
	
	if (m < 12 ) {
		textLinksLI.className="left-col";
	}
	if (m >=12) {
		textLinksLI.className="right-col";	
	}
	var textLinksA = document.createElement("a");
			textLinksA.setAttribute("href", "javascript:goToMarker('" + minZoom + "', '" + m + "', '"  +  zoomLevel + "');");
	
	textLinksText = document.createTextNode(address);
	
	textLinksA.appendChild(textLinksText);
	textLinksLI.appendChild(textLinksA);
	document.getElementById("text-links").appendChild(textLinksLI);
*/	
	
	this.goToMarker = function(minZoom, markerno, zoomLevel) {
		// resize map
		
		document.getElementById("map").className="map";
		map.checkResize();
		i_d = document.getElementById("infodesc");
		n_b = document.getElementById("newsbox");
		if (i_d) {
			i_d.parentNode.removeChild(i_d);							
		}
		if (n_b) {
			n_b.parentNode.removeChild(n_b);
		}
		var levelToZoom = minZoom;
		
		if ( levelToZoom < 16 ) {
			levelToZoom = 16;	
		}
		
		
		
		ctr = new GLatLng(lat,lng);
		//map.zoomTo(new GLatLng(lat,lng),parseFloat(levelToZoom));
		//map.panTo(ctr)
		map.setCenter(ctr);
		map.setZoom(parseFloat(levelToZoom));
		
		
				
		txt = ghtml[markerno-1];
		
		setTimeout(function() {
							m_num = markerno-1
							resetCentre(m_num);							
							gmarkers[markerno-1].openInfoWindowHtml(txt);
							}, 5);
				
							} // end goToMarker
	
	
	
	GEvent.addListener(marker, "click", function() {
		document.getElementById("map").className="map";
		marker.openInfoWindowHtml(tab1Text);
		manageDeptsInfo(address);
	});
	mkrMgr.addMarker(marker, minZoom);
}


// function to centre on stations
var sm = 0;
var stationinfowintext = new Array();	
	
function createStationMarkers(map, mkrMgr, station_type, name, lat, lng, connections) {
	
		
		var lat = lat;
		var lng = lng;
		
		// Name and connection details for info window
		sinfodiv = document.createElement("div");
		
		name_p = document.createElement("p");
		name_p.appendChild(document.createTextNode(name));
		name_p.className = 'station-name';
		conn_ul = document.createElement("ul");
		conn_ul.className="station-connection-list";
		
	
		for (si = 0; si < connections.length; si++) {
		
			conn_li = document.createElement("li");
			conn_text = document.createTextNode(connections[si]);
			conn_li.appendChild(
								conn_text);
			conn_ul.appendChild(conn_li);
		} // end loop
		
	
		
		
		
		
								
		
	
		// Does an overall #stations div exists? If not, create one
/*		if (!document.getElementById("stations")) {
			
			st_div = document.createElement("div");
			st_div.setAttribute("id", "stations");
			st_header = document.createElement("h2");
			st_headerText = document.createTextNode("Connections");
			st_header.appendChild(st_headerText);
			st_div.appendChild(st_header);
			document.getElementById("map").appendChild(st_div);
			
		}	else {
		
			st_div = document.getElementById("stations");
		} // end 
		
		
		// Does a #mainline div exist? If not, create one
		if (!document.getElementById("mainline")) {
			
			mainline_div = document.createElement("div");
			mainline_div.setAttribute("id", "mainline");
			
			mainline_header = document.createElement("p");
			mainline_header.className = "station-type";
			mainline_headerText = document.createTextNode("Rail");
			mainline_header.appendChild(mainline_headerText);
			mainline_div.appendChild(mainline_header);
			st_div.appendChild(mainline_div);
		} else {
			
			mainline_div = document.getElementById("mainline");
			
		} // end 
		
		 // Does a #tube div exist? If not, create one
		 if (!document.getElementById("tube")) {
			 
			 tube_div = document.createElement("div");
			 tube_div.setAttribute("id", "tube");
			 tube_header = document.createElement("p");
			 tube_header.className="station-type";
			 tube_headerText = document.createTextNode("Tube");
			 tube_header.appendChild(tube_headerText);
			 tube_div.appendChild(tube_header);
			 st_div.appendChild(tube_div);
		 } else {
			
			tube_div = document.getElementById("tube");
		 } // end 
		 
		 if (station_type=="tube") {
			div_to_addto = 'tube';						
		 } else  {
			div_to_addto = 'mainline'; 
		 }
		 
		 
		 
		 try {
			 document.getElementById("stations").appendChild(div_to_addto);		 
		 } catch(e) {
			 // pass
		 }
		 document.getElementById(div_to_addto).appendChild(name_p);
		 
		tabText = document.createElement("div");
		tfl = document.createElement("div");
		tfl.innerHTML="<a title=\"Visit the Transport for London Journey Planner\" href=\"http://journeyplanner.tfl.gov.uk/user/XSLT_TRIP_REQUEST2?language=en&execInst=&sessionID=0&ptOptionsActive=-1&place_destination=London\"><img src=\"http://www.tfl.gov.uk/tfl/gettingaround/journeyplanner/banners/images/jp-banner-3.gif\" title=\"Visit the Transport for London Journey Planner\" /></a>";
		tabText.className="stations-info";
		tabText.appendChild(name_p);
		tabText.appendChild(conn_ul);
		tabText.appendChild(tfl);
	
		var spoint = new GLatLng(lat, lng);
		var smarker = new GMarker(spoint);
		map.addOverlay(smarker);
		
		if (!document.getElementById("tube-ul")) {
			var sTextTubeUL = document.createElement("ul");
			sTextTubeUL.setAttribute("id", "tube-ul");
		} else {
		
			sTextTubeUL = document.getElementById("tube-ul");
		}
		
		if (!document.getElementById("rail-ul")) {
			var sTextRailUL = document.createElement("ul");
			sTextRailUL.setAttribute("id", "rail-ul");
		} else {
			sTextRailUL = document.getElementById("rail-ul");	
		}
		
		sgmarkers[sm] = smarker;
		stationinfowintext[sm] = tabText;
	
		
	
		this.addlistener = function(markerno, dom) {
				GEvent.addListener(sgmarkers[sm], "click", function() {
									   smarker.openInfoWindow(dom);
												  
												  });
		}
		addlistener(sm, stationinfowintext[sm]);
		sm++;
			
	
		
		var stextLinksLI = document.createElement("li");
		var stextLinksA = document.createElement("a");
		stextLinksA.setAttribute("href", "javascript:showStation('" + sm + "');");
		stextLinksText = document.createTextNode(name);
		stextLinksA.appendChild(stextLinksText);
		stextLinksLI.appendChild(stextLinksA);
		if (station_type=='rail') {
			sTextRailUL.appendChild(stextLinksLI);	
		} else {
			sTextTubeUL.appendChild(stextLinksLI);	
		}
	
		
		
	this.showStation = function(stationMarkerNo)  {
			
			
			
			setTimeout(function() {sgmarkers[stationMarkerNo-1].openInfoWindow(stationinfowintext[stationMarkerNo-1])}, 500);
		
			
												  
	}
	
	
			mkrMgr.addMarker(smarker);
	
	try {
		document.getElementById("mainline").appendChild(sTextRailUL);
	} catch(e) {
		// pass
	}	
	
	try {
		document.getElementById("tube").appendChild(sTextTubeUL);
	} catch(e) {
		// pass 	
	}
	

	
*/
	
	} // end createStationMarkers()






function showMap() {
// Create map

var map = new GMap2(document.getElementById("map"));
// Add controls
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

// resize map
this.getMap = function() {

	return map;

}

// Default zoom level
var zl = 15;
map.setCenter(new GLatLng(51.52226588191527,-0.13085424900054932),zl);

this.forceResize = function() {
	
	map.checkResize();	
	
}

// Create marker manager -- allows for plotting of points at different resolutions
var mkrMgr = new GMarkerManager(map);

// Set to hybrid type map
//map.setMapType(G_HYBRID_TYPE);
map.enableContinuousZoom();

zoomdiv = document.getElementById("zoomend");


var locations = new Array('malt01', //Tavistock Sq entrance
						  //'mal01', //Malet St entrance
						  'clore', 
						  'mal02', //SNHB
						  'rus26',
						  'rus30',
						  'gow1016',
						  'gow4',
						  'gor50-51',
						  'tor32',
						  'ucls',
						  'gor29',
						  'tav32',
						  'gor3947',
						  'bed7',
						  'em23-29',
						  'braindev',
						  'egmont',
						  // Non-BBK locations 
						  'ich',
						  'soas1',
						  'soas2',
						  'lshtm1',
						  'lshtm2',
						  'lse',
						  'ucl01',
						  'ucl02',
						  'ucl03',
						  'ucl04',
						  'ucl05',
						  'ucl06',
						  'ucl07',
						  'ucl08',
						  'ucl09',
						  'ucl10',
						  'sop',
						  'stewart',
						  'hult',
						  'friends',
						  'wmin1',
						  'wmin2'
						  /*'bm',
						  'senate-house',
						  'ulu',
						  'wat',
						  'ucck',
						  'ucl',
						  'ioe',
						  'soas'*/
						  );

var locationHTML = '';
for (i=0; i < locations.length; i++) {

	// Download the XML files
	locationCode = locations[i];
	fileName = locationCode + ".xml";
	GDownloadUrl(fileName, function(data, responseCode) {
	
		// function to be called by GDownloadURL
		var xml = GXml.parse(data);
		
		// get the location name and co-ordinates
		
		
		var locationId = 	xml.documentElement.getAttribute('id');
		var getCoords = 	xml.documentElement.getElementsByTagName('coords');
		var getAddress = 	xml.documentElement.getElementsByTagName('locationName');
		var minZoom =		parseFloat(xml.documentElement.getAttribute("minZoom"));
		
		/*try {
			var getFullNames =	xml.documentElement.getElementsByTagName('fullName');
			var fullName =		getFullNames[0].firstChild.nodeValue;
		} catch(e) {
			var fullName = '';
		}*/
		
		var address = 		getAddress[0].firstChild.nodeValue;
		var lat = 			parseFloat(getCoords[0].getAttribute("lat"));
		var lng = 			parseFloat(getCoords[0].getAttribute("lng"));
		
		// generate the text for the 2 tabs -- do we need 2?
		
		var tab1 = "<div class=\"tabText\">";
		tab1 = tab1 + "<div class=\"address\">" + address + "</div>";
		// send this off to create the markers
		var inf = document.createElement("div");
		// create a div with an id of the location code
		inf.className = "dept";
		
		var isRestricted = 'no';
		for (i=0; i <= restrictedIds.length; i++ ) {
			if (restrictedIds[i] == locationId) {
				isRestricted = "yes";
				break;
			} 
				
			
		}
		
		if (isRestricted != 'yes') {
				tab1 = tab1 +  "<p class=\"tab\"><a href=\"\javascript:showDepts('" + locationId + "', '" + address + "');\">" + "See a list of departments in " + address + "<\/a><\/p>";
				
		}
	
		tab1 = tab1 + "</div>";


	createMarker(map, mkrMgr, lat, lng, tab1, address, minZoom, zl);
	
	
	
	
	} //end  GDownloadURL function() call
	
	) // end GDownloadURL call
	/* construct the values to be passed to createMarker()
		 lat, lng, tab1Text, tab2Text
	*/
	} // end iterating over location xml files
	

	// Create stations
	
	GDownloadUrl('stations.xml', function(data, responseCode) {
										  
										  var stxml = GXml.parse(data);
										  
										  var stationsList = stxml.documentElement.getElementsByTagName("station");
										  
										  for (j = 0; j <= stationsList.length-1; j++) {
											
												/* for each station tag, get the following information:
													* co-ordinates (lat, lng)
													* station type
													* station name
													* connections												
												*/
												
												station_type = 			stationsList[j].getAttribute("type");
												station_name_list =		stationsList[j].getElementsByTagName("name");
												station_name = 			station_name_list[0].firstChild.nodeValue;
												station_coords_list = 	stationsList[j].getElementsByTagName("coords");
												station_lat = 			station_coords_list[0].getAttribute("lat");
												station_lng = 			station_coords_list[0].getAttribute("lng");
												station_conn_list =		stationsList[j].getElementsByTagName("connection");
												
												sc_conns = '';
												sc_conns = new Array();
												for (sc = 0; sc <= station_conn_list.length-1; sc++) {
														sc_conns[sc] = station_conn_list[sc].firstChild.nodeValue;
												}
												
												createStationMarkers(map, mkrMgr, station_type, station_name, station_lat, station_lng, sc_conns);
												
										  }		
										  
										  });
	// end GDownload for stations
	
	
	
} // end showMap()



	


