﻿/*
Template for AS3 Static Widgets 
*/
import flash.external.ExternalInterface;
import com.adobe.captivate.widgets.*;
import com.adobe.captivate.events.*;

/*
Variable declarations 
*/
var m_WidgetMode : String = '';
var m_WidgetParam : String = null;
var m_VariableHandle : Object = null;
var m_MovieHandle : CPMovieHandle = null;
var m_EventHandle : IEventDispatcher = null;
var m_EditModeWidth : int = 411;
var m_EditModeHeight : int = 328;

/*
Event Handlers 
*/
this.addEventListener( "enterFrame" , FuncOnEnterFrame );

try {
	if(ExternalInterface.available == true)
	{
		/*
		Todo : Register all the ExternalInterface functions 
		*/
		ExternalInterface.addCallback( "isStatic" , isStatic );
		ExternalInterface.addCallback( "getInspectorParameters" , getInspectorParameters );
		ExternalInterface.addCallback( "setInspectorParameters" , setInspectorParameters );
		ExternalInterface.addCallback( "setParameters" , setParameters );
		ExternalInterface.addCallback( "cpSetValue" , cpSetValue );
		ExternalInterface.addCallback( "getEditModeWidth", getEditModeWidth); 
		ExternalInterface.addCallback( "getEditModeHeight", getEditModeHeight); 
		ExternalInterface.addCallback( "IsReadyForSnapShot", IsReadyForSnapShot);
	}
} catch( e ){
};

function FuncOnEnterFrame( inEvent : Event ) : void
{
	/*
	This function provides a sample code for handling values of Widget modes and parameters.
	*/
	var l_WidgetModeParam : String = m_WidgetMode;
	/*
	This variable will be provided by Captivate.
	*/
	if( null == l_WidgetModeParam )
	{
		l_WidgetModeParam = 'Stage';
	}
	if( 'Edit' == l_WidgetModeParam )
	{
		/*
		This mode is used for setting properties of the widget inside Captivate during authoring time.
		*/
	}
	else if( 'Preview' == l_WidgetModeParam )
	{
		/*
		This mode is used for previewing the Widget in the preview window of widget panel.
		*/
	}
	else
	{
		/*
		This mode is used while previewing the widget inside Captivate during authoring or during execution.
		*/
		if( m_MovieHandle != null ) 
		{
			m_WidgetParam = m_MovieHandle.widgetParams();
		}
		if( m_WidgetParam != null )//at runtime inside Captivate movie
       	{
			var myXml:XML = new XML(m_WidgetParam);
		}
	}
}

/* 
Captivate will not recognize a Static Widget unless this function is implemented and returns true. 
*/
function isStatic():Boolean
{
	return true;
}

/*
This function is called by Captivate to obtain values from the Widget SWF.
This is the recommended method of passing values between Captivate and Widget SWF. 
Ideally an object is created and values assigned to parameters of the object.
This is taken by Captivate and stored as an xml string. 
*/
function getInspectorParameters() : Object
{
	var l_Parameters: Object = new Object();
	/*
	Todo : Set the data in l_parameters fields here.
	*/
	return l_Parameters;
}

/*
This function is called by Captivate when the widget is inserted.
Parameters in the object passed to this function are saved
and the SWF is redrawn on the stage appropriately.
*/
function setInspectorParameters( inParam : Object ) : void
{
	if ( null != inParam)
	{
		/*
		Todo : Save the parameters in object here 
		and Redraw the SWF.
		*/	
	}
}

/*
This function is called by Captivate after calling setParameters.
If the widget needs more time to update the SWF to draw on the stage
return false. When the SWF is ready, return true
*/
function IsReadyForSnapShot():Boolean
{
	return true;
}

/*
This function is called by Captivate to update the parameter values.
Parameters in the object passed to this function are saved
and the SWF is redrawn on the stage appropriately.
*/
function setParameters( inParam : Object ) : void
{
	if ( null != inParam )
	{
		/*
		Todo : Save the parameters in object here 
		and Redraw the SWF.
		*/	
	}
}

/*
This function is called by Captivate during execution.
This sets the variables needed by the SWF for execution.
*/
function cpSetValue( inVariable : String, inValue ) : void
{
	if( 'movieHandle' == inVariable ) 
	{
		/* 
		m_VariableHandle can be used to access the variables from Captivate during execution.
		Eg: m_VariableHandle.rdcmndPause = 1; 
		will pause Captivate.
		Save the variable handle here.
		*/
		m_MovieHandle = CPMovieHandle(inValue);
		if(m_MovieHandle)
		{
			var l_MovieProps:CPMovieProperties = m_MovieHandle.getMovieProps();
			if(l_MovieProps)
			{
				m_VariableHandle = l_MovieProps.variablesHandle;
				if(m_EventHandle == null)
				{
						m_EventHandle = l_MovieProps.eventDispatcher;
						if(m_EventHandle != null)
						{
							/*Add The event listeners here which listen to different captivate events*/
						}
				}
			}
		}
	}	
	else if( 'widgetMode' == inVariable )
	{
		/*
		Save the Widget more here. 
		We may wish to do different things based on the Widget mode.
		We can draw the Widget differently on stage in Captivate and during execution.
		*/
		m_WidgetMode = inValue;
	}
}

/**
	Below functions are used to specify the width and height values of the widget parameters swf.
	If the widget parameters dialog should not be shown, return 0 from both these functions
*/
function getEditModeWidth( ): int
{
	return m_EditModeWidth;
}
function getEditModeHeight( ): int
{
	return m_EditModeHeight;
}
