// Settings for autostart of the lightbox Player
var tempPlayerTime = new Object();
var currentLightboxLinkPlayer;
var timeMonitorActive = true;

// Registers the Listeners for TIME- and LINK-events when a thumbnail player is ready
function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	player = document.getElementById(id);
    
    if(id.match("_embed$")=="_embed") {
        player.addModelListener('TIME', 'timeMonitor');
        player.addViewListener('LINK', 'linkMonitor');
    }
};

/**
*
*   setUpLightboxPlayer: sets up the thumbnail player with the ability to open a lightbox player
*   @param pid: Id of the link to the Lightbox Flash
*   @param playerW: Width of the Thumbnail player
*   @param playerH: Height of the Thumbnail player
*   @param flvsrc: Source path to the Thumbnail Flash
*   @param imgsrc: Source of the default placeholder image (start screen)
*   @param pStart: Starting point (seconds) of video replay (streaming only)
*   @param pDuration: Duration (seconds) of video replay (streaming only)
*
**/ 
var registeredPlayers = new Array();
function setUpLightboxPlayer(pid, playerW, playerH, flvsrc, imgsrc, streamer, pStart, pDuration) {
    registeredPlayers[registeredPlayers.length] = pid+"_embed";
    var s1 = new SWFObject(documentRoot + "/js/lightbox/jwplay.swf",pid + "_embed",playerW,playerH,"9","#FFFFFF");
    
    s1.addParam("allowfullscreen","true");
    s1.addParam("allowscriptaccess","always");
    s1.addParam('wmode','opaque');

    var streamerSetup = "";
    if(streamer) {
        streamerSetup = "&streamer=" + streamer;
    }
    var replayOptions = "";
    if (pStart && pDuration) {
    	replayOptions = "&start=" + pStart + "&duration=" + pDuration + "&respectduration=true";
    }
    
    var flashVars = "file=" + flvsrc + "&image=" + imgsrc +
            "&link=javascript:myLightbox.startVideo($(\'"+pid+"\'))&linktarget=_self"+
            "&displayheight="+playerH+"&displaywidth="+playerW+"&overstretch=true&enablejs=true"+
            "&skin=" + documentRoot + "/js/lightbox/previewplayer.swf&usefullscreen=false&showfsbutton=false&autostart=false&showdigits=false"+
            replayOptions +
            streamerSetup; 
    s1.addParam("flashvars", flashVars); 
 
    s1.write(pid+"_div");
}

// monitor the current playback position of the thumbnail player
function timeMonitor(obj) {
    if(timeMonitorActive) {
        tempPlayerTime[obj['id']] = obj.position;
    }
}

// monitor the id of the thumnail player that is about to open a lightbox player
function linkMonitor(obj) {
	var id = obj['id'];
    currentLightboxLinkPlayer = document.getElementById(id);
    timeMonitorActive = false;
    for(i=0;i<registeredPlayers.length;i++) {
        $(registeredPlayers[i]).sendEvent("STOP");
    }
}
    
