<!--
/*
		@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

*/

//Must set this to the actual Flash object reference on the page!!!
lpMTagConfig.flashMovie = null;

/*  
LP Events: The body of these methods should be replaced with a call to the Flash object
*/

//Create a new button object overide (if not created yet)
if(lpMTagConfig.db1 == null) {
    lpMTagConfig.db1 = new Object();
}

//overriding busy action
lpMTagConfig.db1.busyAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL('Busy');
      chatWinURL = chatWinURL.replace(/forceOffline/,'SESSIONVAR%21BusyClickOverride');
      window.open(chatWinURL,'Chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};

//overriding offline action
lpMTagConfig.db1.offlineAction = function (objName) {
      objRef = eval(objName);
      var chatWinURL = objRef.getActionURL('Offline');
      window.open(chatWinURL,'Chat'+lpMTagConfig.lpNumber,'width=472,height=320,status=0,resizable=0,menubar=no,scrollbars=no,location=no');
};


lpMTagConfig.db1.dbStart = function (btname)
{
  try{
	//init flash button only when flash is ready
   setTimeout('lpMTagConfig.initButtonWhenReady(\"' + btname + '\")', 500);
  }catch(e){}
   
   return true;
}

//override the State Change event
lpMTagConfig.db1.dbStateChange = function (btname, state)
{
    try{
       //notify the Flash button the state should change. Make sure the button is ready before doing so
       setTimeout('lpMTagConfig.setButtonStateWhenReady(\"' + btname + '\",\"' + state + '\")', 500);
   }catch(e){}
   
   return true;
}

//chat invitation loaded (NOT shown yet)
lpMTagConfig.inviteChatStart =  function (invitename)
{
    try{
        //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;
        }
        
	    if(divObj.innerHTML != null) {
            divObj.innerHTML = "";
        }
        divObj.style.width = "1px";     
        divObj.style.height = "1px";
	
	    //don't attempt to interact with Flash before it's ready
	    setTimeout('lpMTagConfig.showInviteWhenReady(\"' + invitename + '\")', 500);
	
	}catch(e){}
	
	return true;
}

//chat invitation timed out
lpMTagConfig.inviteChatTimeout =  function (invitename)
{
    try{
	    //hide the invitation
        lpMTagConfig.flashMovie.Content.lpProxy.hideInvitation(invitename);
    }catch(e){}
        
    return true;
}

//voice invitation loaded (NOT shown yet)
lpMTagConfig.inviteVoiceStart =  function (invitename)
{
    try{
        //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;
        }
        
        if(divObj.innerHTML != null) {
            divObj.innerHTML = "";
        }
	    divObj.style.width = "1px";     
        divObj.style.height = "1px";
	
	    //don't attempt to interact with Flash before it's ready
	    setTimeout('lpMTagConfig.showVoiceInviteWhenReady(\"' + invitename + '\")', 500);
	
	}catch(e){}
	
	return true;
}

//voice invitation timed out
lpMTagConfig.inviteVoiceTimeout =  function (invitename)
{
    try{
	    //hide the invitation
        lpMTagConfig.flashMovie.Content.lpProxy.hideVoiceInvitation(invitename);
    }catch(e){}
        
    return true;
}



/************** MultiChannel Invitations *****************/
//voice invitation loaded (NOT shown yet)
lpMTagConfig.inviteMultiChannelStart =  function (invitename)
{
    try{
        //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;
        }
        
        if(divObj.innerHTML != null) {
            divObj.innerHTML = "";
        }
	    divObj.style.width = "1px";     
        divObj.style.height = "1px";
	
	    //don't attempt to interact with Flash before it's ready
	    setTimeout('lpMTagConfig.showMCInviteWhenReady(\"' + invitename + '\")', 500);
	
	}catch(e){}
	
	return true;
}

//voice invitation timed out
lpMTagConfig.inviteMultiChannelTimeout =  function (invitename)
{
    try{
	    //hide the invitation
        lpMTagConfig.flashMovie.Content.lpProxy.hideMCInvitation(invitename);
    }catch(e){}
        
    return true;
}


/*********************************************************/



//Flag to check Flash is ready
lpMTagConfig.bSWFInviteReady = false;
lpMTagConfig.bSWFButtonReady = false;
lpMTagConfig.bSWFMCInviteReady = false;


//must call this from flash when the button flash object is ready
lpMTagConfig.flashevent_MarkSWFInviteReady =  function (){
 lpMTagConfig.bSWFInviteReady = true;
 return true;
 }
//must call this from flash when the invite flash object is ready
lpMTagConfig.flashevent_MarkSWFButtonReady = function (){
 lpMTagConfig.bSWFButtonReady = true;
 return true;
}
//must call this from flash when the MC invite flash object is ready
lpMTagConfig.flashevent_MarkSWFMCInviteReady = function (){
 lpMTagConfig.bSWFMCInviteReady = true;
 return true;
}

