// only if the google-api is proper called in the header of the html-page, this works!
if (typeof google != 'undefined')
{
	google.load("maps", "2.x");
}

/**
 * An object describing any important things about a user.
 */
var myLieuUser = Class.create();

myLieuUser.prototype=
{
	initialize:function()
	{
		this.myUserMarker = null;
		this.myPosition = null;
		this.myAltitude = 0;
		this.myLastSpeed = 0;
		this.myNumSatellites = 0;
		this.myDirection = 0.0;
		this.myLastSatelliteInfo = 0;
	},
	// creates an icon for this user. Depends on the username, where the icon lies
	createUserIcon:function()
	{
		// for working with google you may use a shadow-icon
		return aMapEngine.createIcon("usericon.gif", 40, 44, "shadow.png", 83, 44);
	},
	// Creates a marker on the last known position
	createMarker:function(aPosition) 
	{
		return aMapEngine.createMarker(aPosition, this.createUserIcon());
	},
	changePosition:function(aPosition)
	{
		return aMapEngine.changeMarker(this.myUserMarker, aPosition);
	},
	/* trys to fetch the latest known position of this user
	 */
	getLastPosition:function() 
	{
		var myDate = new Date();
		
		var myXMLFile = "geodata.xml?" + myDate.getTime();

		var thisInstance = this;
		
		var myAjax = new Ajax.Request(myXMLFile,
		{
			method: 'get',
			onSuccess: function(request) 
			{
				if (request.readyState == 4) 
				{
					var xmlDoc = request.responseXML;
	
					if (!xmlDoc || !xmlDoc.documentElement)
					{
						// no doc, no elements, no fun!
						return;
					}
	
					// read the elements from the xml-file
					var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	
					if (!markers || (markers.length < 1))
					{
						// no markers, no fun!
						return;
					}
	
					// take only the last one
					var myLastElement = markers.length - 1;
	
					// thisInstance.myPosition = new GLatLng(parseFloat(markers[myLastElement].getAttribute("lat")), parseFloat(markers[myLastElement].getAttribute("lng")));
					thisInstance.myPosition = aMapEngine.createLatLong(parseFloat(markers[myLastElement].getAttribute("lat")), parseFloat(markers[myLastElement].getAttribute("lng")));
					addMessage("lat: " + parseFloat(markers[myLastElement].getAttribute("lat")) + " lng: " + parseFloat(markers[myLastElement].getAttribute("lng")) );
					thisInstance.myLastSpeed = markers[myLastElement].getAttribute("speed");
					thisInstance.myNumSatellites = markers[myLastElement].getAttribute("numsat");
					thisInstance.myAltitude  = markers[myLastElement].getAttribute("alt");
					thisInstance.myDirection = markers[myLastElement].getAttribute("dir");
	
					// check, if we got an icon for this user
					if (!thisInstance.myUserMarker)
					{
						thisInstance.myUserMarker = thisInstance.createMarker(thisInstance.myPosition);
						aMapEngine.addMarker(thisInstance.myUserMarker);
					}
					else
					{
						thisInstance.changePosition(thisInstance.myPosition);
					}		
	
	
					// then my speed will be displayed
					addMessage("Speed: " + thisInstance.myLastSpeed);
					addMessage("Alt: " + thisInstance.myAltitude);
	
					if (aMapEngine)
					{
						aMapEngine.map().panTo(thisInstance.myPosition);
					}
				}
			}
		});
	}

};




// the google-panel
var aMapEngine = null;

// create an object which has an Icon and a position
var aUser = new myLieuUser();

function checkPHPversion()
{
	var myDate = new Date();
	
	var myCheckFile = "checker.php";
	
	var myParameter = "action=phpversion";

	var myAjax = new Ajax.Request(myCheckFile,
	{
		method: 'get',
		parameters: myParameter,
		onSuccess: function(request) 
		{
			if (request.responseText < '5.0.0')
			{
				alert("php version on the server is too low: " + request.responseText);
			}
			else
			{
				addMessage("php-version: " + request.responseText);
				// go on with the next check
				onCheck(99);
			}
		}
	});
}


/**
 * This one is called instead of onLoad at the beginning
 * of this page.
 * We will check here some preconditions for myLieu:
 * - php-Version
 * - Ajax-support
 * - file-writing on the server
 * 
 */
function onCheck(stage)
{
	switch (stage)
	{
		case 1:
			checkPHPversion();
			break;
			
		// last thing we do: load myLieu finally
		case 99:
			onLoad();
			break;
			
		case 0:
		default:
			break;
	}
}

/**
 * This one is called when the html-page is loaded.
 * @param string anUser
 * the name of the user you want to focus after loading
 */
function onLoad(anUser)
{	
	aMapEngine = new myGeoEngine();
	aMapEngine.init();
	
	// resize the map-div to screen of browser
	onResize();

	// start the periodical updater
	onUpdate();
}

/**
 * This one is a special loader for the viewing page
 */
function onViewLoad()
{
	aMapEngine = new myGeoEngine();
	aMapEngine.init();
	
	// set the map static to 640x480 with some space on the left
	// for the controls
	var myContentArea = $("map");
	
	if (myContentArea != null)
	{
		myContentArea.style.height = "480px";
		myContentArea.style.width = "600px";
		myContentArea.style.left = "200px";
	}
}

/**
 * resizing some div-element to the size of the browser-client-window
 */
function resizeDiv(myDiv)
{
	var myContentArea = $(myDiv);
	
	if (myContentArea != null)
	{
		var myNewHeight = getWindowHeight() + "px";
		myContentArea.style.height = myNewHeight;
		var myNewWidth = getWindowWidth() + "px";
		myContentArea.style.width = myNewWidth;
	}
}


/**
 * resize the inner map to the browser-windowsize
 */
function onResize()
{
	resizeDiv("map");
}

/**
 * does a refresh of the screen and start again the timer
 * every 2 seconds or whatever the value ist
 */
function onUpdate()
{
	// ask for the last position of the user
	var aPosition = aUser.getLastPosition();

	// repeat this every 2 seconds
	window.setTimeout("onUpdate()", 2000);	
}


/**
 * This shows an Text in the message-div and deletes
 * the previous
 */
function showMessage(aText)
{
	var myTextArea = $("message_text");
	myTextArea.value = aText;
}

/**
 * This adds an Text in the message-div 
 */
function addMessage(aText)
{
	var myTextArea = $("message_text");
	myTextArea.value = aText+"\n"+myTextArea.value;
}

/**
 *
 */
function routeChosing(aRouteFileName)
{
	if (aRouteFileName == "0")
	{
		return;
	}
	
	var myAjax = new Ajax.Request(aRouteFileName,
	{
		method: 'get',
		onSuccess: function(request) 
		{
			if (request.readyState == 4) 
			{
				var xmlDoc = request.responseXML;

				if (!xmlDoc || !xmlDoc.documentElement)
				{
					// no doc, no elements, no fun!
					return;
				}

				// read the elements from the xml-file
				var markers = xmlDoc.documentElement.getElementsByTagName("marker");

				if (!markers || (markers.length < 1))
				{
					// no markers, no fun!
					alert("no markers");
					return;
				}
				
				var points = [];
				
				for (var i = 0; i < markers.length; i++) 
				{
					// get each marker and store the latitude and longitude as a pointset into an array
					point = aMapEngine.createLatLong(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")));
					points.push(point);
				}
				aMapEngine.addPolyLine(points);				
			}
		}
	});
		

}

