

//##################### MEDIA PLAYER FUNCTIONS #####################
// //##################### need to check status of player before functions #####################
// //##################### Needs it's own loop counter
//##################### Needs it's own State Controller - loading - play - pause - stop - end detect - unload?
//  If it dosn't respond and it's the current layer transition
// 


function stop_m() {
    MyPlayer = document.getElementById('MediaPlayer1');
    MyPlayer.controls.stop();
}

function play_m() {
    MyPlayer = document.getElementById('MediaPlayer1');
    MyPlayer.controls.play();
    // check if it's playing
    if (MyPlayer.playState != 3) {
        PlayerStateDetecting = true;
        PlayerStateCounter = 0;
        PlayLoop_m();
    }
}

function PlayLoop_m() {
    if (PlayerStateDetecting == false) return;
    MyPlayer = document.getElementById('MediaPlayer1');
    if (MyPlayer.playState == 3) {
        PlayerStateDetecting = false;
        PlayerStateCounter = 0;
    }
    else { 
        MyPlayer.controls.play();
        if (PlayerStateCounter > 50) {
            PlayerStateCounter = 0;
            PlayerStateDetecting = false
            return;
        } 
        PlayerStateCounter += 1;
        PlayerStateTimer = setTimeout('PlayLoop_m()', 100);
    }
}



function pause_m() {
        MyPlayer = document.getElementById('MediaPlayer1');
        MyPlayer.controls.pause();
}

// ##################### PLAYER CONTROL START PAUSE #####################
// ##################### Needs a loop counter 

function startpause_m() {
    //It's loaded
    if (OState.detectrunning == false) return;
    MyPlayer = document.getElementById('MediaPlayer1');
    //if (MyPlayer.playState == 10) play_m();

    //Check for Running every 100 if it is pause it.
    if (MyPlayer.playState == 3) {
    

        // stopit puts it in a loop Just stop the player
        // Want it in a stop loop
        stop_m();

        //MyPlayer.controls.stop();

        OState.detectrunning = false;
        OState.nextready = true;
    }
    else {
        MyPlayer.controls.play();
        PlayerWaitTimer = setTimeout('startpause_m()', 100);
    }
}

/*
MediaPlayer playState 
0 Undefined Windows Media Player is in an undefined state. 
1 Stopped Playback of the current media item is stopped. 
2 Paused Playback of the current media item is paused. When a media item is paused, resuming playback begins from the same location. 
3 Playing The current media item is playing. 
4 ScanForward The current media item is fast forwarding. 
5 ScanReverse The current media item is fast rewinding. 
6 Buffering The current media item is getting additional data from the server. 
7 Waiting Connection is established, but the server is not sending data. Waiting for session to begin. 
8 MediaEnded Media item has completed playback.  
9 Transitioning Preparing new media item. 
10 Ready Ready to begin playing. 
11 Reconnecting Reconnecting to stream. 
*/

function enddetect_m() {
    // It's the current layer
    // on flash must have one for ie and ns
    // detect duration and current

    // Must call state_ctl('move_it');  ???

    if (OState.enddetecting == false) return;
    
    OState.enddetect = false;
    var bolAgain = false;
    var Dur = 0;
    var CurPos = 0;

    MyPlayer = document.getElementById('MediaPlayer1');
    // need to detect if it's running before Dur and cur are available
    if (MyPlayer.playState == 3) {
        try {
            Dur = MyPlayer.currentMedia.duration;
            CurPos = MyPlayer.controls.currentPosition;
            if ((CurPos + .4) < Dur && Dur > 0) {
                bolAgain = true;
            }
        }
        catch (e) {
            Dur = 0;
        }
    }
    else {
        bolAgain = true;
    }


    if (bolAgain == true) {
        //document.form1.txtDetect.value = 'Duration: ' + Dur + '   Current: ' + CurPos;
        EndDetectTimer = setTimeout('enddetect_m()', 100);
    }
    else {
        OState.enddetect = true;
        state_ctl('move_it');
    }
}




