var marks;
var locations;
var cols = 4;
var column = 1;
var row = 1;
var panhtml = '';

	
function windowHtml(xml) {
	speakers = xml.getElementsByTagName('speaker');
	html = '<div style="padding-right: 8px; margin: 0; width: 200px; height: auto;">';
	html += '<h1 style="font-size: 11pt; margin-top:0; margin-bottom: 0;">' + xml.getElementsByTagName('country').item(0).firstChild.data + '</h1>';
	html += '<h2 style="margin-top: 0; 10px; font-size: 8pt;">';
	if (xml.getElementsByTagName('current').item(0).firstChild) {
		html += 'Current Students: '+xml.getElementsByTagName('current').item(0).firstChild.data;
	}
	if (xml.getElementsByTagName('current').item(0).firstChild && xml.getElementsByTagName('alumni').item(0).firstChild) {
		html += ' / ';
	}
	if (xml.getElementsByTagName('alumni').item(0).firstChild) {
		html += 'Alumni: '+xml.getElementsByTagName('alumni').item(0).firstChild.data;
	}
	html += '</h2>';
	for (var s = 0; s < speakers.length; s++) {
		files = speakers[s].getElementsByTagName('file');
		photo = files[0].getElementsByTagName('mp3').item(0).firstChild.data;
		photo =  photo.replace('a.mp3','.jpg');
		html += '<br style="clear: both;"/>';
		html += '<img src="http://www.wcl.american.edu/ilsp/map/photos/' + photo + '" height="80" width="60" border="0" align="left" style="margin-right: 4px;" title="'+ speakers[s].getElementsByTagName('name').item(0).firstChild.data +'">';
		html += '<h2 style="font-size: 9pt; margin-top: 0;">' + speakers[s].getElementsByTagName('name').item(0).firstChild.data;
		if (speakers[s].getElementsByTagName('status').item(0)) {
			html += ',<br /><em>' + speakers[s].getElementsByTagName('status').item(0).firstChild.data + '</em>';
		}
		html += '</h2>';
		for (var f = 0; f < files.length; f++) {
			mp3 = files[f].getElementsByTagName('mp3').item(0).firstChild.data;
			lang = files[f].getElementsByTagName('lang').item(0).firstChild.data;
			mp3id = 'id' + mp3.replace('.','a');
			html +=	'<p style="font-size: 9pt; margin: 2px 0; padding: 0;">';
			html += '<a href="#" name="http://www.wcl.american.edu/ilsp/map/mp3s/' + mp3 + '" id="' + mp3id + '" onclick="return playFlash(this.id);" onkeypress="return playFlash(this.id);">';
			html += '<img id="' + mp3id + '_img" src="play.png" height="18" width="18" border="0" align="absmiddle" style="padding-right: 4px;">';
			html += lang + '</a></p>';					
		}				
	}				
	html += '</div>';
	return html;
}

function openMarker(j) {
	scroll(10,100);
	marks[j].openInfoWindowHtml(windowHtml(locations[j]));
}


