$package('com.trs.wcm.infoview');

var _MULTIVIEW_ = com.trs.wcm.infoview.MULTIVIEW = {
	URLParameters : null,
	ActiveStyle : "background:#FFFFFF; font:bold 10pt; color:#000000; cursor:default; border-bottom:1pt solid #000000; border-top:1pt solid #FFFFFF",
	InactiveStyle : "background:#ECE9D8; font:bold 10pt; color:#933A93; cursor:hand; border-bottom:1pt solid #000000; border-top:0pt solid #000000",
	m_oMultiView : null,
	FetchMultiViewData : function() {
		var nInfoViewId = 0;
		var nFlowDocId = 0;
		if(window.frmAction){
			if(window.frmAction.InfoViewId){
				nInfoViewId = window.frmAction.InfoViewId.value;
			}
			if(window.frmAction.FlowDocId){
				nFlowDocId = window.frmAction.FlowDocId.value;
			}
		}
		var myURL = new com.trs.wcm.infoview.MyURL();
		var sRequestUrl = "infoview_load_views.jsp?InfoViewId="
							+ (myURL.getParameter("InfoViewId") || nInfoViewId)
							+ "&FlowDocId="
							+ (myURL.getParameter("FlowDocId") || nFlowDocId);
		new Ajax.Request(sRequestUrl,{
			method : 'get',
			onSuccess : function(_transport){
				_MULTIVIEW_.InitMultiViewData(_transport.responseText);
			},
			onFailure : function(_transport){
				alert("Problem retrieving XML data");
			}
		});
	},
	InitMultiViewData : function(_sResponseText) {
		var oMultiView = new com.trs.wcm.infoview.MultiView("infoview");
		var arrWCMObjs = WCMObjHelper.parseXMLStrToObjs(_sResponseText);
		var nFlowDocId = 0;
		if(window.frmAction){
			if(window.frmAction.FlowDocId){
				nFlowDocId = window.frmAction.FlowDocId.value;
			}
		}
		if(arrWCMObjs.length==0){
//			alert("\u60A8\u6CA1\u6709\u6743\u9650\u67E5\u770B\u4EFB\u4F55\u89C6\u56FE.");
		}
		for(var i=0; i<arrWCMObjs.length; i++) {
			var oWCMObj = arrWCMObjs[i];
			if(!oWCMObj)
				continue;
			var sIvViewId = oWCMObj.getProperty("IvViewId");
			var sViewId = oWCMObj.getProperty("InfoViewId");
			var sViewName = oWCMObj.getProperty("ViewDesc");
			var sDefaultView = oWCMObj.getProperty("DefaultView");
			var sPublicFill = oWCMObj.getProperty("PublicFill");
			var oArgs = {
				default_view : sDefaultView,
				public_fill  : sPublicFill
			};
			var sURL = 'infoview_view_to_html.jsp?InfoViewId=' + sViewId + '&IVViewId=' + sIvViewId;
			if(PageContext.isShowMode())
				sURL += '&ReadOnly=1';
			else if(PageContext.isAddEditMode())
				sURL += '&isAddEdit=1';
			if(_MULTIVIEW_.URLParameters != null && _MULTIVIEW_.URLParameters.length>0)
				sURL += '&' + _MULTIVIEW_.URLParameters;
			sURL += "&FlowDocId=" +
				(((new com.trs.wcm.infoview.MyURL())).getParameter("FlowDocId") || nFlowDocId);
			oMultiView.AppendItem(sIvViewId, sViewName, sURL, oArgs);
		}
		_MULTIVIEW_.m_oMultiView = oMultiView;
		oMultiView.Create();
	},
	UpdateNavigator : function(_sInfoViewViewIds, _oProps){
		if(!_MULTIVIEW_.m_oMultiView||!_sInfoViewViewIds)return;
		var aViewIds = _sInfoViewViewIds.split(',');
		for(var i=0;i<aViewIds.length;i++){
			if(aViewIds[i]=='')continue;
			var oNavTab = _MULTIVIEW_.m_oMultiView.GetNavigatorTab(aViewIds[i].trim());
			if(oNavTab==null)continue;
			for(var sName in _oProps){
				oNavTab.setAttribute(sName, _oProps[sName]);
			}
		}
	}
}

