﻿var pX = 0;
var pY = 0;
// JavaScript Document
function leeeditorexe(execmd)
{
	tinyMCE.getInstanceById(editorID).execCommand(execmd);	
}
function leeeditorcss(cssName,cssValue)
{
	if($(tinyMCE.getInstanceById(editorID).selection.getNode()).attr("tagName") != "BODY")
	{
		$(tinyMCE.getInstanceById(editorID).selection.getNode()).css(cssName,cssValue);
	}
	//alert($(tinyMCE.getInstanceById(editorID).selection.getNode()).html());
}
function leeeditorInsertContent(insertText)
{
	tinyMCE.getInstanceById(editorID).execCommand("mceInsertContent",false,insertText);	
}
function editorFontBold()
{
	leeeditorexe("Bold");
}
function editorFontItalics()
{
	leeeditorexe("italic");
}
function editorFontUnderline()
{
	leeeditorexe("underline");
}
function editorAlign_Left()
{
	leeeditorexe("justifyleft");
}
function editorAlign_Both()
{
	leeeditorexe("justifycenter");
}
function editorAlign_Right()
{
	leeeditorexe("justifyright");
}
function editorClearAll()
{
	leeeditorexe("mceCleanup");
}
function cancelEditor()
{
	cancelFontFamily();	
	cancelEditorFontSize();
	cancelFontColor();
	cancelEditorLink();
	cancelEditorEmot();
	cancelEditorFlash();
	cancelEditorMusic();
	cancelEditorVideo();
	cancelEditorImg();
}

function reloadXY(ev)
{
	cancelEditor();
	pX = $(ev).attr("clientX");
	pY = $(ev).attr("clientY") + GetScrollTop();
}

function cancelFontFamily()
{
	$("#insertFontFamily").css("left","-999px");
	$("#insertFontFamily").css("top","-999px");	
}

function editorFontFamily(ev)
{
	reloadXY(ev);
	$("#insertFontFamily").css("left",pX);
	$("#insertFontFamily").css("top",pY);
}

function setEditorFontFamily(family)
{
	leeeditorcss("font-family",family);
	cancelEditor();
}

function editorFontSize(ev)
{
	reloadXY(ev);
	$("#insertFontSize").css("left",pX);
	$("#insertFontSize").css("top",pY);
}

function cancelEditorFontSize()
{
	$("#insertFontSize").css("left","-999px");
	$("#insertFontSize").css("top","-999px");	
}

function setEditorFontSize(sizeNum)
{
	leeeditorcss("font-size",sizeNum + "px");
	cancelEditor();
}

function cancelFontColor()
{
	$("#insertFontColor").css("left","-999px");
	$("#insertFontColor").css("top","-999px");	
}

function editorFontColor(ev)
{
	reloadXY(ev);
	$("#insertFontColor").css("left",pX);
	$("#insertFontColor").css("top",pY);	
}

function setEditorFontColor(colorValue)
{
	leeeditorcss("color",colorValue);
	cancelEditor();
}

function cancelEditorLink()
{
	$("#insertLink").css("left","-999px");
	$("#insertLink").css("top","-999px");
}

function EditorLink(ev)
{
	reloadXY(ev);
	$("#insertLink").css("left",pX);
	$("#insertLink").css("top",pY);	
}

function checkeURL(URL){
	var str=URL;
	//在JavaScript中，正则表达式只能使用"/"开头和结束，不能使用双引号
	//判断URL地址的正则表达式为:http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
	//下面的代码中应用了转义字符"\"输出一个字符"/"
	var Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/; 
	var objExp=new RegExp(Expression);
	if(objExp.test(str)==true){
		return true;
	}else{
		return false;
	}
} 

function setEditorLink(setEditorLink)
{
	_linkText = $("#insertLink input[name='LinkText']").val();
	_linkURL = $("#insertLink input[name='LinkURL']").val();
	if(_linkText != "" && checkeURL(_linkURL) && checkeURL!= "")
	{
		insertText = "<a href=\"" + _linkURL + "\" target=\"_blank\">" + _linkText + "</a>"
		leeeditorInsertContent(insertText);
	}
	cancelEditor();
}

function cancelEditorEmot()
{
	$("#insertIcon").css("left","-999px");
	$("#insertIcon").css("top","-999px");
}

function EditorEmot(ev)
{
	reloadXY(ev);
	$("#insertIcon").css("left",pX);
	$("#insertIcon").css("top",pY);	
}

function setEditorEmot(obj)
{
	_imgSrc = $(obj).attr("src");
	insertText = "<img src=\"" + _imgSrc + "\" class=\"Expression_BBS\" />";
	leeeditorInsertContent(insertText);
	cancelEditor();
}

function cancelEditorFlash()
{
	$("#insertFlash").css("left","-999px");
	$("#insertFlash").css("top","-999px");
}

