/*******************************************
to function correctly - you need to include duration.js

1 - set the url of the duration application using setdurationTrackURL()
2 - set the interval at which requests should be sent to the duration app using setDurInterval()
3-  set durTrack [timelineid] = 'true'; for all timelines that should be sending requests to the duration app
    ie: durTrack [500] = 'true'; for live
4 - in the frame with the player - call startDuration(this); (after the player is loaded) *see note on player frame requirements

*note on player frame requirements

frame must define values for:
ARCHIVE
LIVE
currentTimelineLocation
currentPlayerPref - one of 'WMS9', etc

frame must define functions:

checkTimelineLocation ( theTimeline ) - returns true if the player is in that timeline (either naturally or overrriden to that timeframe)
getCurrentVideoPosition (0 if not loaded - else the current timecode)
play
stop
pause
ff  - returns the timecode it is seeking to
fr - returns the timecode it is seeking to
setCurrentIndexPoint ( newIndexPoint, force ) - returns the timecode it is seeked to
setCurrentVideoPosition ( newTimecode )


frame must call the following functions instead of its own

player_wrap()
pause_wrap()
stop_wrap()
ff_wrap()
fr_wrap()
setCurrentIndexPoint_wrap()
setCurrentVideoPosition_wrap()

frame must also have the video object call the following functions in its callbacks

callback_play_duration
callback_stop_duration
callback_pause_duration

*******************************************/
function startDuration( theobj ) {
    parent.setPlayerFrame( theobj );
    startTrackCurrentTimecode();
    startHBDuration();
}

/* for debug alerts */
var alertOnConsoleDNE = false;

/* we are having issues with callbacks - want to be able to turn them off easily */
var doCallbacks = true;

if ( navigator.appName != "Netscape" ) {
    if ( document.domain != 'localhost' ) {
	document.domain = 'capitalreach.com';
    }
}

var playerFrame = null
function setPlayerFrame( theobj ) {
   playerFrame = theobj;
}

/* constants */
var PLAYER_OPEN = 0;var PLAYER_CLOSED = 1;var PLAYER_STOPPED = 2;var PLAYER_INDEXSELECTED = 3;
var PLAYER_FF = 4;var PLAYER_FREW = 5;var PLAYER_PRESSPAUSE = 6;var PLAYER_PAUSERESUME = 7;
var PLAYER_TEXTSEARCH = 8;var PLAYER_BROWSESLIDES = 9;var PLAYER_PLAYERSTART = 10;
var IPSEEK_NO = 0;var IPSEEK_YES = 1;

/* a variable so that it can be read out of web.XML */
var durationTrackURL = '';

var ViewID = 0;         /*inside of each unique player, you could have different views - pressing stop, selecting IP, etc*/

var intialTimecode = 0;  /* this is the timecode at which the user started watching */
var intialDateForTC = new Date();  //we don't have the player object to give us TCs

var isTrackingDuration = false; /* if you should be sending hits to say the user is watching */
var isPaused = false;   /*this is different than not watching - need a viewresume instead of viewstart*/
var isStopped = false;   /*this is because when the indexpt etc is selected - we only want to send notification if the player was not previously stopped*/

var durStartViewType = PLAYER_OPEN;

var myState = IPSEEK_NO;  /* 0 - clear, 1 - busy (selecting ip,  browseslide) - use this so the play callback doesn't wreck havoc  */

var durTrack = new Array();
durTrack [400] = 'true';
durTrack [500] = 'false';
durTrack [700] = 'false';



/* this is just a thread to keep track of the last known timecode BEGIN*/
var trackCurrentTimecode = 0;
var tctInterval = 30;
var tctCurrent = null;

function doTrackCurrentTimecode() {
    var timecodetemp;
    timecodetemp = getCurrentTimecode();
    if ( timecodetemp ) {
        trackCurrentTimecode = timecodetemp;
    }
}
function setCTInterval( theinterval ) {
	tctInterval = theinterval;
}

function startTrackCurrentTimecode() {
    //alert( hbDurInterval ) ;
    tctCurrent = setInterval ( 'doTrackCurrentTimecode()', tctInterval );
}
function resetCTInterval ( newInterval ) {
    stopTrackCurrentTimecode();
    tctInterval = newInterval;
    startTrackCurrentTimecode();
}

function stopTrackCurrentTimecode() {
    clearInterval ( tctCurrent );
}
/* this is just a thread to keep track of the last known timecode END*/


function setdurationTrackURL( thedurationTrackURL ) {
	durationTrackURL = thedurationTrackURL;
}


