﻿// JavaScript Document
//--------------------------------------------

var dirViento = (298+180)%360;

function VerTodos()
{
	lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	lookAt.set(opcionesYLook[0],opcionesYLook[1],opcionesYLook[2],ge.ALTITUDE_RELATIVE_TO_GROUND,opcionesYLook[3],opcionesYLook[4],opcionesYLook[5]);
//	lookAt.set(40.49967,-3.55916,0,ge.ALTITUDE_RELATIVE_TO_GROUND,-136,54,8121);
	ge.getView().setAbstractView(lookAt);
	ge.getOptions().setFlyToSpeed(opcionesYLook[7]); //ge.SPEED_TELEPORT); 
	if(opcionesYLook[8] == 1) toutOla = setTimeout("EntraOla()",5000);
	else clearTimeout(toutOla);
	dirViento = opcionesYLook[9];
}
//===========================================================================================
//			CARGA DEL KML
//===========================================================================================
var currentKmlObject = null;

function fetchKml(kmlUrl) {
	removeKml();
  	google.earth.fetchKml(ge, kmlUrl, finishFetchKml);
}

function finishFetchKml(kmlObject) {
  // check if the KML was fetched properly
  if (kmlObject) {
    // add the fetched KML to Earth
    currentKmlObject = kmlObject;
    ge.getFeatures().appendChild(currentKmlObject);
  } else {
    alert('Bad KML');
  }
}

function removeKml() {
  if (currentKmlObject) {
    ge.getFeatures().removeChild(currentKmlObject);
    currentKmlObject = null;
  }
}
/*
function prueba()
{
	var screenOverlayAviso = getItemById(currentKmlObject,"RESPONSABILIDADLEGAL");
	if(screenOverlayAviso == null) alert(ge.getType()+"   "+ge.getFeatures().hasChildNodes()+"   "+"no encontrado");
	else alert("hola");
}
*/
function CambiosProvincia(tships,tinfos)
{
	if(GEisopen)
	{
		waitships = tships; 
		waitinfos = tinfos;
		screenOverlayFoto.getOverlayXY().setX(0);
		Terminado();
	}
}

function Terminado()
{
	if(terminado)
	{
		if( (waitships.length >0)||(waitborrarships.length > 0))
		{
			rships = waitships;
			rinfos = waitinfos;
			borrarships = waitborrarships;
			waitships = new Array();
			waitinfos = new Array();
			waitborrarships = new Array();
			indiceMover = 0;
			indiceBorrar = 0;
			terminado = false;
			esperaFinMover = true;
		}
		else if(esperaFinMover)
		{
			esperaFinMover = false;
			document.getElementById(paramSWF).EsperaFinMover();	
		}
	}
}

function BorrarProvincia(tships)
{
		waitborrarships = tships;
}

var esperaFinMover = false;
var esperaOla = false;
var esperaBalizas = false;
var esperaMarcas = false;
var esperaLineas = false;
var indiceBorrar = 0;
var indiceMover = 0;
var terminado = true;

function ballooncloseEventListener()
{
	mmsiSelected = 0;
}

function moveendEventListener()
{	
	var totalMover = 1;
	if((borrarships.length - indiceBorrar) > totalMover) totalMover = borrarships.length - indiceBorrar;
	if((rships.length - indiceMover) > totalMover) totalMover = rships.length - indiceMover;
	
	for(var tt=0; tt<totalMover; tt++)
	{
		if(opcionesYLook[10] == 1)RCambiosProvinciaPlanes();
		else RCambiosProvincia();
	}
	if(esperaOla)
	{
		esperaOla = false;
		Ola();
	}
	if(esperaBalizas)
	{
		esperaBalizas = false;
		PresentarBalizas();
	}
	if(esperaMarcas)
	{
		esperaMarcas = false;
		PresentarMarcas();
	}
	if(esperaLineas)
	{
		esperaLineas = false;
		PresentarLineas();
	}
	if(esperaObjeto)
	{
		esperaObjeto = false;
		VerBarcoNow();
	}
}
//===========================================================================================
//			CREAR, ELIMINAR O MODIFICAR LOS BARCOS DE UNA ZONA
//===========================================================================================
var pShipPlacemark = new Array();
var rumbosPlacemark = new Array();
var trackPlacemark = new Array();
var tiemposPlacemark = new Array();
var siluetasPlacemark = new Array();
var modelos3DPlacemark = new Array();
var descriptores = new Array();
var toutPlacemark;
var waitships = new Array();
var rships = new Array();
var waitinfos = new Array();
var rinfos = new Array();
var waitborrarships = new Array();
var borrarships = new Array();
var mmsiSelected = 0;
var anguloCamara = 0;
var sumaAnguloParcial = 0;
var barcosConModelo = new Array(); //indice MMSI 0-no tiene modelo, 1-cargando, 2-esperando, 3-tiene Modelo
var listaBarcosEsperandoModeloNumero = new Array();



