/* show flashADs */
function showFlashAD(viewerurl, w, h, ads) {
	var focus_width = w; //Flash图片宽度
	var focus_height = h; //Flash图片高度
	var text_height = 0; //标题文字高度
	var swf_height = focus_height + text_height; //Flash高度=图片高度+文字高度
	var sp = '|';
	var pics = '';
	var links = '';
	var texts = '';
	for (var i=0; i<ads.length; i++) {
		if (i>0) {
			pics += sp;
			links += sp;
			texts += sp;
		}
		pics += ads[i].pic;
		links += ads[i].link;
		texts += ads[i].title;
	}
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">');
	document.write('<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="'+ viewerurl +'"><param name="quality" value="high">');
	document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
	document.write('<param name="FlashVars" value="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'">');
	document.write('<embed src="'+ viewerurl +'" wmode="opaque" FlashVars="pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'" menu="false" quality="high" width="'+ focus_width +'" height="'+ swf_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}
var flashADPics = [];

/* show newGames */
function showNewGamesList(datas) {
	var htmlTemplate = '<dl><dt class=\"dt1 colorF14E79\" title="{writer}">{miniwriter}</dt><dt class=\"dt2\"><a target=\"_blank\" href=\"{url}\">{title}</a></dt><dt class=\"dt3\"><a target=\"_blank\" href=\"{picurl}\" class=\"link_plug\">{title2}</a></dt><dt class=\"dt4\"><a target=\"_blank\" href=\"{fromurl}\" class=\"link_plug\">{from}</a></dt></dl>';
	var newdatas = __array_sortby(datas, 'writer', '', 'desc');
	for (i=0; i<newdatas.length; i++) {
		if (i>=7) break;
		var html = htmlTemplate;
		for (var item in newdatas[i]) {
			html = html.replace('{'+ item +'}', newdatas[i][item]);
		}
		document.write(html.replace('{miniwriter}', newdatas[i].writer.substring(5,10)));
	}			
}
// 排序
__array_sortby = function(datas, it, dt, od) {
	// sort array data
	// it: item name  dt: int, char  od: asc, desc
	var compareValues = function(v1, v2, dt, od) {
		if (dt == 'int') {
			v1 = parseInt(v1);
			v2 = parseInt(v2);
		} else if (dt == 'float') {
			v1 = parseFloat(v1);
			v2 = parseFloat(v2);
		}
		var ret = 0;
		if (v1 < v2) ret = 1;
		if (v1 > v2) ret = -1;
		if (od == 'desc') {
			ret = 0 - ret;				
		}
		return ret;
	}
	var newdata = new Array();
	for (var i=0; i<datas.length; i++) {
		newdata[newdata.length] = datas[i];
	}
	for (var i=0; i<newdata.length; i++) {
		var minIdx = i;
		var minData = (it != '')?newdata[i][it]:newdata[i];
		for (var j=i+1; j<newdata.length; j++) {
			var tmpData = (it != '')?newdata[j][it]:newdata[j];
			var cmp = compareValues(minData, tmpData, dt, od);
			if (cmp<0) {
				minIdx = j;
				minData = tmpData;
			}
		}
		if (minIdx > i) {
			var _child = newdata[minIdx];
			newdata[minIdx] = newdata[i];
			newdata[i] = _child;
		}
	}
	return newdata;
};

jQuery(document).ready(function(){
	var $ = jQuery;
	var wenwen_defstr = '请输入要查询的文字';
	$('#question_wenwen').val(wenwen_defstr);
	$('#question_wenwen').css('color','#999');
	$('#question_wenwen').focus(function() {
		if (this.value==wenwen_defstr) {
			this.value = '';
			this.style.color = '#333';
		}
	});
	$('#question_wenwen').blur(function() {
		if (this.value=='') {
			this.value = wenwen_defstr;
			this.style.color = '#999';
		}
	});
	$('#search_wenwen').click(function() {
		var key = $('#question_wenwen').val();
		if (key=='' || key==wenwen_defstr) {
			alert(wenwen_defstr);
			return;
		}
		window.open('http://wenda.tianya.cn/wenda/search?q='+encodeURIComponent(key));
	});
	$('#ask_wenwen').click(function() {
		var key = $('#question_wenwen').val();
		if (key=='' || key==wenwen_defstr) {
			alert(wenwen_defstr);
			return;
		}
		window.open('http://wenda.tianya.cn/wenda/ask?subject='+encodeURIComponent(key));
	});
	$('.newGuy').click(function() {
		$.tygApp.popDialog({
			mode:'id',
			content:'newGuyContent',
			css:'newGuyContent',
			notitle:true, setposition:true, op:0.5,
			callback:function() {}
		});
		return false;
	});
	// ------------------------------
	// 图片延迟加载优化
	loadPageImages = function() {
		$('img.imgload-opt').each(function() {
			if ($(this).offset().top < $(window).height() + $(document).scrollTop()) {
				if (this.getAttribute('linksrc')) {
					this.src = this.getAttribute('linksrc');
					this.removeAttribute('linksrc');
				} 
				$(this).removeClass('imgload-opt');
			}
		});
	}
	$(window).scroll(function() {
		loadPageImages();
	});
	loadPageImages();
	// ------------------------------
});