try{
	document.domain = "qq.com";
}catch(e){}

var MGR = {
	domReady:function(){},//回调domready
	expires:0,
	Uname:null,
	cookie:{},
	login:function(){
		$("#login_ifr").attr("src","");
		if($("#login_ifr").attr("src").indexOf("http") < 0){
			var b = window.location + "";
			b = b.indexOf("#") == -1 ? b: b.substring(0, b.indexOf("#"));
			$("#login_ifr").attr("src", "http://ui.ptlogin2.qq.com/cgi-bin/login?f_url=loginerroralert&reset_text=%D6%D8%D6%C3&style=0&appid=13000402&s_url=" + encodeURIComponent(b))
		}
		showDialog.show({id:"loginframe",bgcolor:"#000000",opacity:60});
	},
	logout:function(){
		var e = MGR.cookie;
		e.domain = "qq.com";
		MGR.appcookie("skey", "", e);
		MGR.appcookie("uin", "", e);
		window.location.reload();
	},
	appcookie:function(f, j, m){ 
		var expires = MGR.expires;
		if(typeof j == "undefined"){
			if(0 < document.cookie.length){
				var k = document.cookie.match(new RegExp("(^| )" + f + "=([^;]*)(;|$)"));
				return (null === k) ? "": k[2]
			}
			return ""
		}else{
			if(arguments.callee(f) == j){
				return
			}else{
				m = $.extend({
					expires: 1,
					path: "/",
					domain: window.document.location.hostname
				},
				m || {});
				if(j === null){
					j = "";
					m.expires = -1
				}
				var g = "";
				if(m.expires && (typeof m.expires == "number" || m.expires.toUTCString)){
					var h;
					if(typeof m.expires == "number"){
						h = new Date();
						h.setTime(h.getTime() + (m.expires * 1 * 60 * 60 * 1000))
					}else{
						h = m.expires
					}
					g = h.toUTCString()
				}
				var l = m.path ? "; path=" + (m.path) : "";
				var i = m.domain ? "; domain=" + (m.domain) : jQuery.getHost();
				var e = m.secure ? "; secure": "";
				document.cookie = f + "=" + j + "; path=" + l + "; domain=" + i + "; expires=" + g
			}
		}
	},
	getUinNum:function(f){
		var e = 0;
		if(f.charAt(0) == "o" || f.charAt(0) == "0"){
			e = e + 1;
			while (f.charAt(e) == "0"){
				e++
			}
			f = f.substr(e, f.length)
		}
		return f;
	},
	getUserName:function(){
		$.ajax({
			url: "http://c.pc.qq.com/fcgi-bin/login",
			dataType: "jsonp",
			type: "get",
			cache: false,
			data: "Uin=" + MGR.getUinNum(MGR.appcookie("uin")) + "&skey=" + MGR.appcookie("skey")+"&rd="+Math.random(),
			success:function(a){
				MGR.Uname = a.Uname;
				$("#login_nickname").html(MGR.htmlEncode(a.Uname));
				MGR.getUinInfo();
			}
		})	
	},
	getUinInfo:function(){
		$.ajax({
			url: "http://c.pc.qq.com/fcgi-bin/plogin?cb=userInfoCallback&type=1",
			dataType: "jsonp",
			jsonp: "userInfoCallback",
			type: "get",
			cache: false,
			data: "Uin=" + MGR.getUinNum(MGR.appcookie("uin")) + "&skey=" + MGR.appcookie("skey")+"&rd="+Math.random()
		})
	},
	getSafeInfo:function(){
		$.ajax({
			url: "http://c.pc.qq.com/fcgi-bin/pcmgrsafeinfo?cb=userSafeCallback",
			dataType: "jsonp",
			jsonp: "userSafeCallback",
			type: "get",
			cache: false,
			data: "Uin=" + MGR.getUinNum(MGR.appcookie("uin")) + "&skey=" + MGR.appcookie("skey")+"&rd="+Math.random()
		})
	},
	checkLogin:function(){
		if(MGR.appcookie("uin")){
			return true;
		}else{
			return false;
		}
	},
	titleStatus:function(a){
		if(a.ret=="suc"){
			$("#logined").show();
			$("#nologin").hide();
			$("#face").attr("src","http://q4.qlogo.cn/g?b=qq&nk="+MGR.getUinNum(MGR.appcookie("uin"))+"&s=40");
			if(a.icon_status==1){
				$("#iconElm").attr("class","icon_on");
			}
			MGR.getSafeInfo();
		}else{
			$("#nologin").show();
			//MGR.logout();
		}
	},
	safeStatus:function(a){
		if(a.ret=="suc"){
			$("#safeElm").attr("class","safe_lv"+a.safe_level);	
			$("#safejb").html(a.now_score);
		}
	},
	init:function(){
		var d = document,
		expire = MGR.expires = new Date(( + new Date()) + 1 * 3600000),
		cookie = MGR.cookie = {expires:expire,path:"/",domain:window.document.location.host},
		nav = d.createElement("div"),
		ifrDiv = d.createElement("div"),
		initEvent=function(){
			$('#mgr_nav_login').click(MGR.login);
			$('#mgr_nav_logout').click(MGR.logout);	
			if(MGR.checkLogin()){
				MGR.getUserName();
			}else{
				$("#nologin").show();
			}
			MGR.fixMac();
		},
		initHeader=function(){		
			ifrDiv.className = "loginframe";
			ifrDiv.id = "loginframe";
			ifrDiv.innerHTML = '<iframe name="login_ifr" id="login_ifr" frameborder="0" scrolling="no" width="100%" height="100%" src=""></iframe>';
			d.body.appendChild(ifrDiv);
		},
		loadScript=function(url,callback){
			var script = document.createElement("script");
			script.type = "text/javascript";
			if(script.readyState){//IE
				script.onreadystatechange = function(){
					if(script.readyState == "loaded" || script.readyState == "complete"){
						script.onreadystatechange = null;
						if(typeof callback==="function"){callback();}
					}
				}
			}else{ 
				script.onload = function(){
					if(typeof callback==="function"){callback();}
				}
			}	
			script.src = url;
			document.getElementsByTagName("head")[0].appendChild(script);
		};
		//load jquery
		loadScript("http://pc1.gtimg.com/js/jquery-1.4.4.min.js",function(){
			$(function(){
				initHeader();
				initEvent();
				MGR.domReady();
				MGR.initNav();
				MGR.sent_tcss();
			})
		});
		loadScript("http://pc1.gtimg.com/js/showDialog.fix.js");
	},
	initNav:function(){
		var n = $("body").attr("pageIndex");
		var m = $("body").attr("subIndex");
		var snav = $("#navline");
		slideTo = function(index){
			var nobj = $("#nav >li").eq(index-1);
			var pos = $("#nav >li").eq(index-1).position().left;
			snav.stop().animate({width:nobj.width(),left:pos}, "fast");
		}
		slideTo(n);
		$("#nav >li").bind("mouseenter",function(){
			$(this).children("ul").stop(true,true).slideDown();
			$(this).addClass("set");
			slideTo($("#nav >li").index(this)+1);
		}).bind("mouseleave",function(){
			$(this).children("ul").stop(true,true).slideUp();
			$(this).removeClass("set");
			slideTo(n);
		})
		var tu;
		$("#face,.user_info").bind("mouseenter",function(){
			$(".user_info").show();
			clearTimeout(tu);
		}).bind("mouseleave",function(){
			tu = setTimeout('$(".user_info").hide()',300);
		})
	},
	htmlEncode:function(sStr){
		sStr = sStr.replace(/&/g,"&amp;");
		sStr = sStr.replace(/>/g,"&gt;");
		sStr = sStr.replace(/</g,"&lt;");
		sStr = sStr.replace(/"/g,"&quot;");
		sStr = sStr.replace(/'/g,"&#39;");
		return sStr;
	},
	sent_tcss:function(){
		$.ajax({
			type: 'GET',
			url: 'http://pingjs.qq.com/ping.js',
			success: function(){if(typeof(pgvMain) == 'function'){pgvMain()}},
			dataType: 'script',
			cache: true
		});
		
		$(document).click(function (evt) {
			var tagPrefix = "pcmgr.new"
			var tagAttr = 'data-stats';
			var tagPattern = /^(?!\.)[\w-]+(?:\.[\w-]+)*?$/;
			// check feature
			if ('function' !== typeof(pgvSendClick)) {
				return;
			}
				// get current target
			var curTar = evt.target;
			if (!curTar || 1 !== curTar.nodeType) {
				return;
			}
				// get closest clickable element
			var clickableElm = $(curTar).closest('a[' + tagAttr + '],input[' + tagAttr + ']');
			if (0 === clickableElm.length) {
			return;
			}
				// get hot tag
			var hotTag = clickableElm.attr(tagAttr);
			if (!tagPattern.test(hotTag)) {
			return;
			}
				// send log
			var objArg = {
			'hottag' : tagPrefix + '.' + hotTag
			};
			//virtualDomain && (objArg['virtualDomain'] = virtualDomain);
			pgvSendClick(objArg);
		})
	},
	fixMac:function(){
		if((window.navigator.userAgent).toLowerCase().indexOf("mac os") > 0){
			$.getScript("/assets/js/mac_mask.js?t="+Math.random()); 
		}	
	}
}
function ptlogin2_onResize(a, b) {
    $("#loginframe").css({
        width: a + "px",
        height: b + "px"
    })
}
function ptlogin2_onClose() {
    showDialog.hide()
}
function userInfoCallback(a){
	MGR.titleStatus(a);
}
function userSafeCallback(a){
	MGR.safeStatus(a);
}

MGR.init();//初始化