var hcClickUpdateState = 0;
var hcImage;
var hcIsImage = false;
var hcCounter = 0;
var hcCmd = "startPage";
var hcTimeout = 5;
var hcSendCounter = 0;

var hcPageID = null;

var hcLeft = -1;
var hcTop = -1;

var hcNS = (document.layers) ? true : false;
var hcIE = (document.all) ? true : false;

var hcShowImage = false;

var hcPos = - 30;
var hcStep = 3;
var hcDir = hcStep;
var hcBorder = 100;
var hcAnimate = false;

var hcNeedImage;
var hcCloseImage;
var hcImageFetched = false;

function hcDate()
{
	var d = new Date();

	return d.getTime();
}

function hcSendRequest()
{
	hcSendCounter++;
	if (hcSendCounter == 5)
		hcIsImage = false;

	if (! hcIsImage) {
		hcImage= new Image;
		if (hcPageID==null) hcPageID=Math.round(Math.random()*9999999999);
		var u = 'https://hc2.humanclick.com/hc/21473899/?site=21473899' + 
					'&cmd=' + hcCmd + 
					'&page=' + escape(document.location) + 
					'&title=' + escape(document.title) + 
					'&referrer=' + escape(document.referrer) + 
					'&id=' + hcPageID +
					'&d=' + hcDate();
		hcCmd = "inPage";
		hcImage.src = u;
		hcIsImage = true;
		hcSendCounter = 0;
	}
}

function hcHandleWidth(w)
{
	if (w == 60) {
		openChat();
	} else if (w == 70) {
		openWantsToChat();
	} else if (w == 30) {
		hcTimeout = -1;
	} else if (w == 40) {
		hcTimeout = 15;
	} else if (w == 50) {
		hcTimeout = 30;
	} else if (w >= 80) {
		if (w != 80  &&  isNaN(hcClickUpdateState))
			hcClickUpdateState(w-80);
		hcReloadIcon();
		hcSendRequest();
	}
}

function hcCheckImages()
{
	if (hcIsImage) {
		var w = hcImage.width;
		if (w == 0)
			return;
		hcIsImage = false;
		hcHandleWidth(w);
	}
}

function hcXoomImage()
{
	for (var i = 0; i < document.links.length; i++) {
		if (document.links[i].href.indexOf('javascript') >= 0) {
			document.links[i].target = "_self";
		}
	}
}

function hcLoop()
{
	if (hcTimeout < 0)
		return;

	if (hcCounter == 0)
		hcSendRequest();
	hcCounter = (hcCounter + 1) % hcTimeout;
	hcCheckImages();

	hcXoomImage();
		
	setTimeout('hcLoop()', 1000);
}	

function hcReloadIcon()
{
	document.images['hcIcon'].src = 'https://hc2.humanclick.com/hc/21473899/?cmd=repstate&site=21473899&d=' + hcDate();
}

function openChat()
{
	window.open('http://hc2.humanclick.com/hc/21473899/?cmd=file&file=visitorWantsToChat&site=21473899&d=' + hcDate(), 'chat21473899', 'width=472,height=320,menubar=no,scrollbars=0,resizable=1');
}

function openWantsToChat()
{
	if (hcIE || hcNS) {
		hcShowTheImage();
	} else {
		window.open('http://hc2.humanclick.com/hc/21473899/?cmd=file&file=wantsToChat&site=21473899&d=' + hcDate(), 'wanttochat21473899', 'width=152,height=141,menubar=no,scrollbars=no');
	}	
}

function hcPreload()
{
	hcNeedImage = new Image();
	hcNeedImage.src = "https://hc2.humanclick.com/hcp/corporate/1/en/need_help_on.gif";

	hcCloseImage = new Image();
	hcCloseImage.src = "https://hc2.humanclick.com/hcp/corporate/1/en/close_on.gif";
}

function hcSetImageGo(name, image, go)
{
	hcAnimate = go;
	hcSetImage(name, image);
}

function hcSetImage(name, image)
{
	document[name].src = "https://hc2.humanclick.com/hcp/corporate/1/en/" + image;
}

document.write('<br><center>');
document.write('<table border="0" cellspacing="0" cellpadding="0">');
document.write('<tr>');
document.write('<td align="center" valign="top"><a name="chatRef" href="javascript:openChat()" target="_self"><img name="hcIcon" src="https://hc2.humanclick.com/hc/21473899/?cmd=repstate&site=21473899&d=' + hcDate() + '" alt="Powered by LivePerson" border="0" width=183 height=38></a></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td align="center" valign="top"><a target="_top" href="http://solutions.liveperson.com/ref/lppb.asp"><img name="hcCredit" src="https://hc2.humanclick.com/hcp/icon/lp/poweredoff.gif" border="0" width=106 height=35></a></td>');
document.write('</tr>');
document.write('</table>');
document.write('</center>');

