﻿//put the data into this after we load asycn
var elevationData;
var bolLoadedElevations = false;
var pin ;   //holds the pin

//used for the timer stuff
var playPosition = 0;
var timerID = null;
var timerRunning = false;
var delay = 250;    //1000 is 1 second

if (URLparams != "") {

    //build the URL - URLparams has been built in the calling page
    var request = new JsonRequest("elevationchart", "/webservice/elevationchart.ashx?" + URLparams);
    //add the callback
    request.doRequest("OnGetElevationRequestComplete");

}

function OnGetElevationRequestComplete(result) 
{

//stick this somewhere where we can call it
elevationData= result;

//attach an event to the image now we have loaded the data
$addHandler(imgProfile, "mousemove", elevationGraphOnMouseOver);
$addHandler($get("pointdata"), "mousemove", elevationGraphOnMouseOver);

bolLoadedElevations = true;
}

//pass the event to the parent
function elevationGraphOnMouseOutPointData(e)
{
//debugger;

}

//hide the things
function elevationGraphOnMouseOut(e)
{

$get("pointdata").style.display = 'none';
$get("marker").style.display = 'none';

}

//catch an event and call the function which does the work
function elevationGraphOnMouseOver(e) {

$get("pointdata").style.display = '';

//stop the timer
PausePlay();

var elementBounds = Sys.UI.DomElement.getBounds(imgProfile);

var xPosition = e.clientX - elementBounds.x;

    
//now find the nearest data by looping until we hit one >= to what we want
for(i = 0; i < elevationData.length; i++)
    {

    //now find if we are there yet
    if (elevationData[i].x >= xPosition)
        {

        //so play resumes from where we are mousing
        playPosition = i;

        PlotCaption(elevationData[i]);
        
        if (HasMap == true)
            { 
        
            //***********************************************
            //browser specific version
            //***********************************************

            //now draw on the map
            var myPoint = new VELatLong(elevationData[i].lat, elevationData[i].lon);
            
            //check if the point is in view
            if (IsInView(myPoint) == false)
                map.PanToLatLong(myPoint);

            if (pin != undefined)
            {
            //get rid of the pin if there is one
            pin.HideIcon();
            map.DeleteShape(pin);
            }

            //draw on the map
            //see http://forums.microsoft.com/MSDN/showpost.aspx?postid=1756057&SiteID=1
            pin = new VEShape(VEShapeType.Pushpin, myPoint);

            pin.SetTitle('Current point');
            pin.SetCustomIcon('/icons/orangeblob.png');
            map.AddShape(pin);
            }
        
         break;
                
        }
        
    }


}



//is the supplied point visible?
function IsInView(point)
{

var bolInView = true;

//TODO make this work for the 3d view
var MapCoords = map.GetMapView();


//by lat - seems to be the wrong way around!
if (point.Latitude < MapCoords.BottomRightLatLong.Latitude || point.Latitude > MapCoords.TopLeftLatLong.Latitude)
    bolInView = false;

//by long
if (point.Longitude < MapCoords.TopLeftLatLong.Longitude || point.Longitude > MapCoords.BottomRightLatLong.Longitude)
    bolInView = false;

return bolInView;

}

function OnTimeout(result)
{

    if (gTestMode == true)
        alert("Error:\n\n" + result.get_message() + "\n" + result.get_stackTrace());
    else
        alert("Sorry, but something has gone wrong.\nIf you continue to experience problems please get in touch.");
}




//********************************************************
// timer stuff - VE version - only change is the point stuff
//********************************************************

//click on the play button
function onclickPlayButton()
{

//TODO toggle mode
if (timerRunning == true)
    PausePlay();
else
    StartPlay();
    
}

function StartPlay()
{

//stop if we haven't got a map
if (HasMap == false)
    return;

//do the marker
PlotPoint(playPosition);

//start the timer
timerID = self.setTimeout("TimerTick()", delay)

//store if we are running the timer
timerRunning = true;

//swap the image to pause
$get("imgplaycontrol").src= "/icons/control_pause.png";

}

