var lastTimerId = 'no'
var lastTimerCentraNave = 'no'
var lastTimerShowRotta = 'no'

var lastMouseX = -1;
var lastMouseY = -1;
var curX;
var curY;

// preload dell'img del warning in legenda
warn_legenda = new Image();
warn_legenda.src = "warning.gif";


// faccio blinkare la nave con l'icona del warning
function warningNave(cd_nave) {  // Reusable timer
  setTimeout("imgturn('" +cd_nave+ "')", 1000);
}
function imgturn(cd_nave) {   // Reusable image turner
  var img_legenda = document.getElementById('legenda_'+ cd_nave);
  img_legenda.alt = "WARNING: course and speed might be inaccurate!";
  if (img_legenda.src == warn_legenda.src) {
    img_legenda.src = 'nave_1_' + cd_nave + '.gif';
    warningNave(cd_nave);
  }
  else {
    img_legenda.src = 'warning.gif';
    warningNave(cd_nave);
  }
}


// apro il popup di google map
function popup_google_map(lat, lon){
	pgm = window.open('vessel_pos.php?lat='+lat+'&lon='+lon, '', 'width=920,height=670');
	pgm.focus();
	return false;
}

// apro il popup di help
function popup_help(){
	phelp = window.open('popup_help.html', '', 'width=700,height=632');
	phelp.focus();
	return false;
}

// apro il popup delle demo
function popup_demo(num){
	pdemo = window.open('demo/'+num+'.html', '', 'width=700,height=445');
	pdemo.focus();
	return false;
}

// attivo i TIP per i porti, sul click
function attivaTipPorto(testo, tip_width, id_porto){

   	// nascondo eventuali precedenti tooltip
	//hideddrivetip();

	// elimino eventuali timer precedenti
	//if (lastTimerId != 'no') {
	//	clearTimeout(lastTimerId);
	//	lastTimerId = 'no';
	//}

   	// mostro il tooltip
   	//funcstring = 'ddrivetip(\''+ testo +'\', '+ tip_width +');';
   	//lastTimerShowRotta = setTimeout(funcstring, 100);
	ddrivetip(testo, tip_width);


	// dopo 2 secondi lo nascondo
	//lastTimerId = setTimeout('hideddrivetip()', 2000);

	return;
}

// faccio l'animazione che evidenzia la nave
function evidenziaNave(cd_nave) {

  // elimino eventuali timer precedenti
  if (lastTimerCentraNave != 'no') {
	clearTimeout(lastTimerCentraNave);
	lastTimerCentraNave = 'no';
  }

  // evidenzio la nave
  document.images[cd_nave].src='http://www.tarros.it/gps/centra_nave.gif';
  document.images[cd_nave].style.visibility = 'visible';

  // aspetto x millisecondi e poi la rimetto apposto
  var funcname = 'disevidenziaNave(\'' + cd_nave + '\')';
  lastTimerCentraNave = setTimeout(funcname, 800);

  return false;
}

// nascondo l'evidenziazione della nave
function disevidenziaNave(cd_nave) {
  document.images[cd_nave].src='http://www.tarros.it/gps/blank.gif';
  document.images[cd_nave].style.visibility = 'hidden';
  return true;
}

// mostra in sequenza lo spostamento di una nave, mostrando e nascondendo in sequenza le varie posizioni, fino ad un massimo di maxid
function ciclaNave(idimg, maxid, cd_nave) {

	// nasconde l'ultima nave, visto che il ciclo precedente non me la nasconde
	document.images[cd_nave + '_' + maxid].style.visibility = 'hidden';

	// mostro la nave corrente
	document.images[cd_nave + '_' + idimg].style.visibility = 'visible';

	// nascondo la nave precedente, se questa non è la prima nave dell'animazione
 	if (idimg > 1) {
   		y = idimg - 1;
   		document.images[cd_nave + '_' + y].style.visibility = 'hidden';
 	}

	// se l'animazione non è terminata, richiamo questa funzione con un leggero ritardo
 	if (idimg < maxid) {
 	  x = idimg + 1;
 	  var funcname = 'ciclaNave(' + x + ', ' + maxid + ', \'' + cd_nave + '\')';
 	  setTimeout(funcname, 150);
 	}

	// l'animazione è terminata, aspetto un po' e poi la faccio ripartire
	if (idimg == maxid) {
	  var funcname = 'ciclaNave(1, ' + maxid + ', \'' + cd_nave + '\')';
	  setTimeout(funcname, 4000);
	}
}