/* this is just the thread to keep send the duration hit at a specified interval BEGIN*/
var hbDur = null;
var hbDurInterval = 30000;
var hbDurCounter = 0;

function setDurInterval( theinterval ) {
	hbDurInterval = theinterval;
}

function startHBDuration() {
    isTrackingDuration = true;
    //alert( hbDurInterval ) ;
    hbDur = setInterval ( 'doHBDurationHit()', hbDurInterval );
}
function setHBDurInterval ( newInterval ) {
    stopHBDuration();
    hbDurInterval = newInterval;
    startHBDuration();
}

function stopHBDuration() {
    clearInterval ( hbDur );
}

function doHBDurationHit() {
    sendCPComm( PLAYER_OPEN, hbDurCounter++, false );
}
/* this is just the thread to keep send the duration hit at a specified interval END*/


function stopPlayerViewing ( endType, closeout ) {
    //alert("start stopPlayerViewing");

    if ( playerFrame ) {
        if ( ( playerFrame.checkTimelineLocation (playerFrame.ARCHIVE) ) ||
        ( playerFrame.checkTimelineLocation (playerFrame.LIVE) && (endType == PLAYER_STOPPED) ) ) {

            /* if the player is stopped we do not want to overwrite the prior end point to index selected - etc */
            if ( ! (isStopped || isPaused) ) {
                //alert("send");
                sendCPComm ( endType, -1, closeout );
            }
            if ( endType == PLAYER_PRESSPAUSE ) {
                isPaused = true;
            } else {
                isPaused = false;
            }
            if ( (endType == PLAYER_STOPPED) || (endType == PLAYER_CLOSED) ) {
                isStopped = true;
            }
     } else {
        if ( alertOnConsoleDNE ) {
            alert("playerFrame DNE");
        }
     }

    }


}

function startNewPlayerViewing ( starttype ) {
    durStartViewType = starttype;

	trackCurrentTimecode = 0;
    isStopped = false;
    if ( isPaused ) {
        sendCPComm ( PLAYER_PAUSERESUME, false );
        isPaused = false;
    }
    else {
        ViewID = ViewID + 1;
        // intialTimecode = getCurrentTimecode ();       had issues with this working in real
        //alert (  intialTimecode );
		intialDateForTC = new Date();
    }
    isTrackingDuration = true;
}

function getCurrentTimecode () {
    var timecode = 0;

    if ( playerFrame ) {
        if ( !playerFrame.isStreamless ) {
            timecode = playerFrame.getCurrentVideoPosition ();
            if ( timecode ) {
                timecode = Math.floor ( timecode / 1000 );
            }
        }
     } else {
        if ( alertOnConsoleDNE ) {
            alert("playerFrame DNE");
        }
     }

    return timecode;

}