function onLoad() {
	var map = new GMap2(document.getElementById('map'));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(25.75, 30), 2, G_HYBRID_MAP);

	var largeIcon = new GIcon();
	largeIcon.shadow = 'http://www.wcl.american.edu/ilsp/map/flags/flag_shadow.png';
	largeIcon.transparent = 'http://www.wcl.american.edu/ilsp/map/flags/flag_trans.png';
	largeIcon.imageMap = [0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0, 8,0, 9,0, 10,0, 11,0, 12,0, 13,0, 14,0, 15,0, 16,0, 17,0, 18,0, 19,0, 20,0, 21,0, 22,0, 23,0, 24,0, 25,0, 26,0, 27,0, 28,0, 29,0, 30,0, 31,0, 32,0, 33,0, 34,0, 35,0, 36,0, 37,0, 38,0, 39,0, 39,1, 39,2, 39,3, 39,4, 39,5, 39,6, 39,7, 39,8, 39,9, 39,10, 39,11, 39,12, 39,13, 39,14, 39,15, 39,16, 39,17, 39,18, 39,19, 39,20, 38,20, 37,20, 36,20, 35,20, 34,20, 33,20, 32,20, 31,20, 30,20, 29,20, 28,20, 27,20, 26,20, 25,20, 24,20, 23,20, 22,20, 21,20, 20,20, 19,20, 18,20, 17,20, 16,20, 15,20, 14,20, 13,20, 12,20, 11,20, 10,20, 9,20, 8,20, 7,20, 6,20, 5,20, 4,20, 3,20, 2,20, 1,21, 1,22, 1,23, 1,24, 1,25, 1,26, 1,27, 1,28, 1,29, 0,29, 0,28, 0,27, 0,26, 0,25, 0,24, 0,23, 0,22, 0,21, 0,20, 0,19, 0,18, 0,17, 0,16, 0,15, 0,14, 0,13, 0,12, 0,11, 0,10, 0,9, 0,8, 0,7, 0,6, 0,5, 0,4, 0,3, 0,2, 0,1];
	largeIcon.iconSize = new GSize(40, 30);
	largeIcon.shadowSize = new GSize(52, 32);
	largeIcon.iconAnchor = new GPoint(1, 30);
	largeIcon.infoWindowAnchor = new GPoint(20, 0);
	largeIcon.infoShadowAnchor = new GPoint(30, 25);

	var palIcon = new GIcon();
	palIcon.shadow = 'http://www.wcl.american.edu/ilsp/map/flags/flag_shadow.png';
	palIcon.transparent = 'http://www.wcl.american.edu/ilsp/map/flags/flag_trans.png';
	palIcon.imageMap = [0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0, 8,0, 9,0, 10,0, 11,0, 12,0, 13,0, 14,0, 15,0, 16,0, 17,0, 18,0, 19,0, 20,0, 21,0, 22,0, 23,0, 24,0, 25,0, 26,0, 27,0, 28,0, 29,0, 30,0, 31,0, 32,0, 33,0, 34,0, 35,0, 36,0, 37,0, 38,0, 39,0, 39,1, 39,2, 39,3, 39,4, 39,5, 39,6, 39,7, 39,8, 39,9, 39,10, 39,11, 39,12, 39,13, 39,14, 39,15, 39,16, 39,17, 39,18, 39,19, 39,20, 38,20, 37,20, 36,20, 35,20, 34,20, 33,20, 32,20, 31,20, 30,20, 29,20, 28,20, 27,20, 26,20, 25,20, 24,20, 23,20, 22,20, 21,20, 20,20, 19,20, 18,20, 17,20, 16,20, 15,20, 14,20, 13,20, 12,20, 11,20, 10,20, 9,20, 8,20, 7,20, 6,20, 5,20, 4,20, 3,20, 2,20, 1,21, 1,22, 1,23, 1,24, 1,25, 1,26, 1,27, 1,28, 1,29, 0,29, 0,28, 0,27, 0,26, 0,25, 0,24, 0,23, 0,22, 0,21, 0,20, 0,19, 0,18, 0,17, 0,16, 0,15, 0,14, 0,13, 0,12, 0,11, 0,10, 0,9, 0,8, 0,7, 0,6, 0,5, 0,4, 0,3, 0,2, 0,1];
	palIcon.iconSize = new GSize(40, 45);
	palIcon.shadowSize = new GSize(52, 47);
	palIcon.iconAnchor = new GPoint(1, 45);
	palIcon.infoWindowAnchor = new GPoint(20, 0);
	palIcon.infoShadowAnchor = new GPoint(30, 40);

