﻿var CS = window.CS || {};
CS.DomObjectSizes = {};
CS.DOS = CS.DomObjectSizes;

CS.namespace = function(ns) {

    if (!ns || !ns.length) {
        return null;
    }

    var levels = ns.split(".");
    var nsobj = CS;

    // CS is implied, so it is ignored if it is included
    for (var i=(levels[0] == "CS") ? 1 : 0; i<levels.length; ++i) {
        nsobj[levels[i]] = nsobj[levels[i]] || {};
        nsobj = nsobj[levels[i]];
    }

    return nsobj;
};

CS.namespace('webcast');
CS.namespace('webcast.externalcontent');
CS.namespace('webcast.real');
CS.namespace('webcast.wmp');

var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $A = YAHOO.util.Anim;
var $K = YAHOO.util.KeyListener;
var $ = $D.get;

CS.webcast.externalcontent.ContentContainerID = null;

CS.init = function() {
	var pl = $("PageLayover");
	
	if (pl)
		$D.setStyle(pl, "height", (document.body.offsetHeight) + 'px');
	
	CS.webcast.externalcontent.init();
};

CS.resize = function() {
	var cc = $(CS.webcast.externalcontent.ContentContainerID);
	var ech = $("ExternalContentHeader");
	var ecw = $("ExternalContentWrapper");
	var ecf = $("ExternalContentFrame");
	var pl = $("PageLayover");

	if (!(ecw) || !(cc) || !(ech))
		return;
	
	$D.setStyle(ecw, "width", (cc.offsetWidth - 30) + 'px');
	if (ecf)
		$D.setStyle(ecf, "height", (ecw.offsetHeight - ech.offsetHeight - 10) + 'px');
	if (pl)
		$D.setStyle(pl, "height", (document.body.offsetHeight) + 'px');
};


$E.addListener(window, 'load', CS.init);
$E.addListener(window, 'resize', CS.resize);

CS.webcast.externalcontent.init = function()
{
	var ecw = $("ExternalContentWrapper");
	
	if (!ecw)
		return;

	//$D.setStyle(ecw, "opacity", "0");
};

CS.webcast.externalcontent.close = function ()
{
	var cc = $(CS.webcast.externalcontent.ContentContainerID);
	var ecc = $("ExternalContentContainer");
	var ech = $("ExternalContentHeader");
	var echa = $("ExternalContentHeaderAddress");
	var echt = $("ExternalContentHeaderTitle");
	var ecw = $("ExternalContentWrapper");
	var pl = $("PageLayover");
	
	if (!(ecw) || !(ecc) || !(cc) || !(ech) || !(echt) || !(echa))
		return;
	
	echt.innerHTML = "";
	echa.innerHTML = "";
	ecc.innerHTML = "";

	$D.setStyle(ecw, "display", "none");
	$D.setStyle(pl, "display", "none");
};

CS.webcast.externalcontent.open = function(strHeaderText, strContentURL, strContentContainerID)
{
    window.open(strContentURL,'ContentWindow','left=20,top=20,width=800,height=600,scrollbars=1,toolbar=0,resizable=1');
} 

//deprectated until bugs with IE are worked out
CS.webcast.externalcontent.open_old = function(strHeaderText, strContentURL, strContentContainerID)
{
	
	
	
	var cc = $(strContentContainerID);
	var ecc = $("ExternalContentContainer");
	var ech = $("ExternalContentHeader");
	var echa = $("ExternalContentHeaderAddress");
	var echt = $("ExternalContentHeaderTitle");
	var ecw = $("ExternalContentWrapper");
	var pl = $("PageLayover");
	
	var keyListener = new $K(document, {keys: [27]}, CS.webcast.externalcontent.close);
	keyListener.enable();
	
	CS.webcast.externalcontent.ContentContainerID = strContentContainerID;
	    
	if (!(ecw) || !(ecc) || !(cc) || !(ech) || !(echt) || !(echa))
		return;
	
	echt.innerHTML = "";
	echa.innerHTML = "";
	ecc.innerHTML = "";
	
	echt.innerHTML = strHeaderText;
	echa.innerHTML = '<a href="' + strContentURL + '" target="_blank">' + strContentURL + '</a>';
	
	$D.setStyle(ecw, "display", "block");
	$D.setStyle(pl, "display", "block");
	
	ecc.innerHTML = '<iframe id="ExternalContentFrame" src="' + strContentURL + '" frameborder="0" width="99%" height="' + (ecw.offsetHeight - ech.offsetHeight - 10) + '"></iframe>';


};