if (hcIE) {
	document.write('<div id="mylayer" style="z-index:90;position:absolute;visibility:hidden;left:10;top:10">');
	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td><a name="needRef" href="javascript:hcAcceptCall()" target="_self" onmouseover=hcSetImageGo("need_help","need_help_on.gif",false) onmouseout=hcSetImageGo("need_help","need_help_off.gif",true)><img name="need_help" onload="hcFloatIconLoaded()" border="0" width=129 height=105></a></td></tr>');
	document.write('<tr><td><a href="javascript:hcRejectCall()" target="_self" onmouseover=hcSetImageGo("need_close","close_on.gif",false) onmouseout=hcSetImageGo("need_close","close_off.gif",true)><img name="need_close" border="0" width=129 height=16></a></td></tr>');
	document.write('</table></div>');
} else if (hcNS) {
	document.write('<layer name="mylayer" z-index="90" left="10" top="10" visibility="hidden">');
	document.write('<table border="0" cellspacing="0" cellpadding="0">');
	document.write('<tr><td><a href="javascript:hcAcceptCall()" target="_self" onmouseover=hcSetImageGo("need_help","need_help_on.gif",false) onmouseout=hcSetImageGo("need_help","need_help_off.gif",true)><img name="need_help" onload="hcFloatIconLoaded()" border="0" width=129 height=105></a></td></tr>');
	document.write('<tr><td><a href="javascript:hcRejectCall()" target="_self" onmouseover=hcSetImageGo("need_close","close_on.gif",false) onmouseout=hcSetImageGo("need_close","close_off.gif",true)><img name="need_close" border="0" width=129 height=16></a></td></tr>');
	document.write('</table></layer>');
}

function hcFloatIconLoaded()
{
	hcImageFetched = true;
}

function hcImageTimer()
{
	if (hcShowImage && hcImageFetched) {
		var top;
		var left;

		if (hcIE) {

			top = document.body.scrollTop;
			left = document.body.scrollLeft;

			if ((hcTop < 0) || ((hcTop == top) && (hcLeft == left))) {
				document.all.mylayer.style.visibility = "visible";
			} else {
				document.all.mylayer.style.visibility = "hidden";
			}

		} else if (hcNS) {
		
			top = pageYOffset;
			left = pageXOffset;
		
			if ((hcTop < 0) || ((hcTop == top) && (hcLeft == left))) {
				document.layers.mylayer.visibility = "visible";
			} else {
				document.layers.mylayer.visibility = "hidden";
			}
		}

		hcPlaceImage();

		hcTop = top;
		hcLeft = left;
	}

	setTimeout('hcImageTimer()', 250);
}

function hcAcceptCall()
{
	hcHideTheImage();

	openChat();
}

function hcRejectCall()
{
	hcHideTheImage();

	hcCmd = "rejectChat";
	hcCounter = 0;
}

function hcHideTheImage()
{
	hcShowImage = false;

	if (hcIE) {
		document.all.mylayer.style.visibility = "hidden";
	} else if (hcNS) {
		document.layers.mylayer.visibility = "hidden";
	}
}

function hcShowTheImage()
{
	hcShowImage = true;

	hcSetImage("need_help","need_help_off.gif");
	hcSetImage("need_close","close_off.gif");

	hcAnimate = true;
		
	hcPreload();

	hcAnimateStart();
}

function hcAnimateStart()
{
	if (hcIE) {
		hcBorder = document.body.clientWidth;
	} else if (hcNS) {
		hcBorder = window.innerWidth;
	}

	hcAnimateImage();
}

function hcAnimateImage()
{
	if (hcImageFetched && hcAnimate)
		hcPos = hcPos + hcDir;

	if (hcPos > hcBorder - 160)
		hcDir = - hcStep;

	hcPlaceImage();

	if ((hcPos > 30) || (hcDir > 0))
		setTimeout("hcAnimateImage()", 20);
}

function hcPlaceImage()
{
	var y = 40;

	if (hcIE) {
		document.all.mylayer.style.left = document.body.scrollLeft + hcPos;
		document.all.mylayer.style.top = document.body.scrollTop + y;
	} else if (hcNS) { 
		document.layers.mylayer.left = pageXOffset + hcPos;
		document.layers.mylayer.top = pageYOffset + y;
	}
}

function hcgo()
{
	hcLoop();
	setTimeout('hcImageTimer()', 250);
}

if (true)
	hcgo();
