

var portalXmlPath;


function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}

var RegisterDragable = function(header,root){
		var obj = root;
		obj.container = 1;		
		Drag.init(header, root);
	
		root.onDragStart = function(x,y,mousex, mousey) {
			var left = findPosX(this);
			var top = findPosY(this);
			
			var w = this.offsetWidth;
			this.style.width = (w-5)+"px";
			this.style.left = (left-5)+"px";
			this.style.top = (top-4)+"px";
			moduleGhost.style.height = this.offsetHeight+"px";
			this.parentNode.insertBefore(moduleGhost, this);			
			
			if (this.parentNode.childNodes.length==2){
				this.parentNode.onechild = true;
			}
			
			this.style.position = "absolute";
			this.isDragging = true;
			moduleGhost.col=this.parentNode;
	  }
		
		root.onDrag = function(x,y, mousex, mousey) {
			for(var i = 0;i<App.columnsObjs.length;i++){
				if ((x+this.offsetWidth/2)>=findPosX(App.columnsObjs[i])) col=App.columnsObjs[i];
			}
			//if ((x+this.offsetWidth/2)>=findPosX(App.columnsObj.col1)) col=App.columnsObj.col1;
			//if ((x+this.offsetWidth/2)>=findPosX(App.columnsObj.col2)) col=App.columnsObj.col2;
			//if ((x+this.offsetWidth/2)>=findPosX(App.columnsObj.col3)) col=App.columnsObj.col3;

			if (moduleGhost.col!=col) {
				moduleGhost.removeNode();
				
				if(!moduleGhost.col.hasChildNodes()){
						var oldParent = moduleGhost.col;
						oldParent.appendChild(blankDiv);
				}
				
				moduleGhost.col = col;
				
				col.appendChild(moduleGhost);
			}
			var elems = getModuleArr(col);
			var myPos = getGhostPos(elems, y);
			if (myPos!=0 && y<=findPosY(elems[myPos-1])) {
				moduleGhost.removeNode();
				col.insertBefore(moduleGhost, elems[myPos-1]);
			}
			if (myPos!=(elems.length-1) && y>=findPosY(elems[myPos+1])) {
				if (elems[myPos+2]) {
					col.insertBefore(moduleGhost, elems[myPos+2]);
				} else {
					col.appendChild(moduleGhost);
				}
			}
		}
		
		root.onDragEnd = function(x,y,el) {

			obj.container = moduleGhost.col.getAttribute("colNum");
			var col = moduleGhost.col;
			this.isDragging = false;
			col.replaceChild(this, moduleGhost);
			this.style.position = "static";
			this.style.width = "auto";
			//self.unHighlight();
			//App.saveUserData('order');
		}		
		
		function getModuleArr(column) {
			var arr = [];
			var ln = column.childNodes.length;
			for (var z=0; z<ln; z++) {
				if (!column.childNodes[z].isDragging)  {
					arr.push(column.childNodes[z]);
				}
			}
			return arr;
		}

		function getGhostPos(arr, y) {
			var ln = arr.length;
			for (var z=0; z<ln; z++) {
				if (arr[z]==moduleGhost) return z;
			}
		}
		
		function close() {
			window.event.cancelBubble = true;
			self.close();
			delete self;
		}	
}

