/*
*
* Create a class to store all the map info
*
*/
var MapInfo = Class.create({
	
	initialize: function() {
		//the what is currently going on in the application
		this.state = "MarkerPlacement"
		this.selectedMarker = -1;
	    this.name = "MapInfo";
		//make a variable to keep track of the markers we're dropping
		this.markerList = new Array();
	},
  
	say: function(message) {
    	return this.name + ': ' + message;
	},
	
	//set the Html of a marker
	setHtml: function (marker) {
		//get the id of this marker
		var markerIndex = this.markerList.lastIndexOf(marker);
		
		VVmapInfo.selectedMarker = markerIndex;
		
		var mapUnlocked = true;
		if (passwordSet)
		{
			if(password == "")
			{
				mapUnlocked = false;
			}
		}
		
		if(mapUnlocked)
		{
			marker.html = '<div><form><input type="button" onclick="VVmapInfo.removeMarker(' + markerIndex + ')" value="Delete Marker and Audio"></form>';
		}
		else
			marker.html = "<div>";

		
		//if there is a recordingKey
		if((marker.recordingKey) && (marker.soundAddress!= "undefined"))
		{
			//display the flash player because this marker has sound recorded for it
			marker.html += '<BR><embed src="vvplayer.swf" quality="high" bgcolor="#ffffff" width="300" height="125" name="vvplayer" FlashVars="src=' + PROXY + marker.soundAddress +'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		}
		else
		{
			if(mapUnlocked)
			{
				//display a button to create a recording for this marker
				marker.html += '<form><input type="button" onclick="createRecording(' + markerIndex + ')" value="Call in Audio!"></form>'; 
			}
			else
			{
				marker.html = "There is no audio for this Marker.<BR> Unlock the map and Call in the audio for it!";
			}
		}
		
		marker.html += "</div>";
	},
	
	//set the marker to store the address of the mp3
	setSoundAddress: function (index, soundAddress)
	{
		this.markerList[index].soundAddress = soundAddress;
	},
	
	//remove a marker from the list
	removeMarker: function (index)
	{
		if (passwordSet)
		{
			if(password == "")
			{
				alert("Unlock the map first");
				return;
			}
		}
		
		if(index>-1)
		{
			//remove the sound if there is a sound set
			if(this.markerList[index].recordingKey)
			{
				clearRecording(this.markerList[index].recordingKey);
			}
			//tell google map to take it off
			map.removeOverlay(this.markerList[index]);
			//remove it from our list
			this.markerList.splice(index,1);
		}
	},
	
	//add a marker to the map
	addMarker: function() {
		
		//get the bounds data
		var bounds = map.getBounds();
	    var southWest = bounds.getSouthWest();
	    var northEast = bounds.getNorthEast();

		//use the bounds data to find the mid point
	    var lngSpan = (northEast.lng() - southWest.lng())/2;
	    var latSpan = (northEast.lat() - southWest.lat())/2;
	    
		//make a new latitude and longitude point with the mid point data
		var latlng = new GLatLng(southWest.lat() + latSpan, southWest.lng() + lngSpan);
		
		//make a dragable marker at the mid point
		var marker = new GMarker(latlng, {draggable: true});
		
		//hide any info windows when dragging the marker
		GEvent.addListener(marker, "dragstart", function() {
		map.closeInfoWindow();
		});
		
		//set the soundAddress
		marker.soundAddress = "undefined";
		
		//show the option to add sound when clicking on the marker
		GEvent.addListener(marker, "click", function() {
			//set the html you see specific for this marker
			VVmapInfo.setHtml(marker);
			//open the info window with the html we've set
			marker.openInfoWindowHtml(marker.html);
		});
		
        //add the marker to the map
	    map.addOverlay(marker);
		//add the marker to our markerList
		selectedMarker = this.markerList.length;
		this.markerList.push(marker); 
		
		//let people know right away they can reposition the marker
		marker.openInfoWindowHtml("Drag me!");
	}

});

var VVmapInfo = new MapInfo();