function RCambiosProvincia()
{
//============================================primero los que haya que borrar
	if(indiceBorrar < borrarships.length)
	{
		currentShip = borrarships[indiceBorrar];
		indiceBorrar++;
		tiemposPlacemark[currentShip.mmsi] = 0;
		barcosConModelo[currentShip.mmsi] = 0;
		if(pShipPlacemark[currentShip.mmsi] != null)
		{
			ge.getFeatures().removeChild(pShipPlacemark[currentShip.mmsi]); 
			pShipPlacemark[currentShip.mmsi] = null;
		}
		if(modelos3DPlacemark[currentShip.mmsi] != null)
		{
			ge.getFeatures().removeChild(modelos3DPlacemark[currentShip.mmsi]);
			modelos3DPlacemark[currentShip.mmsi] = null;
		}
		if(rumbosPlacemark[currentShip.mmsi] != null)
		{
			ge.getFeatures().removeChild(rumbosPlacemark[currentShip.mmsi]);
			rumbosPlacemark[currentShip.mmsi] = null;
		}
		if(siluetasPlacemark[currentShip.mmsi] != null)
		{
			ge.getFeatures().removeChild(siluetasPlacemark[currentShip.mmsi]); 
			siluetasPlacemark[currentShip.mmsi] = null;
		}
	}
//============================================despues los que haya que modificar
	if(indiceMover < rships.length)
	{
		currentShip = rships[indiceMover];
		currentInfo = rinfos[indiceMover]
		indiceMover++
		tiemposPlacemark[currentShip.mmsi] = 30;
		
//===================================================================		
		if(pShipPlacemark[currentShip.mmsi] == null) CreatePeloto(urlIcon(currentShip.type),currentShip.mmsi,currentShip.name);	
		if(modelos3DPlacemark[currentShip.mmsi] == null)CrearModeloBarco(currentShip)
//=====================================================================					
		
		if(currentShip.speed > 0.3) var rotation = currentShip.course;
		else{
			if(currentShip.heading != 511) rotation = currentShip.heading;
			else rotation = currentShip.course;
		}

		var strMMSI = currentShip.mmsi.toString();
		var auxstringmmsi = "000000000";
		if(strMMSI.length < 9)
		{
			strMMSI = auxstringmmsi.substr(0,9-strMMSI.length) + strMMSI;
		}

		var strURL = "http://www.localizatodo.com/mapa/ShipImages/"+strMMSI.substr(0,3)+"/"+strMMSI.substr(3,2)+"/"+strMMSI.substr(5,2)+"/"+strMMSI.substr(7,2)+".jpg";
		var strInfo = "<img src='"+strURL+"' ><table width='220'  ><tr><td  ><b>MMSI: </b>"+currentShip.mmsi+"<br>"+currentInfo+"<br><b>Recibido hace: </b>"+MargenTiempo(currentShip.timeStamp)+"</td></tr></table>"
		strInfo += "<i>Ver: <a href='#' onclick='VistaProa("+currentShip.mmsi+","+rotation+" )' >proa</a>  <a href='#' onclick='VistaPopa("+currentShip.mmsi+","+rotation+")' >popa</a>  <a href='#' onclick='VistaBabor("+currentShip.mmsi+","+rotation+")' >babor</a>  <a href='#' onclick='VistaEstribor("+currentShip.mmsi+","+rotation+")' >estribor</a></i>" 
		
		descriptores[currentShip.mmsi] = strInfo;

		if(modelos3DPlacemark[currentShip.mmsi] != null) Move3dPlacemark(currentShip)
		if(pShipPlacemark[currentShip.mmsi] != null)
		{
			MovePeloto(currentShip.latitude,currentShip.longitude,0, currentShip.mmsi);
			if(mmsiSelected == currentShip.mmsi)
			{
				var balloon = ge.getBalloon();
				if(balloon != null)balloon.setContentString(strInfo);
			}
		}
		if(siluetasPlacemark[currentShip.mmsi] != null) MoveSilueta(currentShip,rotation)
		if(controlVerRumbo) RefrescaRumbo(currentShip);
		if(controlVerTrack) RefrescaTrack(currentShip);
		if(currentShip.mmsi == targetBarco)VistaSeguir(targetBarco,rotation,distanciaBarco);
	}
	if( (indiceBorrar >= borrarships.length)&&(indiceMover >= rships.length) ) {terminado = true; Terminado();}
}

