LP = function() {
	
	this.chatButtons = function(){
		return $$('.liveperson');
	}
	
	this.showButtons = function (){
		this.chatButtons().each(function(e){
			//e.show();
			new Effect.Appear(e);
		});
	}
	
	this.hideButtons = function (){
		this.chatButtons().each(function(e){
			//e.hide();
			new Effect.fade(e);
		});
	}

	this.checkOnline = function(){
		
		new Ajax.Request(Vars.get('url') + '/service/livePersonStatus/' , {
			method : 'get',
			onSuccess : function (transport){
				this.onlineResponse(transport.responseText);
			}.bind(this)
		});
	}
	
	this.onlineResponse = function (response){
		if (parseInt(response)){
			this.showButtons();
		}else{
			this.hideButtons();
		}
	}
	
	this.open = function(){
		window.lpButtonCTTUrl = 'http://server.iad.liveperson.net/hc/256883/?cmd=file&file=visitorWantsToChat&site=256883&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/3c&referrer='+escape(document.location); 
		window.lpButtonCTTUrl = (typeof(lpAppendVisitorCookies) != 'undefined' ? lpAppendVisitorCookies(lpButtonCTTUrl) : lpButtonCTTUrl); 
		window.open(lpButtonCTTUrl,'chat256883','width=475,height=400,resizable=yes');
	}
	
	this.init = function() {
		this.chatButtons().each(function(e){
			e.observe('LP:click' , function(){
				this.open();
			}.bindAsEventListener(this));
		}.bind(this));
		this.chatButtons().each(function(e){
			e.observe('click' , function(event){
				
				event.element().fire('LP:click');
			});
		});
		this.checkOnline();
	}
	
	this.init();
		
};

$(document).observe('dom:loaded' , function(){
	
	LP = new LP();
});
