/*
Program:
Husky Mounted Detection System (HMDS)

Source:
EOIR Technologies
PO Box 1240
Spotsylvania, VA  22553

HMDS Development Team
USA CECOM Night Vision and Electronic Sensors Directorate (NVESD)
10221 Burbeck Rd., STE 430
Fort Belvoir, VA 22060

Conditions for Use:
This software is Copyright(C) EOIR Technologies, Inc.

EOIR Technologies has all commercial interest and rights.
NVESD has full rights, title, and interest.

This software is provided "as is", without warranty by 
EOIR Technologies, Inc and NVESD. In no event shall EOIR 
Technologies, Inc. or NVESD be liable for any loss or for
any indirect, special, punitive, exemplary, incidental, or 
consequential damages arising from the use, possession, or
performance of this software.
*/

// Depends on sniffer2
// Depends on EOIR.Utils
// Depends on EOIR.External

var EOIR = EOIR ? EOIR : function()
{
	var obj =
	{
	};

	return obj;

} ();


EOIR.Video = function(strElem, strVideoUrl, bAutoPlay, bLoop, params)
{
	var vid = new EOIR.Video.Flash(strElem, strVideoUrl, bAutoPlay, bLoop, params);

//	var vid = new EOIR.Video.Dummy();
	EOIR.Video._register(strElem, vid);
	return vid;
};

EOIR.Video.ht = [];

EOIR.Video.get = function(strName)
{
	return EOIR.Video.ht[strName];
}

EOIR.Video._register = function(strName, vid)
{
	EOIR.Video.ht[strName] = vid;
}

EOIR.Video.getWMPHTML = function(htParams)
{
}

EOIR.Video.getFLVHTML = function(htParams)
{
}