function VistaBabor(index,icourse)
{
	anguloCamara = (icourse+90)%360;
	tipoVista = 3;
	VistaSeguir(index,icourse,0);
	ge.setBalloon(null);
}
function VistaEstribor(index,icourse)
{
	anguloCamara = (icourse+270)%360;
	tipoVista = 4;
	VistaSeguir(index,icourse,0);
	ge.setBalloon(null);
}
function VistaProa(index,icourse)
{
	anguloCamara = (icourse+180)%360;
	tipoVista = 2;
	VistaSeguir(index,icourse,0);
	ge.setBalloon(null);
}
function VistaPopa(index,icourse)
{
	anguloCamara = icourse;
	tipoVista = 1;
	VistaSeguir(index,icourse,0);
	ge.setBalloon(null);
}

var distanciaBarco = 0;

function VistaSeguir(index,xcourse,newDistance)
{
	targetBarco = index;
	CalculaAnguloCamara(xcourse)
	distanciaBarco = newDistance;
	if(distanciaBarco == 0)
	{
		if(opcionesYLook[10] == 2) distanciaBarco = 100*escalaGeneral;
		else distanciaBarco = 500;
	}
	VistaGeneral(index,anguloCamara,distanciaBarco);
}


function CalculaAnguloCamara(icourse)
{
	if(primerAnguloCamara)
	{
		if(tipoVista == 1) anguloCamara = icourse;  	//1-popa,2-proa,3-babor,4-estribor
		else if(tipoVista == 2) anguloCamara = (icourse+180)%360;  //1-popa,2-proa,3-babor,4-estribor
		else if(tipoVista == 3) anguloCamara = (icourse+90)%360;
		else if(tipoVista == 4) anguloCamara = (icourse+270)%360;
		primerAnguloCamara = false;
	}
	else if(tipoVista != 5)
	{
		if(tipoVista == 2) icourse = (icourse+180)%360;  //1-popa,2-proa,3-babor,4-estribor
		else if(tipoVista == 3) icourse = (icourse+90)%360;
		else if(tipoVista == 4) icourse = (icourse+270)%360;

		var incremento;
		var anguloParcial = anguloCamara - icourse;
		sumaAnguloParcial += anguloParcial;
		if(anguloParcial < 0) anguloParcial = -anguloParcial;
		if(anguloParcial > 180) anguloParcial = 360 - anguloParcial;

		if((anguloParcial >30)||(sumaAnguloParcial > 300)||(sumaAnguloParcial < -300))
		{
			sumaAnguloParcial = 0;
			if(anguloParcial >40) incremento = 10;
			else if(anguloParcial > 5) incremento = 5;
			else incremento = anguloParcial;

			anguloParcial = anguloCamara - icourse;
			if(anguloParcial >0)
			{
				if(anguloParcial > 180) anguloCamara +=incremento;
				else anguloCamara -= incremento;
			}
			else
			{
				if(anguloParcial < -180) anguloCamara -= incremento;
				else anguloCamara += incremento;
			}
			if(anguloCamara > 360) anguloCamara = anguloCamara%360;
			else if(anguloCamara < 0) anguloCamara = 360 +anguloCamara;
		}
	}
}


function VistaGeneral(jindex,jcourse,jdistance)
{
	var pointxy = pShipPlacemark[jindex].getGeometry();	
	lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	lookAt.set(pointxy.getLatitude(),pointxy.getLongitude(),0,ge.ALTITUDE_RELATIVE_TO_GROUND,jcourse,80,jdistance);
	ge.getView().setAbstractView(lookAt);
}

function ExternalSelectTargetBarco(targetMMSI)
{
	targetBarco = targetMMSI;
}
//===========================================================================================
function CreateSiluetaObj(objeto)
{
	var auxstr = objeto.mmsi.toString();
	if(auxstr.length == 9)
	{
		if(siluetasPlacemark[objeto.mmsi]==null)
		{
			siluetasPlacemark[objeto.mmsi] = ge.createGroundOverlay('');
			ge.getFeatures().appendChild(siluetasPlacemark[objeto.mmsi]);
		}
		siluetasPlacemark[objeto.mmsi].setIcon(ge.createIcon(''));
		siluetasPlacemark[objeto.mmsi].getIcon().setHref("http://www.localizatodo.com/mapa/assets/barco.png");
		siluetasPlacemark[objeto.mmsi].setLatLonBox(ge.createLatLonBox(''));
	}
}