//	var smallIcon = new GIcon();
//	smallIcon.shadow = 'http://www.wcl.american.edu/ilsp/map/flags/small_shadow.png';
//	smallIcon.transparent = 'http://www.wcl.american.edu/ilsp/map/flags/flag_trans.png';
//	smallIcon.imageMap = [0,0, 1,0, 2,0, 3,0, 4,0, 5,0, 6,0, 7,0, 8,0, 9,0, 10,0, 11,0, 12,0, 13,0, 14,0, 15,0, 15,1, 15,2, 15,3, 15,4, 15,5, 15,6, 15,7, 15,8, 15,9, 15,10, 14,10, 13,10, 12,10, 11,10, 10,10, 9,10, 8,10, 7,10, 6,10, 5,10, 4,10, 3,10, 2,10, 1,10, 0,10, 0,9, 0,8, 0,7, 0,6, 0,5, 0,4, 0,3, 0,2, 0,1];
//	smallIcon.iconSize = new GSize(16, 11);
//	smallIcon.shadowSize = new GSize(24, 15);
//	smallIcon.iconAnchor = new GPoint(6, 6);
//	smallIcon.infoWindowAnchor = new GPoint(5, 0);
//	smallIcon.infoShadowAnchor = new GPoint(15, 25);

	function createMarker(point, country, xml) {
		if (xml.getElementsByTagName('speaker').length) {
			if (country == 'we'){
			   var icon = new GIcon(palIcon);
			} else {
				var icon = new GIcon(largeIcon);
			}
		} else {
			var icon = new GIcon(smallIcon);
		}
		icon.image = "http://www.wcl.american.edu/ilsp/map/flags/" + country + ".png";
		var marker = new GMarker(point, icon);

		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(windowHtml(xml));
		});

		GEvent.addListener(marker, "infowindowclose", function() {
			if (cur_id != '') {
				stopFlash(cur_id);
			}
			cur_id = '';
		});
		return marker;
	}

	var request = GXmlHttp.create();	
	request.open("GET", "map.xml", true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			locations = xmlDoc.documentElement.getElementsByTagName("location");
			marks = new Array(locations.length);
			rows = Math.round((locations.length/cols)+.4);
			tablecells = (rows*cols);
			for (var j=0;j<tablecells;) {
				if (j < locations.length) {
					var markers = locations[j].getElementsByTagName("point");
					var point = new GPoint(parseFloat(markers[0].getAttribute("lng")),parseFloat(markers[0].getAttribute("lat")));
					var country = locations[j].getAttribute("id").toLowerCase();
					if (locations[j].getElementsByTagName('speaker').length) {
						marks[j] = createMarker(point, country, locations[j]);
						map.addOverlay(marks[j]);
					}
					if (column == 1) {
						if (row == 1) {
							panhtml += '<tr>';
							row = 2;
						} else {
							panhtml += '<tr class="even">';
							row = 1;
						}
					}
					if (locations[j].getElementsByTagName('speaker').length) {
						panhtml += '<td class="country"><a href="javascript:openMarker(' + j + ')">' + locations[j].getElementsByTagName('country').item(0).firstChild.data + '</a></td>';
					} else {
						panhtml += '<td class="country">' + locations[j].getElementsByTagName('country').item(0).firstChild.data + '</td>';
					}
					if (locations[j].getElementsByTagName('current').item(0).firstChild) {
						panhtml += '<td>' + locations[j].getElementsByTagName('current').item(0).firstChild.data + '</td>';
					} else {
						panhtml += '<td>&nbsp;</td>';
					}
					if (locations[j].getElementsByTagName('alumni').item(0).firstChild) {
						panhtml += '<td>' + locations[j].getElementsByTagName('alumni').item(0).firstChild.data + '</td>';
					} else {
						panhtml += '<td>&nbsp;</td>';
					}
				}
				if (j >= locations.length) {
					panhtml += '<td colspan="3">&nbsp;</td>';
				}
				j += rows;
				if (column == 4) {
					panhtml += '</tr>';
					column = 1;

					j -= tablecells;
					j++;
					if (j < 0) {
						j += rows;
					}
					if (j >= rows) {
						j = tablecells;
					}
				} else {
					column++;
				}
			}
			document.getElementById('panel').innerHTML = '<table id="paneltable" cellspacing="0"><thead><tr><th class="country">Country</th><th>Current</th><th>Alumni</th><th class="country">Country</th><th>Current</th><th>Alumni</th><th class="country">Country</th><th>Current</th><th>Alumni</th><th class="country">Country</th><th>Current</th><th>Alumni</th></tr></thead><tbody>'+panhtml+'</tbody></table>';
       	}
	}
	request.send(null);
}
