/// <reference path="OwcDataBound.js" />
/// <reference path="OwcList.js" />
/// <reference path="OwcThumbnail.js" />

if (typeof(JScript) == "undefined")
	throw DHTML.newError(0, "JavaScript OOP API is not included.  Add JScriptCore.js to your file");

if (typeof(Olive) == "undefined" || !Olive.Controls)
	throw DHTML.newError(0, "Olive Web SDK core classes are not included.  Add OwcControl.js to your file");

// -------------------------- Constants -------------------------
Olive.Controls.controlTypeNames.ThumbList = "thumb-list";
Olive.Controls.controlTypeNames.ThumbListItem = "page-list-item";

Olive.Controls.attributes.ThumbListKind = Olive.Controls.prefix + ":" + "kind";
Olive.Controls.attributes.ThumbListTopicName = Olive.Controls.prefix + ":" + "topic-name";
Olive.Controls.attributes.ThumbListShowEmptyPage = Olive.Controls.prefix + ":" + "show-empty-page";
Olive.Controls.attributes.ThumbListPageCriterion = Olive.Controls.prefix + ":" + "page-criterion";
Olive.Controls.attributes.ThumbListCreateCoverBack = Olive.Controls.prefix + ":" + "create-cover-back";
Olive.Controls.attributes.ThumbListUseMagnifier = Olive.Controls.prefix + ":" + "use-magnifier";
Olive.Controls.attributes.ThumbListMagnifierOffset = Olive.Controls.prefix + ":" + "magnifier-offset";

var OwcThumbList_AllQuickNavigation = "all-quick-navigation";

var OwcUrlParam_ThumbListKind = "ThumbListKind";
var OwcUrlParam_ThumbListTopicName = "topic-name";
var OwcUrlParam_ThumbListShowEmptyPage = "ShowEmptyPage";
var OwcUrlParam_ThumbListPageCriterion = "page-criterion";
var OwcUrlParam_ThumbListCreateCoverBack = "create-cover-back";

