// JavaScript Document

/*
The connection expects an identifier that it places into an object. The object is 
referenced for the life of the script. This allows multiple connections without
creating more sockets than necessary since the object items are reused depending on
whether the identifier has been initialized.

send a request like this:

-------------------------------
request(identifier,params);
-------------------------------

In the request provide this structure for the object:
params = {}
params.url = urlToPhp;
params.post = itemsToSendToPhp;
params.returnfunc = functionToHandleDataSentBackFromPhp;


Before the return value can be used, a function must be added to the class. Actually,
The class expects to send data to an external function. The function must be defined 
in the parent script like this:

---------------------------------------------------------
ajaxconnect.functionToHandleDataSentBackFromPhp = function(returnValue){
		// put routines here
	}
---------------------------------------------------------

*/

var flashTalk = function(){


	/**
	+ swf filename
	*/
	//this._swfName = 'LogoFade';
	
	var timerID = 0;
	var _swfName = 'generic';
	var flashTimerParam = 0;
	var flashTimerFunc = 'generic';

	this.swfName = function(value)
	{
		_swfName = value;	
	}

	this.SendData = function SendDataToFlex(param_value,func)
	{		 
		 flashTalk.sendDataToFlex(param_value,func);
	}
	this.SendDataOnDelay = function SendDataToFlexOnDelay(param_value,func,delay)
	{		 
		 flashTalk.sendDataToFlexOnDelay(param_value,func,delay);
	}
	
	flashTalk.sendDataToFlexOnDelay = function postDelay(param_value,func,delay)
	{
		flashTimerParam = param_value;
		flashTimerFunc = func;
		timerID = setTimeout ( "flashTalk.WaitForFlash()", delay );
	}
	
	flashTalk.sendDataToFlex = function post(param_value,func)
	{
		
		var flashMovie=flashMovieObject(_swfName);
		 
		 if(flashMovie && flashMovie.callFlexApp) {
			 //send the event name and its value
			flashMovie.callFlexApp(func, param_value);
			
		 } else
			 setFlashTimer(param_value,func);	
	}
	
	function setFlashTimer(param_value,func)
	{
		flashTimerParam = param_value;
		flashTimerFunc = func;
		////timerID = setTimeout ( "flashTalk.WaitForFlash()", 20 );    
	}
	flashTalk.WaitForFlash = function waitForFlashMovie()
	{

		var flashMovie=flashMovieObject(_swfName);
		if(flashMovie && flashMovie.callFlexApp)
		{
			clearInterval( timerID );
			flashTalk.sendDataToFlex(flashTimerParam,flashTimerFunc);
		} else {
			
			timerID = setTimeout ( "flashTalk.WaitForFlash()", 20 );
		}
		/**/
	}
	
	function flashMovieObject(movieName)
	{
	  
	  if (window.document[movieName]) 
	  {
		  return window.document[movieName];
	  }
	  
	  //
	  if (navigator.appName.indexOf ("Microsoft") ==-1)
	  {
		return window[movieName];
	  } 
	  else 
	  {
		return window.document[movieName];
	  }
	}

}