EOIR.Video.Flash = function(strElem, strVideoUrl, bAutoPlay, bLoop, params)
{
	var m_elem = $(strElem);

	this.m_strElemId = strElem;
	this.m_strInteriorElemId = YAHOO.util.Dom.generateId(null, strElem);
	this._failCount = 0;
	
	this.m_bNotifyParent = true;
	this.m_fVolume = .75; // default level

	this.m_ht = [];
	this.set("::listeners", []);


	//	var strPath = EOIR.Utils.getPathFromMain("/swf/rocv_video_container.swf");
	var strPath = EOIR.Utils.getPathFromMain("/swf/mediacontainer.swf");

	var arrFlashVars = [];
	if(strVideoUrl != null)
	{
		strVideoUrl = EOIR.Video.Flash._fixUpUrl(strVideoUrl);
		arrFlashVars["videoName"] = encodeURIComponent(strVideoUrl);
	}

	if(bAutoPlay == null)
	{
		bAutoPlay = strVideoUrl != null;
	}

	if(bAutoPlay)
	{
		arrFlashVars["autoPlay"] = bAutoPlay;
	}

	if(bLoop)
	{
		arrFlashVars["loopPlayback"] = bLoop;
	}

	arrFlashVars["onready"] = "EOIR.Video.Flash._ready";
	arrFlashVars["onreadyparam"] = strElem;


	var strFlashVars = "";
	for(var key in arrFlashVars)
	{
		if(strFlashVars.length > 0)
		{
			strFlashVars += "&";
		}
		strFlashVars += key + "=" + arrFlashVars[key];
	}

	// Need to potentially add new flashvars values but don't want to modify the input array.
	var paramsClone = [];
	for(var key in params)
	{
		if(key.toLowerCase() == "flashvars" && strFlashVars.length > 0)
		{
			paramsClone["flashvars"] = strFlashVars + "&" + params[key];
		}
		else
		{
			paramsClone[key] = params[key];
		}
	}
	if(paramsClone["flashvars"] == null && strFlashVars.length > 0)
	{
		paramsClone["flashvars"] = strFlashVars;
	}

	if(m_elem.offsetHeight < 10)
	{
		strFlashVars = paramsClone["flashvars"] == null ? "" : paramsClone["flashvars"];
		if(strFlashVars.toLowerCase().indexOf("updatevideoprogress") == -1)
		{
			if(strFlashVars.length > 0)
			{
				strFlashVars += "&";
			}
			strFlashVars += "updateVideoProgress=0&updateInterval=20000";
			paramsClone["flashvars"] = strFlashVars;
		}
	}

	var strHTML;

	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

	if(isIE && isWin && !isOpera)
	{
		var strParams = "";
		for(var key in paramsClone)
		{
			strParams += "<param name='" + key + "' value='" + paramsClone[key] + "' />\n";
		}

		var strProtocol = "http:";
		try
		{
			strProtocol = (window.location.protocol == "https:") ? "https:" : "http:";
		}
		catch(e) { };


		strHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +
						'id="' + this.m_strInteriorElemId + '" width="100%" height="100%" ' +
						'codebase="' + strProtocol + '//fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> ' +
						'<param name="src" value="' + strPath + '" /> ' +
						strParams +
						'</object>';
	}
	else
	{
		var strParams = "";
		for(var key in paramsClone)
		{
			strParams += key + "=\"" + paramsClone[key] + "\" ";
		}

		strHTML = '<embed src="' + strPath + '" ' +
							'id="' + this.m_strInteriorElemId + '" align="middle" ' +
							'type="application/x-shockwave-flash" ' +
							'width="100%" ' +
							'height="100%" ' +
							strParams +
							'pluginspage="http://www.adobe.com/go/getflashplayer"> ' +
						'</embed>';
	}


	this._getControl = function()
	{
		return $(this.m_strInteriorElemId);
	}

	this._setUrl = function(strUrl)
	{
		var strUrl = EOIR.Video.Flash._fixUpUrl(strUrl);
		this._getControl().loadVideo(strUrl);
		
		var bMute = EOIR.Utils.getProperty("audiomute", false);
		this.setMute(bMute);
		
		return true;
	}

	this.setUrl = function(strUrl)
	{
		try
		{
			if(this.m_bReady)
			{
				this._setUrl(strUrl);
			}
			else
			{
				this.set("::url", strUrl);
			}
		}
		catch(e)
		{
		}
	}

	this.play = function(strUrl)
	{
		try
		{
			if(strUrl != null)
			{
				this.setUrl(strUrl);
			}

			if(this.m_bReady)
			{
				this._getControl().playVideo();
			}
			else
			{
				this.set("::play", true);
			}
		}
		catch(e)
		{
		}
	}

	this.stop = function()
	{
		try
		{
			this._getControl().stopVideo();
		}
		catch(e)
		{
			this.set("play", false);
		}
	}
	
	this.pause = function()
	{
		try
		{
			this._getControl().pauseVideo();
		}
		catch(e)
		{
			this.set("play", false);
		}
	}

	this.setVolume = function(vol)
	{
		try
		{
			if(this.m_bReady)
			{
				this._getControl().setVolume(vol);
			}
			else
			{
				this.set("::volume", vol);
			}
		}
		catch(e)
		{
		}
	}
	
	this.setMute = function(bMute)
    {
        try
        {
            if(bMute)
            {                
                this.setVolume(0);
            }
            else
            {
                this.setVolume(this.m_fVolume);
            }
        }
        catch(e)
        {
        }
    }
    
	this.handleVideoStateChange = function(strState)
	{
	    if(this.m_bNotifyParent)
	    {
	        try
            {
                //alert("videostate::" + strState);
                if(strState == "playing")
                {
                    EOIR.Utils.getMainWindow().updateAudioState(true);
                }
                else if(strState == "stopped" || strState == "paused")
                {
                    EOIR.Utils.getMainWindow().updateAudioState(false);
                }
            }
            catch(e){}
        }
	}

	this._addListener = function(strEvt, fn, obj)
	{
		try
		{
			if(this.m_bReady)
			{
				EOIR.External.debugWrite("_addListener " + fn + "  " + obj + "\n");
				$(this.m_strInteriorElemId).addListener(strEvt, fn, this.m_strElemId);
			}
		}
		catch(e)
		{
		}
	}

	this.addListener = function(strEvt, fn, obj)
	{
		// Need to keep our own copy of this stuff b/c the Flash object may not be ready for it yet.
		var ht = this.get("::listeners");
		if(ht[strEvt] == null)
		{
			ht[strEvt] = [];
		}
		ht[strEvt].push({ func: fn, param: obj });
		this.set("::listeners", ht);

		this._addListener(strEvt, fn, obj);
	}

	this.removeListener = function(strEventName, strFn)
	{
		try
		{
			if(this.m_bReady)
			{
				this._getControl().removeListener(strEventName, strFn);
			}
			else
			{
				var ht = this.get("::listeners");
				if(ht[strEventName] != null)
				{
					for(var i = 0; i < ht[strEventName].length; i++)
					{
						if(ht[strEventName].func == strFn)
						{
							ht[strEventName].splice(i, 1);
							return;
						}
					}
				}
			}
		}
		catch(e)
		{
		}
	}

	m_elem.innerHTML = strHTML;
	if(strVideoUrl != null)
	{
		var bMute = EOIR.Utils.getProperty("audiomute", false);
		if(bMute)
		{
		    this.setVolume(0);
		}
	}
	this.addListener("videoState", "EOIR.Video.Flash._videoState");
};

