summaryrefslogtreecommitdiff
path: root/map.js
blob: 748e9ae00fd2ee593c3858d9d9838f9c8d6e5bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//tool to update dataase: display markers and edit positions
//http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback
//nb in google maps it says "DIT" in the middle of the provost's stables
/*
showPlaces(type)
showPlace(index)



*/
//------------------------------------------------------------------------globals
var http = getHTTPObject(); //old ajax method
var markers=[];
var markertypes=['Building','Parking','Entrances']; //--lat/lng based tables to create markers
var tables=[];
var tablenames=['AZ','Lecture_theatres','Pacr','Libraries','Loops','Enabled_toilets']; //--tables reference building table
var tablescreennames={'Lecture_theatres':"Lecture theatres",'Pacr':"Public access computer rooms",'Libraries':"Libraries",'Loops':"Hearing aid looops",'Enabled_toilets':"Enabled toilets"};
var icontypes=['Loops','Enabled_toilets'];
var places=[];
var placetypes=new Array('Lecture_theatres','Libraries','Pacr');
var screenplacetypes=new Array('Lecture theatres','Libraries','Public access computer rooms');
for (i=0;i<placetypes.length;i++) { 
	places[placetypes[i]]=[];
	places[placetypes[i]].screenname=screenplacetypes[i];
}
var popularplaces=[56,59,198];
var popularnames=["Old Library","Campanile","Samuel Beckett Theatre"];
var mainCampus=new G.LatLngBounds(new G.LatLng(53.34,-6.26),new G.LatLng(53.346,-6.2499));
var show_parking_warning=true;
var InfoWindow; //points to created infowindows

var directionsService;
var directionsDisplay;

