/**
* LiveChatInvite
*/
function LiveChatInvite(settings){
    /**
    * Class Constructor 
    */
	this.conf = settings;

    this.d = document;
    this.invite_layer = 'lc_invite_layer';
	
    this.private_invite_id = 'LC_Private_Invite';
    this.invite_body = ''
    this.httpp = (window.location.protocol.indexOf('https') != -1) ? 'https://' : 'http://';
    this.isPrivate = false;
    
    this.lc_timerID;
    this.lc_timerID_wait;

    this.lc_controlImage;


	// check for IE6
	this.ie6 = false;
	/*@cc_on var ua = window.navigator.userAgent, ie = ua.indexOf("MSIE "); this.ie6 = ((ie > 0) ? parseInt(ua.substring(ie+5, ua.indexOf(".", ie))) <= 6 : 0);	@*/

    this.lc_MC = 0;
    this.lc_Z = 1;
    this.lc_W = 0;
    this.lc_I = 0;
    this.lc_R = 5000;

	// overlay settings
	(this.conf.overlay) ? this.conf.overlay.id = 'lc_overlay_layer' : false;

    this.gRoutine = function()
    {
        if((this.conf.analytics_id.indexOf('UA-') != -1) && (typeof window.urchinTracker == 'function')){
            _uacct = this.conf.analytics_id;
            _udn = "none";
            _ulink = 1;
            urchinTracker();
            return true;
        }
        return false;
    };
    
    
    this.gRequest = function()
    {
        if(!this.gRoutine()){ return; }
        try{
            var path = 'ContactCenter Invites/';
            urchinTracker(path);
            __utmSetVar(path);
        }catch(e){}
    };
    

    this.lc_send_message = function() 
    {
        if (this.lc_timerID) { clearTimeout(this.lc_timerID); }
        if (this.lc_timerID_wait) { clearTimeout(this.lc_timerID_wait); }
  
        this.lc_controlImage = new Image;
        pageDate = new Date;
        currTime = pageDate.getTime();
  
        u = this.httpp + this.conf.serv+'/licence/'+this.conf.lic+'/control.cgi';
        u = u + '?' + currTime;
        u = u + '&'+this.conf.lc.session;
  
        if (this.lc_MC == 0) {
            u = u + '&' + this.conf.hostname;
            u = u + '&' + encodeURIComponent(document.location);
   
            if(document.referrer == '') {
                u = u + '&$';
            } else {
                u = u + '&' + encodeURIComponent(document.referrer);
            }
    
            u += '&'+this.conf.lc.last_visit;
            u += '&'+this.conf.lic;
            u += '&' + encodeURIComponent(this.conf.nick);
            u += '&'+this.conf.lc.page_view;
            u += '&'+this.conf.lc.visit_number;
            u += '&'+this.conf.lc.chat_number;
            u += '&'+this.conf.lc.all_invitation;
            u += '&'+this.conf.lc.ok_invitation;
            u += '&'+this.conf.lc.last_operator_id;
            u += '&'+this.conf.lc.client_version;
            u += '&'+this.conf.lc.params;
        
            if(document.title == '') {
                u = u + '&$';
            } else {
                u = u + '&' + encodeURI(document.title);
            }
            
            u = u + '&'+this.conf.lc.groups;
        }

        this.lc_MC++;
        this.lc_controlImage.src = u;
        this.lc_timerID = setTimeout('LC_Invite.lc_send_message()', this.lc_R);
        this.lc_timerID_wait = setTimeout('LC_Invite.lc_after_send()', 500);
    };
    
    this.lc_after_send = function() 
    {
    
    
        if (this.lc_timerID_wait) { clearTimeout(this.lc_timerID_wait); }
        if (this.lc_controlImage.complete == true) {
        
            var w = this.lc_controlImage.width;
            
            if ((w == 33) || (w == 34) || (w == 24)) {
                if (this.lc_I == 0 && this.lc_Z == 1) {
                    if (this.conf.quickchat) {
                        window.open('' + httpp + 'chat.livechatinc.net/licence/1004542/open_chat.cgi?invite=1&lang=pl&groups=0' + '&dc=' + encodeURIComponent(document.cookie+';l='+document.location+';r='+document.referer+';s='+typeof lc_session),'chat_'+this.conf.lic,'width=529,height=520,resizable=yes,scrollbars=no,status='+this.conf.status); 
                    } else if (w == 34) {
                        this.lc_load_invite_ex();
                    } else {
                        this.lc_load_invite();
                    }
      
                } else {
                    if (this.lc_I == 0 && this.lc_W == 0) { this.lc_Z = 1; }
                }
    
            } else if (w == 35) {
                this.lc_MC = 0;
            } else if (( w >=128) && (w < 255)) {
                this.lc_R = (w - 127) * 1000;
            } else if (w == 255) {
                if (this.lc_timerID) { clearTimeout(this.lc_timerID); }
                if (this.lc_timerID_wait) { clearTimeout(this.lc_timerID_wait); } 
            } else {
                this.lc_W = 0;
                this.lc_Z = 1;
            }
    
            if (this.lc_I > 0) { 
                this.lc_I--; 
            }
  
		}
		else {
			this.lc_timerID_wait = setTimeout('LC_Invite.lc_after_send()', 500);
		}

    };
    

    this.drawInvite = function(){
		
		var invdiv = document.createElement('div');
		invdiv.setAttribute('id', this.invite_layer);

		if(this.conf.overlay){
			invdiv.style.position = 'fixed';
			invdiv.style.left = invdiv.style.top = '50%';
		}
		else{
			invdiv.style.position = 'absolute';
		}
		invdiv.style.display = 'none';
		invdiv.style.zIndex = 2147483641;

        this.invite_body ='<div style="position:relative">';
        this.invite_body+='<a style="display:block;position:absolute;top:0;right:0;width:70px;height:25px;outline:none;background:url('+this.httpp+this.conf.serv+'/server/images/pixel.gif)" href="javascript:void(0)" onclick="LC_Invite.lc_popup_close()"></a>';
		this.invite_body+='<a href="javascript:void(0)" onclick="LC_Invite.lc_open_chat()" style="display:block;outline:none" id="lc_button_wrapper"></a>';
        this.invite_body+='</div>';


	    invdiv.innerHTML = this.invite_body;	
	
		if(this.conf.overlay){
			var overlayElem = document.createElement('div');
			
			overlayElem.setAttribute('id', this.conf.overlay.id);
			var es=overlayElem.style;
			es.backgroundColor= this.conf.overlay.color;
			es.opacity = this.conf.overlay.opacity;
			es.position = 'fixed';
			es.left = 0;
			es.top = 0;
			es.zIndex = 2147483640;
			es.display = 'none';
			es.width = '100%';
			es.height = '100%';
			
			document.getElementsByTagName("body").item(0).appendChild(overlayElem);
		}

		document.getElementsByTagName("body").item(0).appendChild(invdiv);

		var image = new Image();
		image.src = this.httpp + this.conf.serv+this.conf.invite_img_name;
		image.border=0;
		image.style.display='block';
		
		this.inviteImage = image;
		
		if(this.conf.overlay){
			__positionInvitation = function(){
				if(image.width==0){
					setTimeout("__positionInvitation()", 30);
				}
				else{
					invdiv.style.marginLeft = "-"+parseInt(image.width/2)+"px";
					invdiv.style.marginTop = "-"+parseInt(image.height/2)+"px";
				}
			};
			
			__positionInvitation();
		}
	
		var lcbw = document.getElementById('lc_button_wrapper');

		(!this.ie6) && (lcbw.appendChild(image));

		/* position:fixed for IE6 and browsers in Quirks Mode */
		if ((this.inQuirksMode() || this.ie6) && this.conf.overlay)
		{
			invdiv.style.position = 'absolute';
			es.position = 'absolute';
			es.height = this.getClientHeight();
			es.width = this.getClientWidth();
			
			setInterval (function(){
				es.top = LC_Invite.getScrollTop();
				invdiv.style.marginTop = 0;
				invdiv.style.top = LC_Invite.getScrollTop() + LC_Invite.getClientHeight()/2 - image.height/2;
			}, 10);
		}
		if(this.ie6){
			var lcbw = document.getElementById('lc_button_wrapper');
			lcbw.innerHTML = '<div style="display: block; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+image.src+'\', sizingMethod=\'crop\'); width: '+image.width+'px; height: '+image.width+'px"></div>';
		}

		this.lc_send_message();
	};


	this.lc_load_invite = function(){
		this.gRequest();
		this.lc_popup_show();
		var toAnimate = this.lc_animate(this.invite_layer);

		(toAnimate) && (toAnimate.floatIt());
	};


    this.lc_load_invite_ex = function(){
		this.gRequest();
		this.isPrivate = true;
		var httpp = this.httpp;
		var head = document.getElementsByTagName('head')[0];
        
		script = document.createElement('script');
		script.id = this.private_invite_id;
		script.type = 'text/javascript';
		script.src = httpp + this.conf.serv + "/invite.cgi?status="+this.conf.status+"&licence="+this.conf.lic+"&lang="+this.conf.lang+"&skill="+this.conf.lc.groups+"&sess="+this.conf.lc.session+"&base_url=" + httpp + this.conf.serv+"&redirect=" + encodeURIComponent('' + httpp + 'chat.livechatinc.net/licence/1004542/open_chat.cgi?invite=1&lang=pl&groups=0') + "&rand=" + Math.floor(Math.random() * 1000);
		head.appendChild(script);
	};

    this.lc_popup_hide = function(){
		element = document.getElementById(this.invite_layer);
		element.style.display = "none";
		if(this.isPrivate){
			LC_PrivateInvite.removeJs(this.private_invite_id);
			element.innerHTML = this.invite_body;
		}
		(this.conf.overlay) && (document.getElementById(this.conf.overlay.id).style.display='none');
	};


	this.lc_popup_show = function() 
	{
		if(this.conf.overlay){
			this.lc_overlay_show();
		}
		element = document.getElementById(this.invite_layer);
		element.style.display = "block";
	};

	this.lc_overlay_show = function(){
		var element = document.getElementById(this.conf.overlay.id);

		changeOpac(0, this.conf.overlay.id);
		element.style.display="block";
		
		opacity(this.conf.overlay.id, '0', this.conf.overlay.opacity*100, this.conf.overlay.speed);
	};
	

	this.lc_popup_close = function(){
		this.lc_popup_hide();
		this.lc_logonImage = new Image;
		this.lc_logonImage.src = this.httpp + this.conf.serv+'/tunnel.cgi?IWCS0014C^inviterefused^'+this.conf.lc.session+'^$^'+"&rand=" + Math.floor(Math.random() * 1000);
		clearTimeout(this.floatItTimeout);
		if(this.conf.overlay){
			var overlay = document.getElementById(this.conf.overlay.id);
			overlay.style.display='none';
		}
    };


	this.lc_open_chat = function(){
		this.lc_popup_hide();
		var httpp = this.httpp;
		win = window.open(decodeURIComponent('' + httpp + 'chat.livechatinc.net/licence/1004542/open_chat.cgi?invite=1&lang=pl&groups=0') + '&dc=' + encodeURIComponent(document.cookie + ';l=' + document.location + ';r=' + document.referer + ';s=' + typeof lc_session), 'chat_'+this.conf.lic, 'width=529,height=520,resizable=1,scrollbars=0,status=1');
    };

    this.DomReadyMonitoring = function(){
		LC_Invite.drawInvite();
    };


	this.inQuirksMode = function()
	{
		return ((window.navigator.userAgent.indexOf("MSIE ") >= 0) && document.compatMode && document.compatMode == 'BackCompat');
	};

	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	this.getPageSize = function()
	{
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}

		return new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	};

	this.getClientWidth = function()
	{
		return this.getPageSize()[2];
	};

	this.getClientHeight = function()
	{
		return this.getPageSize()[3];
	};

	// getPageScroll()
	// Returns array with x,y page scroll values.
	// Core code from - quirksmode.org
	//
	this.getScrollTop = function(){

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		return yScroll;
	};


	this.lc_animate = function(id){
		if(!this.conf.overlay){
			var d = document;
			var de = d.documentElement;
			var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
			var px = document.layers ? "" : "px";
			window[id + "_obj"] = el;
			if(d.layers)el.style=el;
			
			var pos = this.conf.position;
			var that=this;
			
			el.cx = el.sx = pos.arg2;
			el.cy = el.sy = pos.arg1;
			
			el.sP = function(x,y){
				var bottom = -2*pos.arg1 + LC_Invite.getClientHeight() - (that.inviteImage.height);
				if(pos.option=='topLeft'){
					this.style.left=x+px;
					this.style.top=y+px;
				}
				if(pos.option=='topRight'){
					this.style.right=x+px;
					this.style.top=y+px;
				}
				if(pos.option=='topCenter'){
					this.style.top=y+px;
					this.style.left = '50%';
					this.style.marginLeft = -parseInt(that.inviteImage.width/2)+'px';
				}
				if(pos.option=='bottomRight'){
					this.style.right=x+px;
					this.style.top=y+bottom+px;
				}
				if(pos.option=='bottomLeft'){
					this.style.top=y+bottom+px;
					this.style.left=x+px;
				}
				if(pos.option=='bottomCenter'){
					this.style.top=y+bottom+px;
					this.style.left = '50%';
					this.style.marginLeft = -parseInt(that.inviteImage.width/2)+'px';
				}
			};


			el.floatIt=function(){
				var pX, pY;
				pX = (this.sx >= 0) ? 0 : LC_Invite.getClientWidth();
				pY = LC_Invite.getScrollTop();

				if(this.sy<0) 
					pY += LC_Invite.getClientHeight();

				
				this.cx += (pX + this.sx - this.cx)/8;
				this.cy += (pY + this.sy - this.cy)/8;


				this.sP(this.cx, this.cy);
				that.floatItTimeout = setTimeout(this.id + "_obj.floatIt()", 20);
			}
			return el;
		}
		return false;
	}

}