/*=============================================================================
 * Class   : Olive.Controls.ThumbList
 * Summary : Control displaying list of pages (associated with
 *           a single document or by scanning repository (for one publication)).
 *			 There are some limitations in supporting full pagination for
 *			 repository scan type control. It is supported only pagination to those pages that
 *			 were already requested. In internal field m_arrAnchors the control 
 *			 keeps anchors (of the form BaseHRef/PageNo) for requests that were already done.
=============================================================================*/
if (!Olive.Controls.ThumbList)
{
	Olive.Controls.ThumbList = function OwcThumbList()
	{
		// register PageListItem, defined in PageList
		OwcListRegisterItemType(this, "Page", Olive.Controls.controlTypeNames.ThumbListItem, "Olive.Controls.ThumbListItem");
	} // Olive.Controls.ThumbList()

	Olive.Controls.ThumbList.ThumbListKind = 
	{
		Auto : "auto",
		DocPages : "doc-pages",
		QuickNavigation : "quick-navigation",
		RepositoryScan : "repository-scan"
	}
	
	Olive.Controls.ThumbList.DateDefinitions = 
	{
		FromYear : "1600",
		ToYear : "9999",
		FromMonth : "01",
		ToMonth : "12",
		FromDay : "01"
	}
 
    // global functions calling ThumbList methods
	Olive.Controls.ThumbList.onThumbnailMouseOverGlobal = function Thumblist_onThumbnailMouseOver(oElem, oEvent)
    {
        var oHtmlElem = Olive.Controls.ThumbList.findHtmlParentByAttr( oElem , Olive.Controls.attributes.ControlType, Olive.Controls.controlTypeNames.ThumbList );
        var oParentControl = OwcGetControlFromHtmlElem( oHtmlElem );
        if( oParentControl )
        {
            oParentControl.onThumbnailMouseOver(oElem, oEvent);
        }
    };
    
    Olive.Controls.ThumbList.onThumbnailImageLoadGlobal = function Thumblist_onImageLoad(oElem)
    {   
        var oHtmlElem = Olive.Controls.ThumbList.findHtmlParentByAttr( oElem , Olive.Controls.attributes.ControlType, Olive.Controls.controlTypeNames.ThumbList );
        var oParentControl = OwcGetControlFromHtmlElem( oHtmlElem );
        if( oParentControl )
        {
            oParentControl.onImageLoad(oElem);
        }
    };
    
    //search for parent html element where attribute has value sAttrValue
    Olive.Controls.ThumbList.findHtmlParentByAttr = function Thumblist_findHtmlParentByAttr( oHtmlElemArg , sAttrName, sAttrValue )
    {
	    var oHtmlElem = oHtmlElemArg;
	    while (oHtmlElem )
	    {
	        if( DHTML.getAttr(oHtmlElem, sAttrName) === sAttrValue )
	            break;
		    oHtmlElem = oHtmlElem.parentNode;
        }
        return oHtmlElem;
    };
    
	Olive.Controls.ThumbList.Templates = 
	{
		ThumbListFeaturedPages : "ThumbListFeaturedPages"
	}
    
	Olive.Controls.ThumbList.DeclareType = function OwcThumbList_DeclareType(rObject)
	{
		// Overrides
		this.OverrideMethod("navigateToPage", OwcThumbList_navigateToPage);
		this.OverrideMethod("parseControlHtmlAttr", OwcThumbList_parseControlHtmlAttr);
		this.OverrideMethod("initControl", OwcThumbList_initControl);
		this.OverrideMethod("contentBuildControlParams", OwcThumbList_contentBuildControlParams);

		// Public members and methods:
		this.DeclareField("m_sKind", Olive.Controls.ThumbList.ThumbListKind.Auto);	// Thumbnail list kind
		this.DeclareField("m_sTopicName", OwcThumbList_AllQuickNavigation);	// Thumbnail topic name
		this.DeclareField("m_bShowEmptyPage", true);	// show or do not an emtpy page at the beginning, 
											// if all pages of the document are requested (relevant oly for Olive.Controls.ThumbList.ThumbListKind.DocPages kind)
	    this.DeclareField("m_sPageCriterion", null);	    
		
		this.DeclareMethod("displayDocumentThumbnails", OwcThumbList_displayDocumentThumbnails);
		this.DeclareMethod("displayQuickNavigationLinks", OwcThumbList_displayQuickNavigationLinks);
		this.DeclareMethod("displayPublicationThumbnails", OwcThumbList_displayPublicationThumbnails);
		
		this.DeclareMethod("createContentItem", OwcThumbList_createContentItem);
		
		// Properties
		this.DeclareField("m_bCreateCoverBack", false); // whether create cover and back page for quick navigation links
		this.DeclareMethod("get_CreateCoverBack", OwcThumbList_get_CreateCoverBack);
		this.DeclareMethod("set_CreateCoverBack", OwcThumbList_set_CreateCoverBack);
		
		// Internal members and methods, all are relevant only for ThumbList of publication type
		this.DeclareField("m_arrAnchors", null);	// anchors for already done requests
		this.DeclareField("m_nPagesCount", null);	// keeps how many pages in pagination is accessible	
		
		this.DeclareMethod("_assignAnchors", OwcThumbList_assignAnchors);
		this.DeclareMethod("_buildFromDate", OwcThumbList_buildFromDate);
		this.DeclareMethod("_buildToDate", OwcThumbList_buildToDate);

        //Used only if there are no internal thumbnail controls in the Layout.xml template, e.g. TemplateId=="ThumbListNoInternalControls"
		this.DeclareField("m_bUseMagnifier", false);    // change to true if you are not using IImageViewer and you have a magnifier
		this.DeclareField("m_oMagnifier", null);    // the magnifier control on the page
		this.DeclareMethod("onThumbnailMouseOver", OwcThumbList_onThumbnailMouseOver);
		this.DeclareMethod("onImageLoad", OwcThumbList_onImageLoad);
	} // Olive.Controls.ThumbList.DeclareType()

	function OwcThumbList_parseControlHtmlAttr()
	{
		// Call base class
		if (this.BaseClassMethod)
			this.BaseClassMethod.apply(this, arguments);

		// Read contrl-specific HTML attributes
		this.m_sKind = DHTML.getAttr(this.HtmlElement, Olive.Controls.attributes.ThumbListKind, this.m_sKind);
		this.m_sTopicName = DHTML.getAttr(this.HtmlElement, Olive.Controls.attributes.ThumbListTopicName, this.m_sTopicName);
		this.m_bShowEmptyPage = DHTML.getAttrBoolean(this.HtmlElement, Olive.Controls.attributes.ThumbListShowEmptyPage, this.m_bShowEmptyPage);				
		this.m_sPageCriterion = DHTML.getAttr(this.HtmlElement, Olive.Controls.attributes.ThumbListPageCriterion);
		
		this.set_CreateCoverBack(DHTML.getAttrBoolean(this.HtmlElement, Olive.Controls.attributes.ThumbListCreateCoverBack, this.m_bCreateCoverBack));		
		this.m_bUseMagnifier = DHTML.getAttrBoolean(this.HtmlElement, Olive.Controls.attributes.ThumbListUseMagnifier, this.m_bUseMagnifier);
	}

	function OwcThumbList_initControl()
	{
		if (this.BaseClassMethod)
			this.BaseClassMethod.apply(this, arguments);
		
		if( this.m_bUseMagnifier )
		{
		    var oPage = this.getOwnerPage();
		    if (oPage)
    		    this.m_oMagnifier = oPage.findControlByType(Olive.Controls.controlTypeNames.Magnifier);
		}
	}

	// add control-specific parameters
	function OwcThumbList_contentBuildControlParams(oParams)
	{
		// if this.m_sTopicName is defined (request for quick-navigation) and 
		// if this.m_sTemplateId was not defined, 
		// set this.m_sTemplateId to a default template for thumblist of this kind
		var oContent = this.getContentItem();
		var bIsQuickNavigationKind = (this.m_sKind.toLowerCase() == Olive.Controls.ThumbList.ThumbListKind.QuickNavigation);
		var bIsTopicNameDefined = (this.m_sTopicName != OwcThumbList_AllQuickNavigation);//??
		if ((bIsQuickNavigationKind || bIsTopicNameDefined) && !this.m_sTemplateId)
		    this.m_sTemplateId = Olive.Controls.ThumbList.Templates.ThumbListFeaturedPages;
		
		if (this.BaseClassMethod)
			this.BaseClassMethod.apply(this, arguments);
		if (this.m_sKind.toLowerCase() != Olive.Controls.ThumbList.ThumbListKind.Auto)
			oParams.addParam(OwcUrlParam_ThumbListKind, this.m_sKind, true);
		if (this.m_sTopicName && (this.m_sTopicName.toLowerCase() != OwcThumbList_AllQuickNavigation))
			oParams.addParam(OwcUrlParam_ThumbListTopicName, this.m_sTopicName, true);
		if (!this.m_bShowEmptyPage)
			oParams.addParam(OwcUrlParam_ThumbListShowEmptyPage, this.m_bShowEmptyPage, true);
	    if (this.m_sPageCriterion)
			oParams.addParam(OwcUrlParam_ThumbListPageCriterion, this.m_sPageCriterion, true);
		if (this.get_CreateCoverBack())
			oParams.addParam(OwcUrlParam_ThumbListCreateCoverBack, this.get_CreateCoverBack(), true);	
	}

	/***************************************************************************
     * Function     : OwcThumbList_navigateToPage()
     * Description  : Used by base class (List) for unified interface for navigation to 
	 *					page/next/prev/first/last
     * Notes        : For ThumbList of only publication type, assign information about anchors,
     *                to support navigation to previous pages.
     * Parameters   : none.
     * Return Value : none
    ***************************************************************************/
	function OwcThumbList_navigateToPage()
	{	
		if (this.m_bUsePagination)
		{
			var oContent = this.getContentItem();
			if (oContent && oContent.DataObjectType == OwcContentItem_PublicationThumbList && oContent.m_nPageNo !== null)
			{
				// save anchors array with requested anchors from the previous request
				this._assignAnchors(oContent);
			}
		}				
		this.contentLoad();
	} // OwcThumbList_navigateToPage

	// TODO: for all public API - support vFirstPage, page Id which should be visible in result

	/***************************************************************************
     * Function     : OwcThumbList_displayDocumentThumbnails(), public API
     * Description  : Displays thumbnails list for a single document
     * Notes        : If optional parameters are not defined, all pages of the document will be requested
     * Parameters   : sDocHRef		(required, string) - keeps BaseHRef
     *				: arrPages		(optional, array) - keeps array of page numbers or page labels
     *				: bByPageLabels	(optional, boolean) - keeps flag for the previous parameter,
     *					true - means that in arrPages - page labels, otherwise - page numbers
     * Return Value : none
    ***************************************************************************/
	function OwcThumbList_displayDocumentThumbnails(sDocHRef, arrPages, bByPageLabels)
	{
		// create content item of the corresponding type
		var oContentItem = new Olive.ContentItem.DocumentThumbList();
		
		// sDocHRef is requerid parameter, if it is absent - return
		if (sDocHRef && sDocHRef != "")
			oContentItem.m_sDocHRef = sDocHRef;
		else
			return;
			
		if (arrPages && arrPages.length != 0)
		{
			if (!bByPageLabels)
				oContentItem.m_sPageNumbers = arrPages.join(",");// TO DO: think about delimeters
			else
				oContentItem.m_sPageLabels = arrPages.join(",");
		}
	
		this.m_oContentItem = oContentItem;
		this.contentLoad();
	} // OwcThumbList_displayDocumentThumbnails
	
	/***************************************************************************
     * Function     : OwcThumbList_displayQuickNavigationLinks(), public API
     * Description  : Displays thumbnails list for quick navigation links in a single document
     * Notes        : 
     * Parameters   : sDocHRef	(required, string with BaseHRef)
     *				: sTopicName (optional, string) - keeps name of quick navigation topic name, it should
     *					be written in TOC.xml (in Xmd_toc/Logic_np/qn:QuickNavigation/qn:Topic/@NAME). 
     *					If the parameter is absent, all page links from TOC.xml (from qn:QuickNavigation)
     *					will be broght. 
     *					If there is no part <qn:QuickNavigation ..> in TOC.xml,
     *					control will consider it like it is the default quick 
     *					navigation <qn:Topic NAME="Quick Navigation" ...> and will
     *					bring Cover Page (the 1st page), and Back Cover (the last page). 
     *					If in TOC.xml there is part with <qn:Topic NAME="Quick Navigation" ...>,
     *					but there is no links to Cover Page and Back Page, the contol will bring them as well.
	 *				: arrPageTitles	(optional, array) - keeps array of page titles for quick navigation.
	 *					If the parameter is absent, all page links from TOC.xml (from qn:EntityRef inside of 
	 *					specified qn:Topic with sTopicName) will be broght. 
     * Return Value : none
    ***************************************************************************/
	function OwcThumbList_displayQuickNavigationLinks(sDocHRef, sTopicName, arrPageTitles)
	{
		// create content item of the corresponding type
		var oContentItem = new Olive.ContentItem.DocumentThumbList();
		
		// sDocHRef is requerid parameter, if it is absent - return
		if (sDocHRef && sDocHRef != "")
			oContentItem.m_sDocHRef = sDocHRef;
		else
			return;
			
		if (sTopicName)
		{
			this.m_sTopicName = sTopicName;
			// consider page titles only if there is topic name
			if (arrPageTitles && arrPageTitles.length != 0)
				oContentItem.m_sPageTitles = arrPageTitles.join(",");// TO DO: think about delimeters
		}
		else
			this.m_sTopicName = OwcThumbList_AllQuickNavigation;
		
		this.m_oContentItem = oContentItem;
		this.contentLoad();
	} // OwcThumbList_displayQuickNavigationLinks
	
	/***************************************************************************
     * Function     : OwcThumbList_displayPublicationThumbnails(), public API
     * Description  : Displays thumbnails list for publication
     * Notes        : Creates content item of the PublicationThumbList type,
     *                fills it with data from parameters and calls contentLoad().
     * Parameters   : sPublication	(required, string) - keeps publication name
     *				: sFromDate		(optional, string of the format yyyy/mm/dd) - keeps start date 
     *				: sToDate		(optional, string of the format yyyy/mm/dd) - keeps end date
     *				: nDayOfWeek	(optional, number) - keeps a number from range 0-7. 
	 *						0 means all days of the week, 1 - Sunday, 2 - Monday, etc. 
	 *				: sSection		(optional, string) - keep section name
	 *				: sPageCriterion(optional, string) - 
     *						possible values: 
     *							"cover:1" - request for cover pages;
     *							"label:A1" - request for pages with page label A1;
     *							"page:3" - request for pages with page number 3.
     * Return Value : none
    ***************************************************************************/
	function OwcThumbList_displayPublicationThumbnails(sPublication, sFromDate, sToDate, nDayOfWeek, sSection, sPageCriterion)
	{
		// create content item of the corresponding type
		var oContentItem = new Olive.ContentItem.PublicationThumbList();
		
		// sPublication is requerid parameter, if it is absent - return
		if (sPublication && sPublication != "") 
			oContentItem.m_sPublication = sPublication;
		else
			return;
			
		if (sFromDate) 
			oContentItem.m_sFromDate = sFromDate;
		if (sToDate) 
			oContentItem.m_sToDate = sToDate;
		if (nDayOfWeek) 
			oContentItem.m_nDayOfWeek = nDayOfWeek;
		if (sSection) 
			oContentItem.m_sSection = sSection;
		if (sPageCriterion) 
			oContentItem.m_sPageCriterion = sPageCriterion;
		
		this.m_oContentItem = oContentItem;
		this.contentLoad();
		
		// clear internal fields, because request for a new publication was done
		this.m_arrAnchors = null;
		this.m_nPagesCount = null;
	} // OwcThumbList_displayPublicationThumbnails
	
	function OwcThumbList_get_CreateCoverBack()
	{
	    /// <summary>
		///		Get method for the property m_bCreateCoverBack.
		/// </summary>
		/// <returns type="Boolean">
		///     Returns the value of the property
		/// </returns>
		/// <remarks>
		///		Property m_bCreateCoverBack is a boolean property that defines 
		///     whether to create page links for Cover (first) and Back(last) pages.
		/// </remarks>
	    return this.m_bCreateCoverBack;
	}
	
	function OwcThumbList_set_CreateCoverBack(value)
	{
	    /// <summary>
		///		Set method for the property m_bCreateCoverBack.
		/// </summary>
		/// <param name="value" type="Boolean">
	    ///		Reference to the prototype of the class.
	    /// </param>
	    this.m_bCreateCoverBack = value;
	}
	
	//Content item will be created only(!) for publication thumbnail list.
	//If content item for document thumbnail list is required, this function should be extended.
	function OwcThumbList_createContentItem(oContentItem, sValue, sDay, nDayOfWeek, sSection, sPageCriterion)
	{
	    if (!oContentItem )
	        return null;
	    	    
	    // Support for scanning by all publications.
	    var sPublication = oContentItem.getPublication();
	    if (sPublication=="")
	    {
	        sPublication="*";
	    }

	    var sMonth = null;
	    var sFromYear = null;
	    var sToYear = null;

  		//check type of content Item if it is year or month copy the sValue to appropriate variable
		switch(oContentItem.getDataType())
		{
		 case OwcContentItem_MonthRange: sMonth = sValue;   
		    break;
		 case OwcContentItem_YearRange: sFromYear = sValue; 
		          sToYear = sValue;  
		    break;
		}

	    if (oContentItem.getFromYear)
	        sFromYear = oContentItem.getFromYear();
	    if (oContentItem.getToYear)
	        sToYear = oContentItem.getToYear();
	    
	    // Create content item of the corresponding type
		this.m_oContentItem = new Olive.ContentItem.PublicationThumbList();
		
		this.m_oContentItem.m_sPublication = sPublication;
					
		this.m_oContentItem.m_sFromDate = this._buildFromDate(sFromYear, sMonth, sDay);
		this.m_oContentItem.m_sToDate = this._buildToDate(sToYear, sMonth, sDay);
		if (nDayOfWeek) 
			this.m_oContentItem.m_nDayOfWeek = nDayOfWeek;
		if (sSection) 
			this.m_oContentItem.m_sSection = sSection;
		if (sPageCriterion) 
			this.m_oContentItem.m_sPageCriterion = sPageCriterion;
		
		// Clear pagination.
		if (this.m_bUsePagination)
			this.m_nPageNo = 1;
		
		// Clear internal fields, because request for a new publication was done.
		this.m_arrAnchors = null;
		this.m_nPagesCount = null;
		
		return this.m_oContentItem;
	}
	
	function OwcThumbList_buildToDate(sYear, sMonth, sDay)
	{
        var sDate = "";
	    var nYear = parseInt(sYear, 10);
	    var nMonth = parseInt(sMonth, 10);
	    
	    if (!sYear || isNaN(nYear))
	    {
	        if (sMonth && !isNaN(nMonth))
	        {
	            // Passing "*" as a year value means that we want to ignore the year filtering the results by month value only.
	            sYear = "*";
	        }
	        else
	        {
	            sYear = Olive.Controls.ThumbList.DateDefinitions.ToYear;
	            nYear = parseInt(sYear, 10);
	        }
	    }
	    if (!sMonth || isNaN(nMonth))
	    {
	        sMonth = Olive.Controls.ThumbList.DateDefinitions.ToMonth;
	        nMonth = parseInt(sMonth, 10);
	    }
	    if (!sDay || sDay == "undefined")
	    {
	        if (!sYear || isNaN(nYear))
	        {
	            // In case the year was not specified, then the day value will be 31. It's up to the binaries to use the correct
	            // day value in case it will be invalid for some year-month cobination.
	            sDay = 31;
	        }
	        else
	        {
                var nDay = 28;
                
                //get last day of month, possible values: 28, 29, 30, 31 (for gregorian calendar)
                var nZeroBasedMonth = nMonth - 1;
                var oDate = new Date(nYear, nZeroBasedMonth, nDay);
	            var nNewMonth = oDate.getMonth();
    	        
	            while (nZeroBasedMonth == nNewMonth)
	            {
	                nDay += 1;
	                oDate.setDate(nDay);
	                nNewMonth = oDate.getMonth();
	            }
	            sDay = (nDay-1).toString();
            }
	    }
	        
	    sDate = sYear + "/";
	    sDate += sMonth + "/";
	    sDate += sDay;
	    return sDate;
	}
	
	function OwcThumbList_buildFromDate(sYear, sMonth, sDay)
	{
	    var sDate = "";
	    var nYear = parseInt(sYear, 10);
	    var nMonth = parseInt(sMonth, 10);
	    
	    if (!sYear || isNaN(nYear))
	    {
	        if (sMonth && !isNaN(nMonth))
	        {
	            // Passing "*" as a year value means that we want to ignore the year filtering the results by month value only.
	            sYear = "*";
	        }
	        else
	        {
	            sYear = Olive.Controls.ThumbList.DateDefinitions.FromYear;
            }
	    }
	    if (!sMonth || isNaN(nMonth))
	    {
	        sMonth = Olive.Controls.ThumbList.DateDefinitions.FromMonth;
	    }
	    
	    if (!sDay)
	        sDay = Olive.Controls.ThumbList.DateDefinitions.FromDay;
	    
	    sDate = sYear + "/";
	    sDate += sMonth + "/";
	    sDate += sDay;
	    return sDate;
	}
	
	/***************************************************************************
     * Function     : OwcThumbList_assignAnchors(), internal method, only for publication type
     * Description  : Keeps inforamtion (anchors, pages count) for pagination
     * Notes        : Keeps anchors for requests in internal field m_arrAnchors, 
     *                in m_arrAnchors[i] - anchor for requests for (i+1)-th page in pagination.
     *                Anchors has format BaseHRef/PageNo (for ex. Sample/1600/01/01/5).
     *                Keeps how many pages we already have for navigation.
     * Parameters   : oContent	(required) - content item object
     * Return Value : none
    ***************************************************************************/
	function OwcThumbList_assignAnchors(oContent)
	{
		// if there is no m_sNextAnchorPage, we are at the last page
		if (!oContent.m_sNextAnchorPage)
			return;
			
		if (!this.m_arrAnchors)
		{
			this.m_arrAnchors = new Array(""); // anchor to the first page is an empty string
			this.m_nPagesCount = 1;
		}
		
		if (!isNaN(this.m_nPageNo) && this.m_nPagesCount == oContent.m_nPageNo && this.m_nPagesCount < this.m_nPageNo)
		{
			// the first time content item is at the current page
			// => there is no anchors in m_arrAnchor for the page oContent.m_nPageNo,
			// save the next anchor in the internal array
			this.m_arrAnchors.push(oContent.m_sNextAnchorPage);
			this.m_nPagesCount++;			
		}
		
		// this.m_nPageNo - is requested list's page number in pagination
		if (this.m_nPagesCount != this.m_nPageNo - 1)
		{
			// set next anchor page in content for navigation request
			oContent.m_sNextAnchorPage = this.m_arrAnchors[this.m_nPageNo-1];

			// send m_nPagesCount number to the server, only if some previous page
			// was requested (this.m_nPagesCount > this.m_nPageNo)
			oContent.m_nPagesCount = (this.m_nPagesCount > this.m_nPageNo) ? this.m_nPagesCount : null;
		}
	} // OwcThumbList_assignAnchors
	
    // when mouse is over thumbnail: show magnifier
	function OwcThumbList_onThumbnailMouseOver(oHtmlElem, oEvent)
	{
	    if ( this.m_bUseMagnifier && this.m_oMagnifier )
        {
            this.m_oMagnifier.onShow(oEvent, true);
        }
	} //OwcThumbList_onThumbnailMouseOver
	
	// when image is loaded in thumbnail: use CSS filter (for img in IE) or canvas (for FireFox)
	function OwcThumbList_onImageLoad(oImage)
	{
	    var oThumbDiv = oImage.parentNode;     //get the DIV container 
	    var nImageOriginalWidth = 0;
	    var nImageOriginalHeight = 0;
	    
        //Get thumbnail DIV size MUST BE WRITTEN IN HTML ATTRIBUTES width/height (and not in CSS)!
  	    var sDivWidth = DHTML.getAttr( oThumbDiv, "width" );
  	    var sDivHeight = DHTML.getAttr( oThumbDiv, "height" );
        var nThumbDivWidth = parseIntFromCssSize(sDivWidth);      
        var nThumbDivHeight = parseIntFromCssSize(sDivHeight);                  
                                                           
	    if( isVersionOf_IE_AtLeast5_5() ) 
        {                                                                  
            oImage.style.display = "block";

            if( !oImage.width || !oImage.height )  //if display:none, then we need to get the image in JS
            {      
                var oImageSave = new Image();      //In order to save the src url
                oImageSave.src = oImage.src;
                         
                //Get img real size
                nImageOriginalWidth = oImageSave.width;               
	            nImageOriginalHeight = oImageSave.height;             
            }    
            else
            {       
                //Get img real size
                nImageOriginalWidth = oImage.width;
    	        nImageOriginalHeight = oImage.height;
            }        
      	    
            if( nThumbDivWidth > 0 && nThumbDivHeight > 0 )
            {
	            //set IMG with CSS Filter    
                var nZoom = Math.min( nThumbDivWidth /  nImageOriginalWidth , nThumbDivHeight / nImageOriginalHeight );         
                oImage.style.filter = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11='" + String(nZoom) + "', M22='" + String(nZoom) + "')";		                         
            }
        }  
        else if( isMozilla() )   
        {
            //get IMG and is real size
            var oImagesOriginal = getChildObjsByTag(oThumbDiv,"img");
            if( oImagesOriginal && oImagesOriginal.length > 0 )
            {
                var oImageOriginal = oImagesOriginal[0];
            }
            
            //use canvas
            var oCanvases = getChildObjsByTag(oThumbDiv,"canvas");   //get Div canvas child 
            if( oCanvases && oCanvases.length > 0 )
            {
                var oCanvas = oCanvases[0];
            }

            if( oImageOriginal && oCanvas )
            {
                nImageOriginalHeight = oImageOriginal.height;
                nImageOriginalWidth = oImageOriginal.width;
  
                //set canvas size and show it
                var context = oCanvas.getContext("2d");
                           
                if( nThumbDivWidth > 0 && nThumbDivHeight > 0 )
                {
                    oCanvas.width = nThumbDivWidth;
                    oCanvas.height = nThumbDivHeight;
                    oCanvas.style.display = "block";
                
                    var nCanvasHeight = oCanvas.height; 
                    var nCanvasWidth = oCanvas.width;   
                       
                    //find the best resize and draw it in canvas
                    var minHeight = Math.min( nCanvasHeight , nImageOriginalHeight );
                    var minWidth =  Math.min( nCanvasWidth , nImageOriginalWidth );
                    context.scale( 1.0 , 1.0 );
                    context.drawImage( oImageOriginal , 0 , 0 , minWidth , minHeight );     
                }
            }                     
        }              
        else //if( isSafari() )
        {          
            //get IMG and set is size
            oImage.style.display = "block";
            oImage.width = nThumbDivWidth;      
            oImage.height = nThumbDivHeight;    
        }    
             
	} // OwcThumbList_onImageLoad

	// Register Olive.Controls.ThumbList class
	JScript.Type.RegisterClass("Olive.Controls.ThumbList", Olive.Controls.ThumbList, Olive.Controls.List, [Olive.Controls.IDataBound]);
	Olive.Controls.RegisterControlType(Olive.Controls.controlTypeNames.ThumbList, Olive.Controls.ThumbList);
} // Define Olive.Controls.ThumbList class

