var show_course=false;
var teams=new Array();
function toggle_course_overlay(show){
    show_course=show;
    new Ajax.Request("calls.php?action=toggle_course_overlay&show_course="+show,{onFailure:function(t){alert('Error ' + t.status + ' -- ' + t.statusText);}});

    var last_call = $('last_call').value;
    if(last_call==''){
        fetch_latest_gpoints(show);
    }else{
        eval(last_call);
    }

}

function team_selection_show(){
    try{
        Effect.SlideUp('team_name_wrapper',{duration:1});
        $('team_selection_content').innerHTML='Loading Teams...One Moment';
        new Ajax.Updater('team_selection_content','calls.php',{parameters:'action=fetch_team_selection',asynchronous:false});
        Effect.SlideDown('team_selection',{duration:1});
    }
    catch(e){

    }
}

function team_selection_check(obj){
    try{
        teams=new Array();
        len=$("team_selection_content").getElementsByTagName("input").length;
        count=0;
        for(i=0;i<len;i++){
            if($("ck_"+i).checked==true){
                count++;
                if(count>3){
                    alert("You can only select up to 3 teams");
                    obj.checked=false;
                    break;
                }
                else{
                    teams[count]=$("ck_"+i).name;
                }
            }
        }
    }
    catch(e){}
}

function team_selection_save(){
    try{
        var str='';
        for(i=1;i<teams.length;i++){
            str+="&team[]="+teams[i];
        }
        $('gps_data').innerHTML='One Moment - Loading Data';
        new Ajax.Updater('','calls.php',{parameters:'action=save_team_selection'+str,asynchronous:false});
        fetch_gps_data();
        fetch_gps_gpoints();
        team_selection_close();
    }
    catch(e){}
}

function team_selection_close(){
    try{
        Effect.SlideUp('team_selection',{duration:1});
    }
    catch(e){
    }
}
function team_detail_show(){
    //$('team_detail').style.display="";
    $('team_name_wrapper').style.display='';
    try{Effect.SlideDown('team_detail',{duration:1});}catch(e){}
}
function team_detail_close(){
    //$('team_detail').style.display='none';
    try{Effect.SlideUp('team_detail',{duration:1});}catch(e){}
}

function fetch_gps_data(){
    try{
        new Ajax.Updater('gps_data','calls.php',{parameters:'action=fetch_gps_data'});
    }
    catch(e){

    }
}

function fetch_latest_results(showCourse){
    try{
        new Ajax.Updater('gps_data','calls.php',{parameters:'action=fetch_latest_results'});
        fetch_latest_gpoints(showCourse);
    }
    catch(e){

    }
}

function fetch_team_data(bib,all,showCourse){
    try{
        $('team_name_wrapper').style.display='';
        new Ajax.Updater('gps_data','calls.php',{parameters:'action=fetch_team_data&id='+bib+"&all="+all});
        new Ajax.Updater('team_name','calls.php',{parameters:'action=fetch_team_details&id='+bib});
        fetch_team_gpoints(bib,all,showCourse);
    }
    catch(e){

    }
}


function fetch_waypoint_data(){
    try{
        $('last_call').value='fetch_waypoint_data('+(!show_course)+')';
        new Ajax.Updater('gps_data','calls.php',{parameters:'action=fetch_waypoint_data'});
        fetch_waypoints(true);
    }
    catch(e){

    }
}

function fetch_latest_gpoints(showCourse){
    try{
        map.clearOverlays();
        
        if(showCourse==true){
            fetch_waypoints(false);
        }
        
        $('last_call').value='fetch_latest_gpoints('+(!showCourse)+')';
        GDownloadUrl('calls.php?action=fetch_latest_gpoints', function(data, responseCode) {
            var	points=[];
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");
            if(markers.length==0){
                return false;
            }
            for (var i = 0; i < markers.length; i++) {
                var lat=parseFloat(markers[i].getAttribute("lat"));
                var lng=parseFloat(markers[i].getAttribute("lng"));
                //alert(lat+" "+lng);

                var point = new GLatLng(lat,lng);

                var icon = new GIcon();
                icon.image="img/marker_"+markers[i].getAttribute("bib")+".png";
                icon.iconAnchor=new GPoint(14,25);
                icon.infoWindowAnchor=new GPoint(14,14);
                icon.iconSize = new GSize(20,34)

                points.push(point);
                txt="Team "+markers[i].getAttribute("bib")+"<br />"+markers[i].getAttribute("team")+"<br />Date/Time: "+markers[i].getAttribute("date")+"<br />Latitude: "+lat+"<br />Longitude: "+lng+"<br >Ping ID: "+markers[i].getAttribute("positionid");
                map.addOverlay(createMarker(point, txt,icon));
            }
            map.panTo(point);
        }
        );
    }
    catch(e){

    }
}

