
var slider;
var ignoreSlider = false;
var width = 0;
var intervalLength = 500;
var playing = true;
var sliderDiv;
var currentlyPlaying = "demoreel";
var showWelcome = true;

function setup_demo_movie() {
	sliderDiv = document.getElementById("slider");
    slider = YAHOO.widget.Slider.getHorizSlider("slider", "sliderthumb", 0,
        228);
    slider.onChange = function(i) { movieMoveTo(document.mplayer, i); };
    sliderRegion = YAHOO.util.Dom.getRegion("slider");
    sliderthumbRegion = YAHOO.util.Dom.getRegion("sliderthumb");
    width = sliderRegion.right - sliderRegion.left - sliderthumbRegion.left
        + sliderthumbRegion.right;
    updateControls(document.mplayer);
    setInterval("updateControls(document.mplayer)", intervalLength);
        showWelcome = false;
    qt = document.mplayer;        
}

function movieMoveTo(qt, offset) {
    if (ignoreSlider)
        return;
    try {
        // offset is the slider offset
        time = (offset+12) * qt.GetDuration() / width;
        oldRate = document.mplayer.GetRate();
        qt.SetTime(time);
        qt.SetRate(oldRate); // pause if paused, play if was playing
    } catch (e) {
    }
}

function updateControls(qt) {
    try {
        url = qt.GetURL();
        if (url.indexOf("poster") >= 0)
            return; // don't update on the poster movie
        movName = url.substring(url.lastIndexOf("/") + 1,
                url.lastIndexOf("."))
        if (currentlyPlaying != movName)
            loadDescription(movName);
        if (qt.GetRate() != 0) {
            ignoreSlider = true;
            slider.setValue(Math.ceil(qt.GetTime() * width / qt.GetDuration())
                - 12, true);
            ignoreSlider = false;
        }
        //status = qt.GetPluginStatus();
        //if (status == "Playable") {
            // loadedPx is the number of pixels the grey line should be
            loadedPx = 240 * qt.GetMaxTimeLoaded() / qt.GetDuration();
            if (!loadedPx) { // if NaN
                loadedPx = 0;
                slider.setValue(0,true);
            }
            //window.status = loadedPx + " " + qt.GetMaxTimeLoaded() + " " +
                //qt.GetDuration();
            sliderDiv.style.backgroundPosition = (loadedPx-240) + "px 5px";
        //}
        //else if (qt.GetPluginStatus() == "Complete") {
            //if (sliderDiv.style.backgroundPosition != "0px 5px") {
                //sliderDiv.style.backgroundPosition = "0px 5px";
            //}
        //}
        //else {
            //sliderDiv.style.backgroundPosition = "-240px 5px";
        //}
        qt.SetControllerVisible(false);
        playStyle = document.getElementById("vnplay").style;
        audioStyle = document.getElementById("vnaudio").style;
        if (!playing && qt.GetRate() != 0) {
            playStyle.backgroundImage = "url(images/video/vn_pause.gif)";
            playing = true;
        }
        else if (playing && qt.GetRate() == 0) {
            playStyle.backgroundImage = "url(images/video/vn_play.gif)";
            playing = false;
        }
        if (audioStyle.backgroundImage == "url(images/video/vn_mute.gif)") {
            if (!qt.GetMute()) {
                audioStyle.backgroundImage = "url(images/video/vn_audio.gif)";
            }
        }
        else {
            if (qt.GetMute()) {
                audioStyle.backgroundImage = "url(images/video/vn_mute.gif)";
            }
        }
    }
    catch (e) { }
}

function togglePlayPause(qt) {
    try {
        if (qt.GetRate() == 0) {
            // movie is paused
            qt.SetRate(1.0);
            document.getElementById("vnplay").style.backgroundImage =
                "url(images/video/vn_pause.gif)";
        }
        else {
            // movie is playing
            qt.SetRate(0.0);
            document.getElementById("vnplay").style.backgroundImage =
                "url(images/video/vn_play.gif)";
        }
    }
    catch (e) { }
}

function toggleAudio(qt) {
    if (qt.GetMute()) {
        qt.SetMute(false);
        document.getElementById("vnaudio").style.backgroundImage =
            "url(images/video/vn_audio.gif)";
    }
    else {
        qt.SetMute(true);
        document.getElementById("vnaudio").style.backgroundImage =
            "url(images/video/vn_mute.gif)";
    }
}
