var pointer;

var movieplayer="";
var googlemap;
var count = 0;

var mapzoom = 0;
var mapcenter;

var lastmapupdate = 0;

var mapmoving = false;

function error(errortext) {
  var textlist = document.getElementById("textlist");
  textlist.innerHTML = "<li class=\"errormessage\">" + errortext + "</li>\n" + textlist.innerHTML;
}  

/*
we're not using this any more - uncomment if we try using both map types at
once, but for now, we'll just stick with one at a time, and switching is a
global config.

function setupmap() {
	//clear the googlemap and and fill the map div with its sub-divs
	googlemap = null;
	mapdiv = document.getElementById("map");
	mapdiv.style.borderStyle = "none";
	mapdiv.style.backgroundColor = "inherit";

	padding = new Array;
	padding[0] = (mapdiv.offsetWidth - currentmap.size[i]) / 2 - 3;
	padding[1] = (mapdiv.offsetHeight - currentmap.size[i]) / 2 - 3;
	
	mapdiv.innerHTML = '<div id="mapcontainer" style="margin: ' + padding[1] + 'px ' + padding[0] + 'px;"><div id="mapimgcontainer"><img id="mapimg" src="' + currentmap.url + '" alt="map" /></div><div id="pointer" style="left: ' + currentmap.target[0] +'%; top: ' + currentmap.target[1] + '%; width: ' + currentpointer.size[0] + 'px; height: ' + currentpointer.size[1] + 'px;"><img src="' + currentpointer.url + '" alt="pointer" style="right: ' + currentpointer.target[0] + '%; bottom: ' + currentpointer.target[1] + '%;" /></div></div>';



}

function setupgooglemap(coords, zoom) {
	//initialize a googlemap in the map div
	count = count+1;
	//clear the existing contents:

	document.getElementById("map").innerHTML = count;
	//insert the googlemap
	if (GBrowserIsCompatible()) {
		googlemap = new GMap2(document.getElementById("map"));
		googlemap.setCenter(new GLatLng(coords[0],coords[1]), zoom);
		googlemap.setUIToDefault();
		googlemap.enableContinuousZoom();
	}
}
*/

function updatemap() {
	now = new Date();
	lastmapupdate = now.getTime();

	currentzoom = googlemap.getZoom();
	currentcenter = googlemap.getCenter();

	if (!currentcenter) {
		//hasn't finished previous zoom yet. Give it some more time.
		setTimeout("updatemap()", 250);
	}

	else {
		

		//check if we have to do anything
		//use distanceFrom instead of equals since tiny little errors in rounding (or something) trigger it otherwise.
		if (currentzoom != mapzoom || mapcenter.distanceFrom(currentcenter) > 1) { 
		
			if (!googlemap.getBounds().containsLatLng(mapcenter)) {
				googlemap.zoomOut(null, true);
			}

			else if (currentzoom < mapzoom && !currentcenter.equals(mapcenter)) {
				googlemap.zoomIn(mapcenter,true,true);
			}
			
			else if (mapcenter.distanceFrom(currentcenter)> 1) {
				googlemap.panTo(mapcenter);
			}

			else if (currentzoom > mapzoom) {
				googlemap.zoomOut(mapcenter,true);
			}

			else if (currentzoom < mapzoom) {
				googlemap.zoomIn(mapcenter,true,true);
			}

		}
		setTimeout("updatemap()", 500);
	}	
	return;
}

