function edit_module(span_obj){
	refresh_top();
	document.getElementById("dragEdits").style.display="";
	document.getElementById("jobedit").src="ajax.php?inc="+span_obj.offsetParent.offsetParent.id;
}
function edit_other(url){
	refresh_top();
	document.getElementById("dragEdits").style.display="";
	document.getElementById("jobedit").src=url;
}
function refresh_top(){
	tops=window.document.body.clientHeight + window.document.body.scrollTop-430;
	lefts=((window.document.body.clientWidth + window.document.body.scrollWidth)/2-725)/2;
	document.getElementById("dragEdits").style.top=tops;
	document.getElementById("dragEdits").style.left=lefts;
	setTimeout( "refresh_top()" , 10 );
}
document.write('<div class="dragEdits" id="dragEdits" style="display:none;position:absolute;width:725px;height:430px;background:transparent;overflow:hidden;top:180px;z-index:9999;" ><iframe name="jobedit" id="jobedit" src="about:blank" width="100%" height="100%" scrolling="no" ></iframe></div>');

var AJAX={
	http_request:false,
	DivObj:null,
	waitstate:null,
	success:null,
	get:function (divid,url,type) {
		AJAX.http_request = false;
		AJAX.DivObj = document.getElementById(divid);
		if(window.XMLHttpRequest) { //Mozilla 浏览器
			AJAX.http_request = new XMLHttpRequest();
			if (AJAX.http_request.overrideMimeType) {//设置MiME类别
				AJAX.http_request.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject) { // IE浏览器
			try {
				AJAX.http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					AJAX.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		if (!AJAX.http_request) {
			window.alert("不能创建XMLHttpRequest对象实例.");
			return false;
		}
		AJAX.http_request.onreadystatechange = AJAX.processRequest;
		var step=(type==1)?true:false;
		AJAX.http_request.open("GET", url+"&"+Math.random(), step);
		AJAX.http_request.send(null);
	},
    processRequest:function () {
        if (AJAX.http_request.readyState == 4) {
            if (AJAX.http_request.status == 200) {
				if(AJAX.DivObj!=null){
					AJAX.DivObj.innerHTML=AJAX.http_request.responseText;
				}
            } else {
                alert("您所请求的页面有异常。");
            }
        }else{
			if(AJAX.DivObj!=null){
				AJAX.DivObj.innerHTML='<hr>页面正加载中,请等稍候...<hr>';
			}
		}
    }
}

function ShowEditMenu(url,w,h){
	var evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (evt) {
		 ao = (evt.target) ? evt.target : evt.srcElement; //当前事件发生元素
	}
	position=get_position(ao);
	DivId="ajaxdiv"+w+h+url.replace(/([^\?]+)\?([^=]+)=([^&]+)(.*)/g, '$3');
	obj=document.getElementById(DivId);
	if(obj==null){
		obj=document.createElement("div");
		obj.innerHTML=document.getElementById('AjaxEditTable').outerHTML;
		objs=obj.getElementsByTagName("TD");
		//obj.id=DivId;
		objs[1].id=DivId;
		//obj.className="Editdiv";
		obj.style.Zindex='999';
		obj.style.position='absolute';
		obj.style.top=position.bottom;
		obj.style.left=position.left
		obj.style.height=h;
		obj.style.width=w;
		document.body.appendChild(obj);
		//obj.innerHTML='以下是显示内容...';
		AJAX.get(DivId,url,1);
	}else{
		fobj=obj.offsetParent.offsetParent;
		if(fobj.style.display=='none'){
			fobj.style.display='';
		}else{
			fobj.style.display='none';
		}
	}
}

function get_position(o){//取得坐标
	var to=new Object();
	to.left=to.right=to.top=to.bottom=0;
	var twidth=o.offsetWidth;
	var theight=o.offsetHeight;
	while(o!=document.body){
		to.left+=o.offsetLeft;
		to.top+=o.offsetTop;
		o=o.offsetParent;
	}
	to.right=to.left+twidth;
	to.bottom=to.top+theight;
	return to;
}

function choose_stylefile(cssfile){
	AJAX.get('',"ajax.php?inc=ol_style&step=2&style="+cssfile,1);
	
	Mycss.styleSheet.cssText="";
	var head = document.getElementsByTagName('HEAD').item(0); 
	if((cssobj=document.getElementById('Testcss'))!=null){
		cssobj.styleSheet.cssText="";
		head.removeChild(cssobj);
	}
	var style = document.createElement('link'); 
	style.href = "images/skin/"+cssfile+"/css.css"; 
	style.rel = 'stylesheet'
	style.type = 'text/css';
	style.id = 'Testcss'; 
	head.appendChild(style); 
	//本来以下两句简单的话,就能处理了.但是浏览器总死掉
	//Testcss.styleSheet.cssText="";
	//Testcss.styleSheet.addImport("images/skin/"+cssfile+"/css.css"+'?'+Math.random());
}

function choose_layout(num){
	AJAX.get('',"ajax.php?inc=ol_layout&step=2&layout="+num,1);
	window.location.reload();
}

function choose_module(o,moduleid){
	
	ob=document.getElementById(moduleid);
	if(o.checked==true){
		if(ob!=null){
			ob.style.display='';
		}else{
			obj=document.createElement("div");
			obj.id='_'+moduleid;
			document.getElementById("MainTd_0").appendChild(obj);
			//Fobj=document.getElementById("MainTd_0").getElementsByTagName("table");
			//alert(Fobj[0].id)
			//document.getElementById("MainTd_0").insertBefore(obj,Fobj[0]);
			AJAX.get(obj.id,"ajax.php?inc=ol_module&step=2&moduleid="+moduleid,0);
		}
	}else{
		ob.style.display='none';
	}
	
	move_module(); //模块移动
	Drag.inint(); //重新初始化可移动的层
	
}
function ShowDoEdit(){
	obj=document.getElementById('EditMenuTable');
	if(obj.style.display=='none'){
		setCookie('showDoEdit',1);
		obj.style.display='';
	}else{
		setCookie('showDoEdit',0);
		obj.style.display='none'
	}
}

var EditModule={
	tableid:null,
	init:function(){
		var MainTable=document.getElementById("MainTable");
		oo=MainTable.getElementsByTagName("SPAN");
		for(var i=0;i<oo.length;i++){
			if(oo[i].className=='eidtmodule'&&oo[i].getAttribute("onclick")==null){
				oo[i].title='点击可以修改这里的设置';
				if (document.all) { //For IE
					oo[i].attachEvent("onmousedown",EditModule.showdiv);
				}else{ //For Mozilla
					oo[i].addEventListener("onmousedown".substr(2,"onmousedown".length-2),EditModule.showdiv,true);
				}
			}
		}
	},
	showdiv:function(evt){
		var w=150;
		var h=100;
		var evt = (evt) ? evt : ((window.event) ? window.event : "");
		if (evt) {
			 ao = (evt.target) ? evt.target : evt.srcElement;
		}
		oid=ao.offsetParent.offsetParent.id;
		//获取坐标的函数头部有定义
		position=get_position(ao);
		DivId="EditModule_"+oid;
		url="ajax_freepost.php?inc=freepost";
		obj=document.getElementById(DivId);
		if(obj==null){
			obj=document.createElement("div");

			obj.innerHTML=document.getElementById('AjaxEditTable').outerHTML;
			objs=obj.getElementsByTagName("TD");
			objs[1].id=DivId;
			//obj.id=DivId;
			//obj.className="Editdiv";
			obj.style.Zindex='999';
			//obj.style.display='';
			obj.style.position='absolute';
			//obj.style.top=position.bottom - 180;
			//obj.style.left=position.left-w+200;
			obj.style.top=200;
			obj.style.left=500;
			obj.style.height=h;
			obj.style.width=w;
			document.body.appendChild(obj);
			//obj.innerHTML='以下是显示内容...';
			AJAX.get(DivId,url,1);
		}else{
			fobj=obj.offsetParent.offsetParent;
			if(fobj.style.display=='none'){
				fobj.style.display='';
			}else{
				fobj.style.display='none';
			}
		}
	},
	save:function(divid,oid,va){
		//alert(oid)
		//GET方式提交内容,如果有空格的话.会有BUG
		//即时显示,不过没判断是否保存成功也显示了
		if(oid=='intro'&&upFileIframe.document.form1!=null){
			if(upFileIframe.document.form1.postfile.value!=''){
				//upFileIframe.document.form1.submit();
				alert("你刚才选择了一张图片,请先上传");
				return false;
			}
		}
		va=va.replace(/(\n)/g,"@BR@");
		AJAX.get(divid,"ajax.php?inc=ol_EditModule&step=2&TagId="+oid+"&DivId="+divid+"&va="+va,0);
		/*
		//以下转了一个弯是因为table不能使用innerHTML
		fobj=document.getElementById(oid).offsetParent;
		Newobj=document.createElement("div");
		Newobj.id="New_"+oid;
		fobj.insertBefore(Newobj,document.getElementById(oid));
		fobj.removeChild(document.getElementById(oid));
		Newobj.id=oid;
		AJAX.get(oid,"ajax.php?inc=ol_module&step=2&moduleid="+oid+"&ajax=1",0);
		*/

		fs=document.getElementById(oid).getElementsByTagName("TD");
		fs[1].id="TD_"+oid;
		AJAX.get(fs[1].id,"ajax.php?inc=ol_module&step=2&moduleid="+oid+"&ajax=1",0);

		//change_tagName.init();	//重新初始化
		//EditModule.init();	//重新初始化
		//move_module(); //模块移动.可不用,防止而已
		//Drag.inint(); //重新初始化可移动的层
	},
	cancel:function(divid){
		document.getElementById(divid).offsetParent.offsetParent.style.display='none';
	},
	upfile:function(url,name,size,va){
		document.getElementById("introImgInput").value=url;
	}
}



function getlength(str){
	var Goodureg = /^[\u4E00-\u9FA5]*$/;
	var GooduLength = 0;
	var GooduString = str;
	for(var i = 0 ; i < str.length; i ++){
		if(Goodureg.test(GooduString.charAt(i)) == true){
			GooduLength = GooduLength + 2;
		}else{
			GooduLength = GooduLength + 1;
		}
	}
	return GooduLength;
}

//导航菜单的自动适应长度
function _navigation(){
	obj=document.getElementById("navigation").getElementsByTagName("A");
	for(var i=0;i<obj.length;i++){
		//alert(getlength(obj.innerText));
		leng=getlength(obj[i].innerText);
		width=leng*7+20;
		obj[i].style.width=width;
	}
}

function makesmallpic(obj,w,h){
	var srcImage = new Image();
	srcImage.src=obj.src;
	var srcW=srcImage.width;		
	var srcH=srcImage.height;
	if(srcW>srcH){
		if(srcW>w){
			obj.width=newW=w;
			obj.height=newH=(w/srcW)*srcH;
		}else{
			obj.width=newW=srcW;
			obj.height=newH=srcH;
		}
	}else{
		if(srcH>h){
			obj.height=newH=h;
			obj.width=newW=(h/srcH)*srcW;
		}else{
			obj.width=newW=srcW;
			obj.height=newH=srcH;
		}
	}
	if(newW>w){
		obj.width=w;
		obj.height=newH*(w/newW);
	}else if(newH>h){
		obj.height=h;
		obj.width=newW*(h/newH);
	}
}