/**
 * 代表一个Window，可拖拽DIV
 */	
	var WindowDraggable = Class.create();
	WindowDraggable.prototype = (new Rico.Draggable()).extend({
	   initialize: function(tPortletId,tPortletLable,tPortletImgURL,htmlElement ) {
	      this.portletImgURL = tPortletImgURL;
	      this.guiId = htmlElement;
	      this.portletId     = tPortletId;
	      this.portletLable     = tPortletLable;      
	      this.htmlElement   = $(htmlElement);
	      this.selected      = false;
	      this.type="window";
	      this.initializeHTML=this.htmlElement.innerHTML;
	   },
	   select: function() {
	      this.selected = true;
	      if (this.showingSelected)return;
	      var htmlElement = this.getMouseDownHTMLElement();
	      //var color = Rico.Color.createColorFromBackground(htmlElement);
	      //color.isBright() ? color.darken(0.033) : color.brighten(0.033);
	      this.saveBackground = RicoUtil.getElementsComputedStyle(htmlElement, "backgroundColor", "background-color");
	      //htmlElement.style.backgroundColor = color.asHex();
	      this.showingSelected = true;
	      this.getPortletContent(this.guiId,this.portletId,'','','','');
	   },
	   getType: function() {
	   	return this.type;   		
	   },   
	   endDrag: function() {
	   },
	   getPortletId:function(){
	   	return this.portletId;
	   },
	   getPortletLable:function(){
	   	return this.portletLable;
	   },      				
	   getPortletContent:function(tHtmlId,portletId,pageid,blockid,tabid,winid) {
			var cTime = new Date();
			var url = 'custom';
			var params = 'cmd=portletinvoke&portletid='+portletId+"&portalId="+"P0";
			var ajax = new Ajax.Updater(
				{},url,{method: 'post',evalScripts:false, parameters: params,onSuccess: reportSuccess,onFailure: reportError,onComplete:reportComplete}
			);
			function reportSuccess(request) {
			}
			function reportError(request) {
				tPortletContent = "<p>Error Invoke Portlet!</p>";
			}
			function reportComplete(request) {
				var tText = request.responseText;
				tText=tText.substring(tText.indexOf("<![CDATA[")+9,tText.lastIndexOf("]]>"));			
				rText = tText;
				$(tHtmlId).innerHTML=rText;
			}
		},
	   stateRecover:function(){
	   		this.htmlElement.innerHTML=this.initializeHTML;
	   }		   
	});

	var targetPortletId="";
	/**
	 * 代表一个Tab，接收DIV
	 */
	var TabDropzone = Class.create();
	TabDropzone.prototype = (new Rico.Dropzone()).extend( {
	   initialize: function(tHtmlElement,portalId,tPageId,tBlockId,tTabId,tTabWidth) {
		  this.portalId=portalId;
	      this.tabWidth=tTabWidth;
	      this.guiId=tHtmlElement;

	      this.htmlElement  = $(tHtmlElement);
	      this.htmlElement.style.width=this.tabWidth;

	      this.absoluteRect = null;
	      this.width="";
	      this.type="tab";
	      this.initializeHTML=this.htmlElement.innerHTML;

	      this.pageId=tPageId;
   	      this.blockId=tBlockId;
	      this.tabId=tTabId;
	      this.tabConfig=null;

	   },
	   getPageId: function() {
	   	return this.pageId;   		
	   },
	   getBlockId: function() {
	   	return this.blockId;   		
	   },
	   getTabId: function() {
	   	return this.tabId;   		
	   },  	   	   
	   canAccept: function(draggableObjects) {
	      return true;
	   },
	   getType: function() {
	   	return this.type;   		
	   },
	   accept: function(draggableObjects) {  	  
	      var htmlElement = this.getHTMLElement();
	      if ( htmlElement == null ) return;
	      n = draggableObjects.length;
	      for(var i=0;i<n;i++){
	         var theGUI = draggableObjects[i].getDroppedGUI();
	         if (RicoUtil.getElementsComputedStyle( theGUI, "position" ) == "absolute" ){
	            theGUI.style.position = "static";
	            theGUI.style.top = "";
	            theGUI.style.top = "";
	         }
	      	 if(draggableObjects[i] instanceof WindowDraggable){
	      	 	var windowDraggable = draggableObjects[i];
	      	 	var tContentHTML = this.htmlElement.innerHTML;
				windowDraggable.stateRecover();
	      	 	if(confirm("您希望增加此Portlet吗?")){
	      	 		portalCreateWindow(targetPortletId,windowDraggable.getPortletLable(),windowDraggable.getPortletId());
	      	 	}else{
	      	 		this.htmlElement.innerHTML=tContentHTML;
	      	 		window.location.reload();
	      	 	}
	      	 }
	      }
	      this.deactivate();
	   },
	   showHover: function() {
	      var htmlElement = this.getHTMLElement();
	      if ( htmlElement == null || this.showingHover )
	         return;
	
	      this.saveBorderWidth = htmlElement.style.borderWidth;
	      this.saveBorderStyle = htmlElement.style.borderStyle;
	      this.saveBorderColor = htmlElement.style.borderColor;
	
	      this.showingHover = true;
	      htmlElement.style.borderWidth = "2px";
	      htmlElement.style.borderStyle = "solid";
	      //htmlElement.style.borderColor = "#ff9900";
	      htmlElement.style.borderColor = "#00ff00";
	      
	      targetPortletId = this.getPortalId();
	      
	   },
	   	addWindow:function(windowGUI){
	   	  windowGUI.style.position = "";
	      var tr=document.createElement("tr");
	      this.table.appendChild(tr);
	      var td=document.createElement("td");
	      tr.appendChild(td);
	      td.appendChild(windowGUI);
	   	},
	   	stateRecover:function(){
	   		this.htmlElement.innerHTML=this.initializeHTML;
	   	},
		getPortalId:function(){
			return this.portalId;
		},
		setPortalId:function(portalId){
			this.portalId=portalId;
		},
		getGuiId:function(){
			return this.guiId;
		},
		setGuiId:function(tGuiId){
			this.guiId=tGuiId;
		},
		getTabConfig:function(){
			return this.tabConfig;
		},
		setTabConfig:function(tTabConfig){
			this.tabConfig=tTabConfig;
		}
	});
	/**
	 * Profile数据结构类
	 */
	var ProfileConfig = Class.create();
	ProfileConfig.prototype =(new Object()).extend( {
		initialize: function(){
			this.pageArray = new Array();
			this.portalContext="";
		},
		getPageCount:function(){
			return this.pageArray.length;
		},
		getPage:function(tIndex){
			return this.pageArray[tIndex];
		},
		getPageByName:function(pageName){
			for(var i=0;i<this.getPageCount();i++){
				if(this.pageArray[i].getName()==pageName){
					return this.pageArray[i];
				}
			}
			return null;
		},
		addPage:function(tPageConfig){
			var tPageArray = new Array(this.getPageCount()+1);
			for(var i=0;i<this.getPageCount();i++){
				tPageArray[i]=this.getPage(i);
			}
			tPageArray[this.getPageCount()]=tPageConfig;
			this.pageArray=tPageArray;
		},
		getPortalContext:function(){
			return this.portalContext;
		},
		setPortalContext:function(tPortalContext){
			this.portalContext=tPortalContext;
		}
	});	
	/**
	 * Page数据结构类
	 */
	var PageConfig = Class.create();
	PageConfig.prototype =(new Object()).extend( {
		initialize: function(tProfileConfig){
			this.profile=tProfileConfig;
			this.blockArray = new Array();
			this.name="";
			this.lable="";
			this.theme="";
			this.template="";
			this.staple="";
			this.logo="";
			this.id="";
		},
		getBlockCount:function(){
			return this.blockArray.length;
		},
		getProfile:function(){
			return this.profile;
		},
		getId:function(){
			return this.id;
		},
		setId:function(tId){
			this.id=tId;
		},
		getName:function(){
			return this.name;
		},
		setName:function(tName){
			this.name=tName;
		},
		getLable:function(){
			return this.lable;
		},
		setLable:function(tLable){
			this.lable=tLable;
		},
		getTheme:function(){
			return this.theme;
		},
		setTheme:function(tTheme){
			this.theme=tTheme;
		},
		getTemplate:function(){
			return this.template;
		},
		setTemplate:function(tTemplate){
			this.template=tTemplate;
		},
		getStaple:function(){
			return this.staple;
		},
		setStaple:function(tStaple){
			this.staple=tStaple;
		},
		getLogo:function(){
			return this.logo;
		},
		setLogo:function(tLogo){
			this.logo=tLogo;
		},
		getBlock:function(tIndex){
			return this.blockArray[tIndex];
		},
		addBlock:function(tBlockConfig){
			var tBlockArray = new Array(this.getBlockCount()+1);
			for(var i=0;i<this.getBlockCount();i++){
				tBlockArray[i]=this.getBlock(i);
			}
			tBlockArray[this.getBlockCount()]=tBlockConfig;
			this.blockArray=tBlockArray;
		}
	});
	/**
	 * Block数据结构类
	 */
	var BlockConfig = Class.create();
	BlockConfig.prototype =(new Object()).extend( {
		initialize: function(tPageConfig){
			this.page=tPageConfig;
			this.tabArray = new Array();
			this.id="";
			this.pageId="";	
		},
		initialize: function(){
			this.tabArray = new Array();
			this.id="";	
		},		
		getTabCount:function(){
			return this.tabArray.length;
		},
		getPage:function(){
			return this.page;
		},
		setPage:function(tPageConfig){
			this.page=tPageConfig;
		},	
		getPageId:function(){
			return this.pageId;
		},
		setPageId:function(tId){
			this.pageId=tId;
		},
		getId:function(){
			return this.id;
		},
		setId:function(tId){
			this.id=tId;
		},
		getTab:function(tIndex){
			return this.tabArray[tIndex];
		},
		addTab:function(tTabConfig){
			var tTabArray = new Array(this.getTabCount()+1);
			for(var i=0;i<this.getTabCount();i++){
				tTabArray[i]=this.getTab(i);
			}
			tTabArray[this.getTabCount()]=tTabConfig;
			this.tabArray=tTabArray;
		}
	});	
	/**
	 * Tab数据结构类
	 */
	var TabConfig = Class.create();
	TabConfig.prototype =(new Object()).extend( {
		initialize: function(tBlockConfig){
			this.block=tBlockConfig;
			this.windowArray = new Array();			
			this.width="";
			this.style="";
			this.id="";
			this.dropzone=null;
		},
		getWindowCount:function(){
			return this.windowArray.length;
		},
		getBlock:function(){
			return this.block;
		},
		setBlock:function(tBlockConfig){
			this.block=tBlockConfig;
		},
		getId:function(){
			return this.id;
		},
		setId:function(tId){
			this.id=tId;
		},
		getWidth:function(){
			return this.width;
		},
		setWidth:function(tWidth){
			this.width=tWidth;
		},
		getStyle:function(){
			return this.style;
		},
		setStyle:function(tStyle){
			this.style=tStyle;
		},
		getDropzone:function(){
			return this.dropzone;
		},
		setDropzone:function(tTabDropzone){
			this.dropzone=tTabDropzone;
		},
		getWindow:function(tIndex){
			return this.windowArray[tIndex];
		},
		addWindow:function(tWindowConfig){
			var tWindowArray = new Array(this.getWindowCount()+1);
			for(var i=0;i<this.getWindowCount();i++){
				tWindowArray[i]=this.getWindow(i);
			}
			tWindowArray[this.getWindowCount()]=tWindowConfig;
			this.windowArray=tWindowArray;
		}
	});

	/**
	 * Window数据结构类
	 */
	var WindowConfig = Class.create();
	WindowConfig.prototype =(new Object()).extend( {
		initialize: function(tTabConfig,tPortletId){
			this.tab=tTabConfig;
			this.portletId=tPortletId;
			this.lable="";			
			this.id="";
			this.registed=true;
			this.deployed=true;
			this.guiId="";		
		},
		getGuiId:function(){
			return this.guiId;
		},
		setGuiId:function(guiId){
			this.guiId=guiId;
		},
		getTab:function(){
			return this.tab;
		},
		setTab:function(tTab){
			this.tab=tTab;
		},
		getId:function(){
			return this.id;
		},
		setId:function(tId){
			this.id=tId;
		},
		getLable:function(){
			return this.lable;
		},
		setLable:function(tLable){
			this.lable=tLable;
		},
		getPortletId:function(){
			return this.portletId;
		},
		setPortletId:function(tPortletId){
			this.portletId=tPortletId;
		},
		getRegisted:function(){
			return this.registed;
		},
		setRegisted:function(tRegisted){
			this.registed=tRegisted;
		},
		getDeployed:function(){
			return this.deployed;
		},
		setDeployed:function(tDeployed){
			this.deployed=tDeployed;
		},
		drawWindow:function(tContextPath,portalId){
			if(this.getTab().getDropzone()!=null){
				this.getPortletContent(tContextPath,this,this.getGuiId(),this.getPortletId(),portalId,this.getLable());
			}
		},
	   	getPortletContent:function(tContextPath,tWindowConfig,tGuiId,portletId,portalId,winLable) {
	   	        var tWindowContent;
				var cTime = new Date();
				var url = 'custom';
				var params = 'cmd=portletinvoke&portletid='+portletId+"&portalId="+portalId+"&wdlb="+winLable;
				var ajax = new Ajax.Updater(
					{},url,{method: 'post',evalScripts:false, parameters: params,onSuccess: reportSuccess,onFailure: reportError,onComplete:reportComplete}
				);
				function reportSuccess(request) {
				}
				function reportError(request) {
					tPortletContent = "<p>Error Invoke Portlet!</p>";
				}
				function reportComplete(request) {
			   		if(tWindowConfig.getRegisted()&&tWindowConfig.getDeployed()){
						var tText = request.responseText;
						tText=tText.substring(tText.indexOf("<![CDATA[")+9,tText.lastIndexOf("]]>"));
						rText = tText;
						tWindowContent=rText;
					}else{
						var windowBegin = "<table width='100%' cellspacing='0' cellpadding='0'><tr class='window-top-background'><td><table width='100%' cellpadding='0' cellspacing='0'><tr><td class='window-top-left'><a class='menu' style='text-decoration: none'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></td><td class='window-top' valign='middle'>"+winLable+"</td><td class='window-top-right' align='right' valign='bottom'><table><tr><td><a class='menu' title='修改Window' href=javascript:portalEditWindow('"+portalId+"','"+portletId+"','"+winLable+"')><img src='"+tContextPath+"/theme/image/edit.png' border='0'/></a></td><td><a class='menu' title='删除Window' href=javascript:portalDeleteWindow('"+portalId+"','"+portletId+"','"+winLable+"');><img src='"+tContextPath+"/theme/image/delete.jpg' border='0'/></a></td></tr></table></td></tr></table></td></tr><tr><td><table width='100%' class='window-content' cellpadding='0' cellspacing='0' ><tr><td  align='left' valign='top'>";
						var windowEnd = "</td></tr><tr><td></td></tr></table></td></tr></table>";
						if(!tWindowConfig.getRegisted()){
							if(!tWindowConfig.getDeployed()){
								tWindowContent=windowBegin+"未注册且未部署"+windowEnd;
							}else{
								tWindowContent=windowBegin+"未注册"+windowEnd;
							}
						}else{
							tWindowContent=windowBegin+"未部署"+windowEnd;
						}
					}
					checkContentFull(tWindowContent,tGuiId);
				}
				function checkContentFull(tWindowContent,tGuiId) {
					var full=true;
					if(tWindowContent==null){
						full=false;
					}
					if(full){
						$(tGuiId).innerHTML=$(tGuiId).innerHTML+tWindowContent;
						//alert($(tGuiId).innerHTML);
					}
				}			
		}		
	});
	/**
	 * 代表一个回收站，可接收DIV
	 */
	var PortalRecycle = Class.create();
	PortalRecycle.prototype = (new Rico.Dropzone()).extend( {
	   initialize: function(htmlElement) {
	      this.htmlElement  = $(htmlElement);
	      this.absoluteRect = null;
	      this.type="recycle";
	   },
	   canAccept: function(draggableObjects) {
	      return true;
	   },
	   getType: function() {
	   	return this.type;   		
	   },
	   accept: function(draggableObjects) {
	      var htmlElement = this.getHTMLElement();
	      if ( htmlElement == null )
	         return;
	      n = draggableObjects.length;
	      for(var i=0;i<n;i++){
	         var theGUI = draggableObjects[i].getDroppedGUI();
	         if (RicoUtil.getElementsComputedStyle( theGUI, "position" ) == "absolute" ){
	            theGUI.style.position = "static";
	            theGUI.style.top = "";
	            theGUI.style.top = "";
	         }
	      	 if(draggableObjects[i] instanceof WindowDraggable){
	      	 	var windowDraggable = draggableObjects[i];
	      	 	windowDraggable.stateRecover();
	      	 }
	      }
	   }
	});