var PageModule = function( module ,parent){
		var oParent = parent;
		var title = module.getAttribute("name");
		var moduleid = module.getAttribute("id");
				
		this.refreshMode = false; 
		var self = this;
		var divModule = document.createElement("div");
		divModule.className = "module";
		divModule.setAttribute("id","node"+moduleid);
		oParent.appendChild(divModule);
		
		
		//3001(学校首页)和3012(加入班级)样式要求变换
		
					
	  PageModule.addPageContent = function (originalRequest){
		
			var classN=module.getAttribute("clazz");		
			//alert(">>>>>>>>"+classN);
		
			var divModuleFrame = document.createElement("div");
			divModuleFrame.setAttribute("id","frame_"+"node"+moduleid);
			
			if(classN== null){
				divModuleFrame.className = "moduleFrame";
			}else{
				divModuleFrame.className = classN;
			}
			
			var scriptMode = module.getAttribute("script");
			var divModuleHeader = document.createElement("div");
			
			
			var divTitle = document.createElement("div");
			
			if(moduleid!=3001 && moduleid!=3012)  //去掉3001和3012的头部
			{
			
				//除文字以外的头部
				divModuleHeader.className = "titlewrap";				
				
				//带文字头部样式
				divTitle.className = "titleword";
				
				divTitle.appendChild(document.createTextNode(title));
			}
			
			//var divShowHide = document.createElement("div");
			//divShowHide.className = "showHide";
			//divShowHide.innerHTML = '<img src="${ctx}/img/showMod.gif"/>' ;
			
			
	
	//remove button for edit and delete 
	
		/*	var divClose = document.createElement("div");
			divClose.className = "close";
			var imgpath = '<img src="'+ctx+'/image/portal/';
			divClose.innerHTML = imgpath+ 'closeMod.gif"/>';
			divClose.style.display = "";
			divClose.attachEvent('onmousedown',close);*/
		
		/*	var divRefresh = document.createElement("div");
			divRefresh.className = "refresh";
			divRefresh.innerHTML = imgpath+'refresh.gif"/>';
			divRefresh.style.display = "";
			divRefresh.attachEvent('onmousedown',refresh);*/
			
	
		/*	var divEdit = document.createElement("div");
			divEdit.className = "edit";
			divEdit.innerHTML = imgpath+ 'editicon.gif"/>';
			divEdit.style.display = "";
			divEdit.attachEvent('onmousedown',editstyle);*/
			
			//divModuleHeader.appendChild(divShowHide);
			var divEditword = document.createElement("div");
			divEditword.className = "titleeditword";
		//	divEditword.appendChild(divClose);
		//	divEditword.appendChild(divEdit);
			//divModuleHeader.appendChild(divRefresh);
			
			if(moduleid!=3001 && moduleid!=3012)  //去掉3001和3012的头部
			{
				divModuleHeader.appendChild(divTitle);
				divModuleHeader.appendChild(divEditword);
			}
			
	
			divModuleFrame.appendChild(divModuleHeader);
			var divModuleContent = document.createElement("div");
			divModuleContent.className = "moduleContent";
			var content = originalRequest.responseText;
			if(Browser.isMozilla){
				content = content.replace(/\\/g, '/');
			}
			divModuleContent.innerHTML = 		content;
			//divModuleContent.innerText = originalRequest.responseText;		
			divModuleFrame.appendChild(divModuleContent);
			divModule.appendChild(divModuleFrame);
			
			new RegisterDragable(divModuleHeader,divModule);
			
			if(this.refreshMode ){
				//oParent.replaceChild(self,divModule);
				self = divModule;
			}else{
				//alert("test 2");
				self = divModule;
				//oParent.appendChild(divModule);
			}
			
			if(scriptMode == "eval"){
			  //alert(content);
				originalRequest.responseText.evalScripts();
			}
		}
		
	  PageModule.getPageContent = function ( module ){
	  	var url =ctx+module.getAttribute("className")+"?owner="+owner+"&ownertype="+ownertype;
	  	//alert(url);
	  	var pars = "";
	  	var myAjax = new Ajax.Request(
			url,
			{
				method: 'get',
				parameters: pars,
				onComplete: PageModule.addPageContent
			});
	 }
	 
function refresh() {
	var moduleself = $("node"+moduleid);
	this.refreshMode = true;
	//this.self = moduleself;
	
	var module = getModuleById(moduleid);
	new PageModule.getPageContent(module);
}
		function editstyle(){
			var t = document.getElementById('framestyle');
			t.style.display ="block";
			App.currentModuleId = "frame_node"+moduleid ;
			var d = document.getElementById('test');
			//t.style.top = d.style.top +20+"px";
			//t.style.top = '700px';
			//t.style.left = '300px';
			
			with(t.style){  
      			left  =  (document.body.offsetWidth-parseInt(width))/2  ;
      			top  =  (document.body.offsetHeight+400-parseInt(height))/2;
    		}       
			//t.style.background = "#888888";	
			//t.style.background = "#666699";						
		}	
	
function close() {
	//alert(moduleid);
	var moduleself = $("node"+moduleid);
	var parent = moduleself.parentNode;
	parent.removeChild(moduleself);//save close data
}	 
  new PageModule.getPageContent(module);
}