function MoveSilueta(objetoms,xrotation)
{
	var east = objetoms.longitude + (objetoms.width/(2*111120))/Math.cos(objetoms.latitude*Math.PI/180);
	var west = objetoms.longitude - (objetoms.width/(2*111120))/Math.cos(objetoms.latitude*Math.PI/180);
	var north = objetoms.latitude + objetoms.length/(2*111120);
	var south = objetoms.latitude - objetoms.length/(2*111120);
	if(xrotation <180) xrotation = 0 - xrotation;
	else xrotation = 360 - xrotation;
	
	var latLonBox = siluetasPlacemark[objetoms.mmsi].getLatLonBox();
	latLonBox.setBox(north, south, east, west, xrotation);
}
//===========================================================================================
var sizeLabelGeneral = 0.7;
function CreatePeloto(url,index,oname)
{
	var pShipIconN = ge.createIcon('');
	pShipIconN.setHref(url);
	var pShipStyleN = ge.createStyle('');
	pShipStyleN.getIconStyle().setIcon(pShipIconN);
	pShipStyleN.getIconStyle().setScale(0.5);
	pShipStyleN.getLabelStyle().setScale(sizeLabelGeneral);
	
	var pShipIconH = ge.createIcon('');
	pShipIconH.setHref(url);
	var pShipStyleH = ge.createStyle('');
	pShipStyleH.getIconStyle().setIcon(pShipIconH);
	pShipStyleH.getIconStyle().setScale(0.7);
	pShipStyleH.getLabelStyle().setScale(1.0);
		
	var pShipStyleMap = ge.createStyleMap('');
	pShipStyleMap.setHighlightStyle(pShipStyleH);
	pShipStyleMap.setNormalStyle(pShipStyleN);
	
	pShipPlacemark[index] = ge.createPlacemark('');
	pShipPlacemark[index].setVisibility(false);
	pShipPlacemark[index].setStyleSelector(pShipStyleMap);
	pShipPlacemark[index].setName(oname); 
	ge.getFeatures().appendChild(pShipPlacemark[index]);

	var pointxy = ge.createPoint('');	
	pointxy.setAltitudeMode(ge.ALTITUDE_RELATIVE_TO_GROUND);
	pShipPlacemark[index].setGeometry(pointxy);	

	google.earth.addEventListener(pShipPlacemark[index], 'click', function(event) {
		event.preventDefault();
		var balloon = ge.createHtmlStringBalloon('');
		balloon.setFeature(event.getTarget());
		balloon.setMaxWidth(350);
		balloon.setContentString(descriptores[index]);
		ge.setBalloon(balloon);
		mmsiSelected = index;
	  });
	pShipPlacemark[index].setVisibility(true);
}

function MovePeloto(olat,olon,oalt,index)
{
	pShipPlacemark[index].setVisibility(false);
	var pointxy = pShipPlacemark[index].getGeometry();	
	pointxy.setLatitude(olat);
	pointxy.setLongitude(olon);
	pointxy.setAltitude(oalt);
	pShipPlacemark[index].setVisibility(true);
}

function SizeLabelPeloto(newsize)
{
	for(var ixy in pShipPlacemark)
	{
		if(pShipPlacemark[ixy] != null)
		{
			var pShipStyleMap = pShipPlacemark[ixy].getStyleSelector();
			var pShipStyleN = pShipStyleMap.getNormalStyle();
			pShipStyleN.getLabelStyle().setScale(newsize);
			pShipStyleMap.setNormalStyle(pShipStyleN);
			pShipPlacemark[ixy].setStyleSelector(pShipStyleMap);
		}
	}
	sizeLabelGeneral = newsize;
}
//===========================================================================================
//===========================================================================================
var modelos = new Array();
modelos[0] = {name:"pesquero",cargado:0,factorx:1.79,factory:0.52};
modelos[1] = {name:"Velero_madera",cargado:0,factorx:18.7,factory:3.8};
modelos[2] = {name:"Recreo",cargado:0,factorx:26,factory:6.3};
modelos[3] = {name:"Remolcador",cargado:0,factorx:34.5,factory:8.5};
modelos[4] = {name:"Golondrina",cargado:0,factorx:41.6,factory:8.6};
modelos[5] = {name:"pasajeros",cargado:0,factorx:380,factory:74};
modelos[6] = {name:"mercante",cargado:0,factorx:450,factory:93};
modelos[7] = {name:"tanque",cargado:0,factorx:133,factory:24};
modelos[8] = {name:"Velero_regata",cargado:0,factorx:15.5,factory:3.5};
modelos[9] = {name:"Helicoptero",cargado:0,factorx:19.7,factory:5};
//modelos[10] = {name:"SAR4",cargado:0,factorx:2.54*0.437,factory:2.54*0.17};
modelos[10] = {name:"SAR4",cargado:0,factorx:10*0.437,factory:10*0.17};

