var EDITOR_MIN_HEIGHT = -1;
var EDITOR_MAX_HEIGHT = 700;
var DIS_HEIGHT = 100;

var curDiv = null;

function Hanlder(e)
{
	e=e?e:window.event;
	var ele=e.srcElement?e.srcElement:e.target;
	
	while (ele != null)
	{
		if (ele == curDiv)
			return ;
		ele = ele.parentNode;
	}
	
	
	if (curDiv != null)
		remove_div();
}

document.onmousedown = Hanlder;

function getMyStyle(obj,styleName)   
{   
	var value;
	if (obj.currentStyle)   
        value =obj.currentStyle[styleName];   
	else if (window.getComputedStyle)   
		value = document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleName);   
	return value;   
}  

function get_height(obj)
{
	var value = 0;
	var text = getMyStyle(obj, "height");
	for (var i = 0; i < text.length; i++)
	{
		var c = text.charAt(i);
		
		if (c>='0' && c<='9')
			value = value*10 + parseInt(c);
		else
			break;
	}
	
	if (EDITOR_MIN_HEIGHT == -1)
		EDITOR_MIN_HEIGHT = value;
	
	return value;
}

function add_height(eid)
{
	var obj = document.getElementById(eid);
	var cur = get_height(obj);
	if (cur+DIS_HEIGHT < EDITOR_MAX_HEIGHT)
		cur = cur + DIS_HEIGHT;
	obj.style.height = cur + "px"; 
}

function decrease_height(eid)
{
	var obj = document.getElementById(eid);
	var cur = get_height(obj);
	if (cur-DIS_HEIGHT >= EDITOR_MIN_HEIGHT)
		cur = cur - DIS_HEIGHT;
	obj.style.height = cur + "px";
}

function add_tags(eid, tag1,tag2)
{
	var obj = document.getElementById(eid);
	
	//Code for IE
　	if　(document.selection)　
　　{
		obj.focus();
		var sel = document.selection.createRange();
		sel.text = tag1 + sel.text + tag2;
	}
	else　
	{
		obj.focus();
		//Code for Mozilla Firefox
		var len = obj.value.length;
		var start = obj.selectionStart;
		var end = obj.selectionEnd;
		var sel = obj.value.substring(start, end);
		var rep = tag1 + sel + tag2;
		obj.value = obj.value.substring(0,start) + rep + obj.value.substring(end,len);
	}
}

function url_submit(eid1, eid2, eid3)
{
	var obj = document.getElementById(eid2);
	var address = obj.value;
	obj = document.getElementById(eid3);
	var title = obj.value;
	obj = document.getElementById(eid1);
	var tag = "[url=" + address + "]" + title + "[/url]";
	add_tags(eid1, tag, "");
	
	remove_div();
}

function image_submit(eid1, eid2, eid3)
{
	var obj = document.getElementById(eid2);
	var name = obj.value;
	obj = document.getElementById(eid3);
	var index =obj.selectedIndex; //序号，取当前选中选项的序号
	var float_class = obj.options[index].value;
	obj = document.getElementById(eid1);
	var tag = "[img=" + float_class + "]" + name + "[/img]";
	add_tags(eid1, tag, "");
	
	remove_div();
}

function data_submit(eid1, eid2, eid3)
{
	var obj = document.getElementById(eid2);
	var name = obj.value;
	obj = document.getElementById(eid3);
	var index =obj.selectedIndex; //序号，取当前选中选项的序号
	var file_type = obj.options[index].value;
	remove_div();

    var pdid = document.getElementById("pdid").value;
    var url = "index.php?act=data&pdid=" + pdid + "&name=" + name + "&type=" + file_type;

    location.href = url;
}

function color_submit(eid, value)
{
	add_tags(eid, "[color="+value+"]", "[/color]");
	
	remove_div();
}

function emoticon_submit(eid1, value)
{
	var tag = "[:" + value +":]";
	add_tags(eid1, tag, "");
	
	remove_div();
}

function titile_submit(eid1, eid2)
{
	var obj = document.getElementById(eid2);
	var value = obj.value;
	var tag = "[title=" + value +"]";
	add_tags(eid1, tag, "");
	
	remove_div();
}

