﻿///<reference path="..\..\core\jscript\CliPropConfig6.js" />

//--------------------------------------------------------------------------------
function CliLibrarySectionHeaderRenderer( libraryView )
{
    ///<summary>responsible for rendering a header row on a group of library
    //item rows, and handling clicks etc.</summary>
    
    this._libraryView = libraryView;
    this.SortColumnEventHandler = null; // fn( sortId, sortDir )

    this._txtHdrTitle;
    this._txtHdr2ndTitle;
    this._txtHdrPCExpiry;
    this._txtHdrPrice;
    this._txtHdrSize;
    this._txtHdrStatus;
}

CliLibrarySectionHeaderRenderer.prototype =
{
    //--------------------------------------------------------------------------------
    RenderHeader : function( channel, channelName, sortId, sortOrder )
    {
        ///<param name="channel">channel id this header applies to. required for
        ///updating display when column headers are clicked</param>
        ///<param name="sortId">id of column to sort on</param>
        ///<param name="sortOrder">asc or dsc</param>
        
        this._LookupText( channel );
        
        if( sortId == null )
            sortId = "title";
        if( sortOrder == null )
            sortOrder = "asc";
            
        var headerContainer = createElement( "<div class=\"header-container\" channel-id=\"" + channel + "\" channel-name=\"" + channelName + "\" />");

        // add headers with default sort indicators
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdrTitle, "titleCol", "title", sortOrder, sortId ) );
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdr2ndTitle, "partofCol", "secondary-title", sortOrder, sortId ) );
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdrPCExpiry, "expiryCol", "expiry", sortOrder, sortId ) );
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdrPrice, "priceCol", "price", sortOrder, sortId ) );
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdrSize, "sizeCol", "size", sortOrder, sortId ) );
        headerContainer.appendChild( this._RenderSortableHeader( this._txtHdrStatus, "statusCol", "status", sortOrder, sortId ) );
    
        var div = document.createElement( "<div class=\"clear\" />" );
        headerContainer.appendChild( div );
        
        return headerContainer;
    },

    //--------------------------------------------------------------------------------
    _RenderSortableHeader : function( displayName, className, sortId, sortOrder, activeSortId )
    {
        ///<summary>Renders a column header that can trigger a sort when clicked.</summary>
        ///<param name="displayName">Text to display</param>
        ///<param name="className">Classname to add to column</param>
        ///<param name="sortId">code that identifies what property to sort the data by when
        ///sorting by this column</param>
        ///<param name="sortOrder">['asc' | 'dsc'] to indicate the order of the current sort</param>
        ///<param name="activeSortId">sortId of the column that the data is beinng displayed sorted by</param>
        ///<remarks>
        
        var sortOrderValue = "sort-asc";
        var arrowClass = "arrowDown";
        
        // are we sorting on this column
        if( sortId == activeSortId )
        {
            displayName = displayName;
            sortOrderValue = "sort-" + sortOrder;
            
            if( sortOrder == "asc" )
                arrowClass = "arrowDown";
            else
                arrowClass = "arrowUp";
        }
        
        var headerDiv = createElement( "<a class=\"" + className + " "+ sortOrderValue + " " + arrowClass +"\" sort-id=\"" + sortId + "\" sort-order=\"" + sortOrder + "\" />" );
        headerDiv.href = '#';
        headerDiv.innerText = displayName;
                            
        var self = this;
        CliAttachOnclickEventHandler( headerDiv, function( srcElement ) { self._SortableColumnClicked( srcElement ); }  );
        
        return headerDiv;
    },
                    
    //--------------------------------------------------------------------------------
    _SortableColumnClicked : function( srcElement )
    {
        ///<summary>Called when a column header is clicked</summary>
        
        try
        {
            if( this.SortColumnEventHandler != null )
            {
                var sortId = srcElement.getAttribute( "sort-id" );
                var sortDir = srcElement.getAttribute( "sort-order" );
                
                if( sortDir == "asc" )
                    sortDir = "dsc";
                else
                    sortDir = "asc";
                
                this.SortColumnEventHandler( sortId, sortDir );
            }
        }
        catch( e )
        {
            CliDisplayError( CliErrorAction_Sort, 30010 );
        }
    },
    
    //--------------------------------------------------------------------------------
    _LookupText : function( channel )
    {
        this._txtHdrTitle    = "Title";
        this._txtHdr2ndTitle = "Part of...";
        this._txtHdrPCExpiry = "PC expiry";
        this._txtHdrPrice    = "Price";
        this._txtHdrSize     = "Size";
        this._txtHdrStatus   = "Status";

        try
        {
            this._txtHdrTitle    = pageLookupText( "lib-hdr-title", channel );
            this._txtHdr2ndTitle = pageLookupText( "lib-hdr-2ndtitle", channel );
            this._txtHdrPCExpiry = pageLookupText( "lib-hdr-pcexpir", channel );
            this._txtHdrPrice    = pageLookupText( "lib-hdr-price", channel );
            this._txtHdrSize     = pageLookupText( "lib-hdr-size", channel );
            this._txtHdrStatus   = pageLookupText( "lib-hdr-status", channel );
        }
        catch( e ){}
    }
}//