function getModuleById(moduleid){
	var rtn;
	for(var i=0;i<SystemModules.length;i++){   
		var oModule = SystemModules[i];		
		if(oModule.getAttribute("id") == moduleid){
			//alert(oModule.getAttribute("id")+oModule.getAttribute("name")+oModule.getAttribute("className"));
			return oModule;
		}
	}	
	return rtn;	
}





 function LoadFrameData(tag){
 
 	//alert(tag);
 	portalXmlPath = document.getElementById("portalXmlPath").value;
 	//alert(portalXmlPath+"1111---------------");
	if(Browser.isIE){
	 var   x = new   ActiveXObject("Microsoft.XMLDOM")  ;
		 x.loadXML(tag); 
	 SystemModules =x.selectNodes("/frame/system/node/action");
	 //alert(SystemModules.length);
	}else if(Browser.isMozilla){
	  var x = (new DOMParser()).parseFromString(tag, "text/xml");
		//var string = (new XMLSerializer()).serializeToString(xmlobject);					
		SystemModules =x.getElementsByTagName("action");	
	}
	

	
	if( !PersonalModules == 'undefined' && ! PersonalModules ==""){
	 
	 	 LoadingModule(SystemModules,PersonalModules);
	 	 
	}else{
		//alert("2222");
		OnLoadSaveLayout(portalXmlPath);
	}
 }
 
 function LoadingModule(SystemModules,PersonalModules){ 
 
	for(var i=0;i<PersonalModules.length;i++){   
		
		var mContainer = PersonalModules[i];	
		
		var mContainer_id = mContainer.getAttribute("id");
		
		var mContainerNode = $(mContainer_id);
		
		var modules = mContainer.getElementsByTagName("module");
		
		for( var j = 0;j<modules.length;j++){
			
			var module_id = modules[j].getAttribute("id");
		
			var clazz=modules[j].getAttribute("className");
			//alert("The clazz is : "+clazz);		
			var module = getModuleById(module_id);
			if(module == undefined){
					//alert('123');
			}else{	
				if( clazz != null){
					module.setAttribute("clazz",clazz);
				}
							
				new PageModule(module,mContainerNode );						
			}
		}
	} 	
 }
 
 function OnLoadSaveLayout(portalXmlPath){
	dwrtool.loadFromXML(portalXmlPath,setInitLayout);
 }
 
 function setInitLayout(originalRequest) {
 		if(Browser.isIE){
 			//alert("in setInitLayout 1");
			var   x = new  ActiveXObject("Microsoft.XMLDOM")  ;
	 		x.loadXML(originalRequest); 
	 		//注意清空缓存，不然配置文件0_10000000001.xml变换无效。
			PersonalModules =x.selectNodes("/frame/containers/container"); //**************************
		}else if(Browser.isMozilla){			
			var x = (new DOMParser()).parseFromString(originalRequest, "text/xml");
			PersonalModules = x.getElementsByTagName("container");		
		}
		//SystemModules为frame.xml文件中各system节点，personalModules为0_1000000000001.xml各container节点
		LoadingModule(SystemModules,PersonalModules);
		
 }


function onsavelayout(){
	var divsids= $$('#mainwrap div[id]');
	var ids = "";
	var styles = "";
	divsids.each( function(id){
		ids += id.id+",";
		var d = $("frame_"+id.id);
		if(d != null){
			styles+=id.id+"_"+d.getAttribute("className")+",";
		}
	});
	//alert(ids);
	//alert(styles);
	var url = ctx+'/frameajax.do';
	var pars = 'content=' +ids;
	pars += '&stylecontent=' +styles;
	if(owner !=""){
	  	pars += "&owner=";
	  	pars += owner;
	  	pars += "&ownertype=";
	  	pars += ownertype;
	  	pars += "&menuid="+App.menuid;
	}
	//alert(App.menuid);
	//alert(pars);
	//asynchronous: true,
	var myAjax = new Ajax.Request(
	url,
	{
	method: 'get',
	parameters: pars,
	onComplete: showSaveResult
	});
}

function showSaveResult(originalRequest){
	alert(originalRequest.responseText);
} 
	function selectstyle(s)	{
		var d = document.getElementById(App.currentModuleId);
		d.setAttribute("className",s);
		d.style.Height = '350px';		
		//alert(d.getAttribute("className")+d.getAttribute('id'));
	}
	
	function closeFrameStyle(){
		var t = document.getElementById('framestyle');
		t.style.display ="none";		
	}