﻿//Properties
var globalURL = "http://test.renault.co.uk/up/";
var paisInd = 0;
var globosVisibles = false;
var camera;
var polygonPlacemark;

var kmlUrl = new Array(5);
kmlUrl[0] = "kmls/california.kml";
kmlUrl[1] = "kmls/mexico.kml";
kmlUrl[2] = "kmls/peru.kml";
kmlUrl[3] = "kmls/brasil.kml";
kmlUrl[4] = "kmls/venezuela.kml";

var globosLon = new Array(5);
globosLon[0] = "-119.6905517578125";
globosLon[1] = "-105.99609375";
globosLon[2] = "-70.90576171875";
globosLon[3] = "-62.60009765625";
globosLon[4] = "-72.158203125";

var globosLat = new Array(5);
globosLat[0] = "37.80544394934271";
globosLat[1] = "27.15692045688088";
globosLat[2] = "-12.790374787613588";
globosLat[3] = "-5.55038056899795";
globosLat[4] = "8.124491290861204";

var altVisible = new Array(5);
altVisible[0] = "500000";
altVisible[1] = "500000";
altVisible[2] = "500000";
altVisible[3] = "500000";
altVisible[4] = "500000";

var textoFin = new Array(5);
textoFin[0] = "<table border=0 width=200><tr><td align=left><font color=#666666 face=arial size=1>Well done, Sherlock!<br>Now we know that Carl went through California.<br><a href='#' onclick='finMaps()'><font color=#009edf face=verdana size=1><u>Continue looking for him</u></font></a></font></td></tr></table>";
textoFin[1] = "<table border=0 width=200><tr><td align=left><font color=#666666 face=arial size=1>You're on the right track!<br>Looks like you've got the right idea with the balloons!<br><a href='#' onclick='finMaps()'><font color=#009edf face=verdana size=1><u>Go back to the car</u></font></a></font></td></tr></table>";
textoFin[2] = "<table border=0 width=200><tr><td align=left><font color=#666666 face=arial size=1>You're getting closer and closer!<br>If you carry on like that then you'll be at your destination sooner than you thought.<br><a href='#' onclick='finMaps()'><font color=#009edf face=verdana size=1><u>Look for a new clue</u></font></a></font></td></tr></table>";
textoFin[3] = "<table border=0 width=200><tr><td align=left><font color=#666666 face=arial size=1>You don't miss a trick!<br>Guess one more clue and the prize is yours.<br><a href='#' onclick='finMaps()'><font color=#009edf face=verdana size=1><u>This is the last clue...go for it!</u></font></a></font></td></tr></table>";
textoFin[4] = "<table border=0 width=200><tr><td align=left><font color=#ff0000 face=arial size=2><b>Congratulations!</b></font><br><font color=#666666 face=arial face=arial size=1>You've proved yourself to be an excellent explorer. <a href='#' onclick='finMaps()'><font color=#009edf face=verdana size=1><u><b>OK!</u></font></b></a></font></td></tr></table>";

var ge;
google.load("earth", "1");
var posicion = -1;
  
function init() {
	google.earth.createInstance('map3d', initCB, failureCB);
}

function initCB(instance) {
	ge = instance;
	ge.getWindow().setVisibility(true);

	// add a navigation control
	ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);

	// add some layers
	ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
	  var la = ge.createLookAt('');
	  la.set(37, -52,
		25000000, // altitude
		ge.ALTITUDE_RELATIVE_TO_GROUND,
		0, // heading
		0, // straight-down tilt
		5000 // range (inverse of zoom)
		);
	ge.getView().setAbstractView(la);
	google.earth.addEventListener(ge.getView(), 'viewchange', function() {
		camera = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
		eyeAlt = camera.getAltitude();
		if(eyeAlt > altVisible[paisInd]) {
			if(groundOverlay){
				groundOverlay.setVisibility(false);
				globosVisibles = false;
			}
		}
		else{
			if(groundOverlay) {
				groundOverlay.setVisibility(true);
				globosVisibles = true;
			}
		}
	});

	// fetch the KML
	cambiaMundo(paisInd);
}

