/////////////////visits and tours/////////////////////
var a_tour=[];  // list of entities to view
var a_tour_view=[]; //list of entity positions and counts associated with the tour
var a_tour_time;   //time tour takes place.
var all_tours=[]; 
var all_views=[]
var all_tour_POIs=[];
var all_times=[];
var in_tour=false;
var total_count;    //=1820;
var start_long;
var end_long;
var start_lat;
var end_lat;
var tour_heading;
var tour_pitch;
var tour_range;
var this_tour=[]; 
var tour_POIs=[];
var tour_length;
var last_name;
var last_description;
var first_name;
var first_description;
var final_name;
var final_description;
var this_name;
var this_description;
var name_holder;
var this_position;
function close_doc_window(){
    document.getElementById("doc_view").style.visibility = "visible";
}
/////////////////////////////////// Tour and Selection Edits////////////////////////////////
var the_options=[];
the_options.push('<select class="button2" id="WhereTo" onchange="gothere()"><option  value="-2"><b><font color="yellow">Buildings and Tours</font></b></option><option value="4001"><b>The Old Cathedral History</b></option><option  value="6"><b>The Old Courthouse-1919</b></option><option  value="342"><b>The Arch-1965</b></option><option  value="4002"> Other Notable Buildings</b></option><option value="3000"><b>Tour Wharf Street </b></option> <option value="3001"><b>Tour 3rd Street </b></option><option  value="-1"><b><font color="yellow">Overview</font</b></option></select>');
the_options.push('<select class="button2" id="WhereTo" onchange="gothere()"><option  value="-2"><b><font color="yellow">Old Cathedral - Dates</font></b></option> <option  value="340"><b>1770</b></option><option  value="338"><b>1776</b></option><option  value="5"><b>1834-1939</b></option> <option  value="341"><b>1961</b></option> <option  value="4000"><b><font color="yellow">Back</font</b></option></select>');
the_options.push('<select class="button2" id="WhereTo" onchange="gothere()"><option  value="-2"><b><font color="yellow">The Buildings</font></b></option><option  value="3"> Old Rock House-1932</b></option><option value="298"><b>Federal Fur & Wool-1900</b></option> <option  value="332"><b>The Barnum Hotel</b></option><option  value="329"><b>The National Hotel-1835</b></option><option  value="328"><b>The National Hotel-1860</b></option><option  value="201"><b>The Gay Building</b></option> <option  value="191"><b>The Times Building-1870</b></option><option  value="39"><b>The Times Building-1907</b></option> <option  value="4000"><b><font color="yellow">Back</font</b></option></select>');