function EditorFlash(ev)
{
	reloadXY(ev);
	$("#insertFlash").css("left",pX - 200);
	$("#insertFlash").css("top",pY);	
}

function setEditorFlash()
{
	_dW = 480;
	_dH = 360;
	_src = $("#insertFlash input[name='flashSrc']").val();
	_flashType = $("#insertFlash input[name='sizeType'][checked]").val();
	_w = $("#insertFlash input[name='falshW']").val();
	_h = $("#insertFlash input[name='falshH']").val();
	if(_flashType == "default")
	{
		_w = _dW;
		_h = _dH;
	}
	insertText = "<img title=\"src:'" + _src + "',width:'" + _w + "',height:'" + _h + "'\" class=\"mceItemFlash\" src=\"/editor/tiny_mce/plugins/media/img/trans.gif\" alt=\"\" width=\"" + _w + "\" height=\"" + _h + "\" />";
	leeeditorInsertContent(insertText);
	cancelEditor();
}

function cancelEditorMusic()
{
	$("#insertMusic").css("left","-999px");
	$("#insertMusic").css("top","-999px");
}

function EditorMusic(ev)
{
	reloadXY(ev);
	$("#insertMusic").css("left",pX - 200);
	$("#insertMusic").css("top",pY);
}

function setEditorMusic()
{
	_dW = 400;
	_dH = 45;
	_src = $("#insertMusic input[name='musicSrc']").val();
	_flashType = $("#insertMusic input[name='musicSizeType'][checked]").val();
	_w = $("#insertMusic input[name='musicW']").val();
	_h = $("#insertMusic input[name='musicH']").val();
	if(_flashType == "default")
	{
		_w = _dW;
		_h = _dH;
	}
	insertText = "<img title=\"src:'" + _src + "',width:'" + _w + "',height:'" + _h + "'\" class=\"mceItemWindowsMedia\" src=\"/editor/tiny_mce/plugins/media/img/trans.gif\" alt=\"\" width=\"" + _w + "\" height=\"" + _h + "\" />";
	leeeditorInsertContent(insertText);
	cancelEditor();
}

function cancelEditorVideo()
{
	$("#insertVideo").css("left","-999px");
	$("#insertVideo").css("top","-999px");
}

function EditorVideo(ev)
{
	reloadXY(ev);
	$("#insertVideo").css("left",pX - 200);
	$("#insertVideo").css("top",pY);	
}

function setEditorVideo()
{
	_dW = 400;
	_dH = 300;
	_src = $("#insertVideo input[name='videoSrc']").val();
	_flashType = $("#insertVideo input[name='videoSizeType'][checked]").val();
	_w = $("#insertVideo input[name='videoW']").val();
	_h = $("#insertVideo input[name='videoH']").val();
	if(_flashType == "default")
	{
		_w = _dW;
		_h = _dH;
	}
	insertText = "<img title=\"src:'" + _src + "',width:'" + _w + "',height:'" + _h + "'\" class=\"mceItemWindowsMedia\" src=\"/editor/tiny_mce/plugins/media/img/trans.gif\" alt=\"\" width=\"" + _w + "\" height=\"" + _h + "\" />";
	leeeditorInsertContent(insertText);	
	cancelEditor();
}

function cancelEditorImg()
{
	$("#insertImg").css("left","-999px");
	$("#insertImg").css("top","-999px");
}

function EditorImg(ev)
{
	reloadXY(ev);
	$("#insertImg").css("left",pX - 200);
	$("#insertImg").css("top",pY);	
}

function setEditorImg()
{
	_src = $("#insertImg input[name='imgSrc']").val();
	insertText = "<img src=\"" + _src + "\" alt=\"\" />";
	leeeditorInsertContent(insertText);	
	cancelEditor();
}

function editorHigher()
{
	//$("#" + editorID + "_ifr").height("600");
	_tempHeight = $("#" + editorID + "_ifr").height();
	$("#" + editorID + "_ifr").height((_tempHeight + 100).toString());
}

function editorLower()
{
	_tempHeight = $("#" + editorID + "_ifr").height();
	if(_tempHeight - 100 <= 300)
	{
		_tempHeight = 400;	
	}
	$("#" + editorID + "_ifr").height(_tempHeight - 100);
}

function showEditorContent()
{
	$("input[name='Topic']").attr("value",tinyMCE.getInstanceById(editorID).getContent());
}

function InsertLocalImg(obj)
{
	if($(obj).val() == "")
	{
		cancelEditorImg();
		showTips("false","上传图片只允许Jpg,Gif,BMP这三种格式，同时不能超过1.5M");
	}
	else
	{
		_src = $(obj).val()
		insertText = "<img src=\"" + _src + "\" alt=\"\" />";
		leeeditorInsertContent(insertText);	
		cancelEditorImg();
	}
}