﻿//QZone的URL
var QZONE_BLOG_URL = "http://user.qzone.qq.com/";

var QZONE_CITY_URL = "http://imgcache.qq.com/qzone/city/data/qc2.js";

var g_dns_name = null;
var g_iUin = checkLogin();
var imgcacheDomain = "imgcache.qq.com";

var g_iLoginUin = checkLogin();
//用于判断LoginBox是否打开的标记
var LOGINBOX_FLAG = false;

/**
 * 在指定的容器里生成随机的城市达人列表
 * @param {HTMLElement} container 用于存放列表的容器
 * @param {ArrayCollection} dp 数据源
 * @param {uint} num 显示的条数
 */
function createFriendList(container, dp, num){
	var _i = [];
	var len = 0;
	for(var i=0; len < num; i++){
		var index = Math.floor(Math.random() * dp.length);
		if (!haveIndex(_i, index)) {
			_i.push(index);
			len ++;
		}
	}
	var ul = document.createElement("ul");
	for(i=0; i<_i.length; i++){
		var li = document.createElement("li");
		var item = [];
		item.push('<div><a href="' + QZONE_BLOG_URL + dp[_i[i]].uin + '" target="_blank"><img src="' + dp[_i[i]].img + '" alt="' + dp[_i[i]].nick + '" style="width:100px; height:100px;" /></a></div>');
		item.push('<p class="name_friend"><a href="' + QZONE_BLOG_URL + dp[_i[i]].uin + '" target="_blank">' + dp[_i[i]].nick + '</a></p>');
		item.push('<p class="name_daren">' + dp[_i[i]].title + '达人</p>');
		li.innerHTML = item.join("");
		ul.appendChild(li);
	}
	container.appendChild(ul);
}

/**
 * 检查是否该索引存在
 * @param {Array} arr 被查源
 * @param {int} index 检测索引
 */
function haveIndex(arr, index){
	if (!arr.length) {
		return false;
	}
	for(var i=0; i<arr.length; i++){
		if(arr[i] == index){
			return true;
			break;
		}
	}
	return false;
}

var Gateway = com.qq.qzone.net.Gateway;

function loadToolbar(){
	
	var flag=ENV.get("toolbarLoaded");
	if(!flag){
		QZONE.toolbar={};
		var t=new QZONE.jsLoader();
		t.onload = function(){
			ENV.set("toolbarLoaded",true);
			QZONE.toolbar.bootstrap();//工具条启动器
		};
		t.load("/qzone/v5/toolbar.js", null, "utf-8");
	}else{//这里可以是refresh toolbar的场景
		if(QZONE.toolbar){
			QZONE.toolbar.init();
			QZONE.toolbar.activityBind();
		}
	}
}

function checkLogin(){
	if(QZONE.cookie.get("skey")){
		var t=parseInt(QZONE.cookie.get("uin").replace(/[^\d]/g,""),10);
		if (!isNaN(t) && (t > 10000))
			return t;
		else
			return 0;
	} else {
		return 0;
	}
}

function getCityFriend(container_id){
	var t = document.getElementById("friend_list");
	if(t){t.style.height = "2px";}
	var l = t.parentNode.getElementsByTagName("h3");
	if(l && l.length){
		for(var i = 0, len = l.length; i < len; ++i){
			if(!(l[i].className.indexOf("qq_friends") < -1)){
				l[i].className = "none";
			}
		}
	}

}

if(!QZONE.FrontPage){
	QZONE.FrontPage = {};
}

/**
 * 插入一个关闭弹出对话框时的回调函数
 * @param {function} fn 为使用popupDialog方法造成的弹出窗口
 *                      推入一个关闭时的回调函数
 */
QZONE.FrontPage.appendPopupFn = function(fn){
	if(typeof(fn)=='function'){
		var _self=QZONE.FrontPage.popupDialog;
		if(!(_self._fnl instanceof Array)){
			_self._fnl = [];
		}
		_self._fnl.push(fn);
	}
};

