// console.log('entered bldg.js');
/* 
		the mapindex table/db
		=====================

		Instructions on adding buildings
		--------------------------------
		The array is the comma separated database, thus the values are index in the following fashion:
		["Building Name","Abbreviation","Grid","Old Index","select","deselect"]
		["Column 1","Column 2","Column 3", etc.]
		This is essentially like a .csv file.
		When adding a new record the above mentioned parameters are to be followed except that for a new  
		"Old Index" number, which should start with the next available number in the "Old Index" column.
		The old index numbers are kept for backwards compatibility.
		
		Reiner Kramer (BM 2005, MM 2007)
 */
 
/* 
bldg = [ {
    "id": "ART",
    "label": "Art Building",
    "customNum": 17,
    "X": ["884", "1430", 2147],
    "Y": ["137", "235", 348]
    }, {
    "id": "ATHC",
    "label": "Athletic Center",
    "customNum": 69,
    "X": ["458", "745", 1120],
    "Y": ["513", "839", 1266]
    }, {
    "id": "ATH",
    "label": "Athletic Office Building",
    "customNum": 33,
    "X": ["543", "880", 1321],
    "Y": ["340", "558", 846]
    }, {
    "id": "AUDB",
    "label": "Auditorium Building",
    "customNum": 11,
    "X": ["828", "1342", 2014],
    "Y": ["104", "195", 282]
    }, {
    "id": "BAIN",
    "label": "Bain Hall",
    "customNum": 27,
    "X": ["670", "1083", 1630],
    "Y": ["265", "439", 667]
    }, {
    "id": "BIOL",
    "label": "Biology Building",
    "customNum": 6,
    "X": ["776", "1252", 1889],
    "Y": ["181", "304", 463]
    }, {
    "id": "BRUC",
    "label": "Bruce Hall",
    "customNum": 45,
    "X": ["738", "1187", 1783],
    "Y": ["176", "294", 433]
    }, {
    "id": "BUSI",
    "label": "Business Administration Building",
    "customNum": 16,
    "X": ["846", "1366", 2052],
    "Y": ["171", "291", 440]
    }, {
    "id": "CHEM",
    "label": "Chemistry Building",
    "customNum": 68,
    "X": ["745", "1198", 1804],
    "Y": ["103", "184", 273]
    }, {
	"id": "CHNT",
    "label": "Chestnut Hall",
    "customNum": 90,
    "X": ["673", "1089", "1632"],
    "Y": ["177", "292", "436"]
    }, {	
    "id": "CHIL",
    "label": "Chilton Hall",
    "customNum": 29,
    "X": ["709", "1149", 1721],
    "Y": ["212", "357", 546]
    }, {
    "id": "CLAR",
    "label": "Clark Hall",
    "customNum": 38,
    "X": ["702", "1136", 1706],
    "Y": ["330", "545", 822]
    }, {
    "id": "COL",
    "label": "Coliseum",
    "customNum": 32,
    "X": ["616", "986", 1504],
    "Y": ["308", "487", 758]
    }, {
    "id": "CINN",
    "label": "College Inn",
    "customNum": 56,
    "X": ["624", "1012", 1508.5],
    "Y": ["110", "192", 278.5]
    }, {
    "id": "CRUM",
    "label": "Crumley Hall",
    "customNum": 42,
    "X": ["763", 1200, 1854],
    "Y": ["289", 473, 724]
    }, {
    "id": "CURY",
    "label": "Curry Hall",
    "customNum": 14,
    "X": ["849", "1374", 2060],
    "Y": ["121", "207", 320]
    }, {
    "id": "ESSC",
    "label": "Eagle Student Services Center",
    "customNum": 22,
    "X": ["808", "1298", 1961],
    "Y": ["240", "402", 614]
    }, {
    "id": "ENV",
    "label": "Environmental Education, Science &amp; Technology",
    "customNum": 62,
    "X": ["702", "1138", 1702],
    "Y": ["100", "168", 258]
    }, {
    "id": "PLAN",
    "label": "Facilities Complex",
    "customNum": 50,
    "X": ["529", "850", 1277],
    "Y": ["183", "309", 471]
    }, {
    "id": "STAD",
    "label": "Fouts Field",
    "customNum": 34,
    "X": ["508", "819", 1235],
    "Y": ["319", "523", 791]
    }, {
    "id": "GATE",
    "label": "Gateway Center",
    "customNum": 37,
    "X": ["611", "988", 1495],
    "Y": ["344", "567", 853]
    }, {
    "id": "GAB",
    "label": "General Academic Building",
    "customNum": 9,
    "X": ["800", "1294", 1942],
    "Y": ["136", "232", 344]
    }, {
    "id": "CHAP",
    "label": "Goolsby Chapel",
    "customNum": 28,
    "X": ["750", "1213", 1820],
    "Y": ["309", "509", 774]
    }, {
    "id": "HKRY",
    "label": "Hickory Hall",
    "customNum": 10,
    "X": ["806", "1304", 1960],
    "Y": ["94", "163", 256]
    }, {
    "id": "HIGH",
    "label": "Highland Hall",
    "customNum": 47,
    "X": ["884", "1426", 2146],
    "Y": ["298", "490", 739]
    }, {
	"id": "HNRS",
    "label": "Honors College",
    "customNum": 88,
    "X": ["695", "1120", "1685"],
    "Y": ["365", "600", "905"]
    }, {
    "id": "ADMN",
    "label": "Hurley Administration Building",
    "customNum": 1,
    "X": ["792", "1280", 1924],
    "Y": ["212", "353", 539]
    }, {
    "id": "ISB",
    "label": "Information Sciences Building",
    "customNum": 4,
    "X": ["808", "1306", 1960],
    "Y": ["176", "296", 449]
    }, {
	"id": "INTR",
    "label": "Intramural Fields",
    "customNum": 92,
    "X": ["417", "681", "1008"],
    "Y": ["276", "464", "674"]
    }, {
    "id": "MGYM",
    "label": "Ken Bahnsen Gym",
    "customNum": 31,
    "X": ["638", "1034", 1550],
    "Y": ["258", "427", 645]
    }, {
    "id": "KEND",
    "label": "Kendall Hall",
    "customNum": 41,
    "X": ["842", "1358", 2041],
    "Y": ["294", "478", 721]
    }, {
    "id": "KERR",
    "label": "Kerr Hall",
    "customNum": 40,
    "X": ["817", "1320", 1986],
    "Y": ["339", "559", 839]
    }, {
    "id": "LANG",
    "label": "Language Building",
    "customNum": 12,
    "X": ["845", "1367", 2052],
    "Y": ["105", "179", 270]
    }, {
	"id": "LGDS",
    "label": "Legends Hall",
    "customNum": 89,
    "X": ["626", "1012", "1522"],
    "Y": ["140", "239", "368"]
    }, {		
    "id": "LANX",
    "label": "Library Annex",
    "customNum": 60,
    "X": ["120", "195", 291],
    "Y": ["417", "661", 1002]
    }, {
	"id": "LMAL",
    "label": "Library Mall",
    "customNum": 94,
    "X": ["790", "1278", "1919"],
    "Y": ["254", "418", "634"]
    }, {
    "id": "LYCM",
    "label": "Lyceum",
    "customNum": 70,
    "X": ["811", "1299", 1993],
    "Y": ["227", "363", 572]
    }, {
    "id": "MAPL",
    "label": "Maple Hall",
    "customNum": 39,
    "X": ["751", "1203", 1822],
    "Y": ["335", "545", 818]
    }, {
    "id": "MARQ",
    "label": "Marquis Hall",
    "customNum": 49,
    "X": ["777", "1257", 1890],
    "Y": ["137", "221", 347]
    }, {
    "id": "MAST",
    "label": "Masters Hall",
    "customNum": 7,
    "X": ["775", "1258", 1888],
    "Y": ["164", "279", 425]
    }, {
    "id": "MATT",
    "label": "Matthews Hall",
    "customNum": 20,
    "X": ["851", "1376", 2064],
    "Y": ["266", "428", 645]
    }, {
    "id": "MHA",
    "label": "Matthews Hall Annex",
    "customNum": 48,
    "X": ["890", "1439", 2159],
    "Y": ["208", "345", 519]
    }, {
    "id": "MCON",
    "label": "McConnell Hall",
    "customNum": 43,
    "X": ["705", "1141", 1713],
    "Y": ["170", "278", 419]
    }, {
    "id": "MGOG",
    "label": "Mean Green Office Complex and Gym",
    "customNum": 76,
    "X": ["351", "568", 852],
    "Y": ["646", "1046", 1586]
    }, {
    "id": "MGSG",
    "label": "Mean Green Softball and Golf Practice Facility",
    "customNum": 77,
    "X": ["364", "591", 887],
    "Y": ["642", "1050", 1570]
    }, {
    "id": "MOZA",
    "label": "Mozart Square",
    "customNum": 64,
    "X": ["661", "1069", 1603],
    "Y": ["387", "635", 958]
    }, {
    "id": "PAC",
    "label": "Murchison Performing Arts Center",
    "customNum": 63,
    "X": ["588", "957", 1438],
    "Y": ["392", "635", 963]
    }, {
    "id": "Lyric-PAC",
    "label": "Lyric Theater - Murchison Performing Arts Center",
    "customNum": 86,
    "X": ["602", "977", 1468],
    "Y": ["400", "655", "983"]
    }, {
    "id": "Winspear-PAC",
    "label": "Winspear Hall - Murchison Performing Arts Center",
    "customNum": 87,
    "X": ["589", "949", 1418],
    "Y": ["382", "615", 943]
    }, {
    "id": "MUSA",
    "label": "Music Annex",
    "customNum": 23,
    "X": ["741", "1202", 1802],
    "Y": ["264", "438", 665]
    }, {
    "id": "MUSI",
    "label": "Music Building",
    "customNum": 5,
    "X": ["744", "1202", 1801],
    "Y": ["229", "383", 534]
    }, {
    "id": "MPN",
    "label": "Music Practice North",
    "customNum": 25,
    "X": ["708", "1146", 1726],
    "Y": ["245", "408", 618]
    }, {
    "id": "MPS",
    "label": "Music Practice South",
    "customNum": 24,
    "X": ["709", "1144", 1724],
    "Y": ["266", "438", 661]
    }, {
    "id": "OSH",
    "label": "Oak Street Hall",
    "customNum": 13,
    "X": ["888", "1439", 2135.5],
    "Y": ["25", "50", 65.5]
    }, {
    "id": "OSHA",
    "label": "Oak Street Hall Annex",
    "customNum": 54,
    "X": ["898", "1453", 2153.5],
    "Y": ["30", "62", 94]
    }, {
    "id": "PARK",
    "label": "Parking Garage",
    "customNum": 71,
    "X": ["908", "1464", 2194],
    "Y": ["219", "360", 536.5]
    }, {
    "id": "PACX",
    "label": "Performing Arts Center Annex",
    "customNum": 36,
    "X": ["569", "919", 1383],
    "Y": ["379", "624", 944]
    }, {
    "id": "PEB",
    "label": "Physical Education Building",
    "customNum": 30,
    "X": ["655", "1065", 1594],
    "Y": ["221", "368", 562]
    }, {
    "id": "PHYS",
    "label": "Physics Building",
    "customNum": 15,
    "X": ["849", "1371", 2065],
    "Y": ["135", "209", 352]
    }, {
    "id": "ECB",
    "label": "East Classroom Building",
    "customNum": 73,
    "X": ["984", "1594", "2393"],
    "Y": ["191", "322", "490"]
    }, {
    "id": "RTFP",
    "label": "Radio, TV, Film &amp; Performing Arts Building",
    "customNum": 18,
    "X": ["877", "1418", 2131],
    "Y": ["178", "294", 441]
    }, {
	"id": "recsports",
    "label": "Recreational Sports Complex",
    "customNum": 91,
    "X": ["121", "201", "297"],
    "Y": ["450", "732", "1117"]
    }, {		
    "id": "SFE",
    "label": "Santa Fe Square",
    "customNum": 61,
    "X": ["533", "855", 1285],
    "Y": ["238", "393", 600]
    }, {
    "id": "SRB",
    "label": "Science Research Building",
    "customNum": 53,
    "X": ["779", "1261", 1894],
    "Y": ["102", "176", 261]
    }, {
    "id": "SCOU",
    "label": "Scoular Hall",
    "customNum": 52,
    "X": ["833", "1348", 2021],
    "Y": ["248", "411", 621]
    }, {
    "id": "PAV",
    "label": "Shrader Pavilion",
    "customNum": 26,
    "X": ["795", "1284", 1930],
    "Y": ["227", "378", 574]
    }, {
    "id": "SPHS",
    "label": "Speech and Hearing Clinic",
    "customNum": 65,
    "X": ["993", "1606", "2409"],
    "Y": ["168", "280", "432"]
    }, {
    "id": "WGHT",
    "label": "Sports Medicine/Fitness Facility",
    "customNum": 57,
    "X": ["544", "881", 1323],
    "Y": ["327", "528", 796]
    }, {
    "id": "STOV",
    "label": "Stovall Hall",
    "customNum": 21,
    "X": ["810", "1311", 1974],
    "Y": ["260", "433", 652]
    }, {
    "id": "SAAC",
    "label": "Student Athlete Academic Center",
    "customNum": 78,
    "X": ["347", "562", 842],
    "Y": ["617", "1008", 1514]
    }, {
    "id": "HEAL",
    "label": "Student Health &amp; Wellness Center",
    "customNum": 46,
    "X": ["671", "1087", 1659],
    "Y": ["178", "302", 464]
    }, {
    "id": "RECS",
    "label": "Pohl Recreation Center",
    "customNum": 59,
    "X": ["613", "1001", 1502],
    "Y": ["177", "297", 453]
    }, {
    "id": "POLI",
    "label": "Sullivant Public Safety Center",
    "customNum": 58,
    "X": ["702", "1135", 1706],
    "Y": ["399", "652", 987]
    }, {
    "id": "WTEN",
    "label": "Tennis Courts",
    "customNum": 74,
    "X": ["611", "992", 1489],
    "Y": ["262", "428", 649]
    }, {
    "id": "TH",
    "label": "Terrill Hall",
    "customNum": 8,
    "X": ["757", "1225", 1842],
    "Y": ["132", "232", 347]
    }, {
	"id": "TRDF",
    "label": "Traditions Field",
    "customNum": 93,
    "X": ["541", "880", "1299"],
    "Y": ["280", "451", "681"]
    }, {
    "id": "TRAD",
    "label": "Traditions Hall",
    "customNum": 67,
    "X": ["570", "925", 1389],
    "Y": ["231", "391", 579]
    }, {
    "id": "USB",
    "label": "University Services Building",
    "customNum": 55,
    "X": ["470", "762", 1146],
    "Y": ["264", "441", 663]
    }, {
    "id": "UU",
    "label": "University Union",
    "customNum": 2,
    "X": ["832", "1342", 2018],
    "Y": ["222", "367", 555]
    }, {
    "id": "VICT",
    "label": "Victory Hall",
    "customNum": 75,
    "X": ["498", "808", 1200],
    "Y": ["537", "878", 1311]
    }, {
    "id": "TENN",
    "label": "Waranch Tennis Complex",
    "customNum": 79,
    "X": ["417", "677", 1016],
    "Y": ["657", "1079", 1591]
    }, {
    "id": "WEST",
    "label": "West Hall",
    "customNum": 44,
    "X": ["571", "934", 1380],
    "Y": ["188", "319", 465]
    }, {
	 "id": "WSTN",
    "label": "West Tennis Courts",
    "customNum": 95,
    "X": ["570", "923", "1380"],
    "Y": ["171", "280", "432"]
    }, {
    "id": "LIBR",
    "label": "Willis Library",
    "customNum": 3,
    "X": ["776", "1256", 1885],
    "Y": ["251", "416", 630]
    }, {
    "id": "SOCR",
    "label": "Mean Green Village Soccer Stadium",
    "customNum": 80,
    "X": ["356", "577", 870],
    "Y": ["600", "980", 1476]
    }, {
    "id": "WH",
    "label": "Wooten Hall",
    "customNum": 19,
    "X": ["880", "1418", 2130],
    "Y": ["260", "427", 652]
    }, {
    "id": "NTRP",
    "label": "Research Park",
    "customNum": 83,
    "X": ["211", "327", 501],
    "Y": ["103", "177", 246]
    }, {
    "id": "ROWF",
    "label": "Fraternity Row",
    "customNum": 81,
    "X": ["946", "1542", 2311],
    "Y": ["341", "569", 874]
    }, {
    "id": "ROWS",
    "label": "Sorority Row",
    "customNum": 82,
    "X": ["994", "1608", "2414"],
    "Y": ["255", "422", "645"]
    }, {
    "id": "SOFT",
    "label": "Lovelace Stadium - Mean Green Village Softball Field",
    "customNum": 85,
    "X": ["348", "564", 853],
    "Y": ["570", "932", 1401]
	}, {
    "id": "MGVB",
    "label": "Building B",
    "customNum": 96,
    "X": ["363", "588", 885],
    "Y": ["621", "1012", 1524]
	}, {
    "id": "MGVC",
    "label": "Building C",
    "customNum": 97,
    "X": ["377", "611", 920],
    "Y": ["645", "1050", 1585]
	}, {
    "id": "MGVP",
    "label": "Building P",
    "customNum": 98,
    "X": ["371", "601", 905],
    "Y": ["571", "931", 1408]
	}, {
    "id": "AAC",
    "label": "Athletic Academic Center",
    "customNum": 99,
    "X": ["346", "562", 846],
    "Y": ["618", "1007", 1520]
	}, {
    "id": "MGPS",
    "label": "Police Sub Station",
    "customNum": 100,
    "X": ["354", "574", 865],
    "Y": ["619", "1006", 1517]
	}, {
    "id": "EPRS",
    "label": "Eagle Point Intramural Rec Sports Fields",
    "customNum": 101,
    "X": ["443", "715", 1072],
    "Y": ["603", "988", 1461]
	}, {
    "id": "SOFT",
    "label": "East Classroom Building",
    "customNum": 102,
    "X": ["348", "564", 853],
    "Y": ["570", "932", 1401]
	}, {
    "id": "FRSA",
    "label": "Free Speech Area",
    "customNum": 103,
    "X": ["847", "1373", 2058],
    "Y": ["207", "344", 523]
	}, {
    "id": "CGRN",
    "label": "The Campus Greens",
    "customNum": 104,
    "X": ["828", "1340", 2012],
    "Y": ["163", "275", 415]
    }];
*/


