//
// module: mpFlashInterface
//
// Manage Communication to Embed Flash ActiveX Objects
// v1.1 - 11.01.08 - MK
//


//## -------------------------------------------------------- VARS

var mpFlashInterface = new Object();
mpFlashInterface.bInit = false;
mpFlashInterface.bDebug = false;
mpFlashInterface.aReturnFunctions = new Array();



//## -------------------------------------------------------- FUNCTIONS

// function: send
//
// Sends request to certain flash movie
//
// Parameters:
// sMovieID - Name Tag of Flash Embed/Object Script
// sAction - Name of Action to Call
// oValue1,oValue2,oValue3 - Optional Pass Thru Parameters
//
// Return:
// Array - Array with String Objects as Flash Return values
mpFlashInterface.send = function(sMovieID,sAction,oValue1,oValue2,oValue3) {	
	mpFlashInterface.sendAndReceive(sMovieID,sAction,null,oValue1,oValue2,oValue3);
  
}


// function: sendAndReceive
//
// Sends request to certain flash movie and calls CallBack Handler
//
// Parameters:
// sMovieID - Name Tag of Flash Embed/Object Script
// sAction - Name of Action to Call
// sReturnFunction - Name of Function of CallBack Handler
// oValue1,oValue2,oValue3 - Optional Pass Thru Parameters
//
// Return:
// Array - Array with String Objects as Flash Return values
mpFlashInterface.sendAndReceive = function(sMovieID,sAction,fctReturn,oValue1,oValue2,oValue3) {
	
	if (!mpFlashInterface.bInit) {
		mpFlashInterface._debug('Tried to call Action "'+sAction+'" before Window.load');
		return;
	}
	if (sAction==undefined || sAction=='') {
		mpFlashInterface._debug('Action undefined');
		return;
	}
	var oMovie = mpFlashInterface._getObject(sMovieID);
	if (oMovie==undefined || oMovie=='') {
		mpFlashInterface._debug('Movie Object not found');	
		return;
	}
	if (oValue1==undefined)  oValue1 = '';
	if (oValue2==undefined)  oValue2 = '';
	if (oValue3==undefined)  oValue3 = '';
	var iReturnFunction = -1;
	if (fctReturn!=undefined && fctReturn!=null && fctReturn!='') {
		iReturnFunction = mpFlashInterface.aReturnFunctions.length-1;
		mpFlashInterface.aReturnFunctions[iReturnFunction] = fctReturn;
	}
	oMovie.receiveFromJS(sAction,iReturnFunction,oValue1,oValue2,oValue3);
	
}


mpFlashInterface.syncAjaxRequest = function(sUrl) {

	if (window.XMLHttpRequest) {
		AJAX=new XMLHttpRequest();
	} else {
		AJAX=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (AJAX) {
		 AJAX.open("GET", sUrl, false);
		 AJAX.send(null);
		 return AJAX.responseText;
	} else {
		 return false;
	}	
	
}


mpFlashInterface._sendAndReceiveReturn = function(sAction,iReturnFunction,oValue1,oValue2,oValue3) {
	
	var fctReturn = mpFlashInterface.aReturnFunctions[iReturnFunction];
	fctReturn(oValue1,oValue2,oValue3);
	
}


// function: receiveFromFlash
//
// Receives and Flash Calls - Overwrite Function to Call own handler
//
// Parameters:
// sAction - Name of Action to Call
// oValue1,oValue2,oValue3 - Optional Pass Thru Parameters
//
// Return:
// String - Return any value to process within flash
mpFlashInterface.receiveFromFlash = function(sAction,oValue1,oValue2,oValue3) {
	
	mpFlashInterface._debug('Flash Call > '+ sAction+ ' > '+ oValue1+ ' > '+ oValue2+ ' > '+ oValue3);
	return true;
	
}


// function: _getObject
//
// Receive Object for given Flash Movie Name
//
// Parameters:
// sMovieID - ID Tag or Name Tag of Flash Embed/Object Script
//
// Return:
// Object - Flash Movie Object
mpFlashInterface._getObject = function(sMovieID) {
	
	var oMovie = new Object();
	if (document.getElementById)  oMovie = document.getElementById(sMovieID);
	if (oMovie==undefined) {
		if (navigator.appName.indexOf('Microsoft')!=-1) {
			oMovie = window[sMovieID];
		}	else {
			oMovie = document[sMovieID];
		}
	}
	return oMovie;

}


// function: _debug
//
// Local Debug function to report to developer
//
mpFlashInterface._debug = function(sText) {

	if (mpFlashInterface.bDebug)  alert('mpFlashInterface: '+ sText);
	
}


// function: _hookWindowLoad
//
// Hooks Window Load Event and Adds Ready Status to Module
//
mpFlashInterface._hookWindowLoad = function() {
	
	if (window.onload)  mpFlashInterface.onWindowLoad = window.onload;
	window.onload = function() {
		mpFlashInterface.bInit = true;
		if (mpFlashInterface.onWindowLoad)  mpFlashInterface.onWindowLoad();
	}
	
}
mpFlashInterface._hookWindowLoad();

