/*
dbButtons Object
(
    [id] => 169
    [siteid] => 1
    [nickname] => PremiumWebCart 1
    [operator_name] => PremiumWebCart
    [online_message] => Hello, I\'m online now and ready to help you.  Please enter your name and email address. <br> - Thank you.
    [branding_image] => 
    [rb_pos] => normal
    [rb_type] => chat
    [imagefile] => /icons/1.png
    [anchor_items] => Array
        (
            [0] => tl
            [1] => tr
            [2] => br
            [3] => bl
        )

    [anchor] => tl
    [offsetx] => 
    [offsety] => 
)

*/
// <Script>

//|
//|  Insert Mootools into the mix
//|


var jsQueue = document.createElement("script");
jsQueue.language = "Javascript";
jsQueue.type = "text/javascript";
jsQueue.src = "http://pwcagent.com/Livechat/js/message_queue.js?v=1";

var jsGeo = document.createElement("script");
jsGeo.language = "Javascript";
jsGeo.type = "text/javascript";
jsGeo.src = "http://j.maxmind.com/app/geoip.js";

var durationCount = 0;
var myWidth = 0, myHeight = 0;
var imgWidth = 130, imgHeight = 60;

setTimeout('appendScript(169);', 300);

function appendScript(btnID)
{
	var chatID = btnID;
	if (document.getElementsByTagName('body') == null)
	{
		setTimeout('appendScript('+chatID+');', 100);
		return;
	}

	document.getElementsByTagName('body')[0].appendChild(jsGeo);		
	document.getElementsByTagName('body')[0].appendChild(jsQueue);	
	
	//|  mootools is now loaded	
	setTimeout('loadChatSystem('+chatID+');', 100);
}


function livechat_getsizes()
{
	
	if ( typeof( window.innerWidth ) == 'number' ) 
	{
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if ( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
}

function livechat_getstyle()
{
	var style = "position: absolute; position: fixed;"

	var dx, dy;
	var anchor = 'tl';
	var offsetx = '';
	var offsety = '';
	
	if ((anchor == 'tl') || (anchor == ""))
	{
		dx = 0;
		dy = 0;
		if(offsetx != '' && offsety != '')
		{
			style += "left:" + offsetx + "px; top:" + offsety + "px;";	
		}	
		else
		{
			style += "left:0px; top:0px;";
			return style; 	 
		}	
	} else 
	if (anchor == 'tr')
	{
		dx = myWidth - imgWidth;
		dy = 0;
		
		if(offsetx != '' && offsety != '')
			style += "right:" + offsetx + "px; top:" + offsety + "px;";	
		else
			style += "right:" + imgWidth + "px; top:0px;";
	} else
	if (anchor == 'br')
	{
		dx = myWidth - imgWidth;
		dy = myHeight - imgHeight;
		if(offsetx != '' && offsety != '')
			style += "right:" + offsetx + "px; bottom:" + offsety + "px;";	
		else
			style += "right:" + imgWidth + "px; bottom:" + imgHeight + "px;";
	} else 
	{
		dx = 0;
		dy = myHeight - imgHeight;
		if(offsetx != '' && offsety != '')
			style += "left:" + offsetx + "px; bottom:" + offsety + "px;";	
		else
			style += "bottom:" + imgHeight + "px;";
	}
	return style;
}

function livechat_link(refHtml)
{
	var str = "<a href='javascript:void(0);' onClick='livechat_open_window();'>";
	str += refHtml;
	str += "</a>";
	return str;
}

function livechat_open_window()
{
	window.open("http://173.203.242.222/Livechat/OpenChat.php?buttonid=169", "livechat",
		"status=0,toolbar=0,width=640,height=540,resizable=0,menubar=0,scrollbars=yes");
}

function loadChatSystem(chatID)
{	
	
	var divID = chatID;

	if (typeof(mq_init) != "function")
	{
		setTimeout('loadChatSystem('+divID+');', 250);		
		return;
	}

	mq_init(1);
		
	mq_register_command("chat", function(data) {
	});
	
	mq_register_command("join", function(data) {		
	});		
	
	mq_register_command("push", function(data) {		
	});

	sendTracingRequest();
	setInterval('sendTracingRequest();', 10000);
	
			//|
		//|  Normally positioned button
			
			var jsTracker 	= document.createElement("div");
			jsTracker.id 	= 'livechat_click_'+divID;
			
			jsTracker.innerHTML = livechat_link("<img src='http://173.203.242.222/Livechat/icons/1.png' border='0' alt='live chat'/>");
			document.getElementsByTagName('body')[0].appendChild(jsTracker);	
			jsTracker.style.cssText = livechat_getstyle();	
	} 


function sendTracingRequest()
{
	durationCount += 1;
	message_queue_poll_data = mq_send("join", { nickname: 'PremiumWebCart 1', username: 'visitor', url: document.URL, duration: durationCount});	
}
