var postfunc = new Object;

postfunc.init = function (title,content,form,preview,post_btn,targetFrame,formContainer,targetFrameContainer)
{//fs
this.title = title
this.content = content
this.form = form
this.preview = preview
this.post_btn = post_btn
this.targetFrame = targetFrame
this.formContainer = formContainer
this.targetFrameContainer = targetFrameContainer
if (domStorageFuncs && cookieFuncs && cookieFuncs.getCookie('ngaPassportUid')){
	commonui.aE(this.content,'change',function(){
		if(this.value)
			domStorageFuncs.set(domStorageFuncs.domain+'_'+cookieFuncs.getCookie('ngaPassportUid')+'_bbspostbackup',this.value)
		})
	commonui.aE(window,'beforeunload',function(){
		if(postfunc.value)
			domStorageFuncs.set(domStorageFuncs.domain+'_'+cookieFuncs.getCookie('ngaPassportUid')+'_bbspostbackup',postfunc.value)
		})
	}
}//fe

postfunc.addPostBackupLink=function(o){//发帖自动备份
if (domStorageFuncs && cookieFuncs && cookieFuncs.getCookie('ngaPassportUid'))
	put("<a href='javascript:void(0)' onclick='id2e(\""+o.id+"\").value+=domStorageFuncs.get(\""+domStorageFuncs.domain+'_'+cookieFuncs.getCookie('ngaPassportUid')+"_bbspostbackup\")' style='font-weight:bold'>[恢复上次输入的内容]</a>");
}//fe

postfunc.checklength = function()
{
window.alert('您的信息已经有 '+this.content.value.length+' 字节');
}

postfunc.addText = function(NewCode)
{
if(this.content.createTextRange)
	{ //for ie
		this.content.focus();
		document.selection.createRange().duplicate().text = NewCode;
	}
else
	{ //for moz
		var temp = this.content.selectionStart;
		this.content.value = this.content.value.substring(0,temp) + NewCode + this.content.value.substring(this.content.selectionEnd,this.content.value.length)
		temp = temp + NewCode.length;
		this.content.setSelectionRange(temp,temp)
	}
}

postfunc.getSelectText = function()
{
if(document.selection && document.selection.createRange)
	{
	return(document.selection.createRange().text);
	}
else
	{
	return this.content.value.substring(this.content.selectionStart,this.content.selectionEnd)
	}
return '';
}
//fe


postfunc.setFocus = function()
{
this.content.focus();
}

postfunc.addTag = function(tag,value)
{
if(value) this.addText("["+tag+"="+value+"]"+this.getSelectText()+"[/"+tag+"]");
else this.addText("["+tag+"]"+this.getSelectText()+"[/"+tag+"]");
}

postfunc.pc_showsize = function(size) 
{//fs
this.addTag('size',size)
}//fe

postfunc.pc_showfont = function(font)
{
this.addTag('font',font)
}

postfunc.pc_showcolor = function(color)
{
this.addTag('color',color)
}

postfunc.copytext = function() 
{
this.content.focus();
this.content.select();
this.content.createTextRange().execCommand("Copy");
}


postfunc.addsmile = function(NewCode) 
{
this.addText(' '+NewCode+' ');
}

postfunc.stripsomebbscode = function(v)
{
v = v.replace(/\[\/?lessernuke\]/gim,'');
return v;
}
//fe

postfunc.quickpost = function(e)
{
if (!e)
	{
		e = window.event;
	}
if((e.ctrlKey && e.keyCode == 13))
	{
		this.post_v2();
	}
}
//fe

postfunc.inputchar = function(e,o)
{
if (!e)
	{
		e = window.event;
	}
if (e.keyCode == 9)
	{
		this.addText('	');
		return false;
	}
if (o.selectionStart)
	{
	var c = o.value.charCodeAt(o.selectionStart-1)
	var rng = false;
	}
else
	{
	var rng = document.selection.createRange();
	rng.moveStart("character",-1)	     
	var c= rng.text.charCodeAt(0)
	}
if (c==12304)
	{
	if (rng)
		{
		rng.text='['
		}
	else
		{
		rng = o.selectionStart
		o.value=o.value.substring(0,rng-1)+'['+o.value.substr(rng+1)
		o.selectionStart= o.selectionEnd = rng
		}
	}
else if (c==12305)
	{
	if (rng)
		{
		rng.text=']'
		}
	else
		{
		rng = o.selectionStart
		o.value=o.value.substring(0,rng-1)+']'+o.value.substr(rng+1)
		o.selectionStart= o.selectionEnd = rng
		}
	}
else if (c==65309)
	{
	if (rng)
		{
		rng.text='='
		}
	else
		{
		rng = o.selectionStart
		o.value=o.value.substring(0,rng-1)+'='+o.value.substr(rng+1)
		o.selectionStart= o.selectionEnd = rng
		}
	}
else if (c==12289)
	{
	if (rng)
		{
		rng.moveStart("character",-1)
		if (rng.text.charAt(0)=='[')
			{
			rng.text='[/'
			}
		}
	else
		{
		if (o.value.charAt(o.selectionStart-2)=='[')
			{
			rng = o.selectionStart
			o.value=o.value.substring(0,rng-1)+'/'+o.value.substr(rng+1)
			o.selectionStart= o.selectionEnd = rng
			}
		}
	}
}
//fe