function bldgFromJson(json) {
	window.bldg = json;
}

function rowalter(){/* draw alternating background colors for the rows */
	var rows = document.getElementsByTagName('tr');
	for(i = 1; i < rows.length; i++){
		rows[i].onmouseover = function ruleit(){this.className = 'ruled'}; /* highlighting each row */
		i % 2 ? rows[i].className = 'greener' : rows[i].className = 'whiter'; /* short-hand if-else row color alternator */
		i % 2 ? rows[i].onmouseout  = function colorit(){this.className = 'greener'} : rows[i].onmouseout  = function recolor(){this.className = 'whiter'}; 
		/* short-hand if-else mouseout and return to previous color */
		}
	}

function toggle(bid) {
	console.log(bid);
	cmap.bldgClick(bldg[bldgid[bid]]);
}

function sidetoggle() {
	var id = this.id.split('_');
	console.log(id);
	toggle(id[0]);
}

function bldgURL(){
	//console.log('entered bldgURL');
	var url = location.search.substring(1,location.search.length);
	var wts = url.split("&");
	// console.log('wts: ' + wts.toJSON());
	
	new Insertion.Top('thebuildings', "<div id='custommap'><p>The following buildings have been highlighted:</p><ul id='b_name'></ul></div>");

	wts.each( function(getvar){
		nameval = getvar.split('=');
		bldg.each( function(b){
			if (nameval[0] == 'bd' && nameval[1] == b.customNum ) {
				current = $(b.id.toLowerCase())
				//console.log(current);
		//		current.toggle();
				new Insertion.Bottom('b_name', '<li>' + b.label + '</li>');
			}
		});
	});
	
}

