﻿//    Design & Developement   : Sandeep Parandekar
//    Date                    : 2009-18-04  
//    Updated By              : Uday Hattarke 
//    Date                    : 2009-28-04  
//    Comments                : Integrated with template & modified format.

var animRunning = false;
google.load("earth", "1");
google.load("maps", "2");       // to find location of map
var ge = null;

function init()
{
    //google.earth.createInstance("map3d", initCallback, failureCallback);
    
    var content = document.getElementById('contentEarth');
    
    var inputHTML = '<input id="location" type="text" value="San Francisco, CA"/>';
    inputHTML += '<input type="button" onclick="buttonClick()" value="Fly Here!"/>';
    content.innerHTML = inputHTML;
    
    //For Earth View
    google.earth.createInstance('contentEarth', initCallback, failureCallback);
}

function initCallback(instance) 
{
    ge = instance;
    ge.getWindow().setVisibility(true);
    var cam = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
    cam.setAltitude(15000000);
    ge.getView().setAbstractView(cam);
    // startAnimation();
    // add a navigation control
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
    // uncomment to add some layers Yellow color border
    // ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
    placemark = ge.createPlacemark('');
    var icon = ge.createIcon('');
    //icon.setHref('http://maps.google.com/mapfiles/kml/paddle/ylw-blank.png');

    //For Changing the ICON Runtime.......................
    var mypath = '';
    mypath = mypath + 'http://maps.google.com/mapfiles/kml/paddle/';
    mypath = mypath + document.getElementById("ICONNAME").value;
    icon.setHref(mypath);
    var style = ge.createStyle('');
    style.getIconStyle().setIcon(icon);
    placemark.setStyleSelector(style);
    var point = ge.createPoint('');
    point.setLatitude(37);
    point.setLongitude(-122);
    placemark.setGeometry(point);
    ge.getFeatures().appendChild(placemark);
    // placemark.setName('Hello, World');
    // var balloon = ge.createHtmlStringBalloon('');
    // balloon.setMaxWidth(300);
    // Google logo.
    // balloon.setContentString( '<a href="#" onclick="alert(\'Running some JavaScript!\');">Alert!</a>');
    // ge.setBalloon(balloon);
    google.earth.addEventListener(placemark, 'mouseover', 
            function(event) 
            {
                // prevent the default balloon from popping up
                event.preventDefault();
                var balloon = ge.createHtmlStringBalloon('');
                balloon.setFeature(event.getTarget()
            );
            balloon.setMaxWidth(3000);
            // Google logo.
            balloon.setContentString('<img src=" http: //www.orientconsultancy.com/images/Orient_logoBlue.gif" ><br><a href="http://www.indiatimes.com">Breaking News!</a>');
            //balloon.setContentString('<img src="http://www.google.com/googlegulp/images/logo.gif"><br><a href="http://www.indiatimes.com">Breaking News!</a>');
            //balloon.setContentString('<a href="http://www.indiatimes.com" onclick="Breaking News(\'Running some JavaScript!\');">Alert!</a>');
            ge.setBalloon(balloon);
            }
);

function eventHandler(event) 
{
    var text = 'Click:';
    // Prevent default balloon from popping up for marker placemarks
    event.preventDefault();
    // wrap alerts in API callbacks and event handlers
    // in a setTimeout to prevent deadlock in some browsers
    setTimeout(function() 
               {
                if (animRunning) 
                {
                    stopAnimation();
                }
                else if (!animRunning) 
                {
                    startAnimation();
                }
               }, 0);
    }
    // listen to the click event on the globe
    google.earth.addEventListener(ge.getGlobe(), 'click', eventHandler);
    var cam = ge.getView().copyAsCamera(ge.ALTITUDE_RELATIVE_TO_GROUND);
    cam.setAltitude(10000000);
    ge.getView().setAbstractView(cam);
    //Thread.sleep(500);
    startAnimation();
}

function startAnimation() 
{
    if (!animRunning) 
    {
        ge.getOptions().setFlyToSpeed(ge.SPEED_TELEPORT);
        animRunning = true;
        google.earth.addEventListener(ge, 'frameend', tickAnimation);
        // start it off
        tickAnimation();
    }
}

function stopAnimation() 
{
    if (animRunning) 
    {
        google.earth.removeEventListener(ge, 'frameend', tickAnimation);
        animRunning = false;
    }
}

function tickAnimation()
{
    // an example of some camera manipulation that's possible w/ the Earth API
    /* Rotating Earth................*/
    var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
    lookAt.setLatitude(lookAt.getLatitude());
    lookAt.setLongitude(lookAt.getLongitude() - 0.30);
    ge.getView().setAbstractView(lookAt);
}

function failureCallback(instance) 
{    
}

function buttonClick() 
{
    var geocodeLocation = document.getElementById('location').value;
    var geocoder = new google.maps.ClientGeocoder();
    geocoder.getLatLng(geocodeLocation, function(point) 
    {
        if (point) 
        {
            var lookAt = ge.createLookAt('');
            lookAt.set(point.y, point.x, 10, ge.ALTITUDE_RELATIVE_TO_GROUND, 0, 60, 20000);
            ge.getView().setAbstractView(lookAt);
//            alert(point.y);
//            alert(point.x);
        }
    });
    stopAnimation();
}