function create_div(clickEle, type)
{
	if (curDiv != null)
		remove();
    curDiv = document.createElement("div");
	curDiv.className = "subitem";
	var top = getAbsoluteTop(clickEle) + getAbsoluteHeight(clickEle) + 2;
	var left = getAbsoluteLeft(clickEle) + 2;
	curDiv.style.top = top + "px";
	curDiv.style.left = left + "px";
	curDiv.innerHTML = get_inner_html(type);
	document.body.appendChild(curDiv);
}

function get_inner_html(type)
{
	switch (type)
	{
		case "url":
			return url_inner_html();
		case "image":
			return image_inner_html();
		case "emoticon":
			return emoticon_inner_html();
		case "color":
			return color_inner_html();
		case "title":
			return title_inner_html();
		case "data":
			return data_inner_html();
		default:
			return "";
	}
	
	return "";
}

function url_inner_html()
{
	var innerHTML =
	"<div> \
		<span>地址:</span> \
		<input type=\"text\" class=\"text\" id=\"link_address\" /> \
	</div> \
	<div> \
		<span>标题:</span> \
		<input type=\"text\" class=\"text\" id=\"link_title\" /> \
	</div> \
	<div > \
		<input type=\"button\" class=\"button\" value=\"确定\" onclick=\"url_submit('ele', 'link_address', 'link_title');\"/> \
		<input type=\"button\" class=\"button\" value=\"取消\" onclick=\"remove_div();\"/> \
	</div>";
	
    return innerHTML;
}

function title_inner_html()
{
	var innerHTML =
	"<div> \
		<span>标题名称:</span> \
		<select id=\"title_name\" /> \
			<option value=\"Description\" selected>Description</option> \
			<option value=\"Input\">Input</option> \
			<option value=\"Output\">Output</option> \
			<option value=\"Sample Input\">Sample Input</option> \
			<option value=\"Sample Output\">Sample Output</option> \
			<option value=\"Source\">Source</option> \
			<option value=\"Hint\">Hint</option> \
		</select> \
	</div> \
	<div > \
		<input type=\"button\" class=\"button\" value=\"确定\" onclick=\"titile_submit('ele', 'title_name');\"/> \
		<input type=\"button\" class=\"button\" value=\"取消\" onclick=\"remove_div();\"/> \
	</div>";
	
    return innerHTML;
}

function data_inner_html()
{
	var innerHTML =
	"<div> \
		<span>文件名:</span> \
		<input type=\"text\" class=\"text\" id=\"image_name\" /> \
	</div> \
	<div> \
		<span>文件类型:</span> \
		<select id=\"image_float\" /> \
			<option value=\"in\">输入文件</option> \
			<option value=\"out\">输出文件</option> \
			<option value=\"spj\">特殊判定程序</option> \
		</select> \
	</div> \
	<div > \
		<input type=\"button\" class=\"button\" value=\"确定\" onclick=\"data_submit('ele', 'image_name', 'image_float');\"/> \
		<input type=\"button\" class=\"button\" value=\"取消\" onclick=\"remove_div();\"/> \
		<input type=\"button\" class=\"button\" value=\"选择/上传文件\" onclick=\"fileBrowserCallBack('file');\" /> \
	</div>";
	
    return innerHTML;
}

function image_inner_html()
{
	var innerHTML =
	"<div> \
		<span>文件名:</span> \
		<input type=\"text\" class=\"text\" id=\"image_name\" /> \
	</div> \
	<div> \
		<span>浮动方式:</span> \
		<select id=\"image_float\" /> \
			<option value=\"left\">左浮动</option> \
			<option value=\"center\">独自居中</option> \
			<option value=\"right\">右浮动</option> \
		</select> \
	</div> \
	<div > \
		<input type=\"button\" class=\"button\" value=\"确定\" onclick=\"image_submit('ele', 'image_name', 'image_float');\"/> \
		<input type=\"button\" class=\"button\" value=\"取消\" onclick=\"remove_div();\"/> \
		<input type=\"button\" class=\"button\" value=\"选择/上传图片\" onclick=\"fileBrowserCallBack('image');\" /> \
	</div>";
	
    return innerHTML;
}

