<!--
/*
		@Company	: LivePerson Inc.
*		@FileName	:proxy.js
*		@Description: Script file with methods for communicating Flash and LivePerson's monitor tag
*		@Author		: Uri Bahar (ubahar@liveperson.com)
*       @Legal Disclaimer: By downloading or using this software/code you expressly agree to the following: 
*        This software/code is for the limited use only in connection with the LivePerson services purchased by you. 
*        Once the LivePerson services are no longer in use, you must cease all use of the software/code and destroy all copies. 
*        The software/code is provided to you "as is" and without any warranty of any kind, express, implied or otherwise, including without limitation, 
*        any warranty of merchantability or fitness for a particular purpose.  In no event shall LivePerson or any of its affiliates be liable for 
*        any direct, indirect, special, incidental, or consequential damages or any other damages of any kind, whether LivePerson or any of its 
*       affiliates have been advised of the possibility of such loss, however caused, and on any theory of liability, arising out of or related 
*        to the possession, use or performance of this software/code.  
*       IF YOU DO NOT AGREE TO THE FOREGOING TERMS, DO NOT DOWNLOAD OR USE THE SOFTWARE/CODE

*/
/*  
LP Events: The body of these methods should be replaced with a call to the Flash object
*/

//Create a new button object overide
lpMTagConfig.db1 = new Object();
lpMTagConfig.db1.dbStart = function (btname)
{
	//init flash button only when flash is ready
   setTimeout('initButtonWhenReady(\"' + btname + '\")', 500);
   
   return true;
}

//override the State Change event
lpMTagConfig.db1.dbStateChange = function (btname, state)
{
   //notify the Flash button the state should change. Make sure the button is ready before doing so
   setTimeout('setButtonStateWhenReady(\"' + btname + '\",\"' + state + '\")', 500);
   
   return true;
}

//invitation loaded (NOT shown yet)
lpMTagConfig.inviteChatStart =  function (invitename)
{
    //set the inner HTML of the invitation to the Flash object
    var inviteObj = eval(invitename);
    if (inviteObj==null) {
        return true;
    }
    var divObj = inviteObj.GetObj(inviteObj.divID);
    if (divObj==null) {
        return true;
    }
    
	divObj.innerHTML = "";
    divObj.style.width = "1px";     
    divObj.style.height = "1px";
	
	//don't attempt to interact with Flash before it's ready
	setTimeout('showInviteWhenReady(\"' + invitename + '\")', 500);
	
	return true;
}

//invitation timed out
lpMTagConfig.inviteChatTimeout =  function (invitename)
{
	//hide the invitation
    flashMovie.hideInvitation();
    return true;
}

//Flag to check Flash is ready
var bSWFInviteReady = false;
var bSWFButtonReady = false;


function flashevent_MarkSWFInviteReady(){
 bSWFInviteReady = true;
}

function flashevent_MarkSWFButtonReady(){
 bSWFButtonReady = true;
 return "hi";
}

function showInviteWhenReady(invitename){
   
	if(!bSWFInviteReady){
		setTimeout('showInviteWhenReady(\"' + invitename + '\")', 500);
		return;
	}
	flashMovie.createInvitation(invitename);
	flashMovie.showInvitation();

}

function initButtonWhenReady(btname){
	
	if(!bSWFButtonReady){
		setTimeout('initButtonWhenReady(\"' + btname + '\")', 500);
		return;
	}

	flashMovie.createButton(btname);
}

function setButtonStateWhenReady(btname, state){
	
    //alert('setButtonStateWhenReady');

	if(!bSWFButtonReady){
		setTimeout('setButtonStateWhenReady(\"' + btname + '\",\"' + state + '\")', 500);
		return;
	}
	//flashMovie.createButton(btname);
	flashMovie.setState(btname, state);
	
}    
/*
FLASH EVENTS: Flash should call the below methods for click events that occur in the button and invitation
*/
//Notifification from Flash button when button is clicked
function flashevent_ButtonClicked(btname)
{
    //try to convert the button name to the button object
    var objBt = eval(btname);
    if(objBt == null || typeof(objBt) == "undefined")
    {
        alert("Button object not found: " + btname);
    }
    
	//call the internal Monitor tag implementation
    objBt.actionHook();
}
//Notifification from Flash invitation when invitation is accepted
function flashevent_InvitationAccepted(invitename)
{
    //try to convert the invite name to the invite object
    var objInv = eval(invitename);
	
    if(objInv == null || typeof(objInv) == "undefined")
    {
        alert("Invitation object not found: " + invitename);
    }
    
	//now call the Monitor tag implementation that will show the chat window
    objInv.AcceptInvite();
}

//Notifification from Flash invitation when invitation is declined
function flashevent_InvitationDeclined(invitename)
{
    //try to convert the invite name to the invite object
    var objInv = eval(invitename);
    if(objInv == null || typeof(objInv) == "undefined")
    {
        alert("Invitation object not found: " + invitename);
    }
    
    //now call the Monitor tag implementation
    objInv.CloseInvite();
}

/*
* returns reference to flash movie object (button or invitation)
*/
function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    }
    else {
	       return document[movieName];
    }
}
