{
    function LandingPage()
    {
    }
    
    LandingPage.DeclareType = function LandingPage_DeclareType()
    {
        this.DeclareField("m_oICViewer", null);
        this.DeclareField("m_oPreviousChunk", null);
        this.DeclareField("m_oNextChunk", null);
        this.DeclareField("m_oLinkToFullApplication", null);
        this.DeclareField("m_oQueryStringToFullApplication", null);
        this.DeclareField("m_sFullUrlWithoutQueryString", null);
        this.DeclareField("m_sViewMode", "text");
        
        this.ReplaceMethod("postInitialize", LandingPage_postInitialize);
        this.DeclareMethod("_onViewerLoaded", LandingPage_onViewerLoaded);
        this.DeclareMethod("onDataReceived", LandingPage_onDataReceived);
        this.ReplaceMethod("_updateChunks", LandingPage_updateChunks);
        this.ReplaceMethod("_updatePageLinks", LandingPage_updatePageLinks);
        this.DeclareMethod("_updateLinkToFullApplication", LandingPage_updateLinkToFullApplication);
        this.DeclareMethod("_updateWindowTitle", LandingPage_updateWindowTitle);
    }
    
    function LandingPage_postInitialize()
    {
        // parse query string
        if (!window.location)
            return;        
        var sUrl = window.location.href;
        
        var bUrlEncoding = String_parseBoolean(LandingPage.ConfigurationPreferences.EncodeDecodeLandingUrl);
        var sIsURLEncoded = this.m_oQueryString.getParam(Olive.WebAppBase.UrlParam.IsURLEncoded, 0);
		if (sIsURLEncoded != null)
		    bUrlEncoding = String_parseBoolean(sIsURLEncoded);
        
        this.m_oQueryString.m_bBase64 = bUrlEncoding;
        var oContentItem = Olive.ContentItem.CreateByType(OwcContentItem_Entity);
        // read "href" paraneter with BaseHRef
        var sBaseHRef = this.m_oQueryString.getParam(Olive.WebAppBase.UrlParam.HRef, 0);
        if (sBaseHRef)
            oContentItem.m_sDocHRef = sBaseHRef;
        // read "entity" paraneter with entity ID
        var entityId = this.m_oQueryString.getParam(Olive.WebAppBase.UrlParam.Entity, 0);
        if (entityId)
            oContentItem.m_sEntityId = entityId;
        // read "viewmode" parameter with view mode of IC Viewer
	    
	    // read view mode from cookie
	    var sViewMode = DHTML.getCookie("ICViewMode");
	    if (sViewMode && sViewMode.toLowerCase() in Olive.Data.InformationComponentViewerState.ViewModeActual)
	    {
		    this.m_sViewMode = sViewMode.toLowerCase();
		}
		
		// load IC Viewer
		if (oContentItem)
        {
            this.m_oICViewer = OwcGetControl("ctrlComponentViewer");
            this.m_oICViewer.setViewMode(this.m_sViewMode, true);
            this.m_oICViewer.contentLoadItem(oContentItem);
            this.m_oICViewer.attachOwcEventHandler("viewerLoaded", this._onViewerLoaded, this);
            this.m_oICViewer.attachOwcEventHandler("dataReceived", this._onDataReceived, this);
        }
        
        // prepare URL to the full application without query string
        var nLandingUrlIndex = sUrl.indexOf(LandingPage.ConfigurationPreferences.LandingPageUrl);
        this.m_sFullUrlWithoutQueryString = sUrl.substring(0, nLandingUrlIndex);
        this.m_sFullUrlWithoutQueryString += LandingPage.ConfigurationPreferences.AppMainFile;
        
        this.m_oPreviousChunk = OwcGetControl("ctrlPreviousChunk");
        this.m_oNextChunk = OwcGetControl("ctrlNextChunk");
        this.m_oLinkToFullApplication = document.getElementById("Link_FullApplication");
    }
    
    function LandingPage_onViewerLoaded(oEvent)
    {
        this._updateWindowTitle(oEvent);
        this._updateLinkToFullApplication(oEvent);
        this._updateChunks();
        this._updatePageLinks();
    }
    
    function LandingPage_updateLinkToFullApplication(oEvent)
    {
        // build link to the full application
        if (this.m_oLinkToFullApplication)
        {
            if (this.m_oQueryStringToFullApplication == null)
            {
                // at the first time - create link to the full application (without encoding)
                this.m_oQueryStringToFullApplication = new QueryString();
                this.m_oQueryStringToFullApplication.m_bBase64 = String_parseBoolean(LandingPage.ConfigurationPreferences.EncodeDecodeUrl);
                this.m_oQueryStringToFullApplication.addParam(Olive.WebAppBase.UrlParam.HRef, this.m_oQueryString.getParam(Olive.WebAppBase.UrlParam.HRef, 0), true);
                this.m_oQueryStringToFullApplication.addParam(Olive.WebAppBase.UrlParam.PageNo, this.m_oQueryString.getParam(Olive.WebAppBase.UrlParam.PageNo, 0), true);
                this.m_oQueryStringToFullApplication.addParam(Olive.WebAppBase.UrlParam.View, OwcContentItem_Document, true);
                var sQueryString = this.m_oQueryStringToFullApplication.getQueryString();         
                this.m_oLinkToFullApplication.href = this.m_sFullUrlWithoutQueryString  + ("?" + sQueryString);
            }
            else
            {
                // every next time - replace only page number
                this.m_oQueryStringToFullApplication.addParam(Olive.WebAppBase.UrlParam.PageNo, this.m_oICViewer.getContentItem().m_nPageNo, true);
                var sQueryString = this.m_oQueryStringToFullApplication.getQueryString();         
		        this.m_oLinkToFullApplication.href = this.m_sFullUrlWithoutQueryString  + ("?" + sQueryString);
            }
        }
    }
     
    function LandingPage_updateWindowTitle(oEvent)
    {
        var nICMaxTitleLength = 100;
        var sWindowTitle = oEvent.curObject.m_oContentItem.m_sTitle;
        if (sWindowTitle && sWindowTitle.length >= nICMaxTitleLength)
        {
            sWindowTitle = sWindowTitle.substring(0, nICMaxTitleLength) + "...";
        }
        if (sWindowTitle)
            document.title = sWindowTitle;
    }
    
    function LandingPage_onDataReceived(oEvent)
    {
        if (oEvent && oEvent.Request && oEvent.Request.sErrorMessage)
        {
            var oMenu = this.findControlByType(Olive.Controls.controlTypeNames.Menu);
            if (oMenu)
            {
                var oMenuItems = oMenu.findControlsByType(Olive.Controls.controlTypeNames.MenuItem);
                if (oMenuItems)
                {
                    for (var i = 0; i < oMenuItems.length; ++i)
                    {
                        DHTML.addCssClassToElem(oMenuItems[i].HtmlElement, "Hidden");
                    }
                }
            }
            if (this.m_oPreviousChunk && this.m_oNextChunk)
            {
                DHTML.addCssClassToElem(this.m_oPreviousChunk.HtmlElement, "Hidden");
				DHTML.addCssClassToElem(this.m_oNextChunk.HtmlElement, "Hidden");
            }
            if (this.m_oLinkToFullApplication)
            {
                DHTML.addCssClassToElem(this.m_oLinkToFullApplication, "Hidden");
            }
            this.m_oICViewer.detachOwcEventHandler("viewerLoaded", this._onViewerLoaded, this);
            var oError = Olive.Errors.createError(oEvent.Request.nErrorNumber, oEvent.Request.sErrorMessage);
            this.reportError(oError);            
        }
    }
    
    function LandingPage_updateChunks()
	{
		//update 'goto next/previous chunk' command state, and make the navigation menu invisible if necessary
		if (this.m_oPreviousChunk && this.m_oNextChunk)
		{
			this.m_oPreviousChunk.updateCommand();
			this.m_oNextChunk.updateCommand();
			if ((this.m_oPreviousChunk.getState() & Olive.IState.State.Disabled) &&
					(this.m_oNextChunk.getState() & Olive.IState.State.Disabled))
			{
				DHTML.addCssClassToElem(this.m_oPreviousChunk.HtmlElement, "Hidden");
				DHTML.addCssClassToElem(this.m_oNextChunk.HtmlElement, "Hidden");
			}
			else
			{
				DHTML.removeCssClassFromElem(this.m_oPreviousChunk.HtmlElement, "Hidden");
				DHTML.removeCssClassFromElem(this.m_oNextChunk.HtmlElement, "Hidden");
			}
		}
	}
	
	function LandingPage_updatePageLinks()
	{
	    var arrLinks = this.m_oICViewer.findControlsByType(Olive.Controls.controlTypeNames.ICViewerLink);
        if (arrLinks.length == 0)
            return;
            
        var oQueryString = new QueryString();
        oQueryString = Object_Clone(this.m_oQueryStringToFullApplication);
        
        for (var i = 0; i < arrLinks.length; ++i)
        {
            var oContentItem = arrLinks[i].getContentItem();
            if (!oContentItem)
                continue;
            var sType = oContentItem.m_sLinkType;
            if (sType == "PG")
            {
                // build link to the page in the full application
                var nPageNo = oContentItem.m_nPageNo;
                var oLinkHtmlObject = arrLinks[i].get_HtmlElement();
                
                oQueryString.addParam(Olive.WebAppBase.UrlParam.PageNo, nPageNo, true);
                var sQueryString = oQueryString.getQueryString();         
		        var sHref = this.m_sFullUrlWithoutQueryString  + ("?" + sQueryString);
		        oLinkHtmlObject.href = sHref;
            }
        }
	}
	
    JScript.Type.RegisterClass("LandingPage", LandingPage, Olive.Page);
}
Olive.WindowBinder.RegisterPageClass(window,LandingPage);