function resetZoom() {
	var xvals = [], yvals = [], newzoomlevel = cmap.zoomlevel;
		
	active = bldg.findAll(function(b) {
		
		return $(b.id.toLowerCase()).style.display != 'none';
	});
	
	active.each(function (b) {
		xvals.push( b.X[cmap.zoomlevel-1] );
		yvals.push ( b.Y[cmap.zoomlevel-1] );
	});
	
	if ( (xvals.max() - xvals.min()) > cmap.portwidth ) {
		newzoomlevel -= 1;
	}else if ( (yvals.max() - yvals.min()) > cmap.portheight ) {
		newzoomlevel -= 1;
	}   
	 
	cmap.changezoomlevel(newzoomlevel);
}


	
/* generate the links that result in the right building being lit up */

function linkGen(){
	var thelink = 'http://www.unt.edu/pais/map/custommap.htm'; /* http://www.unt.edu/pais/map/ */
	if (location.search.length != 0) {	
		document.write('<p>This is the url for your customized map. Click to see it, or copy and paste this url into your websites.<p>'	+	
									 '<a href="' + thelink + 
									 location.search + '"' +
									 'target="_top">' +
									 thelink +
									 location.search +	
									 '</a>');
		}
	document.write('<form action="generatemap.htm" method="get">' +
                 '<p>Select the UNT campus buildings you want to highlight. ' +
								 'You can choose multiple buildings by holding down the control key (the apple key for mac users) while choosing from your options.</p>' +
								 '<select name="bd" multiple size="10">');
	for(i = 0; i < bldg.length; i++){
		document.write('<option value="' + bldg[i].customNum + '">' + bldg[i].label + '</option>');
		}
	document.write('</select><p>' +
            		 '<input type="Submit" value="Generate Customized Map">' +
            		 '<input type="Reset" value="Clear Choices"></p></form>');
	}

function bldginit() {
	addLoadEvent(rowalter);
	bldgid = {};
	bldgnum = {};
	
	bldg.each(function(b, slot) {	
		bldgid[b.id.toLowerCase()] = slot;
		bldgnum[b.customNum] = slot;
	});
	
	

}
