function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}


var $lexx$ = jQuery.noConflict();


var live_chat = false;

var zpa = 0;
var zpb = 0;

var LiveChatPeriodUpdater = null;


String.prototype.trim = function()
{
    return this.replace(/(?:^\s+|\s+$)/g, "");
}

function SendMessage()
{
	var customermessage = document.getElementById('textmessage').value.trim();
	document.getElementById('textmessage').value = '';
	if (customermessage != '')
	{
		var img = document.getElementById('livachat_ajax_loader');
		if (img != null)
		{
			img.style.display = 'inline';
		}
		var request = new Ajax.Updater(
			'livechat_messages',
			urlSendMessage,
			{
			    method: 'get',
			    parameters: { message: customermessage },
			    onSuccess: function(transport, json) {
			        var img = document.getElementById('livechat_ajax_loader');
			        if (img != null) {
			            img.style.display = 'none';
			        }
			        SetUpdater();
			    },
			    insertion: Insertion.Bottom
			}
		);
	}
}

function SetUpdater()
{
	//alert(LiveChatPeriodUpdater);
	if (LiveChatPeriodUpdater == null)
	{
		LiveChatPeriodUpdater = new Ajax.PeriodicalUpdater(
								'livechat_messages',
								urlUpdater,
								{
									frequency: frequency,
									decay: decay
								}
							);
	}
}

function LiveChatKeyPress(e) {
    if (e.keyCode == 13) {
        SendMessage();
    }
}



function showLiveChat(){
	$lexx$('.mini-livechat').css("display", "block");
	$lexx$('.mini-livechat').animate({ "right": "40px"},500);
	live_chat = true;
}

function hideLiveChat(){
	$lexx$('.mini-livechat').animate({ "right": "-410px"},500, function () { $lexx$('.mini-livechat').css("display", "none"); } );
	live_chat = false;
}

function toggleLiveChat(){
if(live_chat){
	hideLiveChat();
} else {
	$lexx$("#livechat_messages b").each( function(index) {
	zpb = index;
	});
	showLiveChat();
}
}

function getColChat(){
$lexx$("#livechat_messages b").each( function(index) {
zpb = index;
});
if(zpa!=zpb && zpb > 1 && live_chat==false){
zpa = zpb;
//setCookie("zpa", zpa);
showLiveChat();
}

if(live_chat) {
//setCoocke("zpa", zpa);
zpa = zpb;
}


}

$lexx$(document).ready(function() {
	$lexx$('.mini-livechat').css("display", "none");
	$lexx$('.mini-livechat').animate({ "right": "-410px"},0);;
	$lexx$('#close-button-chat').click(function() { 	hideLiveChat(); 	});
	$lexx$(".mini-chat-button").click(function() { toggleLiveChat(); });
	//zpa = getCookie("zpa");
	$lexx$("#livechat_messages b").each( function(index) {
		zpb = index;
	});
		zpa = zpb;
	if($lexx$('div.actions').is(":has(#textmessage)")){
	$lexx$(".mini-chat-button").addClass("online");
	window.setInterval(function (){
				 getColChat();
				}, 1000);
	} else {
	$lexx$(".mini-chat-button").addClass("offline");
	}
//	$lexx$(".mini-livechat").hover(function(){ doclose = false;},function(){ doclose = true;}  );



});

