// JavaScript Document 
function vlc(url){document.write('<OBJECT classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" codebase="http://www.videolan.org/" width="500"  height="340" id="vlc" events="True"><param name="MRL" value="" /><param name="ShowDisplay" value="True" /><param name="Src" value="'+url+'" /><param name="AutoPlay" value="true" /><param name="Volume" value="25" /><EMBED pluginspage="" type="application/x-vlc-plugin" progid="VideoLAN.VLCPlugin.2" width="500" height="340" name="vlc" target="'+url+'" autoplay="true" autoloop="true" volume="25"></EMBED></OBJECT><BR><IMG border=0 src="http://wwitv.com/tv_channels/vlc600.gif" width=500 height=20 useMap=#playerbuttons><MAP name=playerbuttons><AREA title=button href="javascript:doPlay();" shape=rect coords="16, 0, 51, 20"><AREA title=Stop href="javascript:doStop();" shape=rect coords="58, 0, 98, 20"><AREA title="Full Screen" href="javascript:doFS()" shape=rect coords="394, 0, 450, 20"><AREA title="Sound Off." href="javascript:Mute();" shape=rect coords="454, 0, 466, 20"><AREA title="Volume +" href="javascript:updateVolume(5);" shape=rect coords="469, 0, 475, 20"><AREA title="Volume -" href="javascript:updateVolume(-5);" shape=rect coords="476, 0, 489, 20"></MAP>'); doGo(url);}
var prevState = 0;
var monitorTimerId = 0;
var isIE = 0;
//----------------------------------------------------
function startvlc(){
    isIE	= 1;
    if( navigator.appName.indexOf("Microsoft Internet")==-1 )
    {
        onVLCPluginReady()
	isIE	= 0;
    }
    else if( document.readyState == 'complete' )
    {
        onVLCPluginReady();
    }
    else
    {
        /* Explorer loads plugins asynchronously */
        document.onreadystatechange=function() {
            if( document.readyState == 'complete' )
            {
                onVLCPluginReady();
            }
        }
    }
}

//----------------------------------------------------
//----------------------------------------------------
function getVLC(name)
{
    if (window.document[name]) 
    {
        return window.document[name];
    }
    if (navigator.appName.indexOf("Microsoft Internet")==-1)
    {
        if (document.embeds && document.embeds[name])
            return document.embeds[name]; 
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        return document.getElementById(name);
    }
}

//----------------------------------------------------
function onVLCPluginReady()
{
    updateVolume(0);
};

//----------------------------------------------------
function updateVolume(deltaVol)
{
    var vlc = getVLC("vlc");
    vlc.audio.volume += deltaVol;
//    document.getElementById("volumeTextField").innerHTML = vlc.audio.volume+"%";
};

//----------------------------------------------------
function doPlay()
{
    var vlc = getVLC("vlc");
    if( vlc.playlist.isPlaying )
    {
        vlc.playlist.Play();
    }
    else if( vlc.playlist.items.count > 0 )
    {
        vlc.playlist.play();
        monitor();
    }
 };
//----------------------------------------------------
function doStop()
{
    getVLC("vlc").playlist.stop();
//    if( monitorTimerId != 0 )
//    {
//        clearInterval(monitorTimerId);
//        monitorTimerId = 0;
//    }
    onStop();
};
//----------------------------------------------------
function onStop()
{
    var vlc = getVLC("vlc");
    // disable logging
    vlc.log.verbosity = -1;
};
//----------------------------------------------------
function doFS()
{
	getVLC("vlc").video.toggleFullscreen();
};
//----------------------------------------------------
function Mute()
{
	getVLC("vlc").audio.toggleMute();
};
//----------------------------------------------------
function doGo(targetURL)
{
    var vlc = getVLC("vlc");
    var options = new Array(":aspect-ratio=default");
    vlc.playlist.items.clear();
    //while( vlc.playlist.items.count > 0 )
    //{
        // clear() may return before the playlist has actually been cleared
        // just wait for it to finish its job
    //}
    //var itemId = vlc.playlist.add(targetURL, null, options);
  // MS Voodoo, for some obvious reason upper call won't work with IE
    var itemId;
    if( isIE )	{
	    itemId	= vlc.playlist.add(targetURL);
    } else {
    	    itemId 	= vlc.playlist.add(targetURL, null, options);
    }
    if( itemId != -1 )
    {
        // clear the message log and enable error logging
        vlc.log.verbosity = 1;
        vlc.log.messages.clear();
        // play MRL
        vlc.playlist.playItem(itemId);
        if( monitorTimerId == 0 )
        {
            monitor();
        }
    }
    else
    {
        // disable log
        vlc.log.verbosity = -1;
        alert("cannot play at the moment !");
    }
//    updateVolume(50);
};

//----------------------------------------------------
function monitor()
{
    var vlc = getVLC("vlc");
    if( vlc.log.messages.count > 0 )
    {
        // there is one or more messages in the log
        var iter = vlc.log.messages.iterator();
        while( iter.hasNext )
        {
            var msg = iter.next();
            var msgtype = msg.type.toString();
            if( (msg.severity == 1) && (msgtype == "input") )
            {
             
            }
        }
        // clear the log once finished to avoid clogging
        vlc.log.messages.clear();
    }
    var newState = vlc.input.state;
    if( prevState != newState )
    {
        if( newState == 0 )
        {
            // current media has stopped 
            onStop();
        }
        else if( newState == 1 )
        {
            // current media is openning/connecting
            // onOpen();
        }
        else if( newState == 2 )
        {
            // current media is buffering data
            //onBuffer();
        }
        else if( newState == 3 )
        {
            // current media is now playing
            //onPlay();
        }
        else if( vlc.input.state == 4 )
        {
            // current media is now paused
            //onPause();
        }
        prevState = newState;
    }
    else if( newState == 3 )
    {
        // current media is playing
        //onPlaying();
    }
    if( ! monitorTimerId )
    {
        monitorTimerId = setInterval("monitor()", 1000);
    }
};
