/* Tuxia STB related controls */

// initialize STB
function stbInit(setVideo, full)
{
	initSTB(STBVer);
	if (full)
	{
	  HideInactiveMousePointer();
		SetFullScreen();
	}
	else if (setVideo)
	{
		ShowInactiveMousePointer();
		SetTransparentColor(0, 0, 0);
		
		if (GetResolution() == "pal")
		  SetVideoLocationSize(443, 101, 190, 251);
		else
		  SetVideoLocationSize(442, 80, 146, 252);
	}
	else
	{
		NoVideo();
	}
}

// unintialize STB
function stbUninit()
{
	LogPlayerStop();
	stbStop();
	NoVideo();
}

function stbSetVideoTypeName(url, type, stream)
{
    if (null == url || null == type || null == stream || "" == url || "" == type || "" == stream)
        return;

    // retrieve video name and type    
    var pos = url.indexOf("://");
    if (-1 == pos)
        return;

    // live stream
    if ("Live" == stream || "Scheduled" == stream) 
    {
        var tag = url.substring(0, pos);
        var ipToken = "ip=", portToken = "port=";
        var len = url.indexOf("&");

        switch (tag)
        {
            case "vbricksys":		// mpeg1/2 multicast
                // url = "vbricksys://ip=239.22.123.103&port=2103&license=http://scottmdell/CombinedLicensedUser.lic"
                // curVidName = "239.22.123.103:2103"
                curVidName = getIp(url) + ":" + getPort(url);					 
                break;
            case "vbricksys4":		// mpeg4 multicast
                // url = "vbricksys4://buffer=[escaped sdp content]&license=http://scottmdell/CombinedLicensedUser.lic"
                // curVidName = "[escaped sdp content]"
                curVidName = url.substring(url.indexOf("buffer=") + 7, (len == -1) ? url.length : len);
                break;
            case "rtsp":			  // general vbrick mpeg4 rtsp
            case "vbrtsp":			// specific   "     "    "
                // url = "vbrtsp://239.22.123.103/vbrickvideo1&license=http://scottmdell/CombinedLicensedUser.lic"
                // curVidName = "239.22.123.103/vbrickvideo1"
                curVidName = url.substring(pos+3, (len == -1) ? url.length : len);
                break;
            case "http":			  // general vbrick mpeg4 http
            case "vbhttp":			// specific   "     "    "
                // url = "172.22.162.87/vbs1d1.sdp&license=http://scottmdell/CombinedLicensedUser.lic"
                // curVidName = "172.22.162.87/vbs1d1.sdp"
                curVidName = url.substring(pos+3, url.indexOf("&"));
                type ="SDP";
                break;
            case "vbrickunisys":     //vbrick unicast stream
                // url = "vbrickunisys://4444"
                // curVidName = "4444"
                curVidName = url.substring(pos+3, (len == -1) ? url.length : len);
                break;
        }
    }
    // video content (vod)
    else if ("Content" == stream)
    {
        curVidName = url.substring(url.indexOf("://") + 3, url.length);
    }

	curVidType = type;

    if ("" != curVidName && "" != type)
    {
        // remove rew/ff button if MPEG4 stream (not supported yet)
        if (!stbFullScreen && type.indexOf("MPEG-4") != -1)
        {
            document.getElementById("PlayerRewind").style.visibility='hidden';
            document.getElementById("PlayerFastForward").style.visibility='hidden';
            document.getElementById("PlayerControl").innerHTML = "<td width='5'>&nbsp;</td>";				
        }
        else
        {
            if(document.getElementById("PlayerRewind"))
            {
                document.getElementById("PlayerRewind").style.visibility='visible';
                document.getElementById("PlayerFastForward").style.visibility='visible';
                document.getElementById("PlayerControl").innerHTML = "";	
            }
        }
    }
    var videoType = getVidType(type, stream);
    var videoName = (type == "MPEG4") ? fnUnEscSDP(curVidName):curVidName;
    SetVideoTypeName(videoType, videoName);
}

// convert video type to STB accepted string
function getVidType(type, stream)
{
	var vidType = "";

	// from live stream
	if ("Live" == stream || "Scheduled" == stream) 
	{
		if ("MPEG1" == type)
		{
			vidType = "MPEG1Multicast";
		}
		else if ("MPEG2" == type)
		{
			vidType = "MPEG2Multicast";
		}
		else if ("MPEG4" == type)
		{
			vidType = "MPEG4Multicast";
		}
		else if ("RTSP4" == type)
		{
			vidType = "MPEG4Rtsp";
		}
		else if ("RTSP4_TCP" == type)
		{
			vidType = "MPEG4RtspForceTcp";
		}
		else if ("RTSP4_AUTO" == type)
		{
			vidType = "MPEG4RtspAuto";
		}
		else if ("SDP" == type)
		{
			vidType = "SDPFile";
		}
		else if ("UNICAST_MPEG1" == type)
		{    
			vidType = "UnicastMPEG1";
		}
		else if ("UNICAST_MPEG2_TRANSPORT" == type)
		{    
			vidType = "UnicastMPEG2Transport";
		}
  }
	// from vod server
	else if ("Content" == stream)
	{
		if ("MPEG-1" == type)
		{
			vidType = "MPEG1Kasenna";
		}
		else if ("MPEG-2" == type)
		{
			vidType = "MPEG2Kasenna";
		}
		else if ("PARTNER_41_MPEG-4" == type)
		{
			vidType = "MPEG4Rtsp";
		}
		else if ("MPEG-1I" == type)
		{
			vidType = "MPEG1Infovalue";
		}
		else if ("MPEG-2I" == type)
		{
			vidType = "MPEG2Infovalue";
		}
		else if ("MPEG-4I" == type)
		{
			vidType = "MPEG4Rtsp";
		}
		else if ("MPEG-4" == type)
		{
			vidType = "MPEG4Rtsp";
		}
	}
	return vidType;	
}

function stbOpen()
{
    Vod_Open();
}

function stbPlay()
{
    Vod_Play();
}

function stbPause()
{
    Vod_Pause();
}

function stbFForward()
{
    Vod_Fastforward();
}

function stbRewind()
{
    Vod_Rewind();
}

function stbStop()
{ 
	if (typeof(STBInit) != 'undefined')   
		Vod_Stop();
}
