function trim(str) {
	str = str.replace(/(^\s*)|(\s*$)/, "");
	regExp3 = /(^　*)/;
	regExp4 = /(　*$)/;
	str = str.replace(regExp3, "");
	str = str.replace(regExp4, "");
	return str;
}

function JHshStrLen(sString)
{
	var sStr,iCount,i,strTemp ;

	iCount = 0 ;
	sStr = sString.split("");
	for (i = 0 ; i < sStr.length ; i ++)
	{
	strTemp = escape(sStr[i]);
	if (strTemp.indexOf("%u",0) == -1)
	{
	iCount = iCount + 1 ;
	}
	else
	{
	iCount = iCount + 2 ;
	}
	}
	return iCount ;
}


function chkSubmit(varForm){
	//var body = document.getElementsByName(bodyName)[0];
	try{
		if(isMain == 1){
			if (trim(varForm.title.value)=="") {
				alert ('标题必须填写！');
				return false;
			} else if (JHshStrLen(trim(varForm.title.value)) > 50) {
				alert ('请将标题控制在25个汉字以内！');
				return false;
			}
		}
	}catch(e){};

	varForm.sub.disalbed = true;
	return true;

}

function doPreview(oa){
	varForm = document.getElementById("postform");
	varForm.action = "/preview.php";
	varForm.target = "_blank";
	varForm.submit();
	varForm.action = oa;
	varForm.target = "";

}

function r2Click() {

	window.pgNum = parseInt(this.id.substr(6));
	xid = $(this).attr('idv');
	if (window.last_id)	{
		$('#ajax_p' + window.last_id).attr('class', "ajax_pager");
		$('#ajax_g' + window.last_id).attr('class', "ajax_pager");
	}
	window.last_id = pgNum;
	if (window.last_id)	{
		$('#ajax_p' + window.last_id).attr('class', "ajax_pager current");
		$('#ajax_g' + window.last_id).attr('class', "ajax_pager current");
	}

	$.get(hostUrl + '/interface/ajaxThread.php', {'xid': xid}, function(data, textStatus) {
		
		if ( textStatus == 'success' ) {
			
			var r_xid = $("xid", data).text();
			var r_xdescription = $("xdescription", data).text();
			var r_xcontent = $("xcontent", data).text();
			var r_xfilename = $("xfilename", data).text();
			var r_xtextlink = $("xtextlink", data).text();

			$('#x2_content').html(r_xcontent);
			$('#x_pic').attr('src', r_xfilename);
			$('#x2_description').html(r_xdescription);
			$('#x2_textlink').html(r_xtextlink);

			var x = document.getElementById('x_btns');
			if (x) {
				var lnks = x.getElementsByTagName('A');
				var width = 0;
				for (var i = 0; i < lnks.length; i++) {
					width += lnks[i].offsetWidth + 10;
				}
				x.style.width = width + "px";
			}
			setTimeout(ibShowImg, 1500);
			$('#x_pic').load(ibShowImg);
		} else {alert('网络传输错误，请稍候再试');}
	});
}

function ibShowImg() {
	$('#x_pic').attr('style', "");
}

function r2ClickX() {

	window.pgNum = parseInt(this.id.substr(6));
	xid = $(this).attr('idv');
	if (window.last_id)	{
		$('#ajax_p' + window.last_id).attr('class', "ajax_pager");
		$('#ajax_g' + window.last_id).attr('class', "ajax_pager");
	}
	window.last_id = window.pgNum;
	if (window.last_id)	{
		$('#ajax_p' + window.last_id).attr('class', "ajax_pager current");
		$('#ajax_g' + window.last_id).attr('class', "ajax_pager current");
	}

	var query = location.search.replace(/[&?]uxp=([0-9]*)/, '');
	if (query.length) {
		query += "&uxp=" + window.pgNum;
	} else {
		query += "?uxp=" + window.pgNum;
	}
	location.href = "http://bbs.eachnet.com" + location.pathname + query;
}

