/// <reference path="WebSdk.js" />
{
	function ContentsPanePage()
	{
		/// <summary>Viewer HTML page class</summary>
	}
	
	ContentsPanePage.ElementIDs = 
	{
		ContentsTabStripID      : "contentsTabStrip",
		ContentsPageSwitcherID  : "contentsPageSwitcher",
		PaneListContentsInternalID : "paneListContentsInternal",
		ThumbnailsPageId : "pagesThumbnailsItem",
		FeaturedPagesPageId : "featuredPagesItem",
		DocToc : "ctrlDocToc",
		DocThumbnailsId : "ctrlDocThumbnails",
		DocFeaturedPages : "ctrlDocFeaturedPages"
	};
	
	ContentsPanePage.ResourceStrings = 
	{		
		TabItem_TOC         : "TabItem_TOC",
		TabItem_PageThumbnails : "TabItem_PageThumbnails",
		TocPane_NoDocument	: "TocPane_NoDocument"
	}; //TabItem_FeaturedPages : "TabItem_FeaturedPages",

	ContentsPanePage.DeclareType = function ContentsPanePage_DeclareType()
	{
		/// <summary>
		///		Declares methods and properties of ContentsPanePage Web page class
		/// </summary>
		this.DeclareField("m_toc", null);
		this.DeclareField("m_pageThumbnails", null);
		this.DeclareField("m_featuredPages", null);
		this.DeclareField("m_oPaneListContentsInternal", null);
		this.DeclareField("m_thumbnailsPage", null);		
		this.DeclareField("m_featuredPagesPage", null);			

		// Overrides
		this.OverrideMethod("postInitialize", ContentsPanePage_postInitialize);
		this.DeclareMethod("BindDocViewerParts", ContentsPanePage_BindDocViewerParts);

		// Event handlers
        this.DeclareMethod("_updateThumbnailsPageItem", ContentsPanePage_updateThumbnailsPageItem);
        this.DeclareMethod("_updateThumbnailsPageItemIfActive", ContentsPanePage_updateThumbnailsPageItemIfActive);
        this.DeclareMethod("_onPageItemActivated", ContentsPanePage_onPageItemActivated);
		
		// Helper functions
		this.DeclareMethod("_findControls", ContentsPanePage_findControls);
		this.DeclareMethod("_expandFirstTocSection", ContentsPanePage_expandFirstTocSection);
		
	};

	ContentsPanePage.ApplyPrototype = function ContentsPanePage_ApplyPrototype(classProto)
	{
		/// <summary>
		///		Declares events and commands of ContentsPanePage Web page class
		/// </summary>
		/// <param name="classProto" type="object">
		///		Reference to the prototype of <see cref="ContentsPanePage" /> class.
		///		You may use it to declare commands and events for the class.
		/// </param>
	};

	function ContentsPanePage_postInitialize(popupArguments)
	{
		/// <summary>
		///		Initializes HTML pages after all controls are created.
		/// </summary>
		/// <param name="popupArguments" type="object">
		///		Reference to the arguments passed to this page by opener.
		///		If this page is not loaded in a popup window or opener did not
		///		pass any arguments to this page, <parameter name="popupArguments" />
		///		will be <c>null</c>
		/// </param>
		/// <remarks>
		///		This initialization routine is called after all Web controls on 
		///		this page are created.  This is a good place to binds controls 
		///		to each other, attaches event handlers and perform other 
		///		initialization tasks.
		/// </remarks>
				    
		// set resources strings
		for (var res_str in ContentsPanePage.ResourceStrings)
		    this.WebApplication.setResString(window.document, res_str);
		    
		// get controls
		this._findControls();
		
		// bind tab strips and page switchers
		var oContentsTabs = OwcGetControl(ContentsPanePage.ElementIDs.ContentsTabStripID);
		var oContentsPageSwitcher = OwcGetControl(ContentsPanePage.ElementIDs.ContentsPageSwitcherID);		
		if (oContentsTabs && oContentsPageSwitcher)
			Olive.Binding.SyncActiveItems.BindControls(oContentsTabs, oContentsPageSwitcher);

        this.m_thumbnailsPage.attachOwcEventHandler("stateChanged", this._updateThumbnailsPageItem, this);
		//this.m_featuredPagesPage.attachOwcEventHandler("stateChanged", this._updateThumbnailsPageItem, this);    

        // initialize magnifier control
	    if (this.m_magnifierControl)
	    {
	        this.m_magnifierControl.setMaxWidth(this.WebApplication.m_nMagnifierMaxWidth);
	        this.m_magnifierControl.setMaxHeight(this.WebApplication.m_nMagnifierMaxHeight);
	        this.m_magnifierControl.setMagPercent(this.WebApplication.m_nMagnifierMagPercent);
	    }

		this.m_bInitialized = true;
		this.WebApplication.onContentsPaneLoaded(); 
		
		// expand first TOC section with nodes
		Olive.EventSource.RegisterEventHandler("", "dataProcessed", false, this._expandFirstTocSection, this);	
	}
	
	function ContentsPanePage_BindDocViewerParts(docViewerAgg, thumbnailContainers)
	{
		docViewerAgg.set_TocControl(this.m_toc);
		docViewerAgg.m_oBindTocToDocViewer.setHighlightICOnly(!this.WebApplication.m_bHighlightOnTocSection);

		docViewerAgg.set_PageThumbnailsControl(this.m_pageThumbnails);

		//docViewerAgg.set_FeaturedPagesControl(this.m_featuredPages);
		//this.m_featuredPages.set_CreateCoverBack(this.WebApplication.m_bCreateCoverBack);   

		if (thumbnailContainers)
		{
			var index, count = thumbnailContainers.length;
			for (index = 0; index < count; ++index)
			{
				var container = thumbnailContainers[index];
				if (container)
					container.attachOwcEventHandler("stateChanged", this._updateThumbnailsPageItemIfActive, this);
			}
		}
	}
	
	function ContentsPanePage_findControls()
	{
		/// <summary>Find controls needed by the other methods on the page</summary>
        // get toc object        
		this.m_toc = OwcGetControl(ContentsPanePage.ElementIDs.DocToc);
		if (!this.m_toc)
		{
			this.reportError(DHTML.newError(ContentsPanePage.ErrorCodes.TocAbsence, "Internal error: Cannot find Olive.Controls.Toc control on HTML page"));
			return;
		}

		this.m_pageThumbnails = OwcGetControl(ContentsPanePage.ElementIDs.DocThumbnailsId);
		//this.m_featuredPages = OwcGetControl(ContentsPanePage.ElementIDs.DocFeaturedPages);;
		this.m_thumbnailsPage = OwcGetControl(ContentsPanePage.ElementIDs.ThumbnailsPageId);
		//this.m_featuredPagesPage = OwcGetControl(ContentsPanePage.ElementIDs.FeaturedPagesPageId);
		
		// get magnifier object
		this.m_magnifier = this.findControlByType(Olive.Controls.controlTypeNames.Magnifier);
		this.m_oPaneListContentsInternal = OwcGetControl(ContentsPanePage.ElementIDs.PaneListContentsInternalID);	
	}
	
	function ContentsPanePage_updateThumbnailsPageItem(eventArgs)
	{
	    /// <summary>Downloads PageThumbnails and FeaturedPages content</summary>
		/// <param name="eventArgs" type="Olive.Event">
		///		Reference to the event object
		/// </param>
		/// <remarks>
		///		The method is only for solving a bug on Mozilla - 
		///     images for canvas from Invisible page items do not fire onload event
		/// </remarks>	    
        var srcThumbnailList = this.m_pageThumbnails; //(eventArgs.srcObject == this.m_thumbnailsPage) ? this.m_pageThumbnails : this.m_featuredPages;
		if (srcThumbnailList.m_oContentItem)
			return;
		if (((eventArgs.newState & Olive.IState.State.ActiveMask) != Olive.IState.State.Active))
			return;
		srcThumbnailList.contentLoadItem(this.WebApplication.get_DocViewSource());
	}
	
	function ContentsPanePage_updateThumbnailsPageItemIfActive(eventArgs)
	{
	    /// <summary>Downloads PageThumbnails and FeaturedPages content</summary>
		/// <param name="eventArgs" type="Olive.Event">
		///		Reference to the event object
		/// </param>
		/// <remarks>
		///		The method is only for solving a bug on Mozilla - 
		///     images for canvas from Invisible page items do not fire onload event
		/// </remarks>	    
        
        // already updated
        if (this.m_pageThumbnails.m_oContentItem) // || this.m_featuredPages.m_oContentItem)
            return;
        
        var sSrcId = eventArgs.srcObject.m_sId;
        var bUpdate = ((sSrcId == "pageItemContents") || (sSrcId == "ctrlAllContents"));
        
        if (!bUpdate || ((eventArgs.newState & Olive.IState.State.ActiveMask) != Olive.IState.State.Active))
            return; 
        
        if (this.m_thumbnailsPage && this.m_thumbnailsPage.isStateSet(Olive.IState.State.Active) && !this.m_pageThumbnails.m_oContentItem)
            this.m_pageThumbnails.contentLoadItem(this.WebApplication.get_DocViewSource());
        
        //if (this.m_featuredPagesPage && this.m_featuredPagesPage.isStateSet(Olive.IState.State.Active))
            //this.m_featuredPages.contentLoadItem(this.WebApplication.get_DocViewSource());
	}
    
    function ContentsPanePage_onPageItemActivated(eventArgs)
    {
        if (this.m_oPaneListContentsInternal && this.m_oPaneListContentsInternal.recalcLayout)
            this.m_oPaneListContentsInternal.recalcLayout();
    }
    
    // after TOC data loading: expand first section with nodes
    function ContentsPanePage_expandFirstTocSection(eventArgs)
    {  
        if (eventArgs.srcObject.getControlType() == Olive.Controls.controlTypeNames.TOC )
        {
            if ( this.m_toc )
            {   
                // find TOC root
                var oTocRoot = this.m_toc.findControlsImplementing( Olive.ITreeNode );  
                if ( oTocRoot && oTocRoot.length > 0 )
                {
                    // find TOC sections 
                    var arrTocSection = oTocRoot[0].findControlsByType(Olive.Controls.controlTypeNames.TocSection);
                    if ( arrTocSection &&  arrTocSection.length > 0)
                    {
                        // find first section with nodes and expand it
                        for( var i=0 ; i < arrTocSection.length ; i++ )
                        {
                            var oTocSection = arrTocSection[i]; 
                            var arrTocSectionEntry = oTocSection.findControlsByType( Olive.Controls.controlTypeNames.TocEntry ); 
                            if ( arrTocSectionEntry && arrTocSectionEntry.length > 0)    
                            {
                                oTocSection.nodeExpand( OwcTreeNodeExpand_Expand );
                                break;
                            }
                        }
                    }
                }
            } 
        }   
    }

	JScript.Type.RegisterClass("ContentsPanePage", ContentsPanePage, Olive.Page);
}

Olive.WindowBinder.RegisterPageClass(window, ContentsPanePage);

