/*
模板控制函数库，需要global.js
*/

// 将newElement元素插入targetElement元素之后
function insertAfter(newElement, targetElement) {
 var parent = targetElement.parentNode;
	 if(parent.lastChild == targetElement) {
	  parent.appendChild(newElement);
	 } else {
	  parent.insertBefore(newElement, targetElement.nextSibling);
	 }
}
//获取兄弟节点
function get_nextSibling(n)
  {
  var x=n.nextSibling;
  while (x&&x.nodeType!=1)
   {
   x=x.nextSibling;
   }
  return x;
}

function get_previousSibling(n)
  {
  var x=n.previousSibling;
  while (x&&x.nodeType!=1)
    {
    x=x.previousSibling;
    }
  return x;
  }
// 交换节点
if(browser.FF){
	Node.prototype.swapNode=function(Node){

			var nextSibling=this.nextSibling;

			var parentNode=this.parentNode;

			Node.parentNode.replaceChild(this,Node);

			parentNode.insertBefore(Node,nextSibling);

	}
}

//模块移动
function moveDown (e) {
	if(get_nextSibling(e)&&get_nextSibling(e).className.split(" ")[0]==e.className.split(" ")[0])
	//e.swapNode(get_nextSibling(e))
	e.parentNode.insertBefore(get_nextSibling(e),e);
}
function moveUp (e) {
	if(get_previousSibling(e)&&get_previousSibling(e).className.split(" ")[0]==e.className.split(" ")[0])
	e.swapNode(get_previousSibling(e))
}

//FireFox不支持outerHTML
if(window.HTMLElement){
  var _emptyTags = {
    'div': true
  };
  HTMLElement.prototype.__defineGetter__('outerHTML', function(){
    var attrs = this.attributes;
    var str = "<" + this.tagName;
    for(var i=0; i<attrs.length; i++){
      str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";
    }
    
    if(_emptyTags[this.tagName]) return str + "/>";
    return str + ">" + this.innerHTML + "</" + this.tagName + ">";
  });
  HTMLElement.prototype.__defineSetter__('outerHTML', function(sHTML){
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var df = r.createContextualFragment(sHTML);
    this.parentNode.replaceChild(df, this);
  });
}

//获取屏幕坐标
 var sb = {x: 0, y: 0, w: 0, h: 0};
 var cw = 0, ch = 0;
 function getScreen(){
  if (window.scrollWidth) {
	sb = {x: window.scrollLeft, y: window.scrollTop, w: window.scrollWidth, h: window.scrollHeight};
  } else if (document.documentElement.scrollWidth) {
	sb = {x: document.documentElement.scrollLeft, y: document.documentElement.scrollTop, w: document.documentElement.scrollWidth, h: document.documentElement.scrollHeight};
  } else if (document.body.scrollWidth) {
	sb = {x: document.body.scrollLeft, y: document.body.scrollTop, w: document.body.scrollWidth, h: document.body.scrollHeight};
  }

  if (window.innerHeight) {
    cw = window.innerWidth;
    ch = window.innerHeight;
  } else if (document.documentElement.clientHeight) {
    cw = document.documentElement.clientWidth;
    ch = document.documentElement.clientHeight;
  } else if (document.body.clientHeight) {
    cw = document.body.clientWidth;
    ch = document.body.clientHeight;
  } else {
    cw = ch = 0;
  }

}
//新图层
var m = "mask";
function openNewDiv(_id,_width) {
	if ($(_id)) document.body.removeChild($(_id));
	if ($(m)) document.body.removeChild($(m));
	if(typeof(_width)=="undefined") var _width="501";
	var newDiv = document.createElement("div");
	newDiv.id = _id;
	newDiv.style.position = "absolute";
	newDiv.style.zIndex = "9999";
	newDiv.style.width = _width+"px";
	getScreen();
	newDiv.style.top =((ch - newDiv.offsetHeight) / 2 + sb.y-150) + "px";
	newDiv.style.left = ((cw - _width) / 2 +sb.x) + "px"; // 屏幕居中

	newDiv.innerHTML = "加载中...";
	document.body.appendChild(newDiv);
	// mask图层
	var newMask = document.createElement("div");
	newMask.id = m;
	newMask.style.position = "absolute";
	newMask.style.zIndex = "1800";
	newMask.style.width = document.body.scrollWidth + "px";
	newMask.style.height = sb.h + "px";
	newMask.style.top = "0px";
	newMask.style.left = "0px";
	newMask.style.background = "#000";
	newMask.style.filter = "alpha(opacity=40)";
	newMask.style.opacity = "0.40";
	document.body.appendChild(newMask);
}