function CrearModeloBarco(cmbShip)
{		
	if((cmbShip.length >0) &&(cmbShip.length <500) &&(cmbShip.width >0) &&(cmbShip.width <100))
	{
		if( ((cmbShip.type == 30)||(cmbShip.type == 36)||(cmbShip.type == 37)||(cmbShip.type == 51)||(cmbShip.type == 52)||(cmbShip.type == 99)||((cmbShip.type >=60)&&(cmbShip.type <90))   ) )
		{
			if(modelos3DPlacemark[cmbShip.mmsi] == null)
			{
				if(siluetasPlacemark[cmbShip.mmsi] != null)
				{
					ge.getFeatures().removeChild(siluetasPlacemark[cmbShip.mmsi]);
					siluetasPlacemark[cmbShip.mmsi] = null;
				}
				NewCreate3dPlacemark(cmbShip);
			}
		}
		else if(siluetasPlacemark[cmbShip.mmsi] == null)CreateSiluetaObj(cmbShip);
	}
	else if(cmbShip.aisObject == 4)
	{
		cmbShip.type = 200;
		cmbShip.length = 20;
		cmbShip.width = 5;

		if(modelos3DPlacemark[cmbShip.mmsi] == null)
		{
			if(siluetasPlacemark[cmbShip.mmsi] != null)
			{
				ge.getFeatures().removeChild(siluetasPlacemark[cmbShip.mmsi]);
				siluetasPlacemark[cmbShip.mmsi] = null;
			}
			NewCreate3dPlacemark(cmbShip);
		}
	}
}

function NewCreate3dPlacemark(objeto)
{
	var tipoModelo = 0;
	if(objeto.type == 30) tipoModelo = 0;
	else if(objeto.type == 36)
	{ 
		tipoModelo = 8;
		if(opcionesYLook[10] == 2)
		{
			if(objeto.mmsi%2 != 0) tipoModelo = 1;
		}
	}
	else if(objeto.type == 37) tipoModelo = 2;
	else if(objeto.type == 51) tipoModelo = 10;
	else if(objeto.type == 52) tipoModelo = 3;
	else if((objeto.type >=60)&&(objeto.type <70))
	{
		if(objeto.length < 50)tipoModelo = 4;
		else tipoModelo = 5;
	}
	else if((objeto.type >=70)&&(objeto.type <80))tipoModelo = 6;
	else if((objeto.type >=80)&&(objeto.type <90))tipoModelo = 7;
	else if(objeto.type ==99)tipoModelo = 7;
	else if(objeto.type ==200)tipoModelo = 9;

	if(modelos[tipoModelo].cargado == 0)
	{
		modelos[tipoModelo].cargado = 1;
		listaBarcosEsperandoModeloNumero[tipoModelo] = new Array();
		barcosConModelo[objeto.mmsi] = 1; // cargando
		google.earth.fetchKml(ge, "http://www.localizatodo.com/mapa/ge/modelos/"+modelos[tipoModelo].name+".kmz", function (kmlObjectm) {kmlFinishedLoading22(kmlObjectm,objeto,modelos[tipoModelo],tipoModelo);}); 
	}
	if(modelos[tipoModelo].cargado == 1)
	{
		if( (barcosConModelo[objeto.mmsi] != 3)&&(barcosConModelo[objeto.mmsi] != 1)&&(barcosConModelo[objeto.mmsi] != 2) )
		{
			barcosConModelo[objeto.mmsi] = 2; // esperando
			listaBarcosEsperandoModeloNumero[tipoModelo].push(objeto);
		}
	}
	if(modelos[tipoModelo].cargado == 2)
	{
		if( (barcosConModelo[objeto.mmsi] != 3)&&(barcosConModelo[objeto.mmsi] != 1) )
		{
			barcosConModelo[objeto.mmsi] = 1; // cargando
			google.earth.fetchKml(ge, "http://www.localizatodo.com/mapa/ge/modelos/"+modelos[tipoModelo].name+".kmz", function (kmlObjectm) {kmlFinishedLoading22(kmlObjectm,objeto,modelos[tipoModelo],tipoModelo);}); 
		}
	}
}

