/* all your variables are belong to us */
var doc = document.getElementsByTagName('span'); // create an array from the <span>s
var status = 0, startTime = 0, c = 0, i = 0, posScroll = 0, posMiddle = 0, playing = 0;
var movie = null, pointer = null, picWindow = null, myElement = null, myFx = null;
var warpTimeout, posTimeout, lineTimeout;

/* define functions that register each listener */
function RegisterListener(eventName, objID, embedID, listenerFcn) {
	var obj = document.getElementById(objID);
    if (!obj) document.getElementById(embedID);
	if (obj) myAddListener(obj, eventName, listenerFcn, false);
}

/* define function that adds another function as a DOM event listener */
function myAddListener(obj, evt, handler, captures) {
	if ( document.addEventListener ) obj.addEventListener(evt, handler, captures);
    else obj.attachEvent('on' + evt, handler); // IE
}

/* define a single function that registers all listeners to call onload */
function init() {
    resizeMedia();
    if(document.mymovie) {
        RegisterListener('qt_play', 'mymovie', 'qtmovie_embed', moviePlay);
        RegisterListener('qt_pause', 'mymovie', 'qtmovie_embed', moviePause);
        window.onkeydown = key; // when a key is pressed, run key() to check if it's a shortcut
        movie = document.mymovie;
    }
}

/* when the media begins playback, this function loads */
function moviePlay() {
    timeWarp();
    setPos();
}

/* when the media pauses playback, this function loads */
function moviePause() {
    window.clearTimeout(warpTimeout);
    window.clearTimeout(posTimeout);
    window.clearTimeout(lineTimeout);
}

/* defines keyboard shortcuts */
function key(evt) {
    if(document.activeElement.id != "args") {
        if (evt.keyCode == 80) { // if the key pressed is "p"
            if (pointer!=null) { //  if an utterance has been selected...
                startTime = parseInt((pointer / 1000.0000) * movie.GetTimeScale()); // set the start time
                movie.Stop(); // stop the movie 
                movie.SetTime(startTime); // move to the startTime
                movie.Play(); // and play the movie
                i = 0; // set counter to 0
                while(doc[i].id.indexOf(pointer) == -1) { // while the selected utterance doesn't contain the start time...
                    i++ // ...keep searching
                }
                playLine(i); // run playLine() to stop playback at the utterance's end
            }
        }
        else if (evt.keyCode == 83) { // if the key pressed is "s"
            if(movie.GetRate()) movie.Stop(); // if playing, stop the movie...
            else movie.Play(); // ...otherwise, play the movie
            return false;
        }
    }
}

function setLine(lineNumber) {
    pointer = lineNumber; // when a utterance is selected, set the pointer to the utterance's start time
}

/* using the "p" keyboard shortcut, only play the selected utterance */
function playLine(i) {
    status = Math.round((movie.GetTime()/movie.GetTimeScale())*1000.0000); // convert movie seconds to true milliseconds
    if(status > parseInt(doc[i].className)) movie.Stop(); // if the time is after the utterance's end, stop...
    else lineTimeout = window.setTimeout("playLine(i)", 100); // ...otherwise, check again in 100 ms
}

/* figure out which options is being played, and highlight it */
function timeWarp() {
    /*if(isNaN(doc[c].id)) {
        picWindow = window.open(doc[c].id, "pic", "height=300, width=400");
        c++;
    }
    else {*/
        status = Math.round((movie.GetTime()/movie.GetTimeScale())*1000.0000); // convert movie seconds to true milliseconds
        if(status > parseInt(doc[c].className)) { // if the current time is greater than the current utterance's end
            while(status > parseInt(doc[c].className)) { // while the current time is greater...
                doc[c].style.background = "#FFF";
                if (c == doc.length - 1) { // if the transcript has come to an end...
                    break; // ...break
                }
                else { // otherwise...
                    c++ // ...keep looping upward
                }
            }
        }
        else if(status < parseInt(doc[c].id)) { // if the current time is less than current utterance's start time
            while(status < parseInt(doc[c].id) && c > 0) { // while the above is true...
                doc[c].style.background = "#FFF";
                c--; // ...keep looping downward
            }
        }
        else { // if the current time is less than the current utterance's end
            doc[c].style.background = "#FFF68F";
            /*if(picWindow!=null && c==doc.length-1) picWindow.close();*/
        }
    /*}*/
    warpTimeout = window.setTimeout("timeWarp()", 100);
}

/* figure out which options is being played, and scroll to it */
function setPos() {
    if(c < doc.length - 1) { // if the counter is less than the total number of utterances...
        if(window.innerHeight) bodyHeight = window.innerHeight;
        else if(document.documentElement.clientHeight) bodyHeight = document.documentElement.clientHeight;
        else bodyHeight = document.body.clientHeight;
        posScroll = window.scrollY + (4/5)*bodyHeight; // position of scrollbar + viewable screen
        posMiddle = doc[c].offsetTop - bodyHeight/5; // set the "middle" of the page to (height - height/5)
        if(doc[c].offsetTop < window.scrollY || doc[c].offsetTop > posScroll) { // if the playing utterance is above the fold...
            myElement = $(document.body);
            myFx = new Fx.Scroll(myElement).start(0, posMiddle);        
        }
    }
    posTimeout = window.setTimeout("setPos()", 1000);
}

/* resize the Nav, Options, and Media elements to fill the screen */
function resizeMedia() {
    if(window.innerHeight) bodyHeight = window.innerHeight;
    else if(document.documentElement.clientHeight) bodyHeight = document.documentElement.clientHeight;
    else bodyHeight = document.body.clientHeight;
	document.getElementById('nav').style.height = bodyHeight - 320 - document.getElementById('options').clientHeight + "px";
    headerHeight = document.getElementById("left").style.height;
    if(headerHeight != bodyHeight) {
        headerHeight = bodyHeight;
        if(document.getElementById("media")) {
            optionsHeight = document.getElementById("options").clientHeight;
            if(movie) document.getElementById("nav").style.height = bodyHeight - optionsHeight - 320 + "px";
            else document.getElementById("nav").style.height = bodyHeight - optionsHeight - 120 + "px";
        }
        else document.getElementById("nav").style.height = bodyHeight - 110 + "px";
    }
    window.setTimeout("resizeMedia()", 100);
}

/* play the movie from the selected utterance to the end */
function walkMovie(start) {
    doc[c].style.background = "#FFF"; // unhighlight any previously selected utterances
    if(doc[c].id == start) { // if the current utterance is the selected utterance
        if (c == 0) { // if you want to start from the first utterance
            startTime = (start / 1000.0000) * movie.GetTimeScale(); 
            movie.SetTime(startTime);
            movie.Play();
        }
        else {
            c = 0; // set the counter to zero
            movie.walkMovie(start); // and play the movie from the selected utterance to the end
        }
    }
    else {
        c = 0; // set counter to 0
        while(doc[c].id.indexOf(start) == -1) { // while the selected utterance doesn't contain the start time...
            c++ // ...keep searching
        }
        startTime = (start / 1000.0000) * movie.GetTimeScale();
        movie.Stop(); // pause the movie
        movie.SetTime(startTime); // set the movie time
        movie.Play(); // play the movie
    }
}