/*
* 留言和评论输入框的js类
	
	1、简单文本编辑器 表情插入 图片插入
	2、留言发表 删除 引用 回复
	3、留言自动分页
* 
*   Author  :Moon
/   Date    :208-5-4
/*
*/
/**
*	获取对象元素
*/
if(!$)
{
	function $(o)
	{
		if(typeof o =='object')
		return o;
		else if(typeof o=='string')
		{
			e=document.getElementById(o);
			if(e)
			return e
			else
			{
				e=document.getElementsByTagName(o);
				if(e.length!=0)
				return e
				else
				return false
			}
		}
	}
}

/*
	设置cookie
*/
if(!setCookie)
{
	function setCookie(name,value,expire)
	{
		if(!expire)
			expire=5000;
		var expiry = new Date();
		expiry.setTime(expiry.getTime()+expire)
		document.cookie=name+'='+value+';expires='+expiry.toGMTString();
	}
}

/*
    简单的文本编辑器功能
	
    iframe       iframe的id
    insertface   插入表情按扭
    insertpic    插入图片按扭
    facebox		显示表情的div
*/
commentCode=function(iframe,insertface,insertpic,facebox)
{
	_me            = this;
	this._iframe   = $(iframe);
	this._face     = $(insertface);
	this._pic      = $(insertpic);
	this._uploadbox= $('insertpicturebox');
	this._facebox  = $(facebox);
	this._doc      = null;
	this._textarea = this._iframe.contentWindow.document;
	
	/*
		初始化函数
	*/
	this.init = function()
	{
		this._textarea.innerHTML   = '';
		this._textarea.designMode  = 'on';
	
		this._face.onclick         = _me.showFace;
		this._pic.onclick          = _me.showPicture;
		var doc = this._iframe.contentWindow.document;
		doc.onmouseover=function(){this.style.cursor='text'}
		this._textarea = doc;
	
		doc.open();
		var html = "<html>\n";
		html += "<head>\n";
		html += "<style> html,body {font-family:arial;font-size:12px;margin:2;padding:0px;white-space:normal;word-break:break-all;font-size: 14px;line-height: 16px;letter-spacing: 1px;word-spacing:1px; background-color:#FFF;}\n";
		html += ".t {border:1px solid #D4EFF7;border-collapse : collapse}\n";
		html += ".t td {border: 1px solid #D4EFF7;}\n";
		html += "img {border:0; display:inline;}p {margin:0px;}</style>\n";
		html += "</head>\n";
		html += "<body >\n";
		/*需要编辑时 使用*/
		//html += codetohtml(this._textArea.value);
		html += "</body>\n";
		html += "</html>";
		doc.write(html);
		doc.close();
		/*iframe 的高跟随内容变化 需要是使用*/
		//Events.addEvent(doc,'keydown',_me.setIframeHeight);
	}
	
	/*
		文本编辑器的高 跟随内容的高度改变 
	*/
	this.setIframeHeight = function()
	{
		/*if (_me._iframe.contentWindow.document.body.offsetHeight>140)
			_me._iframe.height = _me._iframe.contentWindow.document.body.offsetHeight; 
		else if(_me._iframe.contentWindow.document.body.scrollHeight)
			_me._iframe.height = _me._iframe.contentWindow.document.body.scrollHeight;*/
	}

	/*
		显示表情图层
	*/
	this.showFace = function()
	{
		if(_me._facebox.style.display != 'block')
		{
			_me._facebox.style.display = 'block';
			/* 无效点击时关闭表情层 事件 */
			Events.addEvent(document,'click',_me.hiddenFaceBox)
			Events.addEvent(_me._textarea,'click',_me.hiddenFaceBox);
		}
		else
			_me._facebox.style.display = 'none';
			
		
		return false
	}
	
	/*
		隐藏表情图层
		无效点击时关闭表情层 
	*/
	this.hiddenFaceBox = function(evt)
	{
		var evt=Events.formatEvent(evt)
		if(evt.target.id!='insert_face'&&evt.target.id!='insert_face_box'&&evt.target.parentNode.id!='insert_face_box')
		{
			_me._facebox.style.display = 'none';
			Events.removeEvent(document,'click',_me.hiddenFaceBox);
			Events.removeEvent(_me._textarea,'click',_me.hiddenFaceBox);
		}
	}
	
	/*
		向编辑器里光标位置插入外面元素
		可以是文字 或者 HTML标签
	*/
	this.insertFace = function(src)
	{
			
		if(isIE)
		{
			//window.scrollTo(0,document.body.scrollHeight) by loong
			_me._iframe.contentWindow.focus();			
			var oSelection = _me._iframe.contentWindow.document.selection.createRange();
			oSelection.pasteHTML(src);
		}
		else
		{
			_me._iframe.contentWindow.focus();
			_me._iframe.contentDocument.execCommand('insertHTML', false, src);
		}
		_me._facebox.style.display = 'none';
	}
		/*
		插入图片 
		过度函数 关闭上传图层
	*/
	this.insertPicture = function(url)
	{
		this.insertFace(url)		
	}


}
///////////////////////////////////文本编辑器结束//////////////////////