function kmlFinishedLoading22(kmlObjectx,objeto,xmodelo,xtipo)
{
	barcosConModelo[objeto.mmsi] = 3; // cargado
	var kmlObjetoLista = kmlObjectx.getFeatures().getChildNodes();
	var modeloBarco = kmlObjetoLista.item(1).getGeometry();
	var escalax = escalaGeneral*objeto.length/xmodelo.factorx; 
	var escalay = escalaGeneral*objeto.width/xmodelo.factory;
	if(xtipo == 9)
	{
		escalax = escalaGeneral*1.25;
		escalay = escalaGeneral*1.25;
	}
	escala = ge.createScale('');
	escala.set(escalay,escalax,escalay)
	modeloBarco.setScale(escala)
	
		
	if(modelos3DPlacemark[objeto.mmsi] == null)
	{
		modelos3DPlacemark[objeto.mmsi] = ge.createPlacemark('');
		ge.getFeatures().appendChild(modelos3DPlacemark[objeto.mmsi]);
	}
	modelos3DPlacemark[objeto.mmsi].setVisibility(false);
	modelos3DPlacemark[objeto.mmsi].setName(objeto.name);


	var pRegion = ge.createRegion('');
	var pBox = ge.createLatLonAltBox('');
	var pLod = ge.createLod('');
	pBox.setAltBox(limitesBox[0], limitesBox[1],limitesBox[2],limitesBox[3], limitesBox[4], limitesBox[5], limitesBox[6], ge.ALTITUDE_CLAMP_TO_GROUND );
	pLod.set(4096,  -1, 0, 0); 
	pRegion.set(pBox,pLod);
	modelos3DPlacemark[objeto.mmsi].setRegion(pRegion);

	modelos3DPlacemark[objeto.mmsi].setGeometry(modeloBarco);
	modelos3DPlacemark[objeto.mmsi].setVisibility(true);
	Move3dPlacemark(objeto)
	if(xmodelo.cargado == 1)
	{
		xmodelo.cargado = 2;
		CargaRestoBarcos(xtipo);
	}
	screenOverlayFoto.getOverlayXY().setX(0);
}

function CargaRestoBarcos(ntipo)
{
	for(var ix=0;ix<listaBarcosEsperandoModeloNumero[ntipo].length;ix++)
	{
		CrearModeloBarco(listaBarcosEsperandoModeloNumero[ntipo][ix]);
	}
	listaBarcosEsperandoModeloNumero[ntipo] = new Array();
}

function RecargaTodosModelos()
{
	for(var ix=0;ix<rships.length;ix++)
	{
		CrearModeloBarco(rships[ix]);
	}
}

function Move3dPlacemark(objetom)
{
	modelos3DPlacemark[objetom.mmsi].setVisibility(false);
	var modelo = modelos3DPlacemark[objetom.mmsi].getGeometry();
	if((objetom.speed > 0.3) &&(objetom.speed < 102.3))var rotation = objetom.course;
	else{
		if(objetom.heading != 511) rotation = objetom.heading;
		else rotation = objetom.course;
	}
	var location = modelo.getLocation();
	location.setLatitude(objetom.latitude);
	location.setLongitude(objetom.longitude);
	if(objetom.type == 200) location.setAltitude(50);
	orientacion = modelo.getOrientation();
	orientacion.setHeading(rotation); 
	if((opcionesYLook[10] == 2)&&(objetom.type == 36))
	{
		var demoraViento = dirViento-rotation;
		if(demoraViento < -180) demoraViento += 360;
		else if(demoraViento >180 ) demoraViento -= 360;
		
		if((demoraViento >0)&&(demoraViento <85))orientacion.setRoll(+20);
		else if((demoraViento <=0)&&(demoraViento >-85))orientacion.setRoll(-20);
		else orientacion.setRoll(0)
	}
	if(controlVerModelos)modelos3DPlacemark[objetom.mmsi].setVisibility(true);
}

var gradosOla = 0;
var incOla = true;
var toutOla;
function Ola()
{
	if(incOla) {gradosOla += 0.5; if(gradosOla>4) incOla = false;}
	else {gradosOla -= 0.5; if(gradosOla<-4) incOla = true;}
	for(var ixy=0; ixy< rships.length;ixy++)
	{
		currentShip = rships[ixy];
		if(modelos3DPlacemark[currentShip.mmsi] != null)
		{
			var modelo = modelos3DPlacemark[currentShip.mmsi].getGeometry();
			var location = modelo.getLocation();
			var orientacion = modelo.getOrientation();
			orientacion.setTilt(gradosOla) 
		}
	}
	toutOla = setTimeout("EntraOla()",300);
}
function EntraOla()
{
	esperaOla = true;
	screenOverlayFoto.getOverlayXY().setX(0);
}

var escalaGeneral = 1;

function SizeModelo(newsize)
{
	for(var ixy in modelos3DPlacemark)
	{
		if(modelos3DPlacemark[ixy] != null)
		{
			modelos3DPlacemark[ixy].setVisibility(false);
			var modelo = modelos3DPlacemark[ixy].getGeometry();
			var escala = modelo.getScale();
			var escalax = newsize*escala.getX()/escalaGeneral;
			var escalay = newsize*escala.getY()/escalaGeneral;
			var escalaz = newsize*escala.getZ()/escalaGeneral;
			escala.set(escalax,escalay,escalaz)
			modelos3DPlacemark[ixy].setVisibility(true);
		}
	}
	escalaGeneral = newsize;
}

