google.load("earth", "1");
google.load("maps", "2.99");  // For JS geocoder

var ge = null;
var geocoder;


function init() {
  geocoder = new GClientGeocoder();
  google.earth.createInstance("map3d", initCB, failureCB);
}

function initCB(object) {
  ge = object;
  ge.getWindow().setVisibility(true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BUILDINGS, true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, true);
  ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
  ge.getNavigationControl().setVisibility(ge.VISIBILITY_SHOW);
  ge.getOptions().setStatusBarVisibility(true);
  ge.getOptions().setUnitsFeetMiles(false);
  
	var la = ge.createLookAt('');
	la.set(39.67274, -6.01562, 9000000, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 900);
	ge.getView().setAbstractView(la);
  }

 //-----------------trackLoad networkLink-------------------------- 
 function trackLoad(track)
{
      var loca = track;
      //alert(loca);
      var link = ge.createLink('');
      //link.setHref('http://www.planeta-aventuras.com/main/kmlFiles/01_ponte de lima-vigo.kml');
      link.setHref(loca);

      var networkLink = ge.createNetworkLink('');
      networkLink.setLink(link);
      //networkLink.setFlyToView(true);
  
      ge.getFeatures().appendChild(networkLink);
      //alert(networkLink);
    } 
 
//-------------------------------------------
//--------placemark-----------------------
function placemarkSet(name, lat, lng, pict, link, size)
{
var placemark = ge.createPlacemark('');
placemark.setName(name);
ge.getFeatures().appendChild(placemark);

// Create style map for placemark
var normal = ge.createIcon('');
if(link=="paraiso2008.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/roun_red.gif');
}
if(link=="peru2008.htm")
{
	normal.setHref('http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png');
}
if(link=="libia2007.htm")
{
	normal.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
}
if(link=="lencois.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/roun_blue.gif');
}
if(link=="maroc2006.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/roun_red.gif');
}

if(link=="tica.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}

if(link=="tres_atlas.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}
if(link=="brasil2005.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_green.gif');
}
if(link=="brasil2005.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_green.gif');
}
if(link=="nordeste2006.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.gif');
}

if(link=="abrolhos.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/rect_green.gif');
}

if(link=="bolivia2009.htm")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/bolivia_pin.gif');
}

if(link=="pt")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/pt.gif');
}

if(link=="ecuador")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/ecuador_pin.gif');
}

if(link=="noronha")
{
	normal.setHref('http://www.planeta-aventuras.com/main/icons/noronha.gif');
}


var iconNormal = ge.createStyle('');
iconNormal.getIconStyle().setIcon(normal);
var highlight = ge.createIcon('');
if(link=="paraiso2008.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/roun_red.gif');
}
if(link=="peru2008.htm")
{
	highlight.setHref('http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png');
}
if(link=="libia2007.htm")
{
	highlight.setHref('http://maps.google.com/mapfiles/kml/paddle/red-circle.png');
}
if(link=="lencois.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/roun_blue.gif');
}
if(link=="maroc2006.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/roun_red.gif');
}
if(link=="nordeste2006.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/rect_green.gif');
}

if(link=="tica.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}

if(link=="tres_atlas.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}
if(link=="brasil2005.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}

if(link=="abrolhos.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/rect_red.png');
}

if(link=="bolivia2009.htm")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/bolivia_neg.gif');
}

if(link=="pt")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/pt_neg.gif');
}

if(link=="ecuador")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/ecuador_neg.gif');
}

if(link=="noronha")
{
	highlight.setHref('http://www.planeta-aventuras.com/main/icons/noronha_neg.gif');
}

var iconHighlight = ge.createStyle('');
iconHighlight.getIconStyle().setIcon(highlight);
var styleMap = ge.createStyleMap('');
styleMap.setNormalStyle(iconNormal);
styleMap.setHighlightStyle(iconHighlight);
placemark.setStyleSelector(styleMap);


//alert("lat           " + lat);
// Create point
var la = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
var point = ge.createPoint('');
point.setLatitude(lat);
point.setLongitude(lng);
//point.setAltitude(alt);
placemark.setGeometry(point);




//----------------------------------

//---------DIVballon with link---------

