/*
Create a flash movie object
This object provides the functionality to create a flash object and allows for that flash movie to communicate back and forth with javascript with 
the following caveats:

It is possible to communicate back and forth between flash and javascript, but the communication aspect from javascript to flash does not currently
work on mac osx. Examples of how this communication functions follow. Communication from flash to javascript is also troublesome, but possible. The
caveat here is that fscommands do not work on Mac, but to get around this you can simply call javascript using a getURL command (examples follow).
However, MSIE will allow no more than 468 characters to be passed in this fashion and thus must take advantage of fscommand.

To communicate from flash to javascript, simply add any functions that you need to your FlashObject instances and then call those functions in your
movie using a getUrl or fscommand call. To communicate from javascript back to flash, simply call one of the functions available to javascript
on your flash movie object. For a complete list of the functions available to javascript, check the macromedia site at: http://www.macromedia.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html

ex:
-- communication from javascript to flash (available on gekco and ie browsers for pc, but not available at all on mac as of this writing) --
myFlash = new FlashObject( "/flash/myFlash.swf", "myFlash", 100, 100 );
myFlash.myFunction1 = function()
{ 
	// send information to flash --> find more available functions at http://www.macromedia.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_03.html
	this.objMovie.SetVariable("/objName/myObject:variableName", "variableValue" );
};

-- communication to javascript from flash --
1. // available to all browsers, but with a maximum number of argument characters in MSIE of 468
getUrl( "javascript:myFlash.myFunction2( arg1, arg2 );" );

2. // not available to mac osx browsers, but available to all pc gecko browsers and MSIE
fscommand( "myFunction2", "myFlash,arg1,arg2" );

--------------------------------------------------------------------------------------------------
*/

/*
@argument : varLoc (string) : the file and location of the file to add to your page
@argument : varName (string) : the name of the object/embed to add to your page
@argument: varNode (node) : the node to which to attach this object
@argument : varW (number) : width of movie
@argument : varH (number) : height of movie
@argument : varColor (string) : (optional) the color of the background to use
*/
com.soulfresh.FlashObject = function( varLoc, varName, varNode, varW, varH, varColor )
{
	
	// store properties
	this.strName = varName;
	this.intWidth = varW;
	this.intHeight = varH;
	this.strLocation = varLoc;
	this.objContainer = varNode;
	
	// if no color was passed
	if (varColor == undefined)
		this.strColor = "#000000";
	else
		this.strColor = varColor;
	
	// create the object
	this.funWriteObject();
	
	// set reference to the flash object
	this.funFindMovie();
	
	// write fscommand functionality
	this.funWriteFS();
	
}


/*
Return a reference to the movie embeded in the html page. 
MSIE uses the object tag and can be found with the id "bannerNameIE" while all other browsers will use the embed tag "bannerName"
*/
com.soulfresh.FlashObject.prototype.funFindMovie = function()
{
	
  // IE and Netscape refer to the movie object differently.
  // This function returns the appropriate syntax depending on the browser.
	var varObject = document.getElementById( this.strName );
	
	if ( intMSIE )
		this.objMovie = varObject;
	else
	{
		// declare variables
		var varNodeList = varObject.getElementsByTagName( "embed" );
		var varTotal = varNodeList.length;
		
		// loop
		for ( var i = 0; i < varTotal; i++ )
		{
			// declare variables
			var varNodeName = varNodeList[i].name;
			
			// if the same
			if ( varNodeName == this.strName )
				this.objMovie = varNodeList[i]; // return the correct node
		}
	}
	
}


/*
Checks to see if a FlashObject has completely loaded. This must be true before you can communicate with this object.
@argument : varMovie (object) : reference to the movie
@return : boolean : true = has finished loading, false = has not finished loading
*/
com.soulfresh.FlashObject.prototype.funIsLoaded = function ()
{

  // First make sure the movie's defined.
  if ( typeof(theMovie) != "undefined" )
    return theMovie.PercentLoaded() == 100; // If it is, check how much of it is loaded.
  else
    return false; // If the movie isn't defined, it's not loaded.
	
}


/*
Write the flash object to the document
*/
com.soulfresh.FlashObject.prototype.funWriteObject = function()
{
	
	// if flash is installed
	if ( intFlashCanPlay )
	{
		// embed flash object
		var varBanner = '<object id="' + this.strName + '" name="' + this.strName + '" width="' + this.intWidth + '" height="' + this.intHeight + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" type="application/x-shockwave-flash" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0">';
		varBanner += '<param name="allowScriptAccess" value="sameDomain" />';
		varBanner += '<param name="movie" value="' + this.strLocation + '" />';
		varBanner += '<param name="quality" value="high" />';
		varBanner += '<param name="bgcolor" value="' + this.strColor + '" />';
		varBanner += '<embed src="' + this.strLocation + '" quality="high" width="' + this.intWidth + '" height="' + this.intHeight + '" name="' + this.strName + '" bgcolor="' + this.strColor + '" swLiveConnect="true" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
		varBanner += '</object>';
	}
	else
	{
		// write image
		var varBanner = '<a class="imgLink" href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" rel="external">';
		varBanner += 'Download the Flash plugin</a>';
	}
	
	// write html
	if ( this.objContainer == null )
		document.write( varBanner );
	else
		this.objContainer.innerHTML = varBanner;

}


/*
Write the fscommand functionality that allows this FlashObject to communicate back and forth between javascript
*/
com.soulfresh.FlashObject.prototype.funWriteFS = function()
{
	
	// declare variables
	var varFSCommand = this.strName + "_DoFSCommand";
	var varVBSub = this.strName + "_FSCommand";
	
	// Handle all the FSCommand messages from this FlashObject by adding fscommand functionality to the window.
	// For this to work, you will have to add functions to your instance object and then pass the name of the object to the fscommand
	// as the first item in the comma delimited string that is varArgs (second argument passed to fscommand function)
//	banners_DoFSCommand = function(varCommand, varArgs)
	window[ varFSCommand ] = function( varCommand, varArgs )
	{		
		// get arguments
		var varArgs = varArgs.split(",");
		var varMovie = window[ varArgs[0] ];
		var varNewArgs = varArgs.slice( 1, varArgs.length );
		
		// call the fscommand
		varMovie[ varCommand ]( varNewArgs );	
	}

	// Hook for Internet Explorer.
	if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) 
	{
		document.write('<script language=\"VBScript\"\>\n');
		document.write('On Error Resume Next\n');
		document.write('Sub ' + varVBSub + '(ByVal command, ByVal args)\n');
		document.write('	Call ' + varFSCommand + '(command, args)\n');
		document.write('End Sub\n');
		document.write('</script\>\n');
	}
	
}

