var HumanClickUpdateState = 0;
var HumanImage;
var HumanIsImage = false;
var HumanCounter = 0;
var HumanCmd = "startPage";
var HumanTimeout = 5;
var HumanSendCounter = 0;

var HumanLeft = -1;
var HumanTop = -1;

var HumanNS = (document.layers) ? true : false;
var HumanIE = (document.all) ? true : false;

var HumanShowImage = false;

var HumanPos = - 30;
var HumanStep = 3;
var HumanDir = HumanStep;
var HumanBorder = 100;
var HumanGo = false;

var needImage;
var closeImage;
var imageFetched = false;

var stopImage;

function getDate()
{
	var d = new Date();

	return d.getTime();
}

function sendRequest()
{
	HumanSendCounter++;
	if (HumanSendCounter == 5)
		HumanIsImage = false;

	if (! HumanIsImage) {
		HumanImage= new Image;
		var u = 'http://hc2.humanclick.com/hc/60877126/?site=60877126' + 
					'&cmd=' + HumanCmd + 
					'&page=' + escape(document.location) + 
					'&title=' + escape(document.title) + 
					'&referrer=' + escape(document.referrer) + 
					'&d=' + getDate();
		HumanCmd = "inPage";
		HumanImage.src = u;
		HumanIsImage = true;
		HumanSendCounter = 0;
	}
}

function handleWidth(w)
{
	if (w == 60) {
		openChat();
	} else if (w == 70) {
		openWantsToChat();
	} else if (w == 30) {
		HumanTimeout = -1;
	} else if (w == 40) {
		HumanTimeout = 15;
	} else if (w == 50) {
		HumanTimeout = 30;
	} else if (w >= 80) {
		if (w != 80  &&  isNaN(HumanClickUpdateState))
			HumanClickUpdateState(w-80);
		reloadIcon();
		sendRequest();
	}
}

function checkImages()
{
	if (HumanIsImage) {
		var w = HumanImage.width;
		if (w == 0)
			return;
		HumanIsImage = false;
		handleWidth(w);
	}
}

function xoomImage()
{
	for (var i = 0; i < document.links.length; i++) {
		if (document.links[i].href.indexOf('javascript') >= 0) {
			document.links[i].target = "_self";
		}
	}
}

function loop()
{
	if (HumanTimeout < 0)
		return;

	if (HumanCounter == 0)
		sendRequest();
	HumanCounter = (HumanCounter + 1) % HumanTimeout;
	checkImages();

	xoomImage();
		
	setTimeout('loop()', 1000);
}	

function reloadIcon()
{
	document.images['hcIcon'].src = 'http://hc2.humanclick.com/hc/60877126/?cmd=repstate&site=60877126&d=' + getDate();
}

function openChat()
{
	window.open('http://hc2.humanclick.com/hc/60877126/?cmd=file&file=visitorWantsToChat&site=60877126&d=' + getDate(), 'chat60877126', 'width=419,height=320,menubar=no,scrollbars=0');
}

function openWantsToChat()
{
	if (HumanIE || HumanNS) {
		showTheImage();
	} else {
		window.open('http://hc2.humanclick.com/hc/60877126/?cmd=file&file=wantsToChat&site=60877126&d=' + getDate(), 'wanttochat60877126', 'width=160,height=180,menubar=no,scrollbars=no');
	}	
}

function preload()
{
	needImage = new Image();
	needImage.src = "http://hc2.humanclick.com/hcp/general/set-h/en/need_help_on.gif";

	closeImage = new Image();
	closeImage.src = "http://hc2.humanclick.com/hcp/general/set-h/en/close_on.gif";
}

function setImageGo(name, image, go)
{
	HumanGo = go;
	setImage(name, image);
}