/*=============================================================================
 * Class   : Olive.Controls.ThumbListItem
 * Summary : Control displaying single page in thumnails list control
=============================================================================*/
if (!Olive.Controls.ThumbListItem)
{
	Olive.Controls.ThumbListItem = function OwcThumbListItem()
	{
	} // Olive.Controls.ThumbListItem()

	Olive.Controls.ThumbListItem.DeclareType = function OwcThumbListItem_DeclareType()
	{
		this.DeclareField("m_bDisableContentAutoLoad", true);

		// Overrides
		this.OverrideMethod("bindHtmlElement", OwcThumbListItem_bindHtmlElement);
	}

	Olive.Controls.ThumbListItem.ApplyPrototype = function OwcThumbListItem_ApplyPrototype(rObject, bDynamic)
	{
		// Events
		rObject.registerOwcEventsClass2("thumbListItemMouseOver", "onThumbListItemMouseOver", true);
		rObject.registerOwcEventsClass2("thumbListItemMouseOut", "onThumbListItemMouseOut", true);
	}
	
	/***************************************************************************
     * Function     : OwcThumbListItem_bindHtmlElement(), overriden from Olive.Controls.Control class
     * Description  : Binds HTML element, attach DHTML events 
	 * Parameters   : oHtmlElement - HTML element.
     * Return Value : none
    ***************************************************************************/	
	function OwcThumbListItem_bindHtmlElement(oHtmlElement)
	{
		if (this.BaseClassMethod)
			this.BaseClassMethod.apply(this, arguments);

		DHTML.attachEvent(oHtmlElement, "mouseover", OwcThumbListItem_DHTML_onMouseOver);
		DHTML.attachEvent(oHtmlElement, "mouseout", OwcThumbListItem_DHTML_onMouseOut);
	} // OwcThumbListItem_bindHtmlElement
	
	/***************************************************************************
     * Function     : OwcThumbListItem_DHTML_onMouseOver()
     * Description  : global function, onMouseOver event handler on Html element of thumbnails list item
     *                 Fires thumbListItemMouseOver.
     * Parameters   : oEvent - Html event object.
     * Return Value : none
    ***************************************************************************/
	function OwcThumbListItem_DHTML_onMouseOver(oEvent)
	{
        var oControl = OwcGetControlFromHtmlElem(this);
		if (!oControl)
			return;
			
        // Fire "thumbListItemMouseOver" event
		var oEvent = oControl.createOwcEventObject("thumbListItemMouseOver");
		oControl.fireOwcEvent(oEvent);
		Object_Destroy(oEvent);	
	} // OwcThumbListItem_DHTML_onMouseOver()

    /***************************************************************************
     * Function     : OwcThumbListItem_DHTML_onMouseOut()
     * Description  : global function, onMouseOut event handler on Html element of thumbnails list item
     *                Fires thumbListItemMouseOut.
     * Parameters   : oEvent - Html event object.
     * Return Value : none
    ***************************************************************************/
	function OwcThumbListItem_DHTML_onMouseOut(oEvent)
	{
		var oControl = OwcGetControlFromHtmlElem(this);
		if (!oControl)
			return;
			
        // Fire "thumbListItemMouseOut" event
		var oEvent = oControl.createOwcEventObject("thumbListItemMouseOut");
		oControl.fireOwcEvent(oEvent);
		Object_Destroy(oEvent);		
	} // OwcThumbListItem_DHTML_onMouseOut

	// Register Olive.Controls.ThumbListItem class
	JScript.Type.RegisterClass("Olive.Controls.ThumbListItem", Olive.Controls.ThumbListItem, Olive.Controls.ListItem, [Olive.Controls.IDataBound]);
} // Define Olive.Controls.PageListItem