var g_pTimer = null;
var g_sCookieName = "";

function ACG_GetDomain()
{
	var sFullDomain = document.domain;

	aSplit = sFullDomain.split( "." );

	if ( aSplit.length >= 2 )
	{
		return aSplit[ aSplit.length-2 ] + "." + aSplit[ aSplit.length-1 ] 
	}
	return "";
}

function ACG_SaveCurrentPosition()
{
	if ( document.all.myflash == undefined )
	{
		g_pTimer.Kill();
		return;
	}

	var iCurrentFrame = document.all.myflash.FrameNum;

	//
	// If SWF is loaded and current on frames 1-3, it must
	// be waiting until the next startframe is available.
	// So, we must exit here to avoid resetting the current frame.
	//
	if ( iCurrentFrame <= 3 )
	{
		return;
	}

	//
	// SWF is loaded and content is playing. Save state in cookie...
	//
	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, ACG_GetDomain() );
//	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, null );

	_myCookie.Load();

	if ( document.all.myflash.ReadyState == 4 )
	{
		if ( iCurrentFrame >= (document.all.myflash.TotalFrames-1) )
		{
			_myCookie.Complete = true;

			//
			// If flash is done playing, we no longer need
			// the timer to keep saving position...
			//
			g_pTimer.Kill();
		}
	}
	_myCookie.CurrentFrame = iCurrentFrame;
//alert( iCurrentFrame );

	//
	// Save the cookie
	//
	_myCookie.Save();
}

function ACG_SetStartFrame()
{
	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, ACG_GetDomain() );
//	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, null );

	_myCookie.Load();

	if ( _myCookie.CurrentFrame == undefined )
		iStartFrame = 0;
	else
		iStartFrame = _myCookie.CurrentFrame;

//alert( iStartFrame );

	document.all.myflash.SetVariable( "g_sStartFrame", iStartFrame );

	//
	// Setup polling timer to save flash state...
	//
	g_pTimer = new ACG_Timer( "ACG_SaveCurrentPosition();", 1000, true );
}

function ACG_IncludeMediaElement( sUrl, sAltUrl, iWidth, iHeight, sUniqueId )
{
	//
	// Save cookie name for future reference. Note that the
	// cookie name must be unique, so we will include the media prefix.
	//
	g_sCookieName = "ACG" + sUniqueId + "Cookie";


	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, ACG_GetDomain() );
//	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, null );

	_myCookie.Load();

	if ( _myCookie.Complete )
	{
		ACG_ImageIBM( "myimage", sAltUrl, iWidth, iHeight, "usemap='#mymap'" );
		ACG_ImageMapIBM( "mymap", "400, 178, 440, 198", "ACG_OnReplay();" );
		return;
	}

//	ACG_FlashIBM( "myflash", sUrl, iWidth, iHeight, { bgcolor: "#ffffff", play: "1" } );
	ACG_FlashIBM( "myflash", sUrl, iWidth, iHeight );
}

var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
// Handle all the the FSCommand messages in a Flash movie
function myflash_DoFSCommand(command, args) {
	var myflashObj = InternetExplorer ? myflash : document.myflash;

	if ( command == "setstartframe" )
	  	ACG_SetStartFrame();
	else if ( command == "replay" )
		ACG_OnReplay();
	else if ( command == "debug" )
		alert( args );
}

function ACG_OnReplay()
{
	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, ACG_GetDomain() );
//	var _myCookie = new ACG_Cookie( g_sCookieName, null, null, null );

	_myCookie.Delete();

	location.reload();
}