function setImage(name, image)
{
	document[name].src = "http://hc2.humanclick.com/hcp/general/set-h/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="http://hc2.humanclick.com/hc/60877126/?cmd=repstate&site=60877126&d=' + getDate() + '" alt="Free service from HumanClick" border="0" width=78 height=88></a></td>');
document.write('</tr>');
document.write('<tr>');
document.write('<td align="center" valign="top"><a target="_top" href="http://www.humanclick.com/free" onmouseover=setImage("hcCredit","crediton.gif") onmouseout=setImage("hcCredit","creditoff.gif")><img name="hcCredit" src="http://hc2.humanclick.com/hcp/general/set-h/en/creditoff.gif" border="0" width=54 height=48></a></td>');
document.write('</tr>');
document.write('</table>');
document.write('</center>');

if (HumanIE) {
	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:acceptCall()" target="_self" onmouseover=setImageGo("need_help","need_help_on.gif",false) onmouseout=setImageGo("need_help","need_help_off.gif",true)><img name="need_help" onload="floatIconLoaded()" border="0" width=147 height=147></a></td></tr>');
	document.write('<tr><td><a href="javascript:rejectCall()" target="_self" onmouseover=setImageGo("need_close","close_on.gif",false) onmouseout=setImageGo("need_close","close_off.gif",true)><img name="need_close" border="0" width=147 height=13></a></td></tr>');
	document.write('</table></div>');
} else if (HumanNS) {
	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:acceptCall()" target="_self" onmouseover=setImageGo("need_help","need_help_on.gif",false) onmouseout=setImageGo("need_help","need_help_off.gif",true)><img name="need_help" onload="floatIconLoaded()" border="0" width=147 height=147></a></td></tr>');
	document.write('<tr><td><a href="javascript:rejectCall()" target="_self" onmouseover=setImageGo("need_close","close_on.gif",false) onmouseout=setImageGo("need_close","close_off.gif",true)><img name="need_close" border="0" width=147 height=13></a></td></tr>');
	document.write('</table></layer>');
}

function floatIconLoaded()
{
	imageFetched = true;
}

function imageTimer()
{
	if (HumanShowImage && imageFetched) {
		var top;
		var left;

		if (HumanIE) {

			top = document.body.scrollTop;
			left = document.body.scrollLeft;

			if ((HumanTop < 0) || ((HumanTop == top) && (HumanLeft == left))) {
				document.all.mylayer.style.visibility = "visible";
			} else {
				document.all.mylayer.style.visibility = "hidden";
			}

		} else if (HumanNS) {
		
			top = pageYOffset;
			left = pageXOffset;
		
			if ((HumanTop < 0) || ((HumanTop == top) && (HumanLeft == left))) {
				document.layers.mylayer.visibility = "visible";
			} else {
				document.layers.mylayer.visibility = "hidden";
			}
		}

		placeImage();

		HumanTop = top;
		HumanLeft = left;
	}

	setTimeout('imageTimer()', 250);
}

function acceptCall()
{
	hideTheImage();

	openChat();
}

function rejectCall()
{
	hideTheImage();

	HumanCmd = "rejectChat";
	HumanCounter = 0;
}

function hideTheImage()
{
	HumanShowImage = false;

	if (HumanIE) {
		document.all.mylayer.style.visibility = "hidden";
	} else if (HumanNS) {
		document.layers.mylayer.visibility = "hidden";
	}
}

function showTheImage()
{
	HumanShowImage = true;

	setImage("need_help","need_help_off.gif");
	setImage("need_close","close_off.gif");

	HumanGo = true;
		
	preload();

	animateStart();
}

function animateStart()
{
	if (HumanIE) {
		HumanBorder = document.body.clientWidth;
	} else if (HumanNS) {
		HumanBorder = window.innerWidth;
	}

	animateImage();
}

function animateImage()
{
	if (imageFetched && HumanGo)
		HumanPos = HumanPos + HumanDir;

	if (HumanPos > HumanBorder - 160)
		HumanDir = - HumanStep;

	placeImage();

	if ((HumanPos > 30) || (HumanDir > 0))
		setTimeout("animateImage()", 20);
}

function placeImage()
{
	var y = 40;

	if (HumanIE) {
		document.all.mylayer.style.left = document.body.scrollLeft + HumanPos;
		document.all.mylayer.style.top = document.body.scrollTop + y;
	} else if (HumanNS) { 
		document.layers.mylayer.left = pageXOffset + HumanPos;
		document.layers.mylayer.top = pageYOffset + y;
	}
}

function go()
{
	loop();
	setTimeout('imageTimer()', 250);
}

if (true)
	go();
