﻿dojo.require("entriq.cal.media.WMUI");

var playerui;
var objWMUI;

var strings =
{
    	Undefined: "Undefined...",
    	Stopped: "Stopped...",
    	Paused: "Paused...",
    	Playing: "Playing...",
    	FastForwarding: "Fast forwarding...",
    	Rewinding: "Rewinding...",
    	Buffering: "Buffering...",
    	Waiting: "Waiting...",
    	MediaEnded: "Media ended...",
    	Transitioning: "Transitioning...",
    	Ready: "Ready...",
    	Reconnecting: "Reconnecting..."
};

function writePlayer()
{
    document.getElementById("videoplayer").style.visibility="hidden"
    //document.getElementById("videoplayer").style.display="none"

	var controls =
	{
        	/*StatusArea: "controls_container",*/
	        PlayPause: "controls_container",
	        ProgressBar: "controls_container",
        	MuteButton: "controls_container",
        	VolumeBar: "controls_container",
        	FullScreen: "controls_container"
		    /*Rewind: "controls_container",*/
	        /*Stop: "controls_container",
	        FastForward: "controls_container",*/
	        
        	    
	}
	var strings =
	{
    		Undefined: "Undefined",
	    	Stopped: "Stopped",
	    	Paused: "Paused",
    		Playing: "Playing",
	    	FastForwarding: "Fast forwarding",
    		Rewinding: "Rewinding",
	    	Buffering: "Buffering",
    		Waiting: "Waiting",
	    	MediaEnded: "Media ended",
    		Transitioning: "Transitioning",
	    	Ready: "Ready",
    		Reconnecting: "Reconnecting"
	}	
    var uiArgs = {"Strings": strings, "Container": "player_container", "ReadyCallback": callback, "VariableName": "playerui", "PlayerArgs": {stretchToFit:true, uiMode: "none"}, "Controls": controls};
    objWMUI = new entriq.cal.media.WMUI(uiArgs);

}

function callback(player)
{
    playerui = player;
}

function ShowVideo(highLink, samiUrl, medLink, lowLink)
{
    ShowVideo(highLink, samiUrl, medLink, lowLink, "");
}

function ShowVideo(urlhigh, captiondata, urlmedium, urllow, moviename){
// mgp doesnt work for some reason... bar goes too far
 //url="http://localhost:1126/Content/testdata/dilbert.mpg"
 //url="http://localhost:1126/Content/testdata/windowsmedia.wmv"
 
 // captiondata="http://localhost:59277/Content/testdata/dilbert.smi";

    if(objWMUI==null){
        writePlayer();
    }
    document.getElementById("videoplayer").style.visibility="visible"
    document.getElementById("captions").style.display = "none";
    //
    var urlToPlay = urlhigh;
    var ccBtn = document.getElementById("ccBtn");
    var playerNode =  document.getElementById("videoplayer");
    var mrTop = 866/2-580/2;
    try
    {
        if(dojo.isIE)
        {
            mrTop = document.body.offsetHeight/2 - 580/2;
        }
        else
        {
            mrTop = window.innerHeight/2 - 580/2;
        }
    }
    catch(err)
    {
    
    }
    
    document.getElementById("videoplayer_centerer").style.marginTop = mrTop + "px";
    

    var titleNode = document.getElementById("title_label");
    if (null != titleNode) {
        if (titleNode.firstChild) {
            titleNode.firstChild.nodeValue = moviename;
        }
        else {
            titleNode.appendChild(document.createTextNode(moviename));
        }
    }
    if(urllow){
        document.getElementById("BitRate1").style.visibility="visible"
        document.getElementById("BitRate1").onclick = Function('PlayUrl("'+ urllow + '")');
        urlToPlay = urllow;
        } 
        else {
        document.getElementById("BitRate1").style.visibility="hidden";
    }
    if(urlmedium){
        document.getElementById("BitRate2").style.visibility="visible"
        document.getElementById("BitRate2").onclick = Function('PlayUrl("'+ urlmedium + '")');
        urlToPlay = urlmedium;
    } else {
        document.getElementById("BitRate2").style.visibility="hidden";
    }
    if(urlhigh){
        document.getElementById("BitRate3").style.visibility="visible"
        document.getElementById("BitRate3").onclick = Function('PlayUrl("'+ urlhigh + '")');
        urlToPlay = urlhigh;
    } else {
        document.getElementById("BitRate3").style.visibility="hidden";
    }
    if (urlToPlay) 
    {
        document.getElementById("play_button").className = "play_button_active";
        if (dojo.isFF) {
            player = document.createElement("embed");
            player.setAttribute("id", "EntriqWMP");
            player.setAttribute("SendPlayStateChangeEvents", "true");
            player.setAttribute("type", "video/x-ms-wmv");
            player.setAttribute("hidden", "false");
            player.setAttribute("src", urlToPlay);
            var par = document.getElementById("EntriqWMP").parentNode;
            par.removeChild(document.getElementById("EntriqWMP"));
            par.appendChild(player);
            objWMUI.mediaPlayer._mediaControl = player;
        }
        else {
            objWMUI.mediaPlayer.play(urlToPlay);
        } 
    } else {
        objWMUI.mediaPlayer.stop();
        document.getElementById("play_button").className = "play_button_disabled";
    }
    if (captiondata && captiondata.length) {
        dojo.addClass(ccBtn, "active");
        dojo.removeClass(ccBtn, "inactive");
        ccBtn.style.visibility = "visible";
        ccBtn.onclick = ToggleCC;
        objWMUI.mediaPlayer.setCaptions({ CaptionFile: captiondata, CaptionId: "captions" });
        document.getElementById("captions").style.display = "block";
    } else {
        ccBtn.style.visibility = "hidden";
        ccBtn.onclick = null;
    }       
}

function PlayUrl(url) {
    if(objWMUI==null){
        writePlayer();
    }
    objWMUI.mediaPlayer.stop();
    if (url) {
        document.getElementById("play_button").className = "play_button_active";
        objWMUI.mediaPlayer.play(url);
    } else {
        objWMUI.mediaPlayer.stop();
        document.getElementById("play_button").className = "play_button_disabled";
    }
}

function HideVideo(){
    document.getElementById("videoplayer").style.visibility="hidden";
    document.getElementById("BitRate1").style.visibility="hidden";
    document.getElementById("BitRate2").style.visibility="hidden";
    document.getElementById("BitRate3").style.visibility="hidden";
    document.getElementById("ccBtn").style.visibility = "hidden";
    
    objWMUI.mediaPlayer.stop()
}

function ToggleCC(){
    var cur = document.getElementById("captions").style.display;
    var ccBtn = document.getElementById("ccBtn");
    if(cur=="none") {
        dojo.addClass(ccBtn, "active");
        dojo.removeClass(ccBtn, "inactive");
        document.getElementById("captions").style.display="block";
    } else {
        dojo.addClass(ccBtn, "inactive");
        dojo.removeClass(ccBtn, "active");
        document.getElementById("captions").style.display="none";
    }
}
