
/* show the image in the header */
document.write('<a href="/livechat/client.php?locale=en" target="_blank" onclick="if(navigator.userAgent.toLowerCase().indexOf(\'opera\') != -1 &amp;&amp; window.event.preventDefault) window.event.preventDefault();this.newWindow = window.open(\'/livechat/client.php?locale=en&amp;url=\'+escape(document.location.href)+\'&amp;referrer=\'+escape(document.referrer), \'webim\', \'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1\');this.newWindow.focus();this.newWindow.opener=window;return false;"><img style="position:absolute; left:210px; top:20px;" src="/livechat/b.php?i=inlay01&amp;lang=en" border="0" width="120" height="30" alt=""/></a>');

/* create the floating popup */
document.write('<div id="lcPopup" style="display:none; width:306px; height:176px; position:absolute; left:40%; top:20%; z-index:1000;">');
document.write('	<img src="/livechat/images/popup.png" width="306" height="176" usemap="#liveChatPopupMap">');
document.write('	<map name="liveChatPopupMap" id="liveChatPopupMap">');
document.write('		<area shape="rect" coords="85,80,210,115" href="/livechat/client.php?locale=en" target="_blank" onclick="if(navigator.userAgent.toLowerCase().indexOf(\'opera\') != -1 &amp;&amp; window.event.preventDefault) window.event.preventDefault(); startChatSession(); return false;" alt="Start Chat" title="Start Chat Session">');
document.write('		<area shape="rect" coords="250,7,270,30" href="javascript:hideLiveChatPopup();" alt="Close" title="Close this popup">');
document.write('	</map>');
document.write('</div>');




/* run this when the DOM is ready */
jQuery(document).ready(liveChatPopup);


/* how long the visitor needs to be browsing the site before we show the popup (seconds) */
var lcp_visitTime = 30;

/* delay from when the page has loaded to when we show the live chat popup (seconds) */
var lcp_delay = 10;

/* we hide the popup after it's been shown for this time (seconds) */
var lcp_hidePopup = 60;

var lcp_fv = 0;
var lcp_lc = 0;
var lcp_shown = 0;

function liveChatPopup()
{
	var d = new Date();
	var t = d.getTime() / 1000;	// in seconds!
	var myCookie = readCookie("_lcp");

	if( myCookie == null )
	{
		lcp_fv = t;
		lcp_lc = t;
		lcp_shown = 0;
	}
	else
	{
		var v = myCookie.split('|');
		for(var i=0 ; i<v.length ; i++)
		{
			var p = v[i].split('=');
			switch( p[0] )
			{
				case 'fv':
					lcp_fv = p[1];
					break;

				case 'lc':
					lcp_lc = p[1];
					break;

				case 's':
					lcp_shown = p[1];
					break;
			}
		}

		/* update value */
		lcp_lc = t;
	}

	/* save cookie */
	createCookie("_lcp", "fv=" + lcp_fv + "|lc=" + lcp_lc + "|s=" + lcp_shown);

	if( lcp_shown == 0 )
	{
		var chat = jQuery.getJSON('/livechat/a.php', function(data) {
			if( data['online'] == true )
			{
				var timeout = lcp_visitTime - (lcp_lc - lcp_fv);
				if( timeout < lcp_delay )
				{
					timeout = lcp_delay;
				}
				setTimeout("showLiveChatPopup()", timeout * 1000);
			}
		});
	}
}

function showLiveChatPopup()
{
	/* save cookie */
	lcp_shown = 1;
	createCookie("_lcp", "fv=" + lcp_fv + "|lc=" + lcp_lc + "|s=" + lcp_shown);

	var myDiv = document.getElementById("lcPopup");
	jQuery(myDiv).show('drop', {}, 'slow');

	setTimeout("hideLiveChatPopup()", lcp_hidePopup * 1000);
}

function hideLiveChatPopup()
{
	var myDiv = document.getElementById("lcPopup");
	jQuery(myDiv).fadeOut("slow");
}

function startChatSession()
{
	this.newWindow = window.open('/livechat/client.php?locale=en&amp;url=' + escape(document.location.href) + '&amp;referrer=' + escape(document.referrer), 'webim', 'toolbar=0,scrollbars=0,location=0,status=1,menubar=0,width=640,height=480,resizable=1');
	this.newWindow.focus();
	this.newWindow.opener = window;
	hideLiveChatPopup();
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
		var expires = "";
	document.cookie = name + "=" + value + expires + "; path=/";
}

