Wiki.IView.Window = Object.inherit(Wiki.IView.View, {
	__CSS:["wiki wiki_pop", "wiki_dialog", "wiki_body"],
	__LAYERCSS:["wiki_layer", "wiki wiki_layer wiki_mask", "wiki_layer"],
	__DISPLAY:"block",
	__getCSS:function(){return this.__CSS[this._type];},
	__getLayerCSS:function(){return this.__LAYERCSS[this._type];}
});
Wiki.View.Popup = IX.Class.create();
Wiki.View.Popup.prototype = Object.inherit(IX.IView.Popup, Wiki.IView.Window, {
	__renderItem:function(ifEnabled, node, act){
		return this.pf("popup.tocitem", [ifEnabled?this.__genFunStr(act):"", node.getId(), ifEnabled?"":"wiki_disable",
			this.__renderWIcon("unselected.gif","","", "")+this.i18n("menu."+act)+ " ..."]);
	},
	_renderTocMenu:function(node){
		var menus = {node:[["popNewBelowSibling","popNewAboveSibling", "popNewChild", "popNewParent"],["popEdit"], ["popDelete"], ["popMove"]],
			2:[["popAddKnowledge"]],
			4:[["popAddAttachment"]],
			3:[["popPostComment"]],
			6:[["none"]]
		};
		var menu = menus[node.isNode()?"node":node.getType()];
		if(node.isItem()){menu[1] =["popEditAll", "popEditSummary"];}
		var shouldEnable=function(l, i1, i2){return (!node.isNode()||l>=2||(l==0 && (i1==1||i2==2))||(l==1 && i2!=3));};
		return IX.Utils.loop(menu, "", function(acc, item, idx){
			return acc + ((item && item.length>0)?this.__renderUl(IX.Utils.loop(item,"", function(acc2, item2, idx2){
				return acc2 + this.__renderItem(shouldEnable(node.getLevel(), idx, idx2)&& item2!="none", node, item2);
			}.bind(this))):"");
		}.bind(this));
	},
	_renderActionMenu:function(node) {
		return this.__renderUl(IX.Utils.loop(Wiki.Config.realFolderActions, "", function(acc, item, idx){
			return acc + this.__renderItem(true, node, item);
		}.bind(this)));
	},
	_renderChoseMenu:function(node, type){
		var sel = IX.Utils.loop(node.getViewParams(), -1, function(acc, item){return item[0]==type?item[1]:acc;});
		var menus = {req:["choseROne", "choseRWithR", "choseRNone"], 
			spec:["choseSOne", "choseSWithR", "choseSNone"], 
			ref:["choseShowRef", "choseNotShowRef"],
			img:["choseShowThumbnail", "choseShowList"]};
		var menu = menus[type];
		return this.__renderUl(IX.Utils.loop(menu, "", function(acc, item, idx){
			var ridx = (idx *1 +1)%menu.length;
			var flag = ridx!=sel;
			return acc + this.pf("popup.tocitem", [flag?this.__genFunStr("choseKilist"):"", node.getId()+"_" +type +"_"+ridx, 
					flag?"":"wiki_disable",this.__renderWIcon((flag?"un":"")+"selected.gif","","", "") +"&nbsp;" + this.i18n("menu."+item)]);
		}.bind(this)));
	},
	_renderHint:function(node){
		var path =node.getPath().split("\\");
		path.pop();
		path.push(node.getTitle());
		var htm = "";
		for (var i=path.length-1; i>0; i-=1){
			htm = this.__renderLi("\\"+ path[i] + (htm.length>0?this.__renderUl(htm):""));
		}
		return this.__renderUl(htm, 'class="wiki_folder_hint"');
	},
	_renderTopic:function(node){
		return this.__renderFLink("link2Topic", "Link", "").replace("#ID#", node.getId());
	},
	render:function(type, node){
		if (type =="toc"){return this._renderTocMenu(node);}
		if (type=="kitems" || type=="kipop"){return this._renderActionMenu(node);}
		if (type=="hint"){return this._renderHint(node);}
		if (type=="topic"){return this._renderTopic(node);}
		return this._renderChoseMenu(node, type.substring("chose".length));
	},
	_getDelta:function(type){
		if (type =="toc"){return [2, 2, 180];}
		if (type =="hint"){return [20, 16, 300];}
		if (type =="topic" || type=="kipop"){return [2,-2,98];}
		if (type=="kitems"){return [100, 16, 98];}
		return [10, 10, 200];
	},
	setPanelPos:function(type, x, y){
		var delta = this._getDelta(type);
		this._win.className = this.__CSS[this._type] + (type=="kitems"?" wiki_kilist_action":"");
		this._setPanelPos(x-delta[0], y+delta[1], delta[2]);
	},
	initialize:function(id, pid){ this._initBase(id, pid, false);}
});