function fetch_team_gpoints(bib,all,showCourse){
    try{
        map.clearOverlays();
        $('last_call').value='fetch_team_gpoints('+bib+','+all+','+(!showCourse)+')';
        GDownloadUrl('calls.php?action=fetch_team_gpoints&id='+bib+'&all='+all, function(data, responseCode) {
            var	points=[];
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");
            //if(markers.length==0){
            //   return false;
            //}
            if(showCourse==true){
                fetch_waypoints(false);
            }
            if(markers.length==0){
                return false;
            }
            for (var i = 0; i < markers.length; i++) {
                var lat=parseFloat(markers[i].getAttribute("lat"));
                var lng=parseFloat(markers[i].getAttribute("lng"));
                var point = new GLatLng(lat,lng);
                points.push(point);
                //map.addOverlay(new GMarker(point))
                var icon = new GIcon();
                icon.image="img/marker_"+markers[i].getAttribute("bib")+".png";
                icon.iconAnchor=new GPoint(14,25);
                icon.infoWindowAnchor=new GPoint(14,14);
                icon.iconSize = new GSize(20,34)
                txt="Team "+markers[i].getAttribute("bib")+"<br />"+markers[i].getAttribute("team")+"<br />Date/Time: "+markers[i].getAttribute("date")+"<br />Latitude: "+lat+"<br />Longitude: "+lng+"<br />Ping ID: "+markers[i].getAttribute("positionid");
                map.addOverlay(createMarker(point, txt,icon));
                // map.addOverlay(createMarker(point, txt));
                map.addOverlay(new GPolyline(points,"#FF0000"));
                map.panTo(point);
            }

        }
        );
    }
    catch(e){

    }
}

function fetch_gps_gpoints(showCourse){
    try{
        map.clearOverlays();
        plcolors=new Array();
        plcolors[1]="#0000ff";
        plcolors[2]="#ffff00";
        plcolors[3]="#ff0000";
        if(showCourse==true){
            fetch_waypoints(false);
        }
        
        for(t=1;t<teams.length;t++){
            teamid=teams[t];
            GDownloadUrl('calls.php?action=fetch_team_gpoints&id='+teamid+'&all=true', function(data, responseCode) {
                var	points=[];
                var xml = GXml.parse(data);
                var markers = xml.documentElement.getElementsByTagName("marker");
                if(markers.length==0){
                    return false;
                }
                for (var i = 0; i < markers.length; i++) {
                    var lat=parseFloat(markers[i].getAttribute("lat"));
                    var lng=parseFloat(markers[i].getAttribute("lng"));
                    var point = new GLatLng(lat,lng);
                    points.push(point);
                    //map.addOverlay(new GMarker(point))
                    var icon = new GIcon();
                    icon.image="img/marker_"+markers[i].getAttribute("bib")+".png";
                    icon.iconAnchor=new GPoint(14,25);
                    icon.infoWindowAnchor=new GPoint(14,14);
                    icon.iconSize = new GSize(20,34)
                    txt="Team "+markers[i].getAttribute("bib")+"<br />"+markers[i].getAttribute("team")+"<br />Date/Time: "+markers[i].getAttribute("date")+"<br />Latitude: "+lat+"<br />Longitude: "+lng+"<br >Ping ID: "+markers[i].getAttribute("positionid");
                    map.addOverlay(createMarker(point, txt,icon));
                    //map.addOverlay(createMarker(point, txt));
                }
                map.addOverlay(new GPolyline(points),"#FF00000");
                map.panTo(point);
            }
            );
            $('last_call').value='fetch_gps_gpoints('+(!showCourse)+')';
        }
    }
    catch(e){
    }
}

function fetch_waypoints(clearOverlays){
    try{
        if(clearOverlays!=false){
            map.clearOverlays();
        }
        GDownloadUrl('calls.php?action=fetch_waypoints', function(data, responseCode) {
            var	points=[];
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");
            if(markers.length==0){
                return false;
            }
            for (var i = 0; i < markers.length; i++) {
                var lat=parseFloat(markers[i].getAttribute("lat"));
                var lng=parseFloat(markers[i].getAttribute("lng"));
                //alert(lat+" "+lng);
                var point = new GLatLng(lat,lng);
                points.push(point);
                var icon = new GIcon();
                icon.image="img/waypoint.png";
                icon.iconAnchor=new GPoint(14,25);
                icon.infoWindowAnchor=new GPoint(14,14);
                icon.iconSize = new GSize(20,34)
                txt="Waypoint "+(i+1)+"<br />Location: "+markers[i].getAttribute("tag")+"<br />Latitude: "+markers[i].getAttribute("lat")+"<br />Longitude: "+markers[i].getAttribute("lng");
                map.addOverlay(createMarker(point, txt,icon));
            }
            map.addOverlay(new GPolyline(points));

            if(clearOverlays!=false){
                map.panTo(point);
            }
        }
        );
    }
    catch(e){
    }
}



function createMarker(point,text,icon) {
    try{
        if(icon!=''){
            var marker = new GMarker(point,icon);
        }else{
            var marker = new GMarker(point);
        }

        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(text);
        });
        return marker;
    }
    catch(e){}
}

function update_text(id,t){
    try{
        $(id).innerHTML=t;
    }
    catch(e){

    }
}