function createGroundOverlay(cualInd) {
 	groundOverlay = ge.createGroundOverlay('');
 	groundOverlay.setIcon(ge.createIcon(''))
  	groundOverlay.getIcon().setHref(globalURL+'kmls/globos_buenos.png');
 	groundOverlay.setLatLonBox(ge.createLatLonBox(''));

	var center = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
 	var north = eval(globosLat[cualInd]) + .80;
 	var south = eval(globosLat[cualInd]) - .80;
	var east = eval(globosLon[cualInd]) + .80;
	var west = eval(globosLon[cualInd]) - .80;

	var rotation = 0;
	var latLonBox = groundOverlay.getLatLonBox();
	latLonBox.setBox(north, south, east, west, rotation);

	ge.getFeatures().appendChild(groundOverlay);
	createPlacemark(cualInd);
}

//Failure Alerts User
function failureCB(errorCode) {
	//alert("Google Maps Couble not load - " + errorCode);
}


function createPlacemark(cualInd) {
	polygonPlacemark = ge.createPlacemark('');
	var polygon = ge.createPolygon('');
	polygonPlacemark.setGeometry(polygon);
	var outer = ge.createLinearRing('');
	polygon.setOuterBoundary(outer);
	google.earth.addEventListener(polygonPlacemark, 'click', function(event) {
		if(event.getButton() == 0){
			muestraMensaje(cualInd);
		}
	});
	// Square outer boundary.
	var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
	var coords = outer.getCoordinates();
	var lat = lookAt.getLatitude();
	var lon = lookAt.getLongitude();
	coords.pushLatLngAlt(eval(globosLat[cualInd])- .55, eval(globosLon[cualInd])- .55, 0);
	coords.pushLatLngAlt(eval(globosLat[cualInd])- .55, eval(globosLon[cualInd])+ .55, 0);
	coords.pushLatLngAlt(eval(globosLat[cualInd])+ .55, eval(globosLon[cualInd])+ .55, 0);
	coords.pushLatLngAlt(eval(globosLat[cualInd])+ .55, eval(globosLon[cualInd])- .55, 0);

	ge.getFeatures().appendChild(polygonPlacemark);

	if (!polygonPlacemark.getStyleSelector()) {
		polygonPlacemark.setStyleSelector(ge.createStyle(''));
	}

	// The Style of a Feature is retrieved as
	// feature.getStyleSelector().  The Style itself contains a
	// LineStyle and a PolyStyle, which are what we manipulate to change
	// the line color, line width, and inner color of the polygon.
	var lineStyle = polygonPlacemark.getStyleSelector().getLineStyle();
	lineStyle.setWidth(lineStyle.getWidth() + 2);
	// Color is specified in 'aabbggrr' format.
	lineStyle.getColor().set('00ff0000');

	// Color can also be specified by individual color components.
	var polyColor = polygonPlacemark.getStyleSelector().getPolyStyle().getColor();
	polyColor.setA(1);
	polyColor.setB(0);
	polyColor.setG(255);
	polyColor.setR(255);
}

//Message displays
function muestraMensaje(cualInd){
	var balloon = ge.createHtmlStringBalloon('');
	balloon.setFeature(polygonPlacemark); // optional
	balloon.setMaxWidth(300);
	  // Google logo.
	balloon.setContentString(textoFin[cualInd]);
	ge.setBalloon(balloon);
}

var currentKmlObject = null;

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 {
		// wrap alerts in API callbacks and event handlers
		// in a setTimeout to prevent deadlock in some browsers
		setTimeout(function() {
			alert('Bad or null KML.');
		}, 0);
	}
}

//Changing world
function cambiaMundo(cual) {
	if(cual){
		paisInd = cual;
	}
	else{
		paisInd = 0;
	}

	var c = ge.getFeatures().getFirstChild();
	while (c) {
		var s = c.getNextSibling();
		ge.getFeatures().removeChild(c);
		c = s;
	}

	// remove the old KML object if it exists
	if (currentKmlObject) {
		ge.getFeatures().removeChild(currentKmlObject);
		currentKmlObject = null;
	}

	var url = globalURL+kmlUrl[paisInd];

	google.earth.fetchKml(ge, url, finishFetchKml);
	createGroundOverlay(paisInd)
}