CS.webcast.playersDetected = false;

CS.webcast.detectPlayers = function ()
{
	if (CS.webcast.playersDetected)
		return;
	// Real Player
	try
	{
		if(window.ActiveXObject)
		{
			CS.webcast.real.activex = true;
			//check for ActiveX RP
			CS.webcast.real.Rplayer = new ActiveXObject("rmocx.RealPlayer G2 Control");
		}
	} 
	catch(oError) 
	{ 
		CS.webcast.real.activex = false;
	}

	try
	{
		if(navigator.mimeTypes)
		{
			CS.webcast.real.Rplugin = navigator.mimeTypes['audio/x-pn-realaudio-plugin'].enabledPlugin;
		}
	}
	catch (oError) {}
	CS.webcast.real.Rplugin = CS.webcast.real.Rplugin ? true : false;
	
	//Windows Media Player (wmp)
	try
	{
		if(window.ActiveXObject)
		{
			CS.webcast.wmp.activex = true;
			//check for ActiveX WMP
			CS.webcast.wmp.WMplayer = new ActiveXObject("WMPlayer.OCX.7");
		}
		else if (window.GeckoActiveXObject)
		{
			CS.webcast.wmp.activex = true;    
			CS.webcast.wmp.WMplayer = new GeckoActiveXObject("WMPlayer.OCX.7");
		}
	} 
	catch(oError) 
	{ 
		CS.webcast.wmp.activex = false;
	}

	try
	{
		if(navigator.mimeTypes)
		{
			CS.webcast.wmp.WMplugin = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin; 
		} 
	}
	catch (oError) {}
	
	CS.webcast.playersDetected = true;
};

CS.webcast.checkPlayer = function(strStreamType)
{
	if (strStreamType.toLowerCase() == "wmp" && (!CS.webcast.wmp.WMplayer && !CS.webcast.wmp.WMplugin))
	{
		if (window.confirm("Windows Media Player has NOT been Detected.  Press OK to run the System Check or Cancel to ignore and continue"))
			window.location.replace('http://www.visualwebcaster.com/syscheck/systemcheck.html');
	}
	if (strStreamType.toLowerCase() == "real" && (!CS.webcast.real.Rplayer && !CS.webcast.real.Rplugin))
	{
		if (window.confirm("Real Media Player has NOT been Detected.  Press OK to run the System Check or Cancel to ignore and continue"))
			window.location.replace('http://www.visualwebcaster.com/syscheck/systemcheck.html');
	}
};

CS.webcast.close = function (strContentContainerID)
{
	var cc = $(strContentContainerID);
	var ecw = $("ExternalContentWrapper");
	var wpc = $("WebcastPlayerContainer");
	var wph = $("WebcastPlayerHeader");
	var wpw = $("WebcastPlayerWrapper");
	var attributes;
	var anim;
	
	if (!(wpw) || !(wpc) || !(cc) || !(wph))
		return;
	
	wpc.innerHTML = "";
	$D.setStyle(wph, "display", "none");
	
	attributes = {
		height: {
			from : wpw.DOS.height,
			to : 0
		},
		width: {
			from : wpw.DOS.width,
			to : 0
		}
	};
	
	anim = new $A(wpw, attributes, 0.5, YAHOO.util.Easing.easeOut);
	
	anim.animate();
	
	anim.onComplete.subscribe(
		function()
		{
			$D.setStyle(cc, "marginRight", "0");
			$D.setStyle(ecw, "width", "99%");
			$D.setStyle(wpw, "height", "0");
			$D.setStyle(wpw, "width", "0");
			$D.setStyle(wpw, "visibility", "hidden");
		}
	);
}