function PausePlay()
{
//stop if we haven't got a map
if (HasMap == false)
    return;

//stop the timer
TimerStop();

//swap the image to play
$get("imgplaycontrol").src= "/icons/control_play.png";

}

function RewindPlay()
{
//reset the marker
playPosition = 0;

//stop the timer
TimerStop();

//hide the marker
HideMarker();

//swap the image to play
$get("imgplaycontrol").src= "/icons/control_play.png";
}

function TimerTick()
{

//store if we are running the timer - should be done already
timerRunning = true;

//redo the timer
timerID = self.setTimeout("TimerTick()", delay)

//advance the marker
playPosition++;

//update the display
PlotPoint(playPosition);

//set the timer again
}

function TimerStop()
{
//stop the timer etc
clearTimeout(timerID);
timerRunning = false;
        
}

function HideMarker()
{

//hide the info and marker
var PointDescription = $get("pointdata");
var marker = $get("marker");
var pointer = $get("pointer");
 
PointDescription.style.display = 'none';
marker.style.display = 'none';

}


//plots our point
function PlotPoint(counter)
{

if (bolLoadedElevations == false)
    return;

//check if we have reached the end - and if so reset the counter
if (counter >= elevationData.length)
    {
    RewindPlay();
    return;
    }
    
var elementBounds = Sys.UI.DomElement.getBounds(imgProfile);

//var xPosition = e.clientX - elementBounds.x;
var xPosition =  elementBounds.x;
    
//now find the nearest data by looping until we hit one >= to what we want
    PlotCaption(elevationData[counter]);

        //now draw on the map
    var myPoint = new VELatLong(elevationData[counter].lat, elevationData[counter].lon);
        
        //check if the point is in view
        if (IsInView(myPoint) == false)
            map.PanToLatLong(myPoint)

        //draw on the map
        if (pin == undefined) {
            pin = new VEShape(VEShapeType.Pushpin, myPoint);
            pin.SetTitle('Current point');
            pin.SetCustomIcon('/icons/orangeblob.png');
            map.AddShape(pin);
        }
        else {
            pin.SetPoints(myPoint);
        }
  
    }



    //plots a point
    function PlotCaption(itemToPlot) {


        var PointDescriptionInner = $get("pointdatainner");
        var PointDescription = $get("pointdata");
        var marker = $get("marker");
        var pointer = $get("pointer");

        PointDescription.style.display = '';
        marker.style.display = '';

        //we have found it so extract the data
        PointDescriptionInner.innerHTML = "";

        if (itemToPlot.GroundElev != null) {
            PointDescriptionInner.innerHTML += "<div class='labelvalue'><div class='label'>Altitude</div><div class='value'>" + itemToPlot.Elev + "</div></div>";
            PointDescriptionInner.innerHTML += "<div class='labelvalue'><div class='label'>Ground Elev</div><div class='value'>" + itemToPlot.GroundElev + "</div></div>";
        }
        else
            PointDescriptionInner.innerHTML += "<div class='labelvalue'><div class='label'>Altitude</div><div class='value'>" + itemToPlot.Elev + "</div></div>";

        PointDescriptionInner.innerHTML += "<div class='labelvalue'><div class='label'>Distance</div><div class='value'>" + itemToPlot.Dist + "</div></div>";
        if (itemToPlot.Time != null) {
            PointDescriptionInner.innerHTML += "<div class='labelvalue'><div class='label'>Time</div><div class='value'>" + itemToPlot.Time + " GMT</div></div>";
        }

        marker.style.top = itemToPlot.y + "px";
        marker.style.left = itemToPlot.x + "px";

        PointDescription.style.left = (itemToPlot.x + 10) + "px";
        PointDescription.style.top = (itemToPlot.y - 15) + "px";

    }