postfunc.checkCnt = function()
{
var c = this.form;
if (c.post_subject && c.post_subject.value)
	{
	c.post_subject.value = c.post_subject.value.replace(/\u3010/g,'[').replace(/\u3011/,']')
	}
c = this.content;
var tmp = document.getElementById('attachform')
if (tmp)
	if(tmp.elements.namedItem('attachment_file1').value || (tmp.elements.namedItem('attachment_file1').files && tmp.elements.namedItem('attachment_file1').files[0]))
		if (!window.confirm('确认附件已经全部上传完毕?'))
			return false;
if (c.value)
	{
		if (c.value.substr(0,50).match(/\[reqpg=\d+\]/))
			{
				c.value = 1;
			}
		c.value = c.value.replace(/\r?\n\[::艾泽拉斯国家地理 BBS.NGACN.CC::\]\r?\n/g,'');
		/*
		if (this.tid)
			{
				var t = this.tid;
			}
		else
			{
				var t = this.fid;
			}
		var postlog = '';
		if (getCookie('postlog'))
			{
				postlog = getCookie('postlog');
			}
		var ck = this.geturl(c.value);
		if (ck)
			{
				ck = hex_md5(ck);
			}
		ck = hex_md5(c.value)+ck+t;
		postlog = ck + ',' + postlog;
		if (postlog.length >512)
			{
				postlog = postlog.substring(0,postlog.lastIndexOf(',',postlog.length-2)+1);
			}
		setCookieInSecond('postlog',postlog,7200);
		*/
		return true;
	}
else
	{
		alert('无内容');
		return false;
	}
}
//fe