CS.webcast.open = function (objThis, intStreamSize, strStreamType, strStreamProvider, strContentContainerID, intSID, intAID)
{
	CS.webcast.detectPlayers();
	CS.webcast.checkPlayer(strStreamType);

	var cc = $(strContentContainerID);
	var ecw = $("ExternalContentWrapper");
	var pl = $("PageLayover");
	var wpc = $("WebcastPlayerContainer");
	var wph = $("WebcastPlayerHeader");
	var wpht = $("WebcastPlayerHeaderTime");
	var wphtd = $("WebcastPlayerHeaderTimeDescription");
	var wpw = $("WebcastPlayerWrapper");
	var attributes;
	var anim;
	var loopObj;
	var strStreamURL;
	
	if (!(wpw) || !(wpc) || !(cc) || !(wph) || !(wpht) || !(wphtd))
		return;
	
	$D.setStyle(wph, "display", "none");
	
	wpht.innerHTML = "";
	wphtd.innerHTML = "";

	var arryObj = objThis.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("div");
	
	for (var i = 0; i < arryObj.length; i++)
	{
		loopObj = arryObj[i];
		switch (loopObj.className.toLowerCase())
		{
			case "agendatime":
				if (loopObj.textContent)
					wpht.innerHTML = loopObj.textContent;
				else
				{
					if (loopObj.innerText)
						wpht.innerHTML = loopObj.innerText;
					else
						wpht.innerHTML = "";
				}
				break;
			case "agendatimedescription":
			case "description textleft":
			case "description":
				if (loopObj.textContent)
					wphtd.innerHTML = loopObj.textContent;
				else
				{
					if (loopObj.innerText)
						wphtd.innerHTML = loopObj.innerText;
					else
						wphtd.innerHTML = "";
				}
				break;
		}
	}
	
	wpc.innerHTML = "";

	if (!(CS.DOS[wpw.id]))
	{
		CS.DOS[wpw.id] = {};
		wpw.DOS = CS.DOS[wpw.id];
	}
	
	switch (intStreamSize)
	{
		case 640: // video = 640x480
			CS.DOS[wpw.id].height = 585;
			CS.DOS[wpw.id].width = 650;
			if ($D.getViewportHeight() < 600)
			{
				CS.DOS[wpw.id].height = $D.getViewportHeight() - 25;
				//CS.DOS[wpw.id].width = $D.getViewportWidth() * .5;
				CS.DOS[wpw.id].width = (CS.DOS[wpw.id].height - 100) * 1.3335;
			}
			break;
		case 480: // video = 480x360
			CS.DOS[wpw.id].height = 485;
			CS.DOS[wpw.id].width = 490;
			if ($D.getViewportHeight() < 500)
			{
				CS.DOS[wpw.id].height = $D.getViewportHeight() - 25;
				//CS.DOS[wpw.id].width = $D.getViewportWidth() * .5;
				CS.DOS[wpw.id].width = (CS.DOS[wpw.id].height - 100) * 1.3335;
			}
			break;
		case 240: // video = 240x180
		default:
			CS.DOS[wpw.id].height = 320;
			CS.DOS[wpw.id].width = 250;
			break;
	}
	
	$D.setStyle(wpw, "height", "0");
	$D.setStyle(wpw, "width", "0");
	$D.setStyle(wpw, "visibility", "visible");
	
	attributes = {
		height: {
			from: 0,
			to: wpw.DOS.height
		},
		width: {
			from: 0,
			to: wpw.DOS.width
		}
	};
	
	anim = new $A(wpw, attributes, 0.5, YAHOO.util.Easing.easeIn);
	switch (strStreamType.toLowerCase())
	{
		case "real":
			switch (strStreamProvider.toLowerCase())
			{
				case "vdat":
					strStreamURL = "http://video.vdat.com/playmedia_embed2.asp?sid=" + intSID + "&aid=" + intAID;
					break;
				case "rbn":
					strStreamURL = "http://play.rbn.com/?url=shareholder/shareholder/demand/" + intSID + "tccn.ra&proto=rtsp";
					break;
				default:
					strStreamURL = strStreamProvider;
					break;
			}
			anim.onComplete.subscribe(
				function()
				{
					switch (intStreamSize)
					{
						case 640: // video = 640x480
							CS.webcast.real.embed(wpc, wpw.DOS.height - 105, wpw.DOS.width - 10, strStreamURL);
							break;
						default:
							CS.webcast.real.embed(wpc, wpw.DOS.height - 125, wpw.DOS.width - 10, strStreamURL);
							break;
					}
					$D.setStyle(wph, "display", "");
					$D.setStyle(ecw, "width", (cc.offsetWidth - 30) + 'px');
				}
			);
			break;
		case "wmp":
		default:
			switch (strStreamProvider.toLowerCase())
			{
				case "vdat":
					strStreamURL = "http://video.vdat.com/playmedia_embed2.asp?sid=" + intSID + "&aid=" + intAID;
					break;
				case "rbn":
					strStreamURL = "http://play.rbn.com/play.asx?url=shareholder/shareholder/wmdemand/" + intSID + "tccn.asf&proto=mms?mswmext=.asx";
					break;
				default:
					strStreamURL = strStreamProvider;
					break;
			}
			anim.onComplete.subscribe(
				function()
				{
					switch (intStreamSize)
					{
						case 640: // video = 640x480
							CS.webcast.wmp.embed(wpc, wpw.DOS.height - 60, wpw.DOS.width - 10, strStreamURL);
							break;
						default:
							CS.webcast.wmp.embed(wpc, wpw.DOS.height - 95, wpw.DOS.width - 10, strStreamURL);
							break;
					}
					$D.setStyle(wph, "display", "");
					$D.setStyle(ecw, "width", (cc.offsetWidth - 30) + 'px');
				}
			);
			break;
	}
	anim.animate();
	
	$D.setStyle(cc, "marginRight", (CS.DOS[wpw.id].width + 10) + 'px');
	
	if (pl)
		$D.setStyle(pl, "height", (document.body.offsetHeight) + 'px');
};