Wiki.IView.Dialog = Object.inherit(Wiki.IView.View, {
	_pf:function(key, param){return this.pf("dialog."+key, param);},
	_i18n:function(key){return this.i18n("dialog."+ key);}
});
Wiki.View.DP_Input = IX.Class.create();
Wiki.View.DP_Input.prototype = Object.inherit(Wiki.IDP_Input, Wiki.IView.Dialog, {
	_init_extra:function(model){
		this._model = model;
		this._aux = {setHeight:function(){}};
	}
});

Wiki.View.DP_List = IX.Class.create();
Wiki.View.DP_List.prototype = Object.inherit(Wiki.IDP_List, Wiki.IView.Dialog, {
	_init_extra:function(model){
		this._model = model;
		this._aux = new Wiki.View.List(this._model.find(this._mtype==1?"root":this._nid), this._method);
	}
});
Wiki.View.DP_Edit = IX.Class.create();
Wiki.View.DP_Edit.prototype = Object.inherit(Wiki.IDP_Edit,Wiki.IView.Dialog,{
	_ifHtmlEdit:function(){		
		if (this._mtype== this.__AddAttach){return false;}
		if ((this._node.isImage())&& this._mtype==this.__EditSummary) {return false;}
		return Wiki.Config.enable[this._mtype==this.__EditSummary?"rich_summary":"rich_comment"];
	},
	_init_extra:function(model){
		var node = this._node;
		if (this._mtype==this.__ReplyComment || this._mtype==this.__PostComment){
			node = model.find("unknown");
			node.reset();
		}
		this._aux = new Wiki.View.Editor("", node,  this._ifHtmlEdit());
		this._isReadOnly = this._mtype==this.__EditComment && !this._node.canEdit();
		this._aux.setReadOnly(this._isReadOnly);
	}
});
Wiki.View.DP_EditSection = IX.Class.create();
Wiki.View.DP_EditSection.prototype = Object.inherit(Wiki.IDP_EditSection, Wiki.IView.Dialog,{
	_init_extra: function(model){
		this._ifPreview = false;
		this._dtype = 0;
		this._aux = new Wiki.View.Editor("section", this._node,  0);
	}
});

Wiki.View.Dialog = IX.Class.create();
Wiki.View.Dialog.prototype = Object.inherit(IX.IView.Dialog, Wiki.IView.Dialog, Wiki.IDialogView, Wiki.IView.Window, {
	_render:function(){return this._pf("tmpl", [this.__render(), this._id, this._callFun("renderTitle")]);}, 
	_refeshTitle:function(){},
	getFocus:function(){return this._id;},
	giveReminder:function(str) {
		$X(this._id + "_btns").innerHTML += str.inTag("p");
	},
	setHeight:function(){
		var wh = IX.Window.getInnerSize();
		if (wh[1] >0 && wh[0]>0){
			var l = 40;
			var h =  Math.max(wh[1] -80, 420) ;
			var w =  wh[0] - 80;
			if (this._mtype==10 || this._mtype==12){
				h = (this._mtype==10?420:120) + (isMSIE()?40:0);
				w = 750;
				l= Math.max((wh[0] - w)/2, 0);
			}
			if (isMSIE()) {l = l/2;}
			with(this._win.style){
				marginLeft=l+"px";
				marginRight=l+"px";
				marginTop="40px";
				height = h+ "px";
				width = w+"px";
			}
			with(this._preview.style){
				marginLeft=l+"px";
				marginRight=l+"px";
				marginTop="40px";
				//height = h+ "px";
				width = w+"px";
			}
			if(this._pView){
				this._pView.setHeight(h  - (isMSIE()?20:0) - (this._mtype==4?30:0), true);
			}
		}
	},
	initialize:function(model, dialogId, maskId){
		this._init_DialogView(model, dialogId, maskId);
	}
});

Wiki.View.ModalDialog = IX.Class.create();
Wiki.View.ModalDialog.prototype= Object.inherit(IX.IView.ModalDialog,Wiki.IDialog, {
	initialize:function(model, op, id, param, did){
		this._id = id;
		this._model = model;
		this._op = op;
		this._param = param;
		this._did = did;
		this._mtype = this._getMType(op);
		this._pView =this._getPlugin(this._mtype, id, param);
		this._width = 720;
		//var heights =  [480,350,390,350, 500,420,450,410, 390, 450];
		this._height = 500;//heights[this._mtype] + (isMSIE()?20:0);
		if (this._mtype==10 || this._mtype==12) {
			this._height=(this._mtype==10?410:120) + (isMSIE()?40:0);
		}
	},
	// For sub-componenet;
	getModel:function(){return this._model;},
	setDialog:function(d){
		this._dialog = d;
		this._dialogWin = d._dialogWin;
	}, // used to set dialog object in dialog window's object mdWrapper
	// Fow WikiController:
	_ifClosed:function(){return (!this._dialogWin || this._dialogWin.closed);},
	
	open:function(){ // open or refresh if opened		
		if (this._mtype<0){return alert(this._i18n("unknownType"));}
		if (!this.checkEditing("Pre")) {return false;}
		if(this._ifClosed()){this._open(Wiki.Config.ndialogbase, "did="+this._did, this._width, this._height);	}
		return true;
	},
	close:function(){if(!this._ifClosed()){this._dialogWin.close();}},
	_getDFun:function(name){return this._dialog?this._dialog[name].bind(this._dialog):function(){return "";};}
},{
	getTitle:function(){return this._getDFun("getTitle")();},
	getContent:function(){return this._getDFun("getContent")();},
	getDstId:function(){return this._getDFun("getDstId")();},
	getDtype:function(){return this._getDFun("getDtype")();},
	getInput:function(){return this._getDFun("getInput")();},
	//getExtTopic:function(){return this._getDFun("getExtTopic")();},
	getFirstCheckedTopicInfo:function(){return this._getDFun("getFirstCheckedTopicInfo")();},
	getAllCheckedTopicInfo:function(){return this._getDFun("getAllCheckedTopicInfo")();},
	refreshTopics:function() {return this._getDFun("refreshTopics")();},
	checkEditing:function(type){return type=="Pre"?this._pView._extraPreChecker():this._getDFun("checkEditing")("Post");},
	tryToAddAttach:function(){return this._getDFun("tryToAddAttach")(_projectID, _folderID, _userID, _issueID,_viewType);},
	giveReminder:function(str) {this._getDFun("giveReminder")(str);},
	resetId:function(oldId, newId){
		if (this._id==oldId){this._id==newId;}
		return this._getDFun("resetId")(oldId, newId);
	}
});

