﻿
function init() {
    
    // get initial detail panel
    xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentData(0, draw_panel, OnError, OnError);

    var supported = google.earth.isSupported();
    
    // if browser supports GE plugin
    if (supported) {
        var installed = google.earth.isInstalled();
        google.earth.createInstance('map3d', initCallback, failureCallback);
       
        if (installed) {
            // init update timer for live tracking
            Timer = setTimeout('UpdatePositions()', 15000);
        }
       
    }
    else {
        alert("Google Earth Plugin Not Supported in this Browser");
    }

}




function UpdatePositions() {
    // called when the timer ticks, to update all currently viewed athletes

    // get number of all tracks currently there
    var le = ge.getFeatures().getChildNodes().getLength();
 
    // loop throug the track container each track
    for (x = 0; x < le; x++) {
            Tname = ge.getFeatures().getChildNodes().item(x).getName();

            // get the values of the dropdownlist
            var dropdownIndex = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1").selectedIndex;
            var dropdownValue = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1")[dropdownIndex].value;
            // For Tracks
            // Only the track which is selected from dropdown list will  be updated
            // all other tracks are not updated  
            if (Tname.substring(Tname.length - 6, Tname.length) == ' Track') {

                TrainingID = Tname.substring(0, Tname.length - 6);
                // If track, only do something if this track is selected to follow
                // Fly to athlete who is selected if "follow on map" is checked
                if (TrainingID == dropdownValue && document.getElementById("ctl00_ContentPlaceHolder1_CheckBox3").checked) {
                    // draw and fly to
                    xy = Athlosoft_Live_Portal.GPSTrackService.GetTrackByID(TrainingID, jsReplaceAndFlyPosition, OnError, OnError);
                }
                if (TrainingID == dropdownValue && document.getElementById("ctl00_ContentPlaceHolder1_CheckBox3").checked == false) {
                    // draw and fly to
                    xy = Athlosoft_Live_Portal.GPSTrackService.GetTrackByID(TrainingID, jsReplacePosition, OnError, OnError);
                }
            }
            
            
            // For Positions
            if (Tname.substring(Tname.length - 9, Tname.length) == ' Position') {
                TrainingID = Tname.substring(0, Tname.length - 9);
                // Fly to athlete who is selected if "follow on map" is checked
                if (TrainingID == dropdownValue && document.getElementById("ctl00_ContentPlaceHolder1_CheckBox3").checked) {
                    // draw and fly to
                    xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentPositionByID(TrainingID, jsReplaceAndFlyPosition, OnError, OnError);
                }
                else {
                    // just draw it
                    xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentPositionByID(TrainingID, jsReplacePosition, OnError, OnError);
                }

            }
              
    }
    update_panel();
    Timer = setTimeout('UpdatePositions()', 15000);
}

function jsReplacePosition(arg) {
    // if ge object available
    if (ge) {
        // parse the track
        track = ge.parseKml(arg);
        // if track is not null
        if (track) {

            // find the matching child
            for (x = 0; x < ge.getFeatures().getChildNodes().getLength(); x++) {

                if (track.getName() == ge.getFeatures().getChildNodes().item(x).getName()) {

                    ge.getFeatures().replaceChild(track, ge.getFeatures().getChildNodes().item(x));
                    break;
                }

            }
        }
    }
    
}


function jsReplaceAndFlyPosition(arg) {
    // if ge object available
    if (ge) {
        // parse the track
        track = ge.parseKml(arg);
        // if track is not null
        if (track) {

            // find the matching child
            for (x = 0; x < ge.getFeatures().getChildNodes().getLength(); x++) {

                if (track.getName() == ge.getFeatures().getChildNodes().item(x).getName()) {

                    ge.getFeatures().replaceChild(track, ge.getFeatures().getChildNodes().item(x));
                    break;
                }

            }
            
            // Fly to the LookAt view
            if (track.getAbstractView()) {
                ge.getView().setAbstractView(track.getAbstractView());
            }
        }
    }

}

function update_panel() {

    // get current info and draw the dteail panel
    // called from picklist onchange event and update timer tick
    var dropdownIndex = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1").selectedIndex;
    var dropdownValue = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1")[dropdownIndex].value;
    xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentData(dropdownValue, draw_panel, OnError, OnError);
    

}


function DetailDropDown1_Changed() {
    var dropdownIndex = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1").selectedIndex;
    var dropdownValue = document.getElementById("ctl00_ContentPlaceHolder1_DetailDropDown1")[dropdownIndex].value;
    // check the checkbox
    document.getElementById("ctl00_ContentPlaceHolder1_CheckBox3").checked = true;
    
    // loop throug the track container each track and find if this track is already there
    var oldchild = null;
    var le = ge.getFeatures().getChildNodes().getLength();
    for (x = 0; x < le; x++) {

        Tname = ge.getFeatures().getChildNodes().item(x).getName();
       
        if (Tname.substring(Tname.length - 9, Tname.length) == ' Position') {
            TrainingID = Tname.substring(0, Tname.length - 9);
            // remove the one found
            if (TrainingID == dropdownValue) {
                oldchild = ge.getFeatures().getChildNodes().item(x);
                break;
            }
        }
    }
     // update the panel
    update_panel();

    // draw and fly to the view
    if (oldchild) {
        // replace
        xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentPositionByID(dropdownValue, jsReplaceAndFlyPosition, OnError, OnError);
    }
    else {
        // append
        xy = Athlosoft_Live_Portal.GPSTrackService.GetCurrentPositionByID(dropdownValue, jsDrawAndFlyPosition, OnError, OnError);
    }
    var rows = document.getElementById("ctl00_ContentPlaceHolder1_GridView2").getElementsByTagName("tr");
    var tl = rows.length;
   
   
   // loop throuh page grid view
   for (y = 1; y <= tl - 1; y++) {
    // Get Row Elements
    var elTableCells = rows[y].getElementsByTagName("td");
    // get the elment containing the TraininID
    //TrainingID = elTableCells[2].innerHTML;

    if (showtracks == null) {
        TrainingID = elTableCells[2].innerHTML;
    }
    else {
        if (showtracks.toLowerCase() == "false")
            TrainingID = elTableCells[1].innerHTML;
        else
            TrainingID = elTableCells[2].innerHTML;
    }
    
    
    
    
    
    // if Name is the same TrackID, set the checkbox
    if (dropdownValue == TrainingID) {
        elTableCells[0].children[0].checked = true;
        break;
        }

    }
}


function draw_panel(arg) {
    // drew the detail pannel returned by update_panel() at the placeholder position
    document.getElementById("PanelPlaceholder").innerHTML = arg;

}