lpMTagConfig.showInviteWhenReady = function(invitename){
 
 try{  
	if(!lpMTagConfig.bSWFInviteReady){
		setTimeout('lpMTagConfig.showInviteWhenReady(\"' + invitename + '\")', 500);
		return;
	}
	lpMTagConfig.flashMovie.Content.lpProxy.createInvitation(invitename);
	lpMTagConfig.flashMovie.Content.lpProxy.showInvitation(invitename);
	
 }catch(e){}	

}

lpMTagConfig.showVoiceInviteWhenReady = function(invitename){
 
 try{  
	if(!lpMTagConfig.bSWFInviteReady){
		setTimeout('lpMTagConfig.showVoiceInviteWhenReady(\"' + invitename + '\")', 500);
		return;
	}
	lpMTagConfig.flashMovie.Content.lpProxy.createVoiceInvitation(invitename);
	lpMTagConfig.flashMovie.Content.lpProxy.showVoiceInvitation(invitename);
	
 }catch(e){}	

}

lpMTagConfig.showMCInviteWhenReady = function(invitename){
 
 try{  
	if(!lpMTagConfig.bSWFMCInviteReady){
		setTimeout('lpMTagConfig.showMCInviteWhenReady(\"' + invitename + '\")', 500);
		return;
	}
	lpMTagConfig.flashMovie.Content.lpProxy.createMCInvitation(invitename);
	lpMTagConfig.flashMovie.Content.lpProxy.showMCInvitation(invitename);
	
 }catch(e){}	

}

lpMTagConfig.initButtonWhenReady = function(btname){
 try{
	if(!lpMTagConfig.bSWFButtonReady){
		setTimeout('lpMTagConfig.initButtonWhenReady(\"' + btname + '\")', 500);
		return;
	}

	lpMTagConfig.flashMovie.Content.lpProxy.createButton(btname);
 }catch(e){}
}

lpMTagConfig.setButtonStateWhenReady = function(btname, state){
	
  try{

	if(!lpMTagConfig.bSWFButtonReady){
		setTimeout('lpMTagConfig.setButtonStateWhenReady(\"' + btname + '\",\"' + state + '\")', 500);
		return;
	}
	//flashMovie.createButton(btname);
	lpMTagConfig.flashMovie.Content.lpProxy.setState(btname, state);
	
  }catch(e){}	
	
}    
/*
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
lpMTagConfig.flashevent_ButtonClicked = function (btname)
{
  try{
    //try to convert the button name to the button object
    var objBt = eval(btname);
    if(objBt == null || typeof(objBt) == "undefined")
    {
       return;
    }
    
	//call the internal Monitor tag implementation
    objBt.actionHook();
    
  }catch(e){} 
}

//Notifification from Flash invitation when invitation is accepted
lpMTagConfig.flashevent_InvitationAccepted = function(invitename)
{
    try{
        //try to convert the invite name to the invite object
        var objInv = eval(invitename);
    	
        if(objInv == null || typeof(objInv) == "undefined")
        {
            return;
        }
        
	    //now call the Monitor tag implementation that will show the chat window
        objInv.AcceptInvite();
    }catch(e){}    
}

//Notifification from Flash invitation when invitation is declined
lpMTagConfig.flashevent_InvitationDeclined = function(invitename)
{
    try{
        //try to convert the invite name to the invite object
        var objInv = eval(invitename);
        if(objInv == null || typeof(objInv) == "undefined")
        {
            return;
        }
        
        //now call the Monitor tag implementation
        objInv.CloseInvite();
    }catch(e){}    
}


//Notifification from Flash invitation when MC invitation is accepted
lpMTagConfig.flashevent_MCInvitationAccepted = function(invitename, channel) 
{
    try{
        
        //make sure valid channel
        channel = channel + '';
        if(channel.length > 1 || channel.match(/[123]/) == null)
		{
            return;
        } 
        
        //try to convert the invite name to the invite object
        var objInv = eval(invitename);
    	
        if(objInv == null || typeof(objInv) == "undefined")
        {
            return;
        }
        
	    //now call the Monitor tag implementation that will show the chat window
        objInv.AcceptInvite(parseInt(channel));
    }catch(e){}    
}

lpMTagConfig.flashevent_refreshDynButton = function()
{
    try{
           //loop through buttons
             if(lpMTagConfig.dynButton != null && lpMTagConfig.dynButton.length>0){
                  var j=0;
                  //loop through buttons
                  for(j=0;j<lpMTagConfig.dynButton.length;j++){
                        eval('if(lpMTagConfig.dynButton' + j + '!=null){ setTimeout("lpMTagConfig.dynButton' + j + '.MakeCall()",1000);}'); //force refresh
                  }
             }
      } catch(e){}
};