postfunc.geturl=function(t)
{
var u = '';
t = t.match(/http:[a-zA-Z0-9\?&_\-+=%;:"\'@\$\^\*\/\\~`\|\.]+/ig);
for (k in t)
	{
		if (k!='input' && k!='index' && k!='lastIndex')
			{
				u = u+t[k];
			}
	}
return (u);
}
//fe

postfunc.post_v2=function()
{
this.post_btn.disabled=true;
if (!this.checkCnt())
	{
		this.post_btn.disabled=false;
		return false;
	}
if(window.commonui.loadTopicUpdate && window.commonui.loadTopicUpdate.inited){
	this.form.target = this.targetFrame.id
	this.form.elements.namedItem('nojump').value=1
	this.formContainer.style.display='none'
	this.targetFrameContainer.style.display='block'
	}
this.form.submit();
}
//fe

postfunc.switchFrameDisplay=function()
{
this.post_btn.disabled=false;
this.formContainer.style.display='block'
this.targetFrameContainer.style.display='none'
}
//fe

postfunc.post_preview=function()
{
	if (this.preview.style.display=='none')
		{
			this.preview.style.display='block';
			this.content.onchange=function(){this.preview.innerHTML = ubbcode.bbscode_core(this.content.value.replace(/\n/g,'<br>'))+'<div class="clear"></div>'};
			this.preview.innerHTML = ubbcode.bbscode_core(this.content.value.replace(/\n/g,'<br>'))+'<div class="clear"></div>';
		}
	else
		{
			this.content.onchange = null;
			this.preview.style.display='none';
		}
}
//fe

postfunc.code_help=function()
{
	function tmp(n,t)
	{
	t = t.replace(/\n|\r/g,'<br/>')
	return "<a href='javascript:void(0)' style='display:block;color:gray;float:left;border:1px solid #fff;padding:3px;margin:2px;align:center' onclick='this.parentNode.nextSibling.innerHTML=this.nextSibling.innerHTML'>"+n+"</a><span style='display:none'><div class='quote'>"+t+"</div></span>";
	}
var txt=''
if (ubbcode.codeHelpCommon)
	{
	var x=ubbcode.codeHelpCommon;
	for (var i=0;i<x.length;i++)
		txt +=tmp(x[i][0],x[i][1])
	}
txt += tmp('<b>[customachieve]</b><br/><nobr>自定义成就</nobr>',"<b>插入一个自己定义内容的成就</b>\n\
\n\
[customachieve]\n\
[title]成就的名字(有字数限制)[/title]\n\
[txt]成就的说明文字(有字数限制)[/txt]\n\
[/customachieve]\n\
\n\
[customachieve]\n\
[title]成就的名字(有字数限制)[/title]\n\
[txt]成就的说明文字(有字数限制)[/txt]\n\
[img]成就的图标(绝对地址)[/img]\n\
[/customachieve]\n\
");
txt += tmp('<b>[item]</b><br/><nobr>魔兽世界装备</nobr>',"<b>插入魔兽世界中的物品</b>\n\
\n\
	[item]物品名称[/item]\n\
\n\
	[item=物品ID]任意文字[/item]\n\
");

txt += tmp('<b>[wowdb]</b><br/><nobr>魔兽世界数据库</nobr>',"<b>插入魔兽世界中的物品/任务/成就/NPC</b>\n\
\n\
	[wowdb[任意魔兽世界中的物品/技能/任务/成就/NPC名字]]\n\
");

txt += tmp('<b>[wiki]</b><br/><nobr>插入wiki链接</nobr>',"<b>插入到wiki.ngacn.cc的文章链接</b>\n\
\n\
	[wiki]文章标题[/wiki]\n\
");

txt += tmp('<b>[armory]</b><br/><nobr>魔兽世界人物信息</nobr>',"<b>插入魔兽世界armory的人物信息(因编码不同可能包含特定文字的ID无法获取)</b>\n\
\n\
	[cnarmory 服务器名 玩家名] 插入cn.wowarmory.com的人物信息\n\
\n\
	[usarmory 服务器名 玩家名] 插入www.wowarmory.com的人物信息\n\
\n\
	[twarmory 服务器名 玩家名] 插入tw.wowarmory.com的人物信息\n\
\n\
	[euarmory 服务器名 玩家名] 插入eu.wowarmory.com的人物信息\n\
");
put('<div>'+txt+'<div class="clear"></div></div><div></div>');
}

postfunc.listAlbum = function(o){
o.innerHTML = ''
window.tmp=undefined
httpDataGetter.script_muti_get("http://i.178.com/?&_app=album&_controller=category&_action=getAlbumInfo",
	function(r){
		if(window.tmp)r=window.tmp
		else return false;
		if (r.error)
			{
			if(r.error=='not login')r.error = '未登录'
			o.innerHTML = r.error;
			return true
			}
		var list=''
		for (var i=0;i<r.length;i++)
			{
			if(r[i].preview)r[i].preview='<img src="'+r[i].preview+'"/>'
			list+='<a href="javascript:void(0)" onclick="postfunc.openAlbum(this.parentNode,'+r[i].id+')" style="display:block;width:136px;height:130px;overflow:hidden;text-align:center;float:left;border:1px solid #aaa;margin:3px"><div style="width:130px;height:98px;border:1px solid #000;background:#444;margin:2px auto">'+r[i].preview+'</div>'+r[i].title+' ('+r[i].count+')</a>'
			}
		if (list)
			o.innerHTML = list
		else
			o.innerHTML = '没有相册'
		return true

	},
	function(){
		o.innerHTML = '读取错误'
	},
	'gbk'
	);
}//fe

postfunc.openAlbum = function (o,id,page){
o.innerHTML = ''
if(!page)page=''
window.tmp=undefined
httpDataGetter.script_muti_get("http://i.178.com/?id="+id+"&pagesize=20&page="+page+"&_app=album&_controller=category&_action=getPicByCategory",
	function(r){
		if(window.tmp)r=window.tmp
		else return false;
		if (r.error)
			{
			if(r.error=='not login')r.error = '未登录'
			o.innerHTML = r.error;
			return true
			}
		var info = '';
		if (r.info)
			{
			if (r.info.PAGECOUNT>1)
				{
				for (var i=1;i<r.info.PAGECOUNT;i++)
					info+'<a href="javascript:void(0)" onclick="postfunc.openAlbum(this.parentNode,'+id+','+i+')">['+i+']</a>';
				}
			}
		if(info)info='<div style="clear:both">'+info+'</div>'
		var list=''
		for (var i=0;i<r.ROWS.length;i++)
			{
			if(r.ROWS[i].url)r.ROWS[i].urls='<img src="'+r.ROWS[i].url.replace(/(\.[^\.]+)$/,'_mid$1')+'"/>'
			list+='<a title="点击插入图片代码" href="javascript:void(0)" onclick="postfunc.addText(\'[img]'+r.ROWS[i].url+'[/img]\');" style="display:block;width:136px;height:130px;overflow:hidden;text-align:center;float:left;border:1px solid #aaa;margin:3px"><div style="width:130px;height:98px;border:1px solid #000;background:#444;margin:2px auto">'+r.ROWS[i].urls+'</div>'+r.ROWS[i].title+'</a>'
			}
		if (list)
			o.innerHTML = info+list+info
		else
			o.innerHTML = '没有图片'
		return true

	},
	function(){
		o.innerHTML = '读取错误'
	},
	'gbk'
	);
}//fe