/* 创建文本编辑器 */
var oTextArea =null;
	oTextArea = new commentCode('textarea_iframe','insert_face','insert_pictures','insert_face_box');
	oTextArea.init();
	
/*
	插入表情
*/
function insertFace(i)
{
	if(isIE)//by loong
	{
		var temId='smilies'+Math.random(i);
		var src='<img src="/img/simle/t'+i+'.gif" id="'+temId+'" />';
		oTextArea.insertFace(src);
		oTextArea._textarea.body.all[temId].src="/img/simle/t"+i+".gif";
		oTextArea._textarea.body.all[temId].removeAttribute('id');
	}
	else
	{
		var src='<img src="/img/simle/t'+i+'.gif" />';
		oTextArea.insertFace(src);
	}
}

/*
	上传图片导航条切换函数
*/
function thisAction(obj,i)
{
	obj=obj.parentNode;
	obj.parentNode.childNodes[0].className='';
	obj.parentNode.childNodes[1].className='';
	obj.parentNode.childNodes[2].className='last_li';
	obj.className+=' active';
	$('showThis1').style.display='none';
	$('showThis2').style.display='none';
	$('showThis3').style.display='none';
	$('showThis'+i).style.display='';
	$('whichaction').value=i;
	if(i==2)
	{
		$('showThis2Add').style.display='block';
	}
	else
	{
		$('showThis2Add').style.display='none';
	}
}
/*
	插入外面图片
	过度函数
*/
function insertPic(url)
{
	oTextArea.insertPicture('<img src="'+url+'"/>');
}

/*
	上传图层里的上传按扭
*/
function uploadpic()
{
	
	switch($('whichaction').value)
	{
		case '1':
		{
			if($('httpurl').value.length<=10)
			{
				alert('请输入您要添加的图片');
				return false
			}
			else
			{
				var src='<a href="'+$('httpurl').value+'" target="_blank">'+$('httpurl').value+'</a>';
				oTextArea.insertPicture(src);
			}
			break;
		}
		case '2':
		{
			if(!$('localurl').value)
			{
				alert('请选择您要添加的图片');
				return false;
			}
			else
			{
				$('localform').submit();
			}
			$("submit").value='正在提交,稍等....';
			setTimeout('$("submit").disabled=true',500);
			break;
		}
		case '3':
		{
			var checkbox=$('showThis3').getElementsByTagName('input')
			if(checkbox.length==0)
				alert('你还没有任何附件');
			else
			{
				var isOk=false;
				for(var i=0,j=checkbox.length;i<j;i++)
				{
					if(checkbox[i].checked)
					{
						insertPic(checkbox[i].value);
						checkbox[i].checked=false;
						isOk=true;
					}
				}
				if(!isOk)
				{
					alert('请选择你要添加的附件')
					return false
				}
			}
			break;
		}
		default:
			return false
			break;
	}
}

