/**
 * Object for the martian management.
 */


/**
 * Try to acquire martian control.
 */
function Martian_take() {
	var martianTooltip=document.getElementById('martiantooltip');
	martianTooltip.innerHTML='&nbsp;';
	this.taking=true;
	
}

/**
 * Try to release martian control.
 */
function Martian_release() {
	
	this.releasing=true;
	
}

/**
 * Walking mode.
 */
function Martian_walk() {
	
	this.walking=!this.walking;
	
}

/**
 * Manual movement, using the slider.
 */
function Martian_manualMove() {
	if(this.mode==Martian.modeMine) {
		var pos=Martian.slider.getValue();
		
		this.newPos=new Array(pos,this.track.profile.data[pos][0],this.track.profile.data[pos][1]);
		
	}
	
}

/**
 * Signals local martian position (under your control) to the world.
 */

function Martian_move(x,y) {
	// Sends current martian position to web service.
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.open("GET", "martianstatus.php?action=position&user="+user+"&name="+this.track.name+"&x="+x+"&y="+y+"&linear="+this.pos+"&time="+(new Date()).getTime(), false);
	xmlhttp.send(null);
	var response=xmlhttp.responseText;
	
	if(response=='OK') {
		
		// position signalled
	} else if(response=='SKIP') {
		
	} 
	
}

function Martian_checkMartian() {
	if(this.valid) {
		if(this.releasing) {
			
			// signals martian release to webservice
			var xmlhttp=new XMLHttpRequest();
			xmlhttp.open("GET", "martianstatus.php?action=unlock&user="+user+"&name="+this.track.name+"&time="+(new Date()).getTime(), false);
			xmlhttp.send(null);
			var response=xmlhttp.responseText;
			
			if(response=='OK' || response=='FREE') {
				// martian released
				this.mode=Martian.modeFree;
				this.reset();
			} else if(response=='SKIP') {
				
			} else
				alert('Errore nel rilascio: '+response);
			this.releasing=false;
		} else if(this.taking) {
			// Asks martian control to web service.
			var xmlhttp=new XMLHttpRequest();
			var x=this.track.lines[0].p1.x;
			var y=this.track.lines[0].p1.y;
			xmlhttp.open("GET", "martianstatus.php?action=lock&user="+user+"&name="+this.track.name+"&x="+x+"&y="+y+"&linear="+this.pos+"&time="+(new Date()).getTime(), false);
			xmlhttp.send(null);
			var response=xmlhttp.responseText;
			
			if(response=='OK') {
				this.martianControl(this.track.lines[0].p1.x,this.track.lines[0].p1.y,'undercontrol');
				enableButton('controlbutton');
				this.mode=Martian.modeMine;
				// Control acquired
			} else if(response=='LOCKED')
				alert('Mi dispiace, qualcuno ha acquisito il marziano prima di te');
			else if(response=='SKIP')
				alert('Errore. Riprova.');
			else
				alert('Errore nella lettura dello stato: '+response);
		
			this.taking=false;
		} else if(this.newPos!=null) {
			this.pos=this.newPos[0];
			var x=this.newPos[1];
			var y=this.newPos[2];
			this.martianControl(x,y,'undercontrol');
			this.move(x,y);
			this.newPos=null;
		} else if(this.walking) {
			if(this.pos<this.track.profile.getWalkLength()-1) {
				var x=this.track.profile.data[this.pos][0]
				var y=this.track.profile.data[this.pos][1]
				this.martianControl(x,y,'undercontrol');
				this.move(x,y);
				this.pos++;
			} else
				this.walking=false;

		} else {
			var xmlhttp=new XMLHttpRequest();
			xmlhttp.open("GET", "martianstatus.php?action=get&user="+user+"&name="+this.track.name+"&time="+(new Date()).getTime(), false);
			xmlhttp.send(null);
			var response=xmlhttp.responseText;
			
			if(response=='FREE') {	
				// the martian is free. you can acquire it
				// position the martian on path first point
				this.martianControl(this.track.lines[0].p1.x,this.track.lines[0].p1.y,'free');
				
				this.mode=Martian.modeFree;
				this.pos=0;
				Martian.slider.setValue(0);
			} else if(response.substring(0,7)=='LOCKED:') {
				// martian is locked by someon else
				var respArr=response.split(':');
				// updates martian position on the path
				this.pos=parseInt(respArr[3]);
				this.martianControl(parseInt(respArr[1]),parseInt(respArr[2]),'controlled');
				this.mode=Martian.modeLocked;
				
				
			} else if(response=='YOURS') {
				// the martian is under your control. the status is updated locally.
				var x=this.track.profile.data[this.pos][0];
				var y=this.track.profile.data[this.pos][1];
				this.martianControl(x,y,'undercontrol');
				enableButton('controlbutton');
				this.mode=Martian.modeMine;
				
			} else if(response=='SKIP') {
				// warning received from webservice. retry later
			} else
					alert('Errore nella lettura dello stato: '+response);
		}
		
		try	{
			setTimeout(this.objName+'.checkMartian()',300);
		} catch (e)	{
		}
	}	
}

