function Cookie(document, name, hours, path, domain, secure)
{
    this.$document = document;
    this.$name = name;
    if (hours)
        this.$expiration = new Date((new Date()).getTime() + hours*3600000);
    else this.$expiration = null;
    if (path) this.$path = path; else this.$path = null;
    if (domain) this.$domain = domain; else this.$domain = null;
    if (secure) this.$secure = true; else this.$secure = false;
}

Cookie.prototype.store = function () {
    var cookieval = "";
    for(var prop in this) {
        if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')) 
            continue;
        if (cookieval != "") cookieval += '|';
        cookieval += prop + ':' + escape(this[prop]);
    }
    var cookie = this.$name + '=' + cookieval;
    if (this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    if (this.$secure) cookie += '; secure';
    this.$document.cookie = cookie;
}

Cookie.prototype.load = function() { 
    var allcookies = this.$document.cookie;
		if (allcookies == "") return false;

    var cookieval = getCookie(this.$name);
    if (cookieval == "") return false;

    var a = cookieval.split('|');    
    for(var i=0; i < a.length; i++) 
        a[i] = a[i].split(':');
    
    for(var i = 0; i < a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }
    return true;
}

Cookie.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    if (this.$path) cookie += '; path=' + this.$path;
    if (this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
    this.$document.cookie = cookie;
}

function is_logon()
{
	var uin = parseInt(getCookie("zzpaneluin"));
	if(uin > 10000)
	{
		return true;
	}
	var cookie_uin = getCookie("uin");
	var re = /^o(0)*/;
	var tmp_cookie_uin = cookie_uin.replace(re,'');
	uin = parseInt(tmp_cookie_uin);
	return ((uin>10000)?true:false)
}

function getQQNum()
{
	var iLoginUin = parseInt(getCookie("zzpaneluin"));
	if (iLoginUin >10000 && iLoginUin < 10000000000)
	{
	return iLoginUin;
	}
	var cookie_uin = getCookie("uin");
	var re = /^o(0)*/;
	var tmp_cookie_uin = cookie_uin.replace(re,'');
	var qq = parseInt(tmp_cookie_uin);
	if (qq > 10000 && qq < 10000000000)
	{
		return qq;
	}
	return 0;
}

function getCookie(name){
	var r=new RegExp("(^|;|\\s+)"+name+"=([^;]*)(;|$)");
	var m=document.cookie.match(r);
	return(!m?"":m[2]);
}

function fullzero(b,c)
{
	if(b.length<c)
	{
	  var pad=c-b.length;
    for(var i=0;i<pad;i++)
		{
			b='0'+b;
		}
	}
	return b;
}

function b2bin(b)
{
	if(b.length<16)
		b=fullzero(b,16);

	var binStr='';
	for(var i=0;i<8;i++)
	{
	  var tmp='0x'+b.substr(i*2,2);
		tmp = parseInt(tmp);
    binStr+=fullzero(tmp.toString(2),8);
	}
	return binStr;
}

function is_q(c)
{
	if(c != undefined && c.b != undefined)
	{
		var b=b2bin(c.b);
		if(b.length == 64 && b.substr(63,1)==1) return true;
	}
	return false;
}

function isPortal()
{
	var domain=document.location.hostname;
	return (domain=='qzone.qq.com' || domain=='php.qzone.qq.com' || domain=='life.qzone.qq.com');
}

function ping_city()
{
	var c=new Cookie(document,"qzone_city_key",null,'/','qq.com');
	var r=c.load();
	if(!is_logon() && r) return;
	if(r && c.b!='' && c.b!=undefined && !is_q(c)) return;
	var cDate=new Date().getTime();
	if(r && c.b!='' && c.b!=undefined && c.c!=undefined && (cDate<c.c)) return;
	if(r==false || c.b=='' || c.b==undefined || (r && getQQNum()!=c.uin))
	{
		c.remove();
		c.store();
		var img=new Image(1,1);
		if(isPortal()){
			img.src="http://city.qzone.qq.com/pingcity.php?portal=1&"+Math.random();
		}else{
			img.src="http://city.qzone.qq.com/pingcity.php?"+Math.random();
		}
		return;
	}
	c.c = new Date(new Date().getTime() + 180000 ).getTime();
	c.store();
	var img=new Image(1,1);
	if(isPortal()){
		img.src="http://city.qzone.qq.com/pingcity.php?portal=1&"+Math.random();
	}else{
		img.src="http://city.qzone.qq.com/pingcity.php?"+Math.random();
	}
	return;
}
setTimeout("ping_city()",3000);