function ibGotoPage(nPage) {
	var obj = document.getElementById('ajax_p' + nPage);
	if (obj) {
		r2ClickX.apply(obj);
	} else {
		r2ClickX.apply(document.getElementById('ajax_p1'));
	}
}

function ibNextPage() {
	ibGotoPage(window.pgNum + 1);
}

function ibPrevPage() {
	if (window.pgNum == 1) {
		ibGotoPage(window.nPageLinks);
	} else {
		ibGotoPage(window.pgNum - 1);
	}
}

var emoteImages = ['daxiao.gif','think.gif','cool.gif','sad.gif','cry.gif','smile.gif',
'angry.gif','dizzy.gif','sweat.gif','sleep.gif','rose.gif','shy.gif',
'accuse.gif','angel.gif'];
function changeEmotion(nEmote) {
	var el = document.getElementById('emote_img');
	nEmote = Math.min(Math.max(parseInt(nEmote), 0), emoteImages.length) - 1;
	if (el && nEmote >= 0) {
			el.innerHTML = "<img alt='' src='/images/face2/" + emoteImages[nEmote] + "' />";
	} else {
		el.innerHTML = '';
	}
}

function changeEmotionByKey(el, ev) {
	var brInfo = navigator.userAgent.toString();
	if (brInfo.indexOf('Firefox') != -1) {
		var tre = new RegExp("Firefox.[0-9]*\\.[0-9]*", "gi");
		brInfo = (brInfo.match(tre)[0]).replace('Firefox', "");
		brInfo = brInfo.replace(" ","");
		brInfo = brInfo.replace("/","");
	} else {
		brInfo = '';
	}

	if (brInfo.length && ev.charCode == 0) {
		//var nEmote = this.value + (event.keyCode == 40 ? 1 : -1);
		//this.selectedIndex = Math.min(Math.max(nEmote, 0), emoteImages.length);
		setTimeout(function() {
				var evt = document.createEvent('HTMLEvents');
				evt.initEvent('change', true, true);  
				el.dispatchEvent(evt); 
			}, 10);
	}
}

var hostUrl = 'http://bbs.eachnet.com'
window.last = null;
window.pgNum = 1;
if ("$" in window)
{
	$(document).ready(function() {

		var r = $("span[@idf=ajax_p]");
		var xid = 0;
		window.last = $('#ajax_p1');
		window.last_id = 1;

		r.each(function() {

			$(this).click(function() {

				//changeThreadXContent($(this).attr('idv'));
				xid = $(this).attr('idv');
				if (window.last) {
					window.last.attr('class', "ajax_pager");
				}
				window.last = $(this);
				$(this).attr('class', "ajax_pager current");
				
				$.get(hostUrl + '/interface/ajaxThread.php', {'xid': xid}, function(data, textStatus) {
					
					if ( textStatus == 'success' ) {
						
						var r_xid = $("xid", data).text();
						var r_xdescription = $("xdescription", data).text();
						var r_xcontent = $("xcontent", data).text();
						var r_xfilename = $("xfilename", data).text();
						var r_xtextlink = $("xtextlink", data).text();
						
						$('#x_content').html(r_xcontent);
						$('#x_pic').attr('src', r_xfilename);
						$('#x_pic').attr('style', "");
						$('#x_description').html(r_xdescription);
						$('#x_textlink').html(r_xtextlink);
					} else alert('网络传输错误，请稍候再试');
				});
			});
		});

		window.nPageLinks = 0;
		$("span[@idf=pager]").each(function() {
			window.nPageLinks++;
			$(this).click(r2ClickX);
		});
		window.nPageLinks /= 2;
		$("span[@idf=ajax_prev]").each(function() {
			$(this).click(ibPrevPage);
		});
		$("[@idf=ajax_next]").each(function() {
			$(this).click(ibNextPage);
		});

		var p;
		if (location.search.length) {
			var ret = location.search.match(/uxp=([0-9]*)/);
			if (ret) {
				p = document.getElementById('ajax_p' + ret[1]);
			}
		}
		if (!p) {
			p = document.getElementById('ajax_p1');
		}
		if (p) {
			r2Click.apply(p);
		}
	});
}