if (typeof(lpDefaultCBOnlineURL) == "undefined") 
	lpDefaultCBOnlineURL = "http://www.liveperson.com/visitor/livetalk/call3";
if (typeof(lpDefaultCBOfflineURL) == "undefined") 
	lpDefaultCBOfflineURL = "http://www.liveperson.com/visitor/livetalk/call3/offline.asp";
if (typeof(lpServerName) == "undefined") 
	lpServerName = "server.iad.liveperson.net";
if (typeof(lpSkill) == "undefined") 
	skillparam = "";
else
	skillparam = "&skill=" + escape(lpSkill);
if (typeof(lpOperator) == "undefined") 
	operatorParam = "";
else
	operatorParam = "&operator=" + escape(lpOperator);
var LPREPSTATEONLINE = "online";
var LPREPSTATEOFFLINE = "offline";
var LPREPSTATEOCCUPIED = "occupied";
var LPREPSTATEUNKNOWN = "unknown";

var lpDOM = (document.getElementById) ? true : false;
var lpIE = (document.all) ? true : false;
var lpMAC_IE = ((navigator.platform) && (navigator.platform.toUpperCase().indexOf("MAC") >= 0) && (lpIE));
var lpFindRepstateImage = new Image();
var LPProtocol = document.location.toString().indexOf("https") == 0 ? "https" : "http";

var lpCurrentStateVoice = LPREPSTATEUNKNOWN;

var hcimageCB = hcGetImage("hcIconCB");
if (hcimageCB)
	hciconCBsrc = hcimageCB.src;

if ((! lpIE) && (lpDOM))
{
	lpFindRepstateImage = document.createElement('IMG');
	lpFindRepstateImage.style.visibility = "hidden";
	document.body.appendChild(lpFindRepstateImage);
} else
if (lpMAC_IE)
{
	document.writeln("<div style='visibility:hidden'><img src='blank' id='lpFindRepstateImage' name='lpFindRepstateImage'></div>");
	lpFindRepstateImage = document.lpFindRepstateImage;
}

function lpGetDate()
{
	var d = new Date();
	return d.getTime();
}

function lpFindRepstateCheckImage() {
	if (((lpDOM && !lpIE) || (lpMAC_IE)) && (! lpFindRepstateImage.complete))
		return false;
	var imageWidth = lpFindRepstateImage.width;
	if (imageWidth > 0) {
		if (imageWidth == 40) {
			lpNotifyRepstate(LPREPSTATEONLINE);
		} else 
		if (imageWidth == 50) {
			lpNotifyRepstate(LPREPSTATEOFFLINE);
		} else
		if (imageWidth == 60) {
			lpNotifyRepstate(LPREPSTATEOCCUPIED);
		} else
			lpNotifyRepstate(LPREPSTATEUNKNOWN);
		return true;
	}
	return false;
}

function lpFindRepstateLoopImage() {
	if (lpFindRepstateCheckImage()) {
		if (lpMAC_IE)
			return;
		lpFindRepstateImage = new Image();
		if ((! lpIE) && (lpDOM))
		{
			lpFindRepstateImage = document.createElement('IMG');
			lpFindRepstateImage.style.visibility = "hidden";
			document.body.appendChild(lpFindRepstateImage);
		}
		setTimeout("lpCheckFindRepstate()", 30000);
	} else
		setTimeout("lpFindRepstateLoopImage()", 1000);
}

function lpCheckFindRepstate() {
	lpFindRepstateImage.src = LPProtocol + "://" + lpServerName + "/hc/" + lpNumber + "/?cmd=repstate&useSize=true&channel=voice&site=" + lpNumber + "&imageUrl=" + escape(LPProtocol + "://" + lpServerName + "/hcp/voice/1/en") + skillparam + operatorParam + "&d=" + lpGetDate();
	lpFindRepstateLoopImage();
}
var lpStateFound = false;

function lpNotifyRepstate(state) {
	if (lpCurrentStateVoice != state)
	{
		lpCurrentStateVoice = state;
		hcReloadIconCB();
	
		if (typeof(repStateNotifyFunction) != "undefined")
			repStateNotifyFunction(state);
	}
	if (lpCurrentStateVoice != LPREPSTATEUNKNOWN)
	    lpStateFound = true;
	else
	    lpStateFound = false;

}

function hcDateCB()
{
	var d = new Date();
	return d.getTime();
}

function hcReloadIconCB()
{
	if (hcimageCB)
	{
		hcimageCB.src = hciconCBsrc + "&monitor=1&d=" + hcDateCB();
	}
	else
	{
		hcimageCB = hcGetImage("hcIconCB");
		if (hcimageCB)
		{
			hciconCBsrc = hcimageCB.src;	
			hcimageCB.src = hciconCBsrc + "&monitor=1&d=" + hcDateCB();		
		}
	}
}

function hcGetImage(name)
{
	return hcFindImage(document, name);
}

function hcFindImage(doc, name)
{
	if (doc.getElementsByTagName && (typeof(doc.getElementsByTagName("IMG")[name]) != "undefined"))
		return doc.getElementsByTagName("IMG")[name];
    	if (typeof(doc[name]) != "undefined")
        	return doc[name];

	for (var i = 0; i < doc.images.length; i++) {
		if (doc.images[i].name == name)
			return doc.images[i];
	}
	return null;
}

function lpMakeCall(onlineURL, skill, showID, offlineURL)
{
	return lpMakeCallWithSize(onlineURL, skill, showID, offlineURL, 0, 472, 320);
}

function lpMakeCallInfo(onlineURL, skill, showID, offlineURL, cbInfo)
{
	return lpMakeCallWithSize(onlineURL, skill, showID, offlineURL, cbInfo, 472, 320);
}

function lpMakeCallWithSize(onlineURL, skill, showID, offlineURL, cbInfo, winW, winH)
{
    if (! lpStateFound) return false;
	if (onlineURL == null)
		onlineURL = lpDefaultCBOnlineURL;
	if (offlineURL == null)
		offlineURL = lpDefaultCBOfflineURL;
	
	var winParams = "width=" + winW + ",height=" + winH + ", scrollbars=no, resizable=no";
	
	if (lpCurrentStateVoice == LPREPSTATEONLINE)
	{	
		var cbURL = onlineURL + "?site=" + lpNumber + "&skill=" + skill + "&identifier=" + showID;
		if (cbInfo != "0")
			cbURL = cbURL + cbInfo;
	    if (typeof(lpOnClickVoiceRepOnline) != "undefined")
	        lpOnClickVoiceRepOnline(cbURL);
	    else
		    window.open(cbURL,'window', winParams);
	}
	else
	{
		var cbURL = offlineURL + "?site=" + lpNumber + "&skill=" + skill + "&identifier=" + showID;
		if (cbInfo != "0")
			cbURL = cbURL + cbInfo;
	    if (typeof(lpOnClickVoiceRepOffline) != "undefined")
	        lpOnClickVoiceRepOffline(cbURL);
        else
		    window.open(cbURL,'window', winParams);
	}
	
	return false;
}

setTimeout("lpCheckFindRepstate()", 10);