QZONE.FrontPage.showLoginBox = function(key, callbackFn){
	if (!LOGINBOX_FLAG) {
		LOGINBOX_FLAG = true;
		if (typeof(loginCallback) == 'function') {
			QZONE.FrontPage.appendPopupFn(loginCallback);
			loginCallback = null, loginCallback = void (0);
		}
		if (typeof(callbackFn) == 'function') {
			QZONE.FrontPage.appendPopupFn(loginCallback);
		}
		QZONE.FrontPage.popupDialog("登录Qzone", {
			src: 'http://ui.ptlogin2.qq.com/cgi-bin/login?appid=8000108&hide_title_bar=1&target=self&link_target=blank&f_url=http%3A%2F%2F' + imgcacheDomain + '%2Fqzone%2Fv5%2Floginerr.html&s_url=http%3A%2F%2F' + imgcacheDomain + '%2Fqzone%2Fv5%2Floginsucc.html' + (!!key ? ('%3fpara%3d' + key) : '')
		}, 378, 280);
	}
};

/**
 * 打开一个弹出对话框
 * @param {string} title 对话框标题
 * @param {string/object} html 对话框内容
 * @param {number} [width=430] 宽度
 * @param {number} [height=300] 高度
 */
QZONE.FrontPage.popupDialog = function(title, html, width, height){
	var _self=QZONE.FrontPage.popupDialog;
	if(typeof(width)=='undefined'){
		width=430;
	}
	if(typeof(height)=='undefined'){
		height=300;
	}
	if(typeof(html)=='object'){
		html=_self._ifrmTmp.replace(/__p__/g,
			' src="'+html.src+'" height="'+(height)+'" width="'+(width-2)+'"'
		);
	}else{
		html=html+"";
	}

	var _dh,b;
	//if (!_self._cpp) {
		_dh = _self._cpp = QZONE.dialog.create(title+"", html, width, height);
		b = $("dialog_button_" + _dh._id);
		b.onclick = QZONE.FrontPage.closePopup;
		_self._ppml = QZONE.maskLayout.create();
	//}
};

/**
 * 弹出框iframe模板
 */
QZONE.FrontPage.popupDialog._ifrmTmp = '<iframe frameborder="no" allowtransparency="yes"__p__></iframe>';

/**
 * 关闭弹出对话框
 */
QZONE.FrontPage.closePopup = function(){
	LOGINBOX_FLAG = false;
	var _self=QZONE.FrontPage.popupDialog;
	var tmp,tf;
	//准备干掉了
	if (typeof(window.popupCallback) == 'function') {
		QZONE.FrontPage.appendPopupFn(window.popupCallback);
	}
	//end
	tmp=_self._fnl;
	if (tmp && tmp.length > 0) {
		_self._cpp.onUnload = function(){
			for(;tmp.length>0;){
				tf=tmp.pop();
				if(typeof(tf)=='function'){
					tf();
				}
			}
			window.popupCallback = null;
		}
	}
	
	//if (_self._cpp) {
		_self._cpp.unload();
		_self._cpp = null;
	//}
	//if (_self._ppml) {
		QZONE.maskLayout.remove(_self._ppml);
		_self._ppml = null;
	//}
};

/**
 * 取空间昵称
 * @return {string} 空间主人的昵称
 */
QZONE.FrontPage.getNickname = function(){
	return "我的空间";
};

/**
 * 获取空间的bitmap
 * 按照bitmap位规划1.3，返回指定标记位的值，从右到左分别为1～64
 * @param {number} bit 从右至左1~64的位置
 * @rerurn {number} 0/1的数值
 */
QZONE.FrontPage.getBitMapFlag = function(bit){
	//按照bitmap位规划1.3，返回指定标记位的值，从右到左分别为1～64
	if (typeof g_UserBitmap == "string") {
		return parseInt(g_UserBitmap.charAt(15 - Math.floor((bit - 1) / 4)), 16) >> ((bit - 1) % 4) & 1;
	} else {
		return 0;
	}
};

getCookie = QZONE.cookie.get;/*  |xGv00|cc13afba7a6415082330230b2162991e */