if (!window.placemark) 
{
  
  window.geEventListener = function(event) 
  {
    var text = 'Click:';

    function addToMessage(append1, append2) 
    { 
     text += ' ' + append1 + ': ' + append2 + '<br>\n' ;
    }

    addToMessage('target type', event.getTarget().getType());
    addToMessage('currentTarget type', 
                 event.getCurrentTarget().getType());
    addToMessage('button', event.getButton());
    addToMessage('clientX', event.getClientX());
    addToMessage('clientY', event.getClientY());
    addToMessage('screenX', event.getScreenX());
    addToMessage('screenY', event.getScreenY());
    addToMessage('latitude', event.getLatitude());
    addToMessage('longitude', event.getLongitude());
    addToMessage('altitude', event.getAltitude());
    addToMessage('didHitGlobe', event.getDidHitGlobe());
    addToMessage('altKey', event.getAltKey());
    addToMessage('ctrlKey', event.getCtrlKey());
    addToMessage('shiftKey', event.getShiftKey());
    addToMessage('timeStamp', event.getTimeStamp());
    
    //alert("pict            " + pict + "         link            " + link);
   var feature = null;
	feature = window.placemark;
	var b = ge.createHtmlDivBalloon('');
	b.setMaxWidth(1000);
	b.setMaxHeight(700);
	b.setFeature(feature);
	var div = document.createElement('DIV');
    if (size == "vertical")
    {
	    div.innerHTML =  
        '<img src=http://www.planeta-aventuras.com/main/' + pict + ' Width="267", height="400"><br>' 
        + '<a href="javascript:closeBallon()"><DIV class="H0center1">Fechar</DIV><DIV class="H0center0">Fechar</DIV></a>';;
    }
    else
    {
        	    div.innerHTML =  
        '<img src=http://www.planeta-aventuras.com/main/' + pict + ' Width="600", height="400"><br>' 
        + '<a href="javascript:closeBallon()"><DIV class="H0center1">Fechar</DIV><DIV class="H0center0">Fechar</DIV></a>';;
     }
	b.setContentDiv(div);
	ge.setBalloon(b);


   // Prevent default balloon from popping up for marker placemarks
   event.preventDefault(); 

   log(text);
  }

  google.earth.addEventListener(placemark, "click", 
                                window.geEventListener);
  }                                
}


function closeBallon()
{
ge.setBalloon(null);
}







 //-------display picture- not im use here---------------- 
function displayPicture(pict, lat, lng)
{
var groundOverlay = ge.createGroundOverlay('');
groundOverlay.setIcon(ge.createIcon(''))
groundOverlay.getIcon().
  setHref("http://www.planeta-aventuras.com/main/" + pict + ".jpg");
groundOverlay.setLatLonBox(ge.createLatLonBox(''));
var center = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
center.setLatitude(lat);
center.setLongitude(lng);
var north = center.getLatitude() + .0006;
var south = center.getLatitude() - .0006;
var east = center.getLongitude() + .001;
var west = center.getLongitude() - .001;
var rotation = 0;
var latLonBox = groundOverlay.getLatLonBox();
latLonBox.setBox(north, south, east, west, rotation);

ge.getFeatures().appendChild(groundOverlay);

var la = ge.createLookAt('');
la.set(lat, lng, 900, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, 0);
ge.getView().setAbstractView(la);
 
}




//---------------------------------------------------------------

function failureCB(object) {
  alert('load failed');
}
//------------------------------------------------------------------------



//---------Localizar Cidades com formula--------------------------------
function submitLocation() {
  var address = document.getElementById('address').value;
  geocoder.getLatLng(
    address, 
    function(point) {
      if (point && ge != null) {
        var la = ge.createLookAt('');     
        la.set(point.y, point.x, 100, ge.ALTITUDE_RELATIVE_TO_GROUND, 
               0, 0, 4000);
        ge.getView().setAbstractView(la);
      }
    }
  );
}

//------------localize my places----------------------------------------
function localizaMarca(lat, lng, alt) {
       // alert( lat + long);
       var la = ge.createLookAt('');             
       la.set(lat, lng, 2, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 0, alt);
       ge.getView().setAbstractView(la);  
      }	 
   