function showupdate(update) {
	//just to make sure this is running... kludgy, but hey.
//	now = new Date();
//	if (lastmapupdate+1000 < now.getTime()) updatemap();

        type=update.getElementsByTagName("type")[0].firstChild.data;
        switch(type) {
		case 'erasetext':
		textlist = document.getElementById("textlist");
		textlist.innerHTML = "";
		break;

		case 'image':
		case 'movie':
		newimage = mediaurl + "/" + update.getElementsByTagName(sizetype + "name")[0].childNodes[0].nodeValue;
		alttext = update.getElementsByTagName("value")[0].childNodes[0].nodeValue;

		if (type=='movie') {
			imagesize = update.getElementsByTagName(sizetype + "size")[0].childNodes[0].nodeValue.split("x");
			if (sizetype =='client') autoplay = 'true';
			else autoplay = 'false';
			
			imagesize[1] = parseInt(imagesize[1]) + 24;
			
			if (imagesize[0] < 220) imagesize[0] = 220;

			document.getElementById("images").innerHTML = 
				"<a href=\"" + escape(newimage) + "\" alt=\"" + escape(alttext) + "\" " +
				"style=\"width=" + imagesize[0] + "px; height=" + imagesize[1] + "px;\" " +
				"id=\"moviecontainer\" />\n";
			
			flowplayer("moviecontainer", movieplayer, {
				clip:  {
					autoPlay: autoplay,
					autoBuffering: true,
					scaling: 'orig',
					showMenu: false,
					autoRewind: true
				},

				plugins: {
					controls: {
						stop:false,
						playlist:false, 
						fullscreen:false,
						height:24
					}
				}
			});






		}
		
		else {
			document.getElementById("images").innerHTML = "<img src=\"" + escape(newimage) + "\" alt=\"" + escape(alttext) + "\" />\n";
		}
		break;

		case 'gmapcenter':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			center = update.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			mapcenter = GLatLng.fromUrlValue(center);
			mapzoom = update.getElementsByTagName("extravalue")[0].childNodes[0].nodeValue.split(',')[0];

			//updatemap();
		}
		break;

		case 'gmaplocation':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			pointer.setLatLng(GLatLng.fromUrlValue(update.getElementsByTagName("value")[0].childNodes[0].nodeValue))
		}
		break;
		
		case 'gmaptype':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			switch(update.getElementsByTagName("value")[0].childNodes[0].nodeValue) {
				case "p":
				googlemap.setMapType(G_PHYSICAL_MAP);
				break;

				case "m":
				googlemap.setMapType(G_NORMAL_MAP);
				break;

				case "k":
				googlemap.setMapType(G_SATELLITE_MAP);
				break;

				case "h":
				googlemap.setMapType(G_HYBRID_MAP);
				break;
			}
		}	
		break;

		case 'gmapcustomclear':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			for (var i in custommaps) {
				googlemap.removeOverlay(custommaps[i]);
				delete custommaps[i];			
			} 
		}
		break;

		case 'gmapcustomoff':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			id = update.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			googlemap.removeOverlay(custommaps[id]);
			delete custommaps[id];
		}
		break;


		case 'gmapcustom':
		if (!googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}
		else {
			//add it to the array and activate it.

			url = mediaurl + "/" + update.getElementsByTagName("clientname")[0].childNodes[0].nodeValue;
			coords = update.getElementsByTagName("extravalue")[0].childNodes[0].nodeValue.split('/');
			id = update.getElementsByTagName("id")[0].childNodes[0].nodeValue;
			
			
			if (custommaps[id]) {
				googlemap.removeOverlay(custommaps[id]);
				delete custommaps[id];
			}

			custommaps[id] = new GGroundOverlay(url, new GLatLngBounds(GLatLng.fromUrlValue(coords[0]), GLatLng.fromUrlValue(coords[1])));
			googlemap.addOverlay(custommaps[id]);
		}
		break;

	
		case 'mapcoords':
		if (googlemap)  {
			//this is messy, but necessary
			window.location.reload();
		}
		
		else {
			newcoords = update.getElementsByTagName("extravalue")[0].childNodes[0].nodeValue.split("x");
			pointerdiv = document.getElementById("pointer");
			pointerdiv.style.left = newcoords[0] + "%";
			pointerdiv.style.top = newcoords[1] + "%";
		}
		break;
	
		case 'map':
		if (googlemap) {
			//this is messy, but necessary
			window.location.reload();
		}

		else {

			//if updating status, a lot of this will be different.
		
			//for status:
			//change the image (use thumb size) (put it into mapimgcontainer)
			//getimagesize
		
			//for client
			//change the image (use client size) (change mapimg src)
			//get image size
			//mapcontainer margin
			//get info about new map
			newmap = mediaurl + "/" + update.getElementsByTagName(sizetype + "name")[0].childNodes[0].nodeValue;
			mapsize = update.getElementsByTagName(sizetype + "size")[0].childNodes[0].nodeValue.split("x");
			mapdiv = document.getElementById("mapimgcontainer");
			
			//set the mapdiv's size to the img size, and fill it with the new img code
			mapdiv.style.width = mapsize[0] + "px";
			mapdiv.style.height = mapsize[1] + "px";
			
			if (sizetype == "thumbnail") {
				document.getElementById("mapimg").src = newmap;
			}
			else {
				mapdiv.innerHTML = "<img id=\"mapimg\" src=\"" + escape(newmap) + "\" alt=\"" + escape(newmap) + "\" />\n";
				mainmapdiv = document.getElementById("map");
				divheight = mainmapdiv.offsetHeight;
				divwidth = mainmapdiv.offsetWidth;
				mapcontainer = document.getElementById("mapcontainer");
				newmargin = ((divheight - mapsize[1]) / 2 - 3) + "px " + ((divwidth - mapsize[0]) / 2 - 3) + "px";
				mapcontainer.style.margin = newmargin;
			}
		}
		break;
		
		case 'pointer':
		if (googlemap) {
			googlemap.removeOverlay(pointer);
			pointericon.image = mediaurl + "/" + update.getElementsByTagName(sizetype + "name")[0].childNodes[0].nodeValue;
			pointersize = update.getElementsByTagName(sizetype + "size")[0].childNodes[0].nodeValue.split("x");
			target = update.getElementsByTagName("extravalue")[0].childNodes[0].nodeValue.split("x");
			pointericon.iconSize = new GSize(pointersize[0], pointersize[1]);

			anchor = new Array();
			for (i=0; i<2; i++) {
				anchor[i] = Math.round((target[i] * pointersize[i] / 100.0));
			}
			
			pointericon.iconAnchor = new GPoint(anchor[0], anchor[1]);
			googlemap.addOverlay(pointer);
		}

		else {

			alttext = update.getElementsByTagName("value")[0].childNodes[0].nodeValue;
			pointer = mediaurl + "/" + update.getElementsByTagName(sizetype + "name")[0].childNodes[0].nodeValue;
			pointersize = update.getElementsByTagName(sizetype +"size")[0].childNodes[0].nodeValue.split("x");
			newtarget = update.getElementsByTagName("extravalue")[0].childNodes[0].nodeValue.split("x");
			pointerdiv = document.getElementById("pointer");
			pointerdiv.style.width = pointersize[0] + "px";
			pointerdiv.style.height = pointersize[1] + "px";
			document.getElementById("pointer").innerHTML = "<img src=\"" + escape(pointer) + "\" alt=\"" + escape(alttext) + "\" style=\"right: " + newtarget[0] + "%; bottom: " + newtarget[1] + "%;\" />\n";
		}
		break;
		
		case 'text':
		newtext = update.getElementsByTagName("value")[0].childNodes[0].nodeValue;
		textlist = document.getElementById("textlist");
		textlist.innerHTML = "<li>" + unescape(newtext) + "</li>\n" + textlist.innerHTML;
		break;
		
		default:
		error("invalid XML - element \"" + type + "\" is not defined.")
		
		
		case 'audiostream':
		//switch flowplayer from one stream to the other.
		if (update.getElementsByTagName("value")[0].childNodes[0].nodeValue == 'stream') {
			//switch to live stream
			streamurl = 'http://' + window.location.hostname + ':8000/tour-' + tour + '.mp3';
		}
		else {
			//switch to hold music
			streamurl = 'http://' + window.location.hostname + ':8000/music.mp3';
		}

		//activate the new url
		currentstreamurl = streamurl;
		if (audiostreamplayer.isPlaying())
			audiostreamplayer.play(streamurl);
		else
			audiostreamplayer.addClip(streamurl,0);


		break;
        }
}