Wiki.View.Page = IX.Class.create();
Wiki.View.Page.prototype = Object.inherit(IX.IView.Window, Wiki.IView.Window, {
	getAppxView:function(type){
		return this.isAppxVisible(type)?this.newViewObject(type,!Wiki.Config.getComponentModelClassName(type)?this._model:this._model.getAppx(type)):null;
	},
	getView:function(id){
		if (!id) {return this;}
		if (id==this._model.getPrefix(this.__TOC)) {return this.getAppxView(this.__TOC);}
		if (id=="edall") {
			var view = new Wiki.View.Editor("section", this._model.find("root"), -2);
			view.setHeight(360);
			return view;
		}
		var node = this._model.find(id);
		if (node==null){return null;}
		return this.getNodeView(node);
	},
	getNodeView:function(node){return  this.newViewObject(node.getType(), node);},

	resetId:function(oldId, newId){this._container.replace(oldId, newId);},
	resize:function(width){
		this._container.resize(width);
		if (this.isAppxVisible(this.__QUICKLINKS)){this.getAppxView(this.__QUICKLINKS).resize();}
	},
	setShowIdx:function(isShow){
		if (this.isAppxVisible(this.__TOC)){ this.getAppxView(this.__TOC).idxCtl(isShow);}
		this.getView("root").refreshAllTidx();
	},
	show:function(){
		var page = this.getView("root");
		var  html =  "";
		if(this._model.isNew()){html += this.getView("edall").render(); }
		else {
			html += page.render();
			var list = this.getAppxList();
			var fun = function(acc, item) {
				var atype= this["__"+item];
				if (this.isAppxVisible(atype)){
					acc += this.getAppxView(atype).render();
				}
				return acc;
			}.bind(this);
			html += IX.Utils.loop(list[0], "",  fun) + page.renderChilds() + IX.Utils.loop(list[1], "",  fun);
		}
		this._container.show(html);
		if (this.isAppxVisible(this.__COMMENTS)){
			var view = this.getAppxView(this.__COMMENTS);
			if (view._model.getTotal()==0 && view._getView()){view.vctl(false);}
		}
		if (this.isAppxVisible(this.__QUICKLINKS)){
			this.getAppxView(this.__QUICKLINKS).resize();
		}
	},
	initialize:function(model, containerID, pid){
		this._model = model;
		this._init(containerID, pid, this.__VIEW);
		this._container = new IX.ViewContainer(this._win);
	}
});

Wiki.View.ExtWikiView = IX.Class.create();
Wiki.View.ExtWikiView.prototype = Object.inherit(Wiki.View.Page.prototype, {
	__getLayerCSS:function(){return "wiki_ext";},
	show:function(){
		var  html = IX.Utils.loop(this.getAppxList()[2], "",  function(acc, item) {
			var atype= this["__"+item];
			if (this.isAppxVisible(atype)){
				acc += this.getAppxView(atype).render(true);
			}
			return acc;
		}.bind(this));
		this._container.show(html);
	},
	refresh:function() {
		IX.Utils.loop(this.getAppxList()[2], 0,  function(acc, item) {
			var atype= this["__"+item];
			if (this.isAppxVisible(atype)){
				this.getAppxView(atype).refresh();
			}
		}.bind(this));
	},
	resize:function(w,h){
		//this._container.resize(w*1+40, h);
		if (h){this._layer.style.height=h +"px";}
		//this._win.style.width = (w-8) +"px";
	},
	setVisible:function(ifVisible){
		if (!ifVisible) {
			this._container.show("");
		} else {
			this.show();
		}
		this._display(this._layer, ifVisible);
	},
	isVisisble:function(){return this._layer?this._layer.style.display!="none":false;}
});