// genera l'animazione con il pallino rosso per le navi che sono in porto
function aniPort(idimg, rotation, type, colour) {
	var myimg = document.images[idimg];
	if (type == 1) {
		myimg.src = '/bofl/gps/port.gif';
		funcstring = 'aniPort(\''+idimg+'\', '+rotation+', 2, '+colour+')';
		setTimeout(funcstring, 500);
	}
	else {
		myimg.src = '/bofl/gps/nave_' + rotation + '_' + colour + '.gif';
		funcstring = 'aniPort(\''+idimg+'\', '+rotation+', 1, '+colour+')';
		setTimeout(funcstring, 4000);
	}
}

// mostro/nascondo la gif trasparente della rotta relativa al porto su cui ho posizionato il mouse
// le gif sono tutte caricate insieme alla pagina
function showRotta(id_rotta){
  document.getElementById('rotta_' + id_rotta).style.visibility = 'visible';
}
function hideRotta(id_rotta){
  document.getElementById('rotta_' + id_rotta).style.visibility = 'hidden';
}

function showRottaTimed(id_rotta){

  lastMouseX = -1;
  lastMouseY = -1;
  hideddrivetip();
/*
  if (lastTimerShowRotta != 'no') {
  	clearTimeout(lastTimerShowRotta);
	lastTimerShowRotta = 'no';
  }
*/
  document.getElementById('rotta_' + id_rotta).style.visibility = 'visible';
  funcstring = 'hideRotta(' + id_rotta + ')';
  setTimeout(funcstring, 2000);

}


/***********************************************
* Drop Down/ Overlapping Content- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

document.write('<div id="dhtmltooltip" name="dhtmltooltip"></div>') //write out tooltip DIV

function ddrivetip(the_text, the_width){
    if (document.getElementById){
        var subobj=document.getElementById('dhtmltooltip')
        subobj.style.display = "block"
		subobj.style.width = the_width+"px"

		// questo varia per IE o Firefox...
		if (navigator.userAgent.indexOf('MSIE') == -1) { var img_x = the_width - 11; }
		else { var img_x = the_width - 17; }

        subobj.innerHTML = '<div style="position:absolute; left: ' +img_x+ 'px; width: 13px; height: 13px;"><a href="#" onClick="hideddrivetip(); return false"><img src="close.gif" width="13" height="13" border="0"></a></div>'
		subobj.innerHTML += the_text
		var xpos = "4";
		var ypos = "4";
        subobj.style.left = xpos + "px"
        subobj.style.top = ypos + "px"
        slowhigh(subobj)
        return false
    }
    else
        return true
    }

function hideddrivetip() {
    obj = document.getElementById('dhtmltooltip')
	obj.style.display = "none"
    browserdetect=obj.filters? "ie" : typeof obj.style.MozOpacity=="string"? "mozilla" : ""
    if (browserdetect=="mozilla") { document.getElementById('dhtmltooltip').style.MozOpacity = 0 }
    else if (document.getElementById('dhtmltooltip').filters.alpha) { document.getElementById('dhtmltooltip').filters.alpha.opacity = 0 }
    return false
}

var floatiebgcolor="lightyellow" //default bgcolor of floatie
var fadespeed=30 //speed of fade (5 or above). Smaller=faster.

var baseopacity=10
function slowhigh(which2){
    imgobj=which2
    browserdetect=which2.filters? "ie" : typeof which2.style.MozOpacity=="string"? "mozilla" : ""
    instantset(baseopacity)
    highlighting=setInterval("gradualfade(imgobj)",fadespeed)
}

function instantset(degree){
    cleartimer()
    if (navigator.userAgent.indexOf('MSIE') == -1)
    {    document.getElementById('dhtmltooltip').style.MozOpacity = degree/100;	}
    else if (document.getElementById('dhtmltooltip').filters.alpha) {
    	document.getElementById('dhtmltooltip').filters.alpha.opacity = degree;
    }
}

function cleartimer(){
    if (window.highlighting) clearInterval(highlighting)
}

function gradualfade(cur2){
    if (navigator.userAgent.indexOf('MSIE') == -1) { cur2.style.MozOpacity=Math.min(parseFloat(cur2.style.MozOpacity)+0.1, 0.99); }
    else if (window.highlighting) { clearInterval(highlighting); }
    else if (document.getElementById('dhtmltooltip').filters.alpha) { cur2.filters.alpha.opacity+=10; }
}

function ietruebody(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function paramexists(what){
    return(typeof what!="undefined" && what!="")
}