//===========================================================================================
function RefrescaRumbo(objeto)
{		
	if((objeto.speed > 0.2)&&(objeto.speed <102.2))
	{
		var lineString;
		var dest = destination(objeto.latitude, objeto.longitude, (((  (objeto.speed)/3600)*Math.PI) / 180), objeto.course);
		if(rumbosPlacemark[objeto.mmsi] != null) 
		{
			ge.getFeatures().removeChild(rumbosPlacemark[objeto.mmsi]);
			rumbosPlacemark[objeto.mmsi] = null;
		}
		if(rumbosPlacemark[objeto.mmsi] == null) 
		{
			var pRegion = ge.createRegion('');
			var pBox = ge.createLatLonAltBox('');
			var pLod = ge.createLod('');
			pBox.setAltBox(limitesBox[0], limitesBox[1],limitesBox[2],limitesBox[3], limitesBox[4], limitesBox[5], limitesBox[6], ge.ALTITUDE_CLAMP_TO_GROUND );
			pLod.set( 1024,  -1, 0, 0); 
			pRegion.set(pBox,pLod);

			lineString = ge.createLineString('');
			rumbosPlacemark[objeto.mmsi] = ge.createPlacemark('');
			rumbosPlacemark[objeto.mmsi].setStyleSelector(ge.createStyle(''));
			var lineStyle = rumbosPlacemark[objeto.mmsi].getStyleSelector().getLineStyle();
			lineStyle.setWidth(2);
			lineStyle.getColor().set('a0ffffff');  // 6600ffff aabbggrr format
			rumbosPlacemark[objeto.mmsi].setGeometry(lineString);
			rumbosPlacemark[objeto.mmsi].setRegion(pRegion);
			ge.getFeatures().appendChild(rumbosPlacemark[objeto.mmsi]);
		}
		else
		{
			lineString = rumbosPlacemark[objeto.mmsi].getGeometry();
		}
		lineString.getCoordinates().pushLatLngAlt(objeto.latitude, objeto.longitude, 0);
		lineString.getCoordinates().pushLatLngAlt(dest[0], dest[1], 0);
	}
	else if(rumbosPlacemark[objeto.mmsi] != null)
	{
		ge.getFeatures().removeChild(rumbosPlacemark[objeto.mmsi]);
		rumbosPlacemark[objeto.mmsi] = null;
	}

}
//===========================================================================================
function RefrescaTrack(objeto)
{		
	if((objeto.speed > 0.2)&&(objeto.speed <102.2))
	{
		var lineString;
		if(trackPlacemark[objeto.mmsi] == null) 
		{
			var pRegion = ge.createRegion('');
			var pBox = ge.createLatLonAltBox('');
			var pLod = ge.createLod('');
			pBox.setAltBox(limitesBox[0], limitesBox[1],limitesBox[2],limitesBox[3], limitesBox[4], limitesBox[5], limitesBox[6], ge.ALTITUDE_CLAMP_TO_GROUND );
			pLod.set( 1024,  -1, 0, 0); 
			pRegion.set(pBox,pLod);

			lineString = ge.createLineString('');
			trackPlacemark[objeto.mmsi] = ge.createPlacemark('');
			trackPlacemark[objeto.mmsi].setStyleSelector(ge.createStyle(''));
			var lineStyle = trackPlacemark[objeto.mmsi].getStyleSelector().getLineStyle();
			lineStyle.setWidth(2);
			lineStyle.setColorMode(ge.COLOR_RANDOM);  // 6600ffff aabbggrr format
			trackPlacemark[objeto.mmsi].setGeometry(lineString);
			trackPlacemark[objeto.mmsi].setRegion(pRegion);
			ge.getFeatures().appendChild(trackPlacemark[objeto.mmsi]);
		}
		else
		{
			lineString = trackPlacemark[objeto.mmsi].getGeometry();
		}
		lineString.getCoordinates().pushLatLngAlt(objeto.latitude, objeto.longitude, 0);
	}
}

//===========================================================================================
//===========================================================================================
//===========================================================================================
//===========================================================================================
//===========================================================================================
function EventPlacemark() 
{  
	alert("hola");
/*  	var strSplit = new Array();
  	strSplit = eventmouse.getDescription.split("MMSI: </b>");
  	alert(strSplit[0]+"    "+strSplit[1])
    eventmouse.preventDefault();
*/
//    alert(text);
}
//===========================================================================================
//===========================================================================================