var do_menu_flag = false;
//------------------------------------------------------------------------classes
function place(name,building,info) {
	//represents an entity that can appear in a menu
	this.name=name;
	this.building=building;
	this.info=info;
}
//------------------------------------------------------------------------startup
//NB alert doesn't work within a jquery callback but console.log does
function onLoad() {
	if(document.getElementById("map")) {
		var mapOptions = {
			center: STARTPOS,
			zoom: 17,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		map = new G.Map(document.getElementById("map"),mapOptions);
		directionsService = new G.DirectionsService();
		directionsDisplay = new G.DirectionsRenderer({Map:map});
		//load data via ajax - chaining other loading functions
		loadTables(tablenames); 
		
	}
}
function loadTables(t) {
	t.forEach(function(table) {
		$.getJSON("./loadTable.php","table="+table,function(d) {
			$.each(d, function(idx,v) {
				tables[idx]=[];
				for (i in v) {
					tables[idx].push(v[i]);
				}
				if (DEBUG) console.log("loaded table "+Object.keys(tables).length+":"+idx+","+tables[idx].length+" items");
				if (Object.keys(tables).length==tablenames.length) loadMarkers(markertypes); 
			});
		});
	});
}
//how to know when everything is loaded via ajax and data can be processed
function loadMarkers(m) { //---------build customised markers from a database table
	m.forEach(function(marker) {
		$.getJSON("./loadTable.php","table="+marker,function(d) { 
			$.each(d, function(idx,v) {
				markers[idx]=[];
				for (i in v) {
					markers[idx][i]=makeMarker(v[i],idx);
					//console.log("making "+i+": "+
				}
				markers[idx].visible=false;
				if (DEBUG) console.log("loaded markers "+Object.keys(markers).length+":"+idx+","+markers[idx].length+" items");
				if (Object.keys(markers).length==markertypes.length) {
					prepareData();
					do_menuchoices();
					console.log("search:"+search_posted);
					if (search_posted!="") onsearch(search_posted);
				}
			});
		});
	});
}
function makeMarker(data,type) {
	if (DEBUG) console.log(type+":"+data['lat']+","+data['lng']); //alert doesn't work within a jquery callback but console.log does
	var m=new G.Marker({position:new G.LatLng(data['lat'],data['lng']),map:null,draggable:EDITABLE});
	m["type"]=type;
	for (j in data) m[j]=data[j];
	m.places=[];
	m.tabVisible=false;
	m.visible=false;
	if (EDITABLE){
		G.event.addListener(m,"dragend",setMarkerPosition);
		G.event.addListener(m,"click",displayMarkerInfo);
		if (type=="Building"&&m.google_coords==1) {
			m.setIcon(new G.MarkerImage("http://maps.google.com/mapfiles/marker_orange.png"));
		}
	}
	switch (type) {
		case "Parking":
			m.setIcon(new G.MarkerImage("img/marker_parking.png"));
			break;
		case 'Entrances':
			m.setIcon(new G.MarkerImage("img/marker_entrance.png"));
			break;
		case 'Loops':
			m.setIcon(new G.MarkerImage("img/marker_loop.png"));
			break;
		case 'Enabled_toilets':
			m.setIcon(new G.MarkerImage("img/marker_accessible_toilet.png"));
			break;
		case 'Ramps':
			m.setIcon(new G.MarkerImage("img/marker_ramp.png"));
			break;
		default:
			break;
	}
	m.setShadow(new G.MarkerImage("img/marker_shadow.png",new G.Size(30,30),new G.Point(0,0),new G.Point(6,29)));
	if (type=="Building") {
		m.sortPlaces=function() {m.places.sort(function(a,b) {if (a.name<b.name) return -1; else return 1; }); }
		m.show = function (tab){
			if (!this.visible) {
				this.dispphoto=httpexists("./photos/small/"+this.id+".jpg");
				if (this.places.length>1||this.description) { //multi-tab
					this.showtab = function() {
						purgeInfoWindow();
						InfoWindow=new InfoBubble({disableAnimation:true});
						InfoWindow.addTab('Building',this.getPlacesTab());
						InfoWindow.addTab('More',this.getInfoTab());
						InfoWindow.open(map,this);
						this.tabVisible=true;
					}
				} //single tab
				else {
					this.showtab = function() {
					purgeInfoWindow();
					InfoWindow=new InfoBubble({content:this.getPlacesTab(),disableAnimation:true});;
					InfoWindow.open(map,this);
					this.tabVisible=true;
					}
				}
				G.event.addListener(this,"click",this.showtab);
				this.setMap(map);
				var boundscheck=map.getBounds();
				if ((!boundscheck.contains(this.getPosition()))&&this.type=="Building") //only zoom to accomodate buildings
				{
					boundscheck.extend(this.getPosition());
					var newzoom = getZoomByBounds( map, boundscheck );
					map.setZoom(newzoom);
					map.setCenter(boundscheck.getCenter());
				
				}
				this.visible=true;
			}
			if (tab) this.showtab();
		}
	}
	if (type=="Entrances") { //construct tabs
		m.show = function (tab){
			this.showtab = function() {
				purgeInfoWindow();
				InfoWindow=new InfoBubble({content:this.getEntranceTab(),disableAnimation:true});
				InfoWindow.open(map,this);
				this.tabVisible=true;
			}
			G.event.addListener(this,"click",this.showtab);
			this.setMap(map);
			var boundscheck=map.getBounds();
			if ((!boundscheck.contains(this.getPosition()))&&this.type=="Building") //only zoom to accomodate buildings
			{
				boundscheck.extend(this.getPosition());
				var newzoom = getZoomByBounds( map, boundscheck );
				if (newzoom<15) map.setMapType(G_NORMAL_MAP);
				map.setZoom(newzoom);
				map.setCenter(boundscheck.getCenter());
			}
			this.visible=true;
			if (tab) this.showtab();
		}
	}
	if (type=="Enabled_toilets") { //construct tabs
		m.show = function (tab){
			if (this.data !="") {
				this.showtab = function() {
					purgeInfoWindow();
					InfoWindow=new InfoBubble({content:this.getToiletTab(),disableAnimation:true});
					InfoWindow.open(map,this);
					this.tabVisible=true;
				}
			}
			G.event.addListener(this,"click",this.showtab);
			this.setMap(map);
			var boundscheck=map.getBounds();
			if ((!boundscheck.contains(this.getPosition()))&&this.type=="Building") //only zoom to accomodate buildings
			{
				boundscheck.extend(this.getPosition());
				var newzoom = getZoomByBounds( map, boundscheck );
				if (newzoom<15) map.setMapType(G_NORMAL_MAP);
				map.setZoom(newzoom);
				map.setCenter(boundscheck.getCenter());
			}
			this.visible=true;
			if (tab) this.showtab();
		}
	}
	if (type=="Loops"||type=="Parking") {
		m.show = function (tab){
			this.setMap(map);
			var boundscheck=map.getBounds();
			if ((!boundscheck.contains(this.getPosition()))&&this.type=="Building") //only zoom to accomodate buildings
			{
				boundscheck.extend(this.getPosition());
				var newzoom = getZoomByBounds( map, boundscheck );
				if (newzoom<15) map.setMapType(G_NORMAL_MAP);
				map.setZoom(newzoom);
				map.setCenter(boundscheck.getCenter());
			}
			this.visible=true;
		}
	}
	m.hide = function() { 
		if (!(this.type=="Loops"||this.type=="Parking")) {
			G.event.clearListeners(this,"click");
		}
		if (this.tabVisible) {
			purgeInfoWindow();
			this.tabVisible=false;
		}
		this.visible=false;
		this.setMap(null); 
	}
	m.getPlacesTab=getPlacesTab;
	m.getInfoTab=getInfoTab;
	m.getEntranceTab=getEntranceTab;
	m.getToiletTab=getToiletTab;
	return m;
}
//----------------------------------------------------------------------------------functions to populate tabs called from marker
function getPlacesTab() {
	var popup="<div id='map_building'><h4>"+this.name+"</h4>";
	popup+="<div id='map_building_inner'>";
	if (this.dispphoto) {
			popup+="<div id='map_building_photo'>";
			popup+="<a href='javascript:shadowbox("+this.id+")'><img src='./photos/small/"+this.id+".jpg' style='display:block'; width='40%'; height='40%'></a>"; 
			popup+="</div>";
		}
	if (this.opening_hours) {
		popup+="<div id='map_building_opening_hours'>";
		popup +='<h5>Building Opening Hours:</h5>';
		popup +='<div>'+this.opening_hours+"</div>";
		popup+="</div>";
	}
	if ((this.accessibility)||(this.accessibility_url)) {
		popup+="<div id='map_building_access'>";
		if (this.accessibility) {
			popup+="<h5>Accessibility: </h5><div>"+this.accessibility+" ";
			if (this.accessibility_url) popup+="(<a href='http://www.tcd.ie/disability/accessibility/Building-check/"+this.accessibility_url+"'>more&nbsp;info</a>)</div>";
		
		}
		else if (this.accessibility_url) popup+="<div><a href='http://www.tcd.ie/disability/accessibility/Building-check/"+this.accessibility_url+"'>Accessibility&nbsp;info</a></div>";
		popup+="</div>";
	}
	popup+="</div>";
	
	popup+="<div id='map_building_permalink'>";
	var linkname;
	linkname=this.name;
	popup+="<a href='map.php?q="+urlencode(linkname)+"' >Permalink</a>";
	lat=this.getPosition().lat();
	lng=this.getPosition().lng();
	
	if (!mainCampus.contains(this.getPosition())) {
		bd='"'+urlencode(this.name)+'"';
		popup+="    <a href='javascript:directions("+lat+","+lng+","+bd+")'>Directions</a> ";
	}
	popup+="</div>";

	return popup;
}
function getInfoTab() {
	popup="";
	popup+="<div id='map_info'>";
	popup+="<div id='map_info_inner'><bd>"; //<div id='map_popup_inner'>";
	var placeoffset=0;
	if (this.places.length-placeoffset) {
		popup+="<h4>Also in this building</h4><ul>";
		for (i=0;i<this.places.length;i++) {
			if (this.places[i].name) {
				if (this.places[i].info.match(/^\s*http/)) {
					popup +="<li><a href='"+this.places[i].info+"'>"+this.places[i].name+"</a></li>";
				}
				else {
					popup +="<li>"+this.places[i].name+"</li>";
				}
			}
		}
		popup+="</ul>";
	}
	if (this.description)  popup+="<h4>Info</h4>"+this.description;
	popup+="</bd></div></div>";
	return popup;
}
function getToiletTab() {
	popup="";
	popup+="<div id='map_toilettab'>";
	popup+="<div id='map_toilettab_inner'><bd>"; 
	popup+="<h4>Enabled toilet</h4>"
	popup+="<br>"+this.data;
	popup+="</bd></div></div>";
	return popup;
}
function getEntranceTab() {
	popup="";
	popup+="<div id='map_entrancetab'>";
	popup+="<div id='map_entrancetab_inner'><bd>"; 
	popup+="<h4>Entrance</h4>"
	popup+="<br> "+this.data;
	popup+="</bd></div></div>"; 
	return popup;
}
//----------------------------------------------------------------------------------Setting up data structures while loading
function prepareData() {
	//create markers for ref tables
	for (i in icontypes) {
		markers[icontypes[i]]=[];
		for (t in tables[icontypes[i]]) {
			//console.log("building  "+tables[icontypes[i]][t].building+":"+markers["Building"][tables[icontypes[i]][t].building].name);
			tables[icontypes[i]][t].lat=markers["Building"][tables[icontypes[i]][t].building].lat;
			tables[icontypes[i]][t].lng=markers["Building"][tables[icontypes[i]][t].building].lng;
			tables[icontypes[i]][t].id=tables[icontypes[i]][t].building;
			markers[icontypes[i]].push(makeMarker(tables[icontypes[i]][t],icontypes[i]));
		}

		markers[icontypes[i]].visible=false;
	}
	//process Building and AZ tables
	//create AZ -- is causing problems now for some reason WHERE THERE ARE NO ITEMS
	for (var i=0;i<26;i++) {
		tables[String.fromCharCode(i+65)]=[];
		tables[String.fromCharCode(i+65)].screenname=String.fromCharCode(i+65);
	}
	//Create alphabetical lists of places including exceptions
	for (var i=0;i<tables["AZ"].length;i++)
	{
		if (tables["AZ"][i].info=="") tables["AZ"][i].info=tables["AZ"][i].name;
		
		 var j=0;
		 var startlet=0;
		 
		 while (tables["AZ"][i].name.charCodeAt(j) <64) j++; //find 1st non-number
		 
		 startlet=tables["AZ"][i].name.charCodeAt(j) -65; 
		 if (startlet>25) startlet-=32; //select small letters (which shouldn't even be there)
		 
		 if ((startlet==96)||(startlet==128)) startlet=0; //catch Á
		 if (startlet==104) startlet=4; //catch É
		 if (startlet==108) startlet=8; //catch Í
		 
		 //find array number of building index as buildingindex number can be non-consecutive
		 var bb=tables["AZ"][i].building;
		 for (j in markers['Building'])
		 {
		 	if (markers['Building'][j].id==bb) {
				numplaces=tables[String.fromCharCode(startlet+65)].length;
				tables[String.fromCharCode(startlet+65)].push(tables["AZ"][i]); //add reference to place
				tables[String.fromCharCode(startlet+65)][numplaces].building=j; //resolve reference to building array
				tables[String.fromCharCode(startlet+65)][numplaces].index=i; //resolve reference to AZ array
				markers['Building'][j].places.push(tables["AZ"][i]);
			}
		 }
		 
		 if (tables["AZ"][i].name.slice(0,3)=="St.")
		 {
			 startlet=tables["AZ"][i].name.charCodeAt(4) -65;
			 if (startlet>25) startlet-=32; //select small letters
			 //we know its doesn't start with ÁÉÍ
			 for (j in markers['Building'])
			 {
				if (markers['Building'][j].id==bb) {
					numplaces=tables[String.fromCharCode(startlet+65)].length;
					tables[String.fromCharCode(startlet+65)].push(tables["AZ"][i]); //add reference to place
					tables[String.fromCharCode(startlet+65)][numplaces].building=j; //resolve reference to building array
					markers['Building'][j].places.push(tables["AZ"][i]);
				}
			 }
		 }
	}
	//add buildings
	for (i in markers['Building'])
	{
		if (markers['Building'][i].name) { //trap added 'visible' property
			var j=0;
			 var startlet=0;
			 
			 while (markers['Building'][i].name.charCodeAt(j) <64) j++; //find 1st non-number
			 
			 startlet=markers['Building'][i].name.charCodeAt(j) -65; 
			 if (startlet>25) startlet-=32; //select small letters (which shouldn't even be there)
			 
			 if ((startlet==96)||(startlet==128)) startlet=0; //catch Á
			 if (startlet==104) startlet=4; //catch É
			 if (startlet==108) startlet=8; //catch Í
			 
			 numplaces=tables[String.fromCharCode(startlet+65)].length;
			 tables[String.fromCharCode(startlet+65)].push(new place(markers['Building'][i].name,i,markers['Building'][i].info));
			 tables[String.fromCharCode(startlet+65)][numplaces].building=i; //resolve reference to building array
		}
					
	}
	//sort results alphabetically
	for (i=0;i<26;i++) {
		letter=String.fromCharCode(i+65);
		tables[letter].sort(function(a,b) { if (a.name<b.name) return -1; else return 1;});
	}
	//resolve other tables references
	places.forEach(function(placetable) {
		for (j=0;j<placetable.length;j++) {
			for (k=0; k<markers['Building'].length;k++)
			if (placetable[j].building==markers['Building'][k].num) {
				placetable[j].building=k; //resolve reference to building array
				break;
			}
		}
		placetable.sort(function(a,b) {if (a.name<b.name) return -1; else return 1; });
		
	});
	//create html popups
	for (i in markers['Building']) {
		if (markers['Building'][i].name) {
			markers['Building'][i].sortPlaces();
		}
	}
	for (t in tables) tables[t].screenname=tablescreennames[t];
}
function do_menuchoices() {
	var menu="<ul>";
	for (var i=0;i<26;i++) {
		letter=String.fromCharCode(i+65);
		menu+="<li><a href='javascript:do_menu(\""+letter+"\");'>"+letter+" </a></li>"; 
	}
	menu+="</ul>";
	document.getElementById("searchoptions").innerHTML=menu;
	//action='map.php' 
	menu="<form id='form' method='POST' action='javascript:void(0);' onsubmit='quicklink(this.childNodes[0].childNodes[1]);return false'><div><label for='quicklinks'>Quicklinks: </label><select name='quicklinks' id='quicklinks'><option selected='selected'>Search for lectures, libraries, pacr...</option>";
	for (i=0;i<placetypes.length;i++) menu+="<option value='"+placetypes[i]+"'>"+screenplacetypes[i]+"</option>";
	menu+="</select> <input type='submit' value='Go' /></form>";
	document.getElementById("quicklinkswrap").innerHTML=menu;
}
//----------------------------------------------------------------------------------Editing markers while authoring
function setMarkerPosition() {
	if (this.type=="Building") {
		$.ajax({ 
			url:"setMarkerPos.php",
			data: "table="+this.type+"&id="+this.id+"&lat="+this.getPosition().lat()+"&lng="+this.getPosition().lng()+"&google_coords=1"
		});
		this.setIcon(new G.MarkerImage("http://maps.google.com/mapfiles/marker_orange.png") );
	}
	else {
		$.ajax({ 
			url:"setMarkerPos.php",
			data: "table="+this.type+"&id="+this.id+"&lat="+this.getPosition().lat()+"&lng="+this.getPosition().lng()
		});
	}
}
function displayMarkerInfo(){
	if (this.type=="Building")  document.getElementById("info").innerHTML=this.id+": "+this.name;
	else document.getElementById("info").innerHTML=this.id;
}
//----------------------------------------------------------------------------------Interaction handlers
function shadowbox(n) {
	Shadowbox.open({content:"./photos/"+n+".jpg",player: "img",title:markers['Building'][n].name});
}
function onsearch(query) {
	tables["search"]=[];
	var foundbuildings=new Array();
	fb=0;
	for (var i=0;i<26;i++) {
		//console.log("checking "+String.fromCharCode(i+65));
		for (j in tables[String.fromCharCode(i+65)]) {
			
			
			if (tables[String.fromCharCode(i+65)][j]!=undefined) {
			//console.log("checking "+j+": "+tables[String.fromCharCode(i+65)][j].name);
				
				if ("name" in tables[String.fromCharCode(i+65)][j]) {
					if (tables[String.fromCharCode(i+65)][j].name.toLowerCase().indexOf(query.toLowerCase())>-1) {
						tables["search"].push(tables[String.fromCharCode(i+65)][j]);
						if (foundbuildings[tables[String.fromCharCode(i+65)][j].building]!="1") fb++;
						foundbuildings[tables[String.fromCharCode(i+65)][j].building]="1";
					}
				}
				
			}
			
		}
	}
	//auto-open if 1 search result
	if (fb==1) {
		clearmarkers();
		markers["Building"][tables["search"][0].building].show(true,tables["search"][0]);
	}
	tables["search"].screenname=query;
	tables["search"].sort(function(a,b) {if (a.name<b.name) return -1; else return 1; });
	do_menu("search");
}
function show_markers(type) {
	if (DEBUG) console.log("showing "+type);
	if (!markers[type].visible) {
		for(i in markers[type]) {
			if (markers[type][i].id) {
				markers[type][i].show();
			}
		}
	}
	else {
		for(i in markers[type]) {
			if (markers[type][i].id) {
				markers[type][i].hide();
			}
		}
	}

	markers[type].visible=!markers[type].visible;
}
function show_place(type,index) {
	show_building(tables[type][index].building);
}
function show_building(index) { //funnel through here
	markers['Building'][index].show(true);
}
function quicklink(owner) {
	name=owner.options[owner.selectedIndex].value;
	for (i=1;i<markertypes.length;i++) if (name==markertypes[i]) show_markers(name);
	for (i=0;i<placetypes.length;i++) if (name==placetypes[i]) do_menu(name);
	resize_map_and_results();
}
function do_menu(type) {
	var title="<h1>"+tables[type].length+" results for ";
	if (type.length==1) title+="letter ";
	if (tables[type].screenname) {
		title+=tables[type].screenname;
	}
	else {
		title+=type;
	}
	title+="</h1>";
	if (DEBUG) console.log("doing menu: "+type);
	if (DEBUG) console.log(tables[type].length+" items");
	if(document.getElementById("searchresulttitle")) {
		document.getElementById("searchresulttitle").innerHTML=title;
		var content="<ul>";
		for(var i=0;i<tables[type].length;i++) {
			content+="<li><a href='javascript:show_place(\""+type+"\","+i+"); '>"+tables[type][i].name+"</a></li>";
		}
		content+="</ul>";
		document.getElementById("searchresults").innerHTML=content;
		if(do_menu_flag) {
			resize_map_and_results();
		}
		else {
			do_menu_flag = true;
		}
		if (DEBUG) alert(content);
	}
}
function purgeInfoWindow() {
	if (InfoWindow) {
		//console.log("closing infowindow");
		InfoWindow.close();
		InfoWindow=undefined;
	}
}
function clearmarkers() {
	for (i in markers) { 
		for (j in markers[i]) {
			if (markers[i][j].name) markers[i][j].hide;
		}
	}
}
function directions(lat,lng,place){
	document.getElementById("searchresulttitle").innerHTML="<h1>From Trinity College to "+place+"</h1>";
	document.getElementById("searchresults").innerHTML="";
	var request = {
            origin:new G.LatLng(53.3445,-6.259),
            destination:new G.LatLng(lat,lng),
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
	directionsDisplay.setPanel(document.getElementById("searchresults"));
	directionsService.route(request, function(response, status) {
          if (status == G.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
          }
        });
}
//----------------------------------------------------------------------------------misc utils, legacy ajax stuff to bypass TCD fuzzy filename filter
//Equivalent of getBoundsZoomLevel() in gmaps api 3
/**
* Returns the zoom level at which the given rectangular region fits in the map view. 
* The zoom level is computed for the currently selected map type. 
* @param {google.maps.Map} map
* @param {google.maps.LatLngBounds} bounds 
* @return {Number} zoom level
**/
function getZoomByBounds( map, bounds ){
  var MAX_ZOOM = map.mapTypes.get( map.getMapTypeId() ).maxZoom || 21 ;
  var MIN_ZOOM = map.mapTypes.get( map.getMapTypeId() ).minZoom || 0 ;

  var ne= map.getProjection().fromLatLngToPoint( bounds.getNorthEast() );
  var sw= map.getProjection().fromLatLngToPoint( bounds.getSouthWest() ); 

  var worldCoordWidth = Math.abs(ne.x-sw.x);
  var worldCoordHeight = Math.abs(ne.y-sw.y);

  //Fit padding in pixels 
  var FIT_PAD = 40;

  for( var zoom = MAX_ZOOM; zoom >= MIN_ZOOM; --zoom ){ 
      if( worldCoordWidth*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).width() && 
          worldCoordHeight*(1<<zoom)+2*FIT_PAD < $(map.getDiv()).height() )
          return zoom;
  }
  return 0;
}
function urlencode (string) {
       string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }
 
        return escape(utftext);
}
function httpexists(url) {
	exists=false;
	http.open("GET",'httpexists-proxy.php?httpexistsurl=' + url, false);
	//http.open("GET", url, false);
	http.send(null);
	if (http.status == 200) {
		exists=true;
	}
	return exists;
}
function getHTTPObject() {
  var xmlhttp;
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}