CS.webcast.wmp.embed = function (objContainer, intHeight, intWidth, strStreamURL)
{
	var strWinPlayer = '';

	if (!CS.webcast.wmp.activex)
		strWinPlayer = '<embed type="application/x-mplayer2" Name="MediaPlayer" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" src="' + strStreamURL + '" height="' + intHeight + '" width="' + intWidth + '" showdisplay="0" showpositioncontrols="0" showstatusbar="1"></embed>';

	objContainer.innerHTML = '<object classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" height="' + intHeight + '" width="' + intWidth + '" id="MediaPlayer" type="application/x-oleobject" standby="Loading Microsoft Windows Media Player components..." hspace="0" vspace="0">'
			+ '<param name="FileName" value="' + strStreamURL + '" />'
			+ '<param name="ShowDisplay" value="0" />'
			+ '<param name="ShowPositionControls" value="0" />'
			+ '<param name="ShowStatusBar" value="1" />'
			+ strWinPlayer
		+ '</object>';
};

CS.webcast.real.embed = function (objContainer, intHeight, intWidth, strStreamURL)
{
	objContainer.innerHTML = '<object id="realplayer" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="' + intHeight + '" width="' + intWidth + '">'
			+ '<param name="SRC" value="' + strStreamURL + '" />'
			+ '<param name="CONSOLE" value="one" />'
			+ '<param name="CONTROLS" value="imagewindow" />'
			+ '<param name="AutoStart" value="true" />'
			+ '<embed name="realplayer" type="audio/x-pn-realaudio-plugin" src="' + strStreamURL + '" height="' + intHeight + '" width="' + intWidth + '" controls="imagewindow" console="one" autostart="true" />'
		+ '</object>'
		+ '<br />'
		+ '<object id="realplayer1" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="30" width="' + intWidth + '">'
			+ '<param name="CONSOLE" value="one" />'
			+ '<param name="CONTROLS" value="ControlPanel" />'
			+ '<embed name="realplayer1" type="audio/x-pn-realaudio-plugin" height="30" width="' + intWidth + '" controls="ControlPanel" console="one" />'
		+ '</object>'
		+ '<br />'
		+ '<object id="realplayer2" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" height="30" width="' + intWidth + '">'
			+ '<param name="CONSOLE" value="one" />'
			+ '<param name="CONTROLS" value="PositionField" />'
			+ '<embed name="realplayer2" type="audio/x-pn-realaudio-plugin" height="30" width="' + intWidth + '" controls="PositionField" console="one" />'
		+ '</object>';
};