//////////////////////////////以下是回复模块函数///////////////

/*
	回复 i为记录内容的id
	显示出回复框
*/
function reply(i)
{
	if(!i)
		return
	if(isIE)
		$('reply_'+i).style.display='block';
	else
		$('reply_'+i).style.display='table-row';
	parent.document.getElementById('commentiframe').height = document.body.offsetHeight+addHeight;
	parent.document.getElementById('commentiframe').style.height = document.body.offsetHeight+addHeight+'px';
}

/*
	隐藏回复框
*/
function releaseReply(i)
{

	$('reply_'+i).style.display='none';
	parent.document.getElementById('commentiframe').height = parent.document.getElementById('commentiframe').height-120;
}

/*
	引用 i为引用对象id
*/
function copy(i)
{
	var name=$('copy_n_'+i).innerHTML;
	var ind =$('copy_i_'+i).innerHTML;
	var date=$('copy_d_'+i).innerHTML;
	var content=$('copy_c_'+i).innerHTML;
	var sCopy='<BLOCKQUOTE style="BORDER-RIGHT: #F00 1px dashed; PADDING-RIGHT: 10px; BORDER-TOP: #F00 1px dashed; PADDING-LEFT: 10px; PADDING-BOTTOM: 10px; MARGIN: 5px; BORDER-LEFT: #F00 1px dashed; WIDTH: 400px; PADDING-TOP: 10px; BORDER-BOTTOM: #F00 1px dashed" onkeydown="return false">引自：<CITE>'+name+' '+ind+'</CITE>&nbsp;&nbsp;于<INS>'+date+'</INS>发表的评论<BR>引用内容：<BR><BR><Q>'+content+'</Q></BLOCKQUOTE>';
	oTextArea.insertPicture(sCopy);
}

/*
	删除留言/评论
	i为留言或者评论id
	type msg或者com 对应留言或者评论
*/
function del(i,type)
{
	if(i&&type)
	{
		if(confirm('确认要删除这条信息吗?'))
		{
			setCookie('del_id',i);
			setCookie('del_type',type);
			window.location.href=window.location.href.replace('#','');
		}
		else
		{
			return false;
		}
	}
}

/*
	发表评论验证
*/
function uploadData(obj)
{
	if($('checkcodevalue').value.toUpperCase()!=$('checkcode2').value.toUpperCase())
	{
		alert('验证码不正确')
		$('checkcode2').focus();
		return false
	}
	else
	{
		if($('hiddenUser1').checked)
			$('hiddenUser').value=1;
		else
			$('hiddenUser').value=0;

       
		$('editor_content').value=oTextArea._textarea.body.innerHTML;
		$('import_yzm').value=$('checkcode2').value;
		$('cur_yzm').value=$('checkcodevalue').value;
		if ($('editor_content').value=="")
		{
			alert('您输入的内容为空，请重新输入！')
			oTextArea._textarea.body.focus();
			return false
		}
		var re = /<img.+?>/gim;
		var aImg=$('editor_content').value.match(re);
		var imglen=0;
		if(aImg)
		{
			for(i=0;i<aImg.length;i++)
			{
				imglen+=aImg[i].length+2;
			}
		}
		if($('editor_content').value.replace(/<.+?>/gim,'').length+imglen>500)  
		{
			alert('您输入内容必须低于500个字符！')
			oTextArea._textarea.body.focus();
			return false;
		}
		//obj.submit();
		makeCode();
		$('checkcode2').value='';
		//$('editor_content').value='';
		oTextArea._textarea.body.innerHTML='';
	}
}
function addCacheAttch(src)
{
	var dl = document.createElement('dl');
	var dt = document.createElement('dt');
	var dd = document.createElement('dd');
	dt.innerHTML = '<a href="#"><img src="'+src+'" onload="autoSize(this,100,69)"  width:"0" height:"0" /></a>';
	dd.innerHTML = '<input type="checkbox" value="'+src+'"/>';
	dl.appendChild(dd);
	dd.appendChild(dt);
}