EOIR.Video.Flash._videoState = function(strState, strElem)
{
    try
	{
		var vid = EOIR.Video.get(strElem);
		if(vid)
		{
		    vid.handleVideoStateChange(strState);
		    /*if(vid.m_bNotifyParent)
		    {
		        try
                {
                    YAHOO.util.Dom.getElementsByClassName("PageTitle")[0].innerHTML = "Video State:" + strState;
                }
                catch(e){}
		    }*/
		}
	}
	catch(e)
	{
	}
}

EOIR.Video.Flash.prototype.setVisible = function(bShow)
{
	$(this.m_strInteriorElemId).style.visibility = bShow ? "inherit" : "hidden";
}
/*
EOIR.Video.Flash._fixUpUrl = function(strUrl) 
{
	strUrl = EOIR.Utils.replaceExtension(strUrl, "flv");

	try 
	{
		if (EOIR.Utils.isInROCBrowser() || EOIR.Utils.isInROCBrowserGecko()) {
			
			strUrl = EOIR.Utils.downloadUrl(strUrl, true);
			alert(" _fixUpUrl |" + strUrl);
			strUrl = "file:///"+strUrl.replace(/\\/g, "/");

			var nIndex = strUrl.lastIndexOf("/");
			if (nIndex != -1) {
				strUrl = strUrl.substr(0, nIndex + 1) + encodeURIComponent(strUrl.substr(nIndex + 1));
			}
			EOIR.External.debugWrite("Cached url " + strUrl + "\n");
		}

		//		strUrl = strUrl.replace(/\\x/g, "/x").replace(/\\u/g, "/u");
	}
	catch (e) {
				alert("ROC.Video.Flash._fixUpUrl error: " + e.description);
	}

	return strUrl;
}
*/
EOIR.Video.Flash._fixUpUrl = function(strUrl)
{
	strUrl = EOIR.Utils.replaceExtension(strUrl, "flv");

	try
	{
		if(EOIR.Utils.isInROCBrowser() || EOIR.Utils.isInROCBrowserGecko())
		{
			strUrl = EOIR.Utils.cacheUrl(strUrl);
			strUrl = strUrl.replace(/\\/g, "/");
//			var nIndex = strUrl.lastIndexOf("/");
//			if(nIndex != -1)
//			{
//				strUrl = strUrl.substr(0, nIndex + 1) + encodeURIComponent(strUrl.substr(nIndex + 1));
//			}
			EOIR.External.debugWrite("Cached url " + strUrl + "\n");
		}

		//		strUrl = strUrl.replace(/\\x/g, "/x").replace(/\\u/g, "/u");
	}
	catch(e)
	{
		//		alert("EOIR.Video.Flash._fixUpUrl error: " + e.description);
	}

	return strUrl;
}

EOIR.Video.Flash.prototype.set = function(key, val)
{
	this.m_ht[key] = val;
}

EOIR.Video.Flash.prototype.get = function(key, def)
{
	var val = this.m_ht[key];
	if(val == null)
	{
		val = def;
	}
	return val;
}

EOIR.Video.Flash._ready = function(strElem)
{
	YAHOO.lang.later(10, this, function()
	{
		try
		{
			var vid = EOIR.Video.get(strElem);
			if(vid)
			{
			    vid._initialize();
			}
		}
		catch(e)
		{
			alert("Flash video ready error: " + e.description);
		}
	});
}

EOIR.Video.Flash.prototype._initialize = function()
{
	EOIR.External.debugWrite("Initializing " + this.m_strInteriorElemId + "\n");

	this.m_bReady = true;

	var ht = this.get("::listeners");
	for(var key in ht)
	{
		for(var i = 0; i < ht[key].length; i++)
		{
			this._addListener(key, ht[key][i].func, ht[key][i].param);
		}
	}

	if(this.get("::volume")!=null)
	{
		this.setVolume(this.get("::volume"));
	}
	if(this.get("::play"))
	{
		this.play(this.get("::url"));
	}
	else
	{
		if(this.get("::url"))
		{
			this.setUrl(this.get("::url"));
		}
	}
}

EOIR.Video.Dummy = function(strElem, strVideoUrl, bAutoPlay, bLoop, params)
{
	this.setUrl = function(strUrl)
	{
	}

	this.play = function(strUrl)
	{
	}

	this.stop = function()
	{
	}

	this.addListener = function(strEventName, strFn)
	{
	}

	this.removeListener = function(strEventName, strFn)
	{
	}
};

EOIR.Video.Dummy.prototype.setVisible = function(bShow)
{
}
