var timeout;

function doTimer() {
    if (tocDone && transcriptDone && notesDone) {
        movie = document.getElementById("vidPlayer");
        var movieUrl = transcriptIndex[curIndex].value;

        if (movie != null)
        {
            //movie.initMovie("");
            if (movie.getCurrentUrl()!=movieUrl){
                movie.initMovie(movieUrl);

                //addCuePoints("transcript");
                //addCuePoints("note");
                addCuePoints("anchor");
                movie.playMovie();
            } else {
                seekToAndPlay(0);
            }


        //            addCuePoints("anchor");
        //           movie.playMovie();
        }
    } else {
        timeout = setTimeout("doTimer();", 1000);//check again in 1 second
    }
}


/**
 * Fire off initMoviePlayer when the page finishes loading.
 */
window.onload = function() {
    ajaxRequestToc(pid, segId, contentModel, coid); // populate the anchors array
    ajaxRequestTranscript(pid, segId, contentModel); // populate the transcripts array
    ajaxRequestNotes(pid, segId, contentModel, coid); // populate the notes array

    searchInit();

    doTimer();
}

function addCuePoints(type) {
    var cuepoints = new Array();

    if (type == "anchor") {
        for (var i=0; i<anchors.length; i++) {
            cuepoints.push(parseFloat(anchors[i].time));
        }
    }

    if (type == "transcript") {
        for (var i=0; i<transcripts.length; i++) {
            cuepoints.push(parseFloat(transcripts[i].time));
        }
    }

    if (type == "note") {
        for (var i=0; i<notes.length; i++) {
            cuepoints.push(parseFloat(notes[i].time));
        }
    }
    movie.addCuePoints(cuepoints, type);
}

/**
 * Video controls for ExternalInterfaces
 */
function play() {
    document.getElementById("vidPlayer").playMovie();
}

function pause() {
    document.getElementById("vidPlayer").pause();
}

function seekToAndPlay(time) {
    var movie = document.getElementById("vidPlayer");
    movie.seekToAndPlay(time);
    onCuePoint(time);
}


function onCuePoint(cuePointTime, cuePointName){
    cuePointCalls++;
    //alert(cuePointTime+" "+cuePointName);
    onCuePointReal(cuePointTime,cuePointName);
}

function onCuePointReal(cuePointTime, cuePointName){
    for (var i=0; i<anchors.length; i++) {
        if (parseFloat(anchors[i].time) == cuePointTime) {
            if (anchors[i].tag == "index") {
                highlightAnchorPlaying(anchors[i].xmlid);
                searchCalcDelta(anchors[i].xmlid)
            }
        }
    }

    var transcriptCleared = false;
    for (var i=0; i<transcripts.length; i++) {
        if (parseFloat(transcripts[i].time) == cuePointTime && transcripts[i].type == "TRANSCRIPT") {
            fixTranscript(transcripts[i].text);
            if (transcriptCleared) { // if already cleared, just add more
                addTranscript("transcriptArea", transcripts[i].text, transcripts[i].speaker, "transcriptItem");
            } else {
                replaceTranscript("transcriptArea", transcripts[i].text, transcripts[i].speaker, "transcriptItem");
                transcriptCleared = true;
            }
        }
    }

    searchSelectFirstPhrase();

    var noteCleared = false;
    for (var i=0; i<notes.length; i++) {
        if (parseFloat(notes[i].time) == cuePointTime && notes[i].type == "NOTE") {
            if (noteCleared) { // if already cleared, just add more
                addText("noteArea", notes[i].text, "noteItem");
            } else {
                replaceText("noteArea", notes[i].text, "noteItem");
                transcriptCleared = true;
            }
        }
    }
}

function switchToSegment(seg) {
    play();
    cuePointCalls = 1;
    curIndex = parseInt(seg);

    segId = transcriptIndex[curIndex].key;

    tocDone = false;
    transcriptDone = false;
    notesDone = false;

    clearAnchors();
    ajaxRequestToc(pid, segId, contentModel, coid); // populate the anchors array
    ajaxRequestTranscript(pid, segId, contentModel); // populate the transcripts array
    ajaxRequestNotes(pid, segId, contentModel, coid); // populate the notes array

    replaceText("transcriptArea", "", "transcriptText");
    replaceText("noteArea", "", "noteText");

    doTimer();
}

function onMovieFinish() {
    //    if (segmentLoaded) {
//    if (movie.verifyEndofMovie() == 1) {
        replaceText("transcriptArea", "", "transcriptText");
        replaceText("noteArea", "", "noteText");

        tocDone = false;
        transcriptDone = false;
        notesDone = false;

        if (curIndex < transcriptIndex.length) {
            curIndex = curIndex + 1;
            segId = transcriptIndex[curIndex].key;
            clearAnchors();
            ajaxRequestToc(pid, segId, contentModel, coid); // populate the anchors array
            ajaxRequestTranscript(pid, segId, contentModel); // populate the transcripts array
            ajaxRequestNotes(pid, segId, contentModel, coid); // populate the notes array

            doTimer();
        }
            segmentLoaded = false;
//    }
}

function saveState(time, event) {
	
    var i = flashEvents.length;
    flashEvents[i] = "ev: " + event + " tm: " + time;

    }

function onMovieLoaded(cuePointTime) {
    //alert("movie finished loading!");//DEBUG
    segmentLoaded = true;
  
    clearAnchors();
    activateLinks = true;
    ajaxProcessToc(savedResponseXML); // activate the navigation links after the movie has loaded completely

    // get the current cue point, and highlight it again
    for (var i=0; i<anchors.length; i++) {
        if (parseFloat(anchors[i].time) == cuePointTime) {
            if (anchors[i].tag == "index") {
                highlightAnchorPlaying(anchors[i].xmlid);
                searchCalcDelta(anchors[i].xmlid)
            }
        }
    }


}
