

function wimsetcookie(name, value, expires, path, domain, secure) {
	if (!name || !value) return false;
	var str = name + '=' + encodeURIComponent(value);
	var exp = new Date(expires*1000);
	
	if (expires) str += '; expires=' + exp;
	if (path)    str += '; path=' + path;
	if (domain)  str += '; domain=' + domain;
	if (secure)  str += '; secure';
	
	document.cookie = str;
	return true;
}



function wimgetcookie(name) {
	var pattern = "(?:; )?" + name + "=([^;]*);?";
	var regexp  = new RegExp(pattern);
	
	if (regexp.test(document.cookie))
	return decodeURIComponent(RegExp["$1"]);
	
	return false;
}

function wimdeletecookie(name, path, domain) {
	wimsetcookie(name, null, new Date(0), path, domain);
	return true;
}

function wimiscookieenabled()
{
		
		if(typeof(navigator.cookieEnabled) != "undefined")
			return navigator.cookieEnabled;
		else
		{
			var tmpcookie = "testCookieForCheck";
			wimsetcookie(tmpcookie, "1");
			if(wimgetcookie(tmpcookie))
  		{
  			wimdeletecookie(tmpcookie);
  			return true;
  		}
			return false;
  	}
}


function wimwebcookie()
{
 var wimuid=wimgetcookie("wimuid");
 var wimpass=wimgetcookie("wimpass");
 var href = window.location.href;
 var ref =document.referrer;
 var tit = document.title;
 href =(href.replace(/&/g, '(*amp)')); 
 ref = (ref.replace(/&/g, '(*amp)')); 
 tit = (tit.replace(/&/g, '(*amp)')); 
 href =(href.replace(/#/g, '(*hash)')); 
 ref = (ref.replace(/#/g, '(*hash)')); 
 tit = (tit.replace(/#/g, '(*hash)')); 
 $.getJSON(wim_path+"wim.php?id="+wimuid+"&pass="+wimpass+"&page="+href+"&ref="+ref+"&tit="+tit+"&type=cookie&rnd="+Math.random()+"&callback=?", function(data){

  					
				wimsetcookie("wimuid",data.cookie,data.cookie_time);
				wimsetcookie("wimpass",data.pass,data.cookie_time);
				wim_uid=data.cookie;
				wim_pass=data.pass;
				wimgetstatus();
				setInterval("wimgetstatus()",5000);
	  					
			
 		});
 		return false;

 		}
 		
jQuery.preloadImages = function() {
    jQuery.each (arguments,function (e) {
        jQuery("<img>").attr("src", this);
    });
} 	
$.preloadImages(wim_path+"images/online.gif",wim_path+"images/offline.gif");
	
 		



function wimchatrequest(text) 
{
$("#wim_chatrequest").html(text);
}

function getchatstatus()
{
$.getJSON(wim_path+"wim.php?id="+wim_uid+"&pass="+wim_pass+"&type=getchatstatus&rnd="+Math.random()+"&callback=?", function(data){
if(data.status==1)
{return true;}
}
);
return false;
}

function wimchat() 
{if (!getchatstatus())
{wim_popup = window.open(wim_path+"wim.php?id="+wim_uid+"&pass="+wim_pass+"&type=chat&rnd="+Math.random(), "", "width=450, height=450, top=0"); wim_popup.focus();}
}

function wimgetstatus() 
{
 $.getJSON(wim_path+"wim.php?id="+wim_uid+"&pass="+wim_pass+"&type=getstatus&rnd="+Math.random()+"&callback=?", function(data){

			
			if(data.status!=1)
  			{
				wimoffline();
  			}
  			else
  			{ 		
				wimonline(); wimchatrequest("testtesttest");
				if (data.chat==0) {wimchatrequest("testtesttest");}
	  		}  			
			
 		});
 		return false;

 		} 	
 		
 		
function wimonline()
{
$("#wim_button").html("<img style='cursor: pointer;' onclick='javascript: wimchat();' src='"+wim_path+"images/online.gif'>");
$("#wim_chatrequest").show();
}
function wimoffline()
{
$("#wim_button").html("<img style='cursor: pointer;' onclick='javascript: wimchat();' src='"+wim_path+"images/offline.gif'>");
$("#wim_chatrequest").hide();
} 		

function wimsendmessage () {
	  var wim_message = $("#wim_message").val();
    if (wim_message)
 
	  {$.post(wim_path+"wim.php?id="+wim_uid+"&pass="+wim_pass+"&type=sendmessage&rnd="+Math.random(),{ message: wim_message }, 
	  function(data)
	  {
	  if (data.status==1)
	  {wimgetmessage(); $("#wim_message").val("");}
	  },"json");}
	}


function wimgetmessage () {
	  $.getJSON(wim_path+"wim.php?id="+wim_uid+"&pass="+wim_pass+"&lastmessage="+wim_lastmessage+"&type=getmessage&rnd="+Math.random()+"&callback=?", 
	  function(data)
	  {
	  if (data.status==1)
	  {$("#wim_allmessage").append(data.text); $("#wim_allmessage").scrollTop($("#wim_allmessage").attr('scrollHeight')); wim_lastmessage=data.wim_lastmessage;}
	  });
	}

var wim_chat;
var wim_uid;
var wim_pass;
var wim_popup;
var wim_lastmessage=0;

if (wimiscookieenabled()) 
{
wimwebcookie();
}