function sendCPComm ( endType, phbcount, closeout ) {
    var hbOnly = false;
    var timecode = null;

    if ( playerFrame ) {

        if ( durTrack [playerFrame.currentTimelineLocation] || durTrack [playerFrame.overrideTimelineLocation]  )  {

            if (endType != PLAYER_OPEN) {
                isTrackingDuration = false;
            }
            else if ( !isTrackingDuration ) {
                 hbOnly = true;
            }

            if ( !hbOnly ) {
                var durationinMS = 0;
                if ( playerFrame.isStreamless ) {
                    var currentDateForTC = new Date();
                    durationinMS =  Math.floor( (currentDateForTC-intialDateForTC) / 1000);
                } else {
                    timecode = getCurrentTimecode ();
                    //alert ( timecode);
                    /* the console is not playing */
                    if ( timecode == 0) {
                        /*alert ( 'Not currently playing' );*/
                        timecode = trackCurrentTimecode;
                    }
                    durationinMS =  Math.floor ( timecode - intialTimecode );
                }

                //alert ( endType );
               // alert (trackCurrentTimecode);

                if ( durationinMS < 0 ) { durationinMS = 0; }
                durURL = durationTrackURL + "&dms=" + durationinMS + "&det=" + endType + "&vid=" + ViewID +
                    "&itc="  +  Math.floor (intialTimecode) + "&hbc=" + phbcount + "&etl=" + playerFrame.currentTimelineLocation +
                    "&dst=" + durStartViewType ;
            } else {
                    isTrackingDuration = false;
                    durURL = durationTrackURL + "&det=" + endType + "&vid=" + ViewID +
                        "&hbc=" + phbcount  + "&etl=" + playerFrame.currentTimelineLocation ;
            }

            if (closeout) {
                if ( navigator.appName != "Netscape" ) {
                    window.open( durURL + "&close=1", "closedur", 'screenX=2000,screenY=2000,width=150,height=100' );
                } else {
                    window.open( durURL + "&close=1", "closedur", 'left=2000,top=2000,width=150,height=100' );
                }
            } else {
                hit.location.replace(durURL);
            }
        }
     } else {
        if ( alertOnConsoleDNE ) {
            alert("playerFrame DNE");
        }
     }

}

   /* these are only called when video events are triggered*/
   function callback_play_duration() {
        if ( doCallbacks && (isStopped || isPaused)  ) {
            play_postForDuration();
        }
   }
   function callback_pause_duration() {
        if ( doCallbacks ) {
            pause_preForDuration();
        }
   }
   function callback_stop_duration() {
        if ( doCallbacks ) {
            stop_preForDuration();
        }
   }


 /* the following are wrapper functions for seth's video functions -
        so I can do duration type operations pre and post
   */

   function play_postForDuration () {
        if ( myState == IPSEEK_NO ) {
            if (!isPaused) {
                intialTimecode = getCurrentTimecode ();
            }
            startNewPlayerViewing ( PLAYER_PLAYERSTART );
        }
   }

   function stop_preForDuration () {
        if ( !isStopped ) {
            stopPlayerViewing ( PLAYER_STOPPED, false );
        }
        myState = IPSEEK_NO;
   }

   function pause_preForDuration () {
       stopPlayerViewing ( PLAYER_PRESSPAUSE, false );
       myState = IPSEEK_NO;
   }

   function ff_preForDuration () {
        stopPlayerViewing ( PLAYER_FF, false );
   }
   function ff_postForDuration () {
        /* you can only FF in archive mode */
        if ( playerFrame.checkTimelineLocation (playerFrame.ARCHIVE) ) {
            startNewPlayerViewing ( PLAYER_FF );
        }
   }

   function fr_preForDuration () {
        stopPlayerViewing ( PLAYER_FREW, false );
   }
   function fr_postForDuration () {

        if ( playerFrame ) {

            /* you can only FF in archive mode */
            if ( playerFrame.checkTimelineLocation (playerFrame.ARCHIVE) ) {
                startNewPlayerViewing ( PLAYER_FREW );
            }

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
        }

   }

   function play_wrap () {

        if ( playerFrame ) {

            var return_val = playerFrame.play();
            play_postForDuration ()
            return return_val;

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }

   }

   function stop_wrap () {
        if ( playerFrame ) {

            stop_preForDuration ();
            var return_val = playerFrame.stop();
            return return_val;

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }
   }

   function pause_wrap () {

        if ( playerFrame ) {

            pause_preForDuration ();
            var return_val = playerFrame.pause();
            return return_val;

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }

   }

   function ff_wrap () {

        if ( playerFrame ) {

            ff_preForDuration ();
            var return_val = playerFrame.ff();
            if ( return_val ) {
                intialTimecode = Math.floor ( return_val / 1000 ) ;
            }

            ff_postForDuration ();
            return return_val;

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }
   }

   function fr_wrap () {

        if ( playerFrame ) {

            fr_preForDuration ();
            var return_val = playerFrame.fr();
            intialTimecode = return_val;
            if ( return_val ) {
                intialTimecode = Math.floor ( return_val / 1000 ) ;
            }
            fr_postForDuration ();
            return return_val;

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }
   }

   function setCurrentIndexPoint_wrap ( newIndexPoint, force ) {

        if ( playerFrame ) {

            stopPlayerViewing ( PLAYER_INDEXSELECTED, false );

            myState = IPSEEK_YES;
            /* you can only use indexpoints in archive mode */
            if ( playerFrame.checkTimelineLocation (playerFrame.ARCHIVE) ) {
                intialTimecode = playerFrame.setCurrentIndexPoint ( newIndexPoint, force );
                startNewPlayerViewing ( PLAYER_INDEXSELECTED );
            }

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }
   }

   function  setCurrentVideoPosition_wrap ( newTimecode, type ) {

        if ( playerFrame ) {

            stopPlayerViewing ( type, false );

            myState = IPSEEK_YES;
            /* you can only use indexpoints in archive mode */
            if ( playerFrame.checkTimelineLocation (playerFrame.ARCHIVE) ) {
                playerFrame.setCurrentVideoPosition ( newTimecode );
                intialTimecode = Math.floor ( newTimecode / 1000 ) ;
                startNewPlayerViewing ( type );
            }

         } else {
            if ( alertOnConsoleDNE ) {
                alert("playerFrame DNE");
            }
         }
   }


