/*
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 yahoo event, dom

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

	return obj;

} ();

EOIR.SWF = function(el, strFlashId, strSWFUrl, strWidth, strHeight, params)
{
	var swf = new EOIR.SWF.Object(el, strFlashId);
	EOIR.SWF._register(strFlashId, swf);
	
	// Set the URL after registering instead of in the constructor to avoid issues with the FSCommand callback
	if(strSWFUrl != null)
	{
	    swf.setUrl(strSWFUrl, strWidth, strHeight, params);
	}
	
	return swf;
};

EOIR.SWF.ht = [];

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

EOIR.SWF._register = function(strName, swf)
{
	EOIR.SWF.ht[strName] = swf;
}

EOIR.SWF._unregister = function(strName, swf)
{
	EOIR.SWF.ht[strName] = null;
}

EOIR.SWF.destroy = function(strName)
{
    try
    {
        EOIR.SWF.get(strName).destroy();
    }
    catch(e){}
    
    EOIR.SWF._unregister(strName);
}

// el: the div container
// strFlashId: element id for the flash object
// strSWFUrl: swf url
// strWidth: width of flash object
// strHeight: height of flash object
// params: a hashtable { key: value, key: value, ... } 
EOIR.SWF.Object = function(el, strFlashId, strSWFUrl, strWidth, strHeight, params)
{
    if(el)
    {
        if(typeof el == "string")
        {
            el = document.getElementById(el);
        }
    }
    if(el == null)
    {
    }
    
    this.m_elem = el;
	this.m_strInteriorElemId = strFlashId == null ? YAHOO.util.Dom.generateId(null, strElem) : strFlashId;
	
	this.m_arrMovieClips = null;
	
    this.onEnd = new YAHOO.util.CustomEvent('end', this, true);
    this.onFSCommand = new YAHOO.util.CustomEvent('fscommand', this, true);
    this.m_nVolume = 50;
    
    this.m_bMediaStopped = false;
    
    this.m_bHideMode = false;
    
	this.setUrl = function(strSWFUrl, strWidth, strHeight, params)
	{
	    try
	    {
	        if(strWidth == null)
	            strWidth = "100%";
	        if(strHeight == null)
	            strHeight = "100%";
        	    
	        // set default parameters, these can be overwritten by params    
	        var paramsClone = [];
	        paramsClone["wmode"] = "transparent"; 
	        paramsClone["quality"] = "high"; 
	        paramsClone["bgcolor"] = "#000000"; 
	        paramsClone["allowscriptaccess"] = "always"; 
	        paramsClone["menu"] = "false"; 
	        
	        /*if(!allowWindowlessControls())
	        {
	            paramsClone["wmode"] = "window"; 
	        }
	        else
	        {
	            paramsClone["wmode"] = "transparent"; 
	        }*/
	        
	        for(var key in params)
	        {
	            paramsClone[key.toLowerCase()] = params[key];
	        }
        	
	        var strParams = ""; 
	        var strEmbed = "";
	        for(var key in paramsClone)
	        {
		        strParams += "<param name='" + key + "' value='" + paramsClone[key] + "' />\n";
		        strEmbed += key + "='" + paramsClone[key] + "' ";
	        }

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


	        var strHTML = "<object id='" + this.m_strInteriorElemId + "' classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' " + 
			                "width='" + strWidth + "' height='" + strHeight + "' " + 
			                "codebase='" + strProtocol + "//fpdownload.macromedia.com/get/flashplayer/current/swflash.cab'> " +
				            "<param name='movie' value='" + strSWFUrl + "' /> " + strParams +
				            "<embed name='" + this.m_strInteriorElemId + "' src='" + strSWFUrl + "' align='middle' " +
				            "type='application/x-shockwave-flash' width='" + strWidth + "' height='" + strHeight + "' " + 
				            strEmbed + " pluginspage='http://www.adobe.com/go/getflashplayer'/>" + 
				            "</object>";
			
			this.m_arrMovieClips = null;	
			this.m_bMediaStopped = false;            
			this.m_elem.innerHTML = strHTML;
        	
        	/*try
        	{
        	    if(getProperty("hidevideo")==true)
        	    {
        	        getNavWindow().onHideVideo(true);
        	    }
        	}
        	catch(e){}*/
	    }
	    catch(e)
	    {
	    }
	}
	
	this._getControl = function()
	{
	    if (navigator.appName.indexOf("Microsoft") != -1) 
	    {
            return window[this.m_strInteriorElemId];
        }
        else 
        {
            return document[this.m_strInteriorElemId];
        }
		return $(this.m_strInteriorElemId);
	}
    
    this._initializeFSCommandScripts = function()
    {
        var strScript = "<script type='text/javascript' for='" + this.m_strInteriorElemId + "' event='FSCommand(command,args)'></" + "script>";
	    var objScript = null;	
	    try
	    {
		    objScript = document.createElement(strScript);
	    }
	    catch(e)
	    {
		    objScript = document.createElement("script");
		    objScript.type = "text/javascript";
		    objScript.htmlFor = this.m_strInteriorElemId;
		    objScript.event = "FSCommand(command,args)";
	    }

	    try
	    {	
	        var strScriptText = this.m_strInteriorElemId + "_DoFSCommand(command, args);\n";
	        objScript.text = strScriptText;
	    }
	    catch(e)
	    {
	    }
	    
	    document.body.appendChild(objScript);
	    
	    strScript = "<script type='text/javascript'></script>";
	    objScript = null;	
	    try
	    {
		    objScript = document.createElement(strScript);
	    }
	    catch(e)
	    {
		    objScript = document.createElement("script");
		    objScript.type = "text/javascript";
	    }
	    
	    try
	    {	
	        var strScriptText = "function " + this.m_strInteriorElemId + "_DoFSCommand(command, args){ ";
	        strScriptText += "try { ";
	        strScriptText += "EOIR.SWF.get('" + this.m_strInteriorElemId +"').handleFSCommand(command, args);\n";
	        strScriptText += "} catch(e){ alert(e.description); }";
	        strScriptText += "}";
	        objScript.text = strScriptText;
	    }
	    catch(e)
	    {
	    }
	    
	    var docHead = document.getElementsByTagName("head")[0];   
	    docHead.appendChild(objScript);
    }
    
    this.hasAudio = function()
    {
        // all embedded audio SWFs should call the "movieClipsToPause" fscommand
        return (this.m_arrMovieClips != null);
    }
    
    this.updateAudioState = function(bPlaying)
    {
        try
        {
            var wnd = EOIR.Utils.getMainWindow();
            if(wnd != null)
            {
                wnd.updateAudioState(bPlaying);
            }
        }
        catch(e){}
    }
    
    this.initAudio = function(strMovieClips)
    {
        this.m_arrMovieClips = strMovieClips.split(",");
        for(var i=0; i<this.m_arrMovieClips.length; i++) 
        {
            if(this.m_arrMovieClips[i] != null) {
                this.m_arrMovieClips[i].replace(" ", ""); // remove spaces
            }
        }

	    this._setVolume();
	    if(EOIR.Utils.getProperty("audiomute",false) == true)
	    {
	        this.setMute(true);
	    }
	    
	    this.updateAudioState(true);
    }
    
    this._setVolume = function()
    {
        try
        {
            try
            {
                this._getControl().setVolume(this.m_nVolume);
            }
            catch(e)
            {
                //ExternalInterface calls are not working
                EOIR.SWF.Object.invokeFlashMethod(this._getControl(), "setVolume", [this.m_nVolume]);
            }
        }
        catch(e)
        {
        }
    }
    
    this.setVolume = function(nVolume)
    {
        if(nVolume >= 0 && nVolume <= 100)
        {
            this.m_nVolume = nVolume;
            this._setVolume();
        }
    }
    
    this.onMediaStopped = function()
    {
        this.m_bMediaStopped = true;
        this.onEnd.fire();
        
        if(this.hasAudio())
        {
            this.updateAudioState(false);
        }
    }
    
    this.pause = function()
    {
        try
        {
            var ctrlSWF = this._getControl();
            ctrlSWF.StopPlay();
            
            if(this.m_arrMovieClips != null && this.m_arrMovieClips.length > 0)
            {
                for (var i=0; i < this.m_arrMovieClips.length; i++) 
                {
                    ctrlSWF.TStopPlay(this.m_arrMovieClips[i]);
                }
            }
            
            if(this.hasAudio())
            {
                this.updateAudioState(false);
            }
        }
        catch(e){}
    }
    
    this.play = function()
    {
        try
        {
            if(this.isMediaStopped())
            {
                if(this.getTotalFrames() <= 1)
                {
                    this.m_elem.innerHTML = this.m_elem.innerHTML;
                    this.m_bMediaStopped = false;
                    return;
                }
                else
                {   
                    this._getControl().Rewind();
                }
            }
            
            var ctrlSWF = this._getControl();
            ctrlSWF.Play();
              
            if(this.m_arrMovieClips != null && this.m_arrMovieClips.length > 0)
            {
                for (var i=0; i < this.m_arrMovieClips.length; i++) 
                {
                    var strMovieClipPath = this.m_arrMovieClips[i];
                    var nframe = ctrlSWF.TCurrentFrame(strMovieClipPath); // Record the current frame
                    ctrlSWF.TGotoFrame(strMovieClipPath, nframe-1);       // Go back one frame
                    ctrlSWF.TPlay(strMovieClipPath);                      // Resume play
                }
            }
            
            this.m_bMediaStopped = false;
            
            if(this.hasAudio())
            {
                this.updateAudioState(true);
            }
        }
        catch(e){}
    }
    
    this.isMediaStopped = function()
    {
        if(!this.m_bMediaStopped)
        {
            try
            {
                if(!this.isPlaying() && (this.getTotalFrames() > 1 && this.getCurrentFrame()+1 >= this.getTotalFrames()))
                {
                    this.m_bMediaStopped = true;
                }
            }
            catch(e){}
        }
        return this.m_bMediaStopped;
    }
    
    this.getCurrentFrame = function()
    {
        var ctrlSWF = this._getControl();
        try
        {
            var nFrame = ctrlSWF.FrameNum;
            if(isNaN(nFrame))
            {
                nFrame = ctrlSWF.CurrentFrame();
            }
            return nFrame;
        }
        catch(e)
        {
            return ctrlSWF.CurrentFrame();
        }
    }
    
    this.getTotalFrames = function()
    {
        var ctrlSWF = this._getControl();
        try
        {
            var nFrames = ctrlSWF.TotalFrames;
            if(isNaN(nFrames))
            {
                nFrames = ctrlSWF.TotalFrames();
            }
            return nFrames;
        }
        catch(e)
        {
            return ctrlSWF.TotalFrames();
        }
    }
    
    this.isPlaying = function()
    {
        var ctrlSWF = this._getControl();
        try
        {
            var bPlaying = ctrlSWF.Playing;
            if(bPlaying == undefined)
            {
                bPlaying = ctrlSWF.IsPlaying();
            }
            return bPlaying;
        }
        catch(e)
        {
           return ctrlSWF.IsPlaying();
        }
    }
    
    this.handleFSCommand = function(command, args)
    {
        //alert("handleFSCommand: " + command + " " + args); 
        switch(command) 
        {
            case "movieClipsToPause":
                this.initAudio(args);
                break;
            case "mediaStopped":
                this.onMediaStopped(args);
                break;
            default:
                break;
        }
        
        this.onFSCommand.fire(command, args);
    }
    
    this.setMute = function(bMute)
    {
        try
        {
            try
            {
                if(bMute)
                {
                    this._getControl().mute();
                }
                else
                {
                    this._getControl().unmute();
                }
            }
            catch(e)
            {
                //ExternalInterface calls are not working
                EOIR.SWF.Object.invokeFlashMethod(this._getControl(), (bMute ? "mute" : "unmute"));
            }
        }
        catch(e)
        {
        }
    }
    
    this.destroy = function()
    {
        try
        {
            this.m_elem.removeChild(this._getControl());
        }
        catch(e)
        {
        }
    }
    
    this.isVisible = function()
    {
        return this.m_elem.style.display != "none";
    }
    
    this.hideVideo = function(bHide)
    {
        if(this.m_bHideMode != bHide)
        {
            this.m_bHideMode = bHide;
            if(bHide)
            {
                this.pauseAndHide();
            }
            else
            {
                this.resumeAndShow();
            }
        }
    }
    
    this.pauseAndHide = function()
    {
        try
	    {
	        this.pause();
	        this.m_elem.style.visibility = "hidden";
	    }
	    catch(e)
	    {
	    }		
    }

    this.resumeAndShow = function()
    {
	   
	    try 
	    {
	        if(!this.isMediaStopped())
	            this.play();
	        this.m_elem.style.visibility = "inherit";
	    }
	    catch(e)
	    {
	    }
    }
    
    this._initializeFSCommandScripts();
    if(strSWFUrl != null)
    {
        this.setUrl(strSWFUrl, strWidth, strHeight, params);
    }
   
};

/* if the flash object is added dynamically -- not via markup -- then
   the ExternalInterface doesn't seem to work, this seems to bypass that
   problem (from http://mihai.bazon.net/blog/flash-s-externalinterface-and-ie)
*/
EOIR.SWF.Object.invokeFlashMethod = function(objSWF, strMethodName, arrArgs)
{
    try
    {            
        var xmlArgs = "";
        if(arrArgs != null && arrArgs.length > 0)
        {
            try
            {
                xmlArgs = window.self.__flash__argumentsToXML(arrArgs, 0);
            }
            catch(e){}
        }
       
        var xml = '<invoke name=\"';
        xml += strMethodName;
        xml += '\" returntype=\"javascript\">';
        xml += xmlArgs;
        xml += '</invoke>';
        var funcRes = objSWF.callFunction(xml);
        if (funcRes != null && funcRes != "undefined" ) {
            return eval(funcRes);
        }
    }
    catch(e)
    {
    }
    
    return null;
}