com.trs.wcm.infoview.MultiView = Class.create('wcm.infoview.MultiView');
com.trs.wcm.infoview.MultiView.prototype = {
	initialize : function(_sInfoViewBaseId){
		this.m_sInfoViewBaseId = _sInfoViewBaseId || "infoview";
		this.m_oContainer = $(this.m_sInfoViewBaseId);
		if(this.m_oContainer == null) {
			alert("can not find infoview container [" + this.m_sInfoViewBaseId + "]!");
			return;
		}
		this.m_oContainer.innerHTML = '';
		this.m_sIframeId = this.m_sInfoViewBaseId + "_iframe";
		this.m_oIframe = document.createElement("DIV");
		this.m_oContainer.appendChild(this.m_oIframe);
		this.m_oIframe.id = this.m_sIframeId;
		this.m_oIframe.style.cssText = "background:#FFFFFF; overflow:auto";
		this.m_oIframe.width = "100%";

		this.m_sNavigatorId = this.m_sInfoViewBaseId + "_navigator";
		this.m_oNavigator = document.createElement("DIV");
		this.m_oContainer.appendChild(this.m_oNavigator);
		this.m_oNavigator.id = this.m_sNavigatorId;
		this.m_oNavigator.style.cssText = "height:20px;background:#ECE9D8";

		this.m_arrViews = new Array();
	},
	AppendItem : function(_sId, _sName, _sURL, _oArgs) {
		this.m_arrViews.push({
			id      : _sId,
			name    : _sName,
			url     : _sURL,
			args    : _oArgs
		});
	},
	SwitchView : function(_nViewId){
		if(this._LastActiveViewId==_nViewId)return;
		if(this._LastActiveViewId!=null&&this._LastActiveViewId!=0){
			var oLastNavTabLeft = $(this.m_sNavigatorId +"_l_" + this._LastActiveViewId);
			if(oLastNavTabLeft)
				oLastNavTabLeft.innerHTML = '<img src="sheet_close_head.gif" border="0" align="absmiddle" height="16px">';
			var oLastNavTabRight = $(this.m_sNavigatorId +"_r_" + this._LastActiveViewId);
			if(oLastNavTabRight)
				oLastNavTabRight.innerHTML = '<img src="sheet_close_tail.gif" border="0" align="absmiddle" height="16px">';
			var oLastNavTabCenter = $(this.m_sNavigatorId +"_" + this._LastActiveViewId);
			if(oLastNavTabCenter)
				oLastNavTabCenter.style.cssText = _MULTIVIEW_.InactiveStyle;
			var oLastView = $(this.m_sIframeId + "_" + this._LastActiveViewId);
			if(oLastView)
				oLastView.style.display = 'none';
		}
		var oNavTabLeft = $(this.m_sNavigatorId +"_l_" + _nViewId);
		if(oNavTabLeft)
			oNavTabLeft.innerHTML = '<img src="sheet_open_head.gif" border="0" align="absmiddle" height="16px">';
		var oNavTabRight = $(this.m_sNavigatorId +"_r_" + _nViewId);
		if(oNavTabRight)
			oNavTabRight.innerHTML = '<img src="sheet_open_tail.gif" border="0" align="absmiddle" height="16px">';
		var oNavTabCenter = $(this.m_sNavigatorId +"_" + _nViewId);
		if(oNavTabCenter)
			oNavTabCenter.style.cssText = _MULTIVIEW_.ActiveStyle;
		var oView = $(this.m_sIframeId + "_" + _nViewId);
		if(oView)
			oView.style.display = 'inline';
		this._LastActiveViewId = _nViewId;
	},
	_NavTab : function(oInfoView,_bActive){
		this.m_oNavigator.appendChild(this._NavTabLeft(oInfoView,_bActive));
		this.m_oNavigator.appendChild(this._NavTabCenter(oInfoView,_bActive));
		this.m_oNavigator.appendChild(this._NavTabRight(oInfoView,_bActive));
	},
	_NavTabLeft : function(oInfoView,_bActive){
		var oSpan = document.createElement("span");
		oSpan.height = "30pt";
		oSpan.id = this.m_sNavigatorId +"_l_" + oInfoView.id;
		oSpan.style.cssText = "border-bottom:0pt solid #000000; border-top:0pt solid #000000";
		oSpan.setAttribute("trs_type", "image_head");
		if(_bActive){
			oSpan.innerHTML = '<img src="sheet_open_head.gif" border="0" align="absmiddle" height="16px">';
		}
		else{
			oSpan.innerHTML = '<img src="sheet_close_head.gif" border="0" align="absmiddle" height="16px">';
		}
		return oSpan;
	},
	GetNavigatorTab : function(_nViewId){
		return $(this.m_sNavigatorId + "_" + _nViewId);
	},
	_NavTabCenter : function(oInfoView,_bActive){
		var oSpan = document.createElement("span");
		var nViewId = oInfoView.id;
		oSpan.id = this.m_sNavigatorId + "_" + nViewId;
		oSpan.innerHTML = "&nbsp;" + oInfoView.name + "&nbsp;";
		if(_bActive){
			oSpan.style.cssText = _MULTIVIEW_.ActiveStyle;
		}
		else{
			oSpan.style.cssText = _MULTIVIEW_.InactiveStyle;
		}
		oSpan.verticalAlign = "middle";
		oSpan.setAttribute('infoview_navigator_tab', 1);
		oSpan.setAttribute("trs_base_id", this.m_sInfoViewBaseId);
		oSpan.setAttribute("trs_item_id", oInfoView.id);
		oSpan.setAttribute("trs_item_default_view", oInfoView.args.default_view);
		oSpan.setAttribute("trs_item_public_fill", oInfoView.args.public_fill);
		oSpan.setAttribute("element-type", 12);
		oSpan.setAttribute("trs_type", "text");
		Event.observe(oSpan, 'click', function(){
			this.SwitchView(nViewId);
		}.bind(this));
		PageElementHelper.EventObserveMe(oSpan);
		return oSpan;
	},
	_NavTabRight : function(oInfoView,_bActive){
		var oSpan = document.createElement("span");
		oSpan.id = this.m_sNavigatorId +"_r_" + oInfoView.id;
		oSpan.height = "16pt";
		oSpan.style.cssText = "border-bottom:0pt solid #000000; border-top:0pt solid #000000";
		if(_bActive){
			oSpan.innerHTML = '<img src="sheet_open_tail.gif" border="0" align="absmiddle" height="16px">';
		}
		else{
			oSpan.innerHTML = '<img src="sheet_close_tail.gif" border="0" align="absmiddle" height="16px">';
		}
		oSpan.setAttribute("trs_type", "image_tail");
		return oSpan;
	},
	_InfoViewView : function(oInfoView,_bActive){
		var oIframe = document.createElement("iframe");
		oIframe.id = this.m_sIframeId + "_" + oInfoView.id;
		oIframe.frameBorder = "0";
		oIframe.width = "100%";
		if(this.m_oContainer.height) {
			oIframe.height = this.m_oContainer.height - 30;
		} else {
			if(window.INFOVIEW_MODE) {
				var nClientHeight = parseInt(document.body.clientHeight);
				if(window.INFOVIEW_MODE == "Setting") {
					oIframe.height = nClientHeight - 175;
				} else if(window.INFOVIEW_MODE == "Preview") {
					oIframe.height = nClientHeight - 60;
				} else if(window.INFOVIEW_MODE == "AddEdit") {
					if($('DocTitle')==null){
						oIframe.height = nClientHeight - 90;
					}
					else{
						oIframe.height = nClientHeight - 120;
					}
					//oIframe.height = nClientHeight - 160;
				} else if(window.INFOVIEW_MODE == "Show") {
					oIframe.height = nClientHeight - 120;
				} else if(window.INFOVIEW_MODE == "WorkFlow_Setting") {
					oIframe.height = nClientHeight - 64;
				} else {
					oIframe.height = "500";
				}
			} else {
				oIframe.height = "500";
			}
		}
		oIframe.scrolling = "auto";
		oIframe.src = oInfoView.url;
		oIframe.style.display = (_bActive)?"":"none";
		oIframe.setAttribute("trs_type", "iframe");
		this.m_oIframe.appendChild(oIframe);
	},
	Create : function() {
		this.m_oNavigator.innerHTML = "";
		this.m_oIframe.innerHTML = "";
		this._LastActiveViewId = 0;
		for(var i=0; i<this.m_arrViews.length; i++) {
			var oInfoView = this.m_arrViews[i];
			var bActive = oInfoView.args.default_view==1;
			this._NavTab(oInfoView,bActive);
			this._InfoViewView(oInfoView,bActive);
			if(bActive){
				this._LastActiveViewId = oInfoView.id;
			}
		}
		if(this._LastActiveViewId==0&&this.m_arrViews.length>0){
			this.SwitchView(this.m_arrViews[0].id);
		}
	}
}