function change_emoticon(img, flag)
{
	if (flag == 1)
		img.className = "on_emoticon";
	else
		img.className = "out_emoticon";
}

function emoticon_inner_html()
{
	var innerHTML = "";
	var i = 0;
	while (i < 25)
	{
		innerHTML += "<div>";
		for (var j = 0; j<5 && i<25; j++, i++)
		{
			innerHTML += "<img class='out_emoticon' src=\"" + "images/emoticons/"+ (i+1) +".gif\""
					  + " onmouseover=\"change_emoticon(this, 1);\""
					  + " onmouseout=\"change_emoticon(this, 0);\""
					  + " onclick=\"emoticon_submit('ele', '" + i +"');\"/>";
		}
		innerHTML += "</div>";
	}
	
    return innerHTML;
}

function change_color(color, flag)
{
	if (flag == 1)
		color.className = "on_color";
	else
		color.className = "out_color";
}

function color_inner_html()
{
	var colors = ["#000000", "#CC6633", "#FFFF00", "#00CC00", "#00CC99",
				"#808080", "#330066", "#330099", "#CCCCCC", "#3366FF",
				"#33CCCC", "#660000", "#660099", "#99CC99", "#666633",
				"#33FF00", "#9900FF", "#993300", "#99CC66", "#99FF00",
				"#99CCFF", "#CC0000", "#CCFF66", "#FF66CC", "#FFCCCC"];
	
	var innerHTML = "";
	var i = 0;
	while (i < colors.length)
	{
		innerHTML += "<div class='div_color'>";
		for (var j = 0; j<5 && i<colors.length; j++, i++)
		{
			innerHTML += "<div class='out_color' style=\"background:" + colors[i] + "\""
					  + " onmouseover=\"change_color(this, 1);\""
					  + " onmouseout=\"change_color(this, 0);\""
					  + " onclick=\"color_submit('ele', '" + colors[i] +"');\"></div>";
		}
		innerHTML += "</div>";
	}
	
    return innerHTML;
}


function remove_div()
{   
	if (curDiv != null)
	{
		var p = curDiv.parentNode;   
		p.removeChild(curDiv);
		curDiv = null;
	}
}

function getAbsoluteHeight(ob)
{
	return ob.offsetHeight
}

function getAbsoluteWidth(ob)
{
	return ob.offsetWidth;
}

function getAbsoluteLeft(ob)
{
	var mendingLeft = ob .offsetLeft;
	while( ob != null && ob.offsetParent != null && ob.offsetParent.tagName != "BODY" )
	{
		mendingLeft += ob .offsetParent.offsetLeft;
		ob = ob.offsetParent;
	}
	return mendingLeft; 
}

function getAbsoluteTop(ob)
{
	var mendingTop = ob.offsetTop;
	while( ob != null && ob.offsetParent != null && ob.offsetParent.tagName != "BODY" )
	{
	mendingTop += ob .offsetParent.offsetTop;
	ob = ob .offsetParent;
	}
	return mendingTop;
}

function fileBrowserCallBack(type) {
	var connector = "./js/filemanager/browser.html?Connector=connectors/php/connector.php";
	var enableAutoTypeSelection = true;
	
	var cType;
	
	switch (type) {
		case "image":
			cType = "Image";
			break;
		case "flash":
			cType = "Flash";
			break;
		case "file":
			cType = "File";
			break;
	}
	
	if (enableAutoTypeSelection && cType) {
		connector += "&Type=" + cType;
	}
	
	window.open(connector, "上传", "modal,width=600,height=400");
}

function create_tag(clickEle, id, name, alias, desc, action)
{
	if (curDiv != null)
		remove();
    curDiv = document.createElement("div");
	curDiv.className = "subitem";
	var top = getAbsoluteTop(clickEle) + getAbsoluteHeight(clickEle);
	var left = getAbsoluteLeft(clickEle);
	curDiv.style.top = top + "px";
	curDiv.style.left = left + "px";
	curDiv.innerHTML = tag_inner_html(id, name, alias, desc, action);
	document.body.appendChild(curDiv);
}

function quote_reply(eid, eid1)
{
	add_tags(eid, document.getElementById(eid1).value, '');
}