function opacity(id, opacStart, opacEnd, millisec) { 
	//speed for each frame 
	var speed = Math.round(millisec / 85); 
	var timer = 0; 

	if(opacStart < opacEnd) { 
		for(i = opacStart; i <= opacEnd; i++) 
		{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++;
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) { 
	var object = document.getElementById(id).style; 
	object.opacity = object.MozOpacity = object.KhtmlOpacity = (opacity / 100); 
	object.filter = "alpha(opacity=" + opacity + ")";
}


function __addOnload(myfunc)
{
	if (document.readyState === 'complete') {
		return myfunc();
	}

	if(window.addEventListener){
		window.addEventListener('load', myfunc, false);
	}
	else if(window.attachEvent){
		window.attachEvent('onload', myfunc);
	}
}


var __lc_settings = {
	position: {
		option: 			'left',
		arg1: 				50,
		arg2: 				250
	},
	overlay: {
		opacity: 			0.7,
		id: 				'lc_overlay_layer',
		color: 				'#000',
		speed: 				700
	},
	lc: {
		last_visit: 		'1280465016',
		session: 			'S1280465016.fbadad59da',
		page_view:			'1',
		visit_number:		'1',
		chat_number:		'0',
		all_invitation: 	'0',
		ok_invitation: 		'0',
		last_operator_id: 	'$',
		client_version: 	'$',
		params: 			'$',
		groups: 			'0'
	},
	lic: 					1004542,
	serv: 					'chat.livechatinc.net',
	analytics_id: 			'',
	nick: 					'$',
	hostname: 				'$',
	quickchat: 				0,
	status: 				'1',
	invite_img_name: 		'/licence/1004542/gfx/invite_pl.gif',
	lang: 					'pl'
}

// backwards compatibility
if(__lc_settings.position.option == 'left'){
	__lc_settings.position.option = 'centered';
	__lc_settings.position.arg1 = 0;
	__lc_settings.position.arg2 = 0;
}

if(__lc_settings.position.option != 'centered'){
	__lc_settings.overlay = false;
}

var LC_Invite = new LiveChatInvite(__lc_settings);
__addOnload(LC_Invite.DomReadyMonitoring);