function openSaveDiv(_id,_width) {
	if ($(_id)) document.body.removeChild($(_id));
	if(typeof(_width)=="undefined") var _width="201";
	var newDiv = document.createElement("div");
	newDiv.id = _id;
	newDiv.style.position = "absolute";
	newDiv.style.zIndex = "9998";
	newDiv.style.width = _width+"px";
	getScreen();
	newDiv.style.top =((ch - newDiv.offsetHeight) / 2 + sb.y-250) + "px";
	newDiv.style.left = ((cw - newDiv.offsetWidth) / 2 + sb.x) + "px"; // 屏幕居中
	newDiv.style.background = "#EFEFEF";
	newDiv.innerHTML = "保存中...";
	document.body.appendChild(newDiv);
}

function closeDiv(_id){
	if ($(_id)) document.body.removeChild($(_id));
	if ($(m)) document.body.removeChild($(m));	
	//loadEdit();
}

//检查图片上传表单
function checkpic(id){
	if($F(id)==""){
		alert("请先选择图片！");
		return false;
		}
	return true;
}

//提示保存
function leaving(oEvent) {
	if(!oEvent) oEvent = window.event;
	if  (oEvent.clientY<0)
    oEvent.returnValue = "您所做的修改尚未保存！";
}

//保存内容
function dosubmit(id){
	//document.body.onbeforeunload=null;
	openNewDiv('msg');$('msg').innerHTML='<h3>正在保存中...</h3>';
	$('htmlcontent').value=getAllContent();
	$(id).submit();
}

function autoSave () {
	openSaveDiv("savediv");
	$("auto").value=1;
	$('htmlcontent').value=getAllContent();
	AJAXFORM("saveForm",autoSaveBack);
}
function autoSaveBack (doc) {
	//alert(doc);
	$("auto").value=0;
	closeDiv("savediv");
}


//add

function openNewNull(_id,_width) {
	var newMask = document.createElement("div");
	newMask.id = _id;
	newMask.style.position = "absolute";
	newMask.style.zIndex = "1300";
	newMask.style.width = document.body.scrollWidth + "px";
	newMask.style.height = document.body.scrollHeight + "px";
	newMask.style.top = "0px";
	newMask.style.left = "0px";
	newMask.style.background = "#000";
	newMask.style.filter = "alpha(opacity=40)";
	newMask.style.opacity = "0.40";
	document.body.appendChild(newMask);
	//document.getElementById("savetopic").style.display = "block";
	showBuyDiv("savetopic");

	//ld.style.left = (iw - ld.offsetWidth) / 2 + x + "px";
	//ld.style.top = (ih - ld.offsetHeight) / 2 + y + "px";

}

function showBuyDiv(id) {
    id = "savetopic";
	var x = 0, y = 0, iw = 0, ih = 0;
	x = document.documentElement.scrollLeft || document.body.scrollLeft || window.scrollLeft || 0;
	y = document.documentElement.scrollTop || document.body.scrollTop || window.scrollTop || 0;
    iw = document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
	ih = document.documentElement.clientHeight || document.body.clientHeight || window.innerHeight;
	
	//alert(x + '/' + y + '/' + iw + '/' + ih);
    var ld = $(id);
	ld.style.zIndex = "1500";
	ld.style.display = "block";
	ld.style.left = (iw - ld.offsetWidth) / 2 + x + "px";
	ld.style.top = (ih - ld.offsetHeight) / 2 + y + "px";
}