function which_options(x){
    document.getElementById("this_try").innerHTML =the_options[x]; 
}
function back(){which_options(0);}
function gothere() {
    var e = document.getElementById("WhereTo");
    var the_bldg = e.options[e.selectedIndex].value;
    if (the_bldg==-2) {return}
    document.getElementById("the_looper").style.visibility = "visible";
    document.getElementById("tour_state").style.visibility = "hidden";
    if (the_bldg>3999) {
        var sub_options=the_bldg-4000;
        which_options(sub_options);
        /*    if (the_bldg==4001) {
                which_options(1);        
            } else if (the_bldg==4000) {
                which_options(0);
            } else if (the_bldg==4002) {
                which_options(2);
            }*/
        return;
    }
    if (anima_tour) {
        //  reset_tour(the_tour_number);
        in_tour=false;
        clearInterval(the_stepper);
        the_stepper=null;
        anima_tour=false;
    }
    if (anima_loop) {
        anima_loop=false;
        clearInterval(the_loop);
        in_the_loop=false;
    }
    document.getElementById("tour_state").innerHTML = "  The Control  " ;
    present_count=0;
    clearInterval(the_stepper);
    //       var e = document.getElementById("WhereTo");
    //       var the_bldg = e.options[e.selectedIndex].value;
    var this_pitch = gCesium.Math.toRadians(-10.0); //Default
    // var the_target;
    if (the_bldg==-1) {
        // heading=7.0;
        // lheading=heading;
        //  range=171.0;
        document.getElementById("tour_state").style.visibility = "hidden";
        document.getElementById("myRange").value=startrange;
        document.getElementById("range_value").innerHTML = startrange;
        center=Cesium.Cartesian3.fromDegrees( clong,clat );
        gviewer.selectedEntity=null;
        gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1900-01-01T00:00:00Z");
        gviewer.camera.lookAt(startcenter, new gCesium.HeadingPitchRange(startheading, startpitch, startrange));
        parent.SiteClick("<html><bocy><center><h1>The Saint Louis Riverfront</h1></center></body></html>");
        first_click=true;
        in_fullC=true;
        div_open=true;
        selected_holder=null;
        document.getElementById("the_data").innerHTML="The Saint Louis Riverfront";
        // parent.full_screen1();
    } else {
        if (the_bldg<3000) {
            document.getElementById("tour_state").style.visibility = "hidden";
            div_open=true;
            in_fullC=true;
            this_name=all_buildings[the_bldg].name; 
            this_description=all_buildings[the_bldg].description;
            this_position=Cesium.Cartesian3.fromDegrees(bldg_lon_lats[the_bldg][0],bldg_lon_lats[the_bldg][1],0);
            gviewer.selectedEntity=all_buildings[the_bldg];
            document.getElementById("the_data").style.display="none";
            document.getElementById("the_data").innerHTML =all_buildings[the_bldg].name; //description ;
            parent.SiteClick(this_description);
            if (first_click) {
                first_click=false;
            } else {
                selected_holder.description=description_holder;
            }                                       
            selected_holder=all_buildings[the_bldg];                  
            description_holder=this_description;
        }
        if (!in_fullC) {
            console.log("not full C");
            gviewer.selectedEntity=selected_holder;
        }
        if (the_bldg==338) {
            heading=Cesium.Math.toRadians(45.0);
            lheading=heading;
            range=50.0;
            pitch = Cesium.Math.toRadians(-10.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.187285,38.624034,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1778-01-01T00:00:00Z");

        }
        if (the_bldg==340) {
            heading=Cesium.Math.toRadians(45.0);
            lheading=heading;
            range=40.0;
            pitch = Cesium.Math.toRadians(-10.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.187285,38.624034,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1771-01-01T00:00:00Z");

        }
        if (the_bldg==3) {
            heading=4.905037599462777;
            lheading=heading;
            range=50.0;
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.18414,38.624723,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1932-01-01T00:00:00Z")
        } else if (the_bldg==5) {
            heading=Cesium.Math.toRadians(-10.0);
            lheading=heading;
            range=150.0;
            pitch = Cesium.Math.toRadians(-10.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.187285,38.624034,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1860-01-01T00:00:00Z");
        } else if (the_bldg==2) {
            heading=4.905037599462777;
            lheading=heading;
            range=50.0;
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.18414,38.624723,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1819-01-01T00:00:00Z");
        } else if (the_bldg==341) {
            heading=Cesium.Math.toRadians(-10.0);
            lheading=heading;
            range=150.0;
            pitch = Cesium.Math.toRadians(-10.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.187285,38.624034,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1961-01-01T00:00:00Z");
        } else if (the_bldg==6) {
            heading=1.7457796260818064;
            lheading=heading;
            range=300.0;
            pitch = Cesium.Math.toRadians(-15.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1893056966,38.625056698,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1919-01-01T00:00:00Z");
        } else if (the_bldg==298) {
            heading=1.7957796260818064;
            lheading=heading;
            range=18;   <!--50.0; -->
            this_pitch = Cesium.Math.toRadians(-5.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1849248503835,38.6247988064905,7.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1900-01-01T00:00:00Z");
        } else if (the_bldg==332) {
            heading=Cesium.Math.toRadians(-10.0);;
            lheading=heading;
            range=140.0; 
            this_pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1866329363722,38.6240353638296,7.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1860-01-01T00:00:00Z");
        } else if (the_bldg==328) {
            heading=Cesium.Math.toRadians(170.0);;
            lheading=heading;
            range=70.0;
            pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1877974897737,38.6248000881992,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1860-01-01T00:00:00Z");
        } else if (the_bldg==329) {
            heading=Cesium.Math.toRadians(170.0);;
            lheading=heading;
            range=70.0;
            pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1877974897737,38.6248000881992,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1835-01-01T00:00:00Z");
        } else if (the_bldg==201) {
            heading=Cesium.Math.toRadians(170.0);;
            lheading=heading;
            range=155.0;
            pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1859822154191,38.6259900824625,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1870-01-01T00:00:00Z");
        } else if (the_bldg==39) {
            heading=Cesium.Math.toRadians(90.0);;
            lheading=heading;
            range=170.0;
            pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1894674444491,38.6264585015218,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1906-01-01T00:00:00Z");
        } else if (the_bldg==191) {
            heading=Cesium.Math.toRadians(90.0); 
            lheading=heading;
            range=170.0;
            pitch = Cesium.Math.toRadians(-18.0);
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.1894674444491,38.6264585015218,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1890-01-01T00:00:00Z");
        } else if (the_bldg==342) {
            heading= Cesium.Math.toRadians(290.0);  //1.7957796260818064;
            lheading=heading;
            range=500.0;
            pitch = Cesium.Math.toRadians(-35.0);
            this_pitch=pitch;
            document.getElementById("myRange").value=range;
            document.getElementById("range_value").innerHTML = range;
            center=Cesium.Cartesian3.fromDegrees(-90.18519780521553,38.624640301242944,0.0);
            gviewer.clock.currentTime =Cesium.JulianDate.fromIso8601("1967-01-01T00:00:00Z");
        }
        if (the_bldg<3000) {
            // console.log(center);
            // console.log(this_position);
            gviewer.camera.lookAt(center, new gCesium.HeadingPitchRange(lheading, pitch, range));//the center of the view, maybe not the building position.
            document.getElementById("doc_view").style.visibility = "hidden";
            open_div();
            return;
        }
        if (the_bldg>=3000&&the_bldg<4000) {
            anima_tour=true;
            document.getElementById("tour_state").style.visibility = "visible";
            var tour_val=the_bldg-3000;
            present_count=0;
            the_tour_number=tour_val;
            document.getElementById("the_looper").style.visibility = "hidden";
            document.getElementById("WhereTo").style.visibility = "hidden";
            document.getElementById("doc_view").style.visibility = "hidden";
            gviewer.selectedEntity=all_buildings[this_tour[tour_val]];
            open_div();
            take_tour(tour_val);
        }
    }
}
a_tour=[3,42,77,278];
all_tours.push(a_tour);
all_tour_POIs.push([10,150,325,530]);
a_tour_view=[-90.18414,-90.1844593729466,38.624723,38.6241738013305,4.905037599462777,Cesium.Math.toRadians(-10.0),50.0,550];
all_views.push(a_tour_view);
all_times.push(Cesium.JulianDate.fromIso8601("1925-01-01T00:00:00Z"));
a_tour=[];
a_tour=[68,292,272,250,213,301,211,208];
all_tours.push(a_tour);
all_tour_POIs.push([4,110,190,290,380,550,600,670]); 
a_tour_view=[-90.1869645105869,-90.1875486468281,38.6263692671663,38.6249205138303,121,Cesium.Math.toRadians(-10.0),50.0,700];
all_views.push(a_tour_view);
all_times.push(Cesium.JulianDate.fromIso8601("1920-01-01T00:00:00Z"));
////////////////////////////////////End Edits/////////////////////////////

function set_tour_parms(x){
    this_tour=all_tours[x];
    gviewer.clock.currentTime =all_times[x];
    start_long=all_views[x][0];
    end_long=all_views[x][1];
    start_lat=all_views[x][2];
    end_lat=all_views[x][3];
    tour_heading=all_views[x][4];
    tour_pitch =all_views[x][5];
    tour_range =all_views[x][6];
    total_count=all_views[x][7];
    tour_POIs=all_tour_POIs[x];
    tour_length=tour_POIs.length;
}

function take_tour(tour_val){
    the_stepper=null;
    in_tour=true;
    anima_tour=true;
    set_tour_parms(tour_val);
    parent.SiteClick(all_buildings[this_tour[0]].description);
    document.getElementById("tour_state").innerHTML = "Pause Tour" ;
    //  console.log(document.getElementById("tour_state").style.visability);
    the_stepper=setInterval(stepper, 100);
}
function highlight(x){
    gviewer.selectedEntity=all_buildings[this_tour[x]];
    parent.SiteClick(all_buildings[this_tour[x]].description);
}

function stepper(){ 
    var this_ratio=(present_count-100)/2000;
    var increment_long=end_long-start_long;
    var increment_lat=end_lat-start_lat;
    var this_long;
    var this_lat;
    var tour_center;
    var ipoi;
    if (present_count<50) {
        this_long=start_long;
        this_lat=start_lat;
        if (present_count==tour_POIs[0]) {
            console.log("position 0");
            highlight(0);
        }
    } else if (present_count<total_count) {
        this_ratio=(present_count-50)/(total_count-50);
        this_long=start_long+(this_ratio*increment_long);
        this_lat=start_lat+(this_ratio*increment_lat);
        for (ipoi=1;ipoi<tour_length;ipoi++) {
            if (present_count==tour_POIs[ipoi]) {
                highlight(ipoi);
                break;
            }
        }
    }
    tour_center = Cesium.Cartesian3.fromDegrees(this_long,this_lat,0.0);
    gviewer.camera.lookAt(tour_center, new Cesium.HeadingPitchRange(tour_heading, tour_pitch, tour_range));
    present_count++;
    //  console.log(present_count);
    if (present_count==total_count) {
        clearInterval(the_stepper);
        the_stepper=null;
        present_count=0;
        gviewer.selectedEntity=null;
        document.getElementById("tour_state").innerHTML = "The Control" ;
        document.getElementById("the_looper").style.visibility = "visible";
        document.getElementById("WhereTo").style.visibility = "visible";
        //      reset_tour(the_tour_number);
        in_tour=false;
    }
}
/*function reset_tour(x){
    var this_tour=all_tours[x];
    var steps=this_tour.length;
    for (i=0;i<steps;i++ ) {
        all_buildings[this_tour[i]].description=name_desc[this_tour[i]][1];
        all_buildings[this_tour[i]].name=       name_desc[this_tour[i]][0];
    }
}  */
function pause_tour(){
    clearInterval(the_stepper);
    the_stepper=null;
}
function close_the_div(){
    document.getElementById("doc_view").style.visibility = "visible";
}
</script>

<script>
document.getElementById("myRange").min=20;
document.getElementById("myRange").max=3*crange;
document.getElementById("myRange").value=crange;
function mySpareFunction() {
    document.getElementById("tester").value=2+1;
}
</script>