/**
 * Updates GUI martian position (on plant and on profile).
 */
function Martian_martianControl(x,y,msg) {
	var martianImg=document.getElementById('martianplant');
	var martianProfile=document.getElementById('martian');
	var martianTooltip=document.getElementById('martiantooltip');
	martianImg.style.visibility='visible';
	martianImg.style.left=(x-8)+'px';
	martianImg.style.top=(y-8)+'px';

	martianProfile.style.left=(10+this.pos)+'px';
	martianProfile.style.top=(Profile.Height-parseInt(this.track.profile.data[this.pos][2]))+'px';
	
	var tooltip=parseInt(this.track.profile.data[this.pos][3]);
	document.getElementById('tooltip').innerHTML=tooltip+'m';
	
	martianTooltip.style.left=(x+2)+'px';
	martianTooltip.style.top=(y+2)+'px';
	martianTooltip.style.visibility='visible';
	
	if(msg!=this.msg)
		martianTooltip.innerHTML='<font face="Tahoma" size="2" color="blue">'+Martian.Messages[msg]+'</font>';
	this.msg=msg;
}

function Martian_reset() {
	this.taking=false;
	this.walking=false;
	this.releasing=false;
	this.newPos=null;
	this.pos=0;
	this.msg=null;
	Martian.slider.setValue(0);

}

/**
 * Hides martian.
 */
function Martian_clear() {
	this.taking=false;
	this.walking=false;
	this.releasing=false;
	this.newPos=null;
	this.pos=0;
	this.valid=false;
	this.msg=null;
	
	Martian.slider.setValue(0);
	document.getElementById('martianplant').style.visibility='hidden';
	document.getElementById('martiantooltip').style.visibility='hidden';
	var martianTooltip=document.getElementById('martiantooltip');
	martianTooltip.innerHTML='&nbsp;';
}

function Martian(mode,track,objName) {
	this.valid=true;
	this.taking=false;
	this.walking=false;
	this.releasing=false;
	this.newPos=null;
	this.track=track;
	this.objName=objName;
	this.mode=mode;
	this.pos=0;
	this.msg=null;
	
	setTimeout(this.objName+'.checkMartian()',300);
}

Martian.prototype.reset=Martian_reset;
Martian.prototype.clear=Martian_clear;
Martian.prototype.checkMartian=Martian_checkMartian;
Martian.prototype.martianControl=Martian_martianControl;
Martian.prototype.take=Martian_take;
Martian.prototype.release=Martian_release;
Martian.prototype.walk=Martian_walk;
Martian.prototype.manualMove=Martian_manualMove;
Martian.prototype.move=Martian_move;

Martian.modeFree=0;
Martian.modeMine=1;
Martian.modeLocked=2;
Martian.Messages=new Array();
Martian.Messages['free']='Il marziano è libero.<br/>Clicca <span style="cursor:pointer;color:red;text-decoration:underline" onclick="takeMartian()">qui</span> per prenderne il controllo.';
Martian.Messages['undercontrol']='Il marziano è sotto il tuo controllo.<br/>Comandalo attraverso il pannello di controllo';
Martian.Messages['controlled']='Il marziano è sotto il controllo di qualcuno.<br/>Segui i suoi movimenti sulla mappa e/o sul profilo.';
Martian.slider=null;