/***********************************************************************************************/
com.trs.wcm.infoview.MultiView.setSerialCode = function() {
	var sXMLString = frmAction.ObjectXML.value;
	var sHasDocSerial = document.getElementById("HasDocSerial").value || "0";
	var sSerialPattern = document.getElementById("SerialPattern").value || "";
	var sSerialPeriod = document.getElementById("SerialPeriod").value || "-1";
	var sSerialField = document.getElementById("SerialField").value || "";
	var oArgs = {
		XMLString    : sXMLString,
		HasDocSerial  : sHasDocSerial,
		SerialPattern : sSerialPattern,
		SerialPeriod  : sSerialPeriod,
		SerialField   : sSerialField
	};
	TRSDialogContainer.display("SerialCodeDialog", oArgs);
}
com.trs.wcm.infoview.MultiView.setReplyNotice = function() {
	var sXMLString = frmAction.ObjectXML.value;
	var sReplyNoticeRequired = document.getElementById("ReplyNoticeRequired").value || "0";
	var sNoticeRelatedField = document.getElementById("NoticeRelatedField").value || "";
	var sNoticeSubjectPattern = document.getElementById("NoticeSubjectPattern").value || "-1";
	var sNoticeContentPattern = document.getElementById("NoticeContentPattern").value || "";
	var nInfoViewId = (new com.trs.wcm.infoview.MyURL()).getParameter("InfoViewId") || "0";
	var oArgs = {
		InfoViewId           : nInfoViewId,
		XMLString            : sXMLString,
		ReplyNoticeRequired  : sReplyNoticeRequired,
		NoticeRelatedField   : sNoticeRelatedField,
		NoticeSubjectPattern : sNoticeSubjectPattern,
		NoticeContentPattern : sNoticeContentPattern
	};
	TRSDialogContainer.display("ReplyNoticeDialog", oArgs);
}