function urlIcon(tipobarco)
{
	var strurl;
	if(tipobarco == 51)strurl='http://www.localizatodo.com/mapa/assets/eugenio/bam.png'; //60x60 SAR (amarillo Pasaje&nbsp;)
	else if( (tipobarco == 36)||(tipobarco == 37))strurl='http://www.localizatodo.com/mapa/assets/eugenio/bgr.png'; //60x60 verde velero o recreo(SAR&nbsp;)
	else if((tipobarco >99)&&(tipobarco<132)) strurl='http://www.localizatodo.com/mapa/assets/eugenio/faro.png'; //60x60  faro Ayudas a la navegaci&oacute;n
	else
	{
		var subtipo = parseInt(tipobarco/10)
		if(subtipo == 8) strurl='http://www.localizatodo.com/mapa/assets/eugenio/br.png'; //60x60  rojo tanque ( Sin especificaci&oacute;n&nbsp;)
		else if(subtipo == 6) strurl='http://www.localizatodo.com/mapa/assets/eugenio/bac.png'; //60x60 azul claro Pasajeros (Carga&nbsp;)
		else if(subtipo == 7) strurl='http://www.localizatodo.com/mapa/assets/eugenio/ba.png'; //60x60  azul oscuro Mercante (Tanque&nbsp;)
		else strurl='http://www.localizatodo.com/mapa/assets/eugenio/bg.png'; //60x60  gris Otros&nbsp;
	}
	return strurl
}

function removeShips(){
	for(var ixy in pShipPlacemark)
	{
		if(pShipPlacemark[ixy] != null) ge.getFeatures().removeChild(pShipPlacemark[ixy]); 
		if(modelos3DPlacemark[ixy] != null) ge.getFeatures().removeChild(modelos3DPlacemark[ixy]); 
		if(rumbosPlacemark[ixy] != null) ge.getFeatures().removeChild(rumbosPlacemark[ixy]); 
		if(trackPlacemark[ixy] != null) ge.getFeatures().removeChild(trackPlacemark[ixy]); 
		if(siluetasPlacemark[ixy] != null) ge.getFeatures().removeChild(siluetasPlacemark[ixy]); 
	}
	for(var ixy in balizasPlacemark)
	{
		if(balizasPlacemark[ixy] != null) ge.getFeatures().removeChild(balizasPlacemark[ixy]); 
	}
	for(var ixy in marcasPlacemark)
	{
		if(marcasPlacemark[ixy] != null) ge.getFeatures().removeChild(marcasPlacemark[ixy]); 
	}
	for(var ixy in lineasPlacemark)
	{
		if(lineasPlacemark[ixy] != null) ge.getFeatures().removeChild(lineasPlacemark[ixy]); 
	}
	pShipPlacemark = new Array();
	modelos3DPlacemark = new Array();
	rumbosPlacemark = new Array();
	trackPlacemark = new Array();
	tiemposPlacemark = new Array();
	siluetasPlacemark = new Array();
	balizasPlacemark = new Array();
	marcasPlacemark = new Array();
	lineasPlacemark = new Array();
	barcosConModelo = new Array();
	listaBarcosEsperandoModeloNumero = new Array();
	clearTimeout(toutPlacemark);
	clearTimeout(toutOla);
	terminado = true;
}

function ToutPlacemark(){
	for(var ixy in tiemposPlacemark)
	{
		if(tiemposPlacemark[ixy] > 0)
		{
			tiemposPlacemark[ixy]--;
			if(tiemposPlacemark[ixy] == 0)
			{
				barcosConModelo[ixy] = 0;
				if(pShipPlacemark[ixy] != null)
				{
					ge.getFeatures().removeChild(pShipPlacemark[ixy]); 
					pShipPlacemark[ixy] = null;
				}
				if(modelos3DPlacemark[ixy] != null)
				{
					ge.getFeatures().removeChild(modelos3DPlacemark[ixy]);
					modelos3DPlacemark[ixy] = null
				}
				if(rumbosPlacemark[ixy] != null)
				{
					ge.getFeatures().removeChild(rumbosPlacemark[ixy]);
					rumbosPlacemark[ixy] = null;
				}
				if(trackPlacemark[ixy] != null)
				{
					ge.getFeatures().removeChild(trackPlacemark[ixy]);
					trackPlacemark[ixy] = null;
				}
				if(siluetasPlacemark[ixy] != null)
				{
					ge.getFeatures().removeChild(siluetasPlacemark[ixy]);
					siluetasPlacemark[ixy] = null;
				}
			}
		}
	}
	toutPlacemark = setTimeout("ToutPlacemark()",60000);
}