//保存弹出层
function open_div()
{
	openNewNull("center_div");
	return;
}

var sign	= 1;

function sc4(){
 $("float_r").style.top=(document.documentElement.scrollTop + 50)+"px";
 $("float_r").style.left=(document.documentElement.scrollLeft+document.documentElement.clientWidth-$("float_r").offsetWidth - 10)+"px";
}



//显示，隐藏层  //plan_l
function o_save(op)
{
	//none   block
	if(op == '1')
	{
		$("plan_name").style.display = "block";
	}
	else if(op == '2')
	{
		$("plan_name").style.display = "none";
		sign	= 1;
	}

}

function only_one(op)
{
	sign = op;
}

//清除所有数据
function clear_div()
{
	if(sign == '1')
	{
		o_save('2');
		o_plan('2', '');
	}
	else if(sign == '2')
	{
	//alert(sign);
		o_plan('2', '');
	}
	else if(sign == '3')
	{
		o_save('2');
	}
}
//plan_n
function o_plan(op, tid)
{
	if(op == '1')
	{
		get_backlist(tid);
		$("plan_list").style.display = "block";
	}
	else if(op == '2')
	{
		$("plan_list").style.display = "none";
		sign	= 1;
	}
}

function get_backlist(tid)
{
	var re	= function(xy)
	{
		document.getElementById("backlist").innerHTML = xy;
	}
	AJAXCALL("back_list.php?tid="+$E(tid), re);
}

//关闭层
function hidden_div()
{
	var ida	= "savetopic";	
	var idb	= "center_div";
	$(ida).style.display = "none";
	if ($(idb)) document.body.removeChild($(idb));
	try{event.cancelBubble=true}catch(e){};try{event.preventDefault();}catch(e){};return false;
	return ;
}

function outtime_report(op)
{
	if(op == '1')						//显示
	{
		$("register").style.display = "block";
		setTimeout("outtime_report(2)", 10000);
	}
	else if(op == '2')
	{
		$("register").style.display = "none";
	}
}

function strCharLen(str)
{
	var nLen	= str.length
	for(i = 0; i < str.length; i ++)
	{
		if(str.charCodeAt(i) > 255)	nLen ++;
	}
	return nLen;
}

String.prototype.Trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

function backup_sub()
{
	var bname	= $("bname").value;
	var len	= strCharLen(bname.Trim()); 
	if(len > 12 || len <= 0)
	{
		alert("请输入不大于12个字符/6个汉字的进度题目");
		return false;
	}
	openSaveDiv("savediv");
	$('content').value=getAllContent();
	o_save('2');
	AJAXFORM("backup",autoSaveBack);
	$('bname').value = '临时保存';
}

function check_page(id)
{
	$('back_content').value=getAllContent();
	//alert(getAllContent());
	$(id).submit();
	loadEdit();
}
function Keypress(e) {
    e = e ? e : window.event;
    if (e.keyCode == 13) {
        return;
    }
}

//confirm box
function TomConfirm(flag,html){
	if(flag==1){
	openNewDiv("editbox");
	$("editbox").className="editmod";
	$("editbox").innerHTML ='<ul class="editcont">'+html+'</ul>';
	}else{
		closeDiv("editbox");
	}
}
function TomAlert(flag,html){
	if(flag==1){
	openNewDiv("alertbox");
	$("alertbox").className="editmod";
	$("alertbox").innerHTML ='<ul class="editcont"><li><strong class="center"><strong>'+html+'</strong></li><li class="center"><input type="button" class="editsub" onclick="TomAlert(0)" value="确定" /></li></ul>';
	}else{
		closeDiv("alertbox");
	}
}
