
//a cross-browser version of getElementById
function returnObjById( id )
{
	if (document.getElementById)
	var returnVar = document.getElementById(id);
	else if (document.all)
	var returnVar = document.all[id];
	else if (document.layers)
	var returnVar = document.layers[id];

	return returnVar;
}



/*
	when a profile is clicked, either on a map infowindow, or from the flash grid,
	this function opens the window
*/
function showbio(sid) {
	window.open("http://bureau.espeakers.com/"+bcode+"/viewspeaker"+sid,"profile","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,height=700,width=625");
}



//it's critical to toggleLayer(true) from the loadFrame function rather
//than a separate call, because ie has a bug that only allows 1 js call in
//a certain time period from Lz, so you have to load the URL and visible the layer in one call
function toggleLayer(showme, whichLayer)
{
	elem = returnObjById(whichLayer);

	if (elem) {

		vis = elem.style;
		// if the style.display value is blank we try to figure it out here
		if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = showme ? 'none' : 'block';

		vis.display = showme ? 'block' : 'none';
	}
}





function createMarker(point, speakername, eventdate, eventcountry, eventcity, travelsfrom, sid)
{

	var marker = new GMarker(point, iconSpeaker);
	var html = generateMarkerHTML(speakername, eventdate, eventcountry, eventcity, travelsfrom, sid);


	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

	return marker;
}



function generateMarkerHTML(speakername, eventdate, eventcountry, eventcity, travelsfrom, sid) {
	var html = "<div class='infowindow'>";
	html += "<p><span class='iwheavy'>"+speakername+"</span> <a href=\"javascript:showbio("+sid+");\">(profile)</a></p>";
	html += "<p>"+travelsfrom+"</p>";
	html += "<hr /> ";
	html += "<p>appearing</p><p>";
	if (eventcity > '')
		html += "in <span class='iwheavy'>"+eventcity+"</span>";
	html += " on <span class='iwheavy'>"+eventdate+"</span></p>";
	html +="</div>";

	return html;
}




/*
	function that Flex calls once it has marker data to show
*/
function showMap(markerxml)
{
//alert('showmap');
	toggleLayer(true, 'mapdiv');
	toggleLayer(true, 'controls');
	toggleLayer(false, 'searching');


	if ((gmap != 'yo') && (markerxml > '')) {
		loadmarkers(markerxml);
	}
}



/*
	called once when the page is loaded
*/
function createmap(markerxml) {
	if (!GBrowserIsCompatible()) {
		alert('Your browser is not compatible with Google Maps');
		return false;
	}

	//create the special icon
	iconSpeaker = new GIcon();
	iconSpeaker.image = 'http://espeakers.com/witw/images/pin.png';
	iconSpeaker.shadow = 'http://espeakers.com/witw/images/pinshadow.png';
	iconSpeaker.iconSize = new GSize(12, 20);
	iconSpeaker.shadowSize = new GSize(22, 20);
	iconSpeaker.iconAnchor = new GPoint(6, 20);
	iconSpeaker.infoWindowAnchor = new GPoint(5, 1);

	//generate the map
	gmap = new google.maps.Map2(returnObjById("map"));
	gmap.addControl(new GSmallMapControl());
	gmap.setCenter(new google.maps.LatLng(43,-79),1);
	//gmap.enableContinuousZoom();
	gmap.enableScrollWheelZoom();
//alert('map created');
	return true;

}



/*
	called from Flash when a row is clicked on. We want to move the map to the corresponding
	point and open the info window
*/
function panTo(lat, lng, speakername, eventdate, eventcountry, eventcity, travelsfrom, sid) {
	//it's possible they've click a 'map' link while the map is hidden, so let's force it shown
	toggleLayer(true, 'mapdiv');
	toggleLayer(true, 'controls');
	toggleLayer(false, 'searching');
	
	
	gmap.panTo(new google.maps.LatLng(lat,lng));
	//if (gmap.getZoom() < 5) {
	//	gmap.setZoom(5);
	//}
	if (speakername > '') {
		var html=generateMarkerHTML(speakername, eventdate, eventcountry, eventcity, travelsfrom, sid);
		var point = new GLatLng(lat,lng);
		gmap.openInfoWindowHtml(point,html);
	}
}


/*
	called when we have markers in XML to pass in and create
*/
function loadmarkers(markerxml) {
//alert('loadmarkers:'+markerxml.length);

	//clear any existing markers
	gmap.clearOverlays();
	//translate the XML
	var xml = GXml.parse(markerxml);
	var markers = xml.documentElement.getElementsByTagName("marker");

	//iterate over each point
	for (var i = 0; i < markers.length; i++) {
		if ((markers[i].getAttribute("lat") == '') || (markers[i].getAttribute("lng") == ''))
			continue;
		var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
		parseFloat(markers[i].getAttribute("lng")));
		var marker = createMarker(point, markers[i].getAttribute('firstlast'), markers[i].getAttribute('eventdate'), markers[i].getAttribute('eventcountry'), markers[i].getAttribute('eventcity'), markers[i].getAttribute('travelsfrom'), markers[i].getAttribute('sid'));
		gmap.addOverlay(marker);
	}

	//center on the first created marker, hoping it will be representative of the group
	if (marker) {
		gmap.panTo(new GLatLng(parseFloat(markers[0].getAttribute("lat")),
		parseFloat(markers[0].getAttribute("lng"))));
		//gmap.setZoom(2);
	}


}



