// JScript File
function GymTableCollection( pageSizeIn, collectionNameIn )
{
    this._gymCollection = new HashTable();
    this._pageSize = pageSizeIn;
    this._pageNo = 0;
    this._collectionName = collectionNameIn;    /* name of the variable that holds the collection */
    this._markBlue = '<div class="markblue"></div>';
    this._crossBlue = '<div class="crossblue"></div>';
    this._markWhite = '<div class="markwhite"></div>';
    this._crossWhite = '<div class="crosswhite"></div>';
}

GymTableCollection.prototype = {
    RenderTableToContainer : function( containerToRenderIn, containerToRenderPagesIn, pageNoToRenderIn )
    {
        this._pageNo = pageNoToRenderIn;
        //  render the items on _pageNo, items are from index _pageNo * _pageSize to ((_pageNo + 1) * _pageSize) - 1
        
        var startIndex = this._pageNo * this._pageSize;
        var lastPage = false;
        var endIndex = 
            ((this._pageNo + 1) * this._pageSize) > this._gymCollection.length 
                ? this._gymCollection.length : ((this._pageNo + 1) * this._pageSize);
            
        if( endIndex == this._gymCollection.length )
        {
            lastPage = true;
        }
        
        //  iterate from startIndex to endIndex and render the table into the container
        var containerToRenderJs = GetElement( containerToRenderIn );
        if( HasValue( containerToRenderJs ) )
        {
            containerToRenderJs.innerHTML = '';
        }
        
        var ctIndex = startIndex;
        var rowString = '';
        for( ; ctIndex < endIndex; ctIndex++ )
        {
            //  render each Item
             rowString += this.RenderItem( ctIndex );
            
        }
        AssignTextToBlock( containerToRenderIn, rowString );
        
        var totalPages = Math.ceil( this._gymCollection.length / this._pageSize );
        if( totalPages > 1 )
        {
            this.RenderPages( containerToRenderIn, containerToRenderPagesIn, pageNoToRenderIn, totalPages );
        }
        else
        {
            AssignTextToBlock( containerToRenderPagesIn, '');
        }
        
    },
    
    RenderPages : function( containerToRenderIn, containerToRenderPagesIn, userPageIn, totalPages )
    {
        //  iterate from startIndex to endIndex and render the table into the container
        var containerToRenderPagesJs = GetElement( containerToRenderPagesIn );
        if( ! HasValue( containerToRenderPagesJs ) || !HasValue( GetElement ( containerToRenderIn ) ) )
        {
            return;
        }
        
        AssignTextToBlock( containerToRenderPagesIn, '' );
        var previousLink = '<div class="clsGymsFound_position"><< Previous</div>';
        var nextLink = '<div class="clsGymsFound_position"> Next>></div>';
        var middleLink = '';
        
        // at any time the page counts are from _pageNo - (_pageNo % 4) to (_pageNo - (_pageNo % 4) + 4) - 1
        var startPageNo = (this._pageNo - ( this._pageNo % 4 ));
    
        if( startPageNo > 0 ) 
        {           
            //  render the previous link
            previousLink = this.WrapperDivLink(
'<a href="#" onclick="' + this._collectionName + '.RenderTableToContainer(\'' + containerToRenderIn + '\',\'' + containerToRenderPagesIn + '\',' + (startPageNo - 4) 
                        + '); return false;"><< Previous</a>'
                                     );
        } 
         
        
        var endPage = (startPageNo + 4) > totalPages ? totalPages : (startPageNo + 4);
        var ctPage = startPageNo;
        //alert( 'Total Pages=' + totalPages + 'pageNo=' + this._pageNo + 'endPage=' + endPage + 'ctPage=' + ctPage );

        for( ctPage;ctPage < endPage; ctPage++ )
        {
            //  render each page link
            if( ctPage < totalPages )
            {
                middleLink += this.RenderThisPage( containerToRenderIn, containerToRenderPagesIn, ctPage, userPageIn );
            }
        }
        
        if( ctPage < totalPages)
        {
            nextLink = this.WrapperDivLink(
'<a href="#" onclick="' + this._collectionName + '.RenderTableToContainer(\'' + containerToRenderIn + '\',\'' + containerToRenderPagesIn + '\',' + endPage 
                        + '); return false;">Next >></a>'
                                      );
        }
        
        
        //alert( middleLink );
        AssignTextToBlock( containerToRenderPagesIn, previousLink + middleLink + nextLink );
    },
    
    RenderThisPage : function( containerToRenderIn, containerToRenderPagesIn, ctPageIn, userPageIn )
    {
        var linkStr = '';
        if( ctPageIn != userPageIn )
        {
            linkStr = this.WrapperDivLink( '<a href="#" onclick="' + this._collectionName + '.RenderTableToContainer(\'' + containerToRenderIn + '\', \'' + containerToRenderPagesIn + '\',' + ctPageIn + '); return false;">' + ( ctPageIn + 1 ) + '</a>' );
        }
        else
        {
            linkStr = '<div class="clsGymsFound_position">' + ( ctPageIn + 1 ) + '</div>';
        }
        
        return linkStr;
    },
    
    /*
        generate a wrapper div for the link
    */
    WrapperDivLink : function( link )
    {
        return '<div class="clsGymsFound_positionlink">' + link + '</div>';
    },

/*     
        <div class="liaddress1">Corpus Fitness Club</div>
        <div class="liaddress2">Silver</div>
        <div class="liaddress3">7 Tolovera Road Sydney 2113 NSW Australia</div>
        <div class="liaddress2"><div class="markblue"></div></div>
        <div class="liaddress2"><div class="markblue"></div></div>
        <div class="liaddress2"><div class="markblue"></div></div>
*/    
    
    RenderItem : function( ctIndexIn )
    {
        var rowString = '';
        
        if( HasValue( this._gymCollection.GetItemAtLocation(ctIndexIn) ) )
        {
            rowString = this.RenderFirstColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
            rowString += this.RenderSecondColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
            rowString += this.RenderThirdColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
            rowString += this.RenderFourthColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
            rowString += this.RenderFifthColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
            rowString += this.RenderSixthColumn( this._gymCollection.GetItemAtLocation(ctIndexIn), ctIndexIn );
        }
        
        return '<div>' + rowString + '</div>';
    },
    
    
   
    RenderFirstColumn : function( ctItem, itemIndex )
    {
        var firstColStartStr = '<a class="liaddress1' + ((itemIndex%2)==0 ? '"':'w"') + ' style="text-decoration:none;" href="gymprofile.aspx?GymID=' + ctItem.GymID + '">';
        var firstColEndStr = '</a>';
        
        var textStr = HasValue( ctItem.GymName ) ? ctItem.GymName : '';
        textStr = textStr.length > 23 ? textStr.substring(0, 22) + '... >>' : textStr + ' >>';  
        var link;
        return firstColStartStr + textStr + firstColEndStr;
    },
    
    RenderSecondColumn : function( ctItem, itemIndex )
    {
        var SecondColStartStr = '<div class="liaddress2' + ((itemIndex%2)==0 ? '">':'w">');
        var SecondColEndStr = '</div>';
        
        var textStr = HasValue( ctItem.GymRating ) ? ctItem.GymRating : '';
        
        return SecondColStartStr + textStr + SecondColEndStr;
    },

    RenderThirdColumn : function( ctItem, itemIndex )
    {
        var ThirdColStartStr = '<div class="liaddress3' + ((itemIndex%2)==0 ? '">':'w">');
        var ThirdColEndStr = '</div>';
        
        var textStr = HasValue( ctItem.Address ) ? ctItem.Address : '';
        
        return ThirdColStartStr + textStr + ThirdColEndStr;
    },
    
    RenderFourthColumn : function( ctItem, itemIndex )
    {
        return this.RenderBoolColumn( ctItem.CV, itemIndex );
    },
    
    RenderFifthColumn : function( ctItem, itemIndex )
    {
        return this.RenderBoolColumn( ctItem.Weights, itemIndex );
    },
    
    RenderSixthColumn : function( ctItem, itemIndex )
    {
        return this.RenderBoolColumnlast( ctItem.Classes, itemIndex );
    },
    
    RenderBoolColumn : function( itemValue, itemIndex )
    {
        var colStartStr = '<div class="liaddress2' + ((itemIndex%2)==0 ? '">':'w">');
        var colEndStr = '</div>';
        var textStr = (itemIndex%2)==0? this._crossBlue:this._crossWhite;
        
        if( HasValue( itemValue ) && itemValue == true )
        {
            textStr = (itemIndex%2)==0? this._markBlue:this._markWhite;
        }
        
        return colStartStr + textStr + colEndStr;
    },    
    
    RenderBoolColumnlast : function( itemValue, itemIndex )
    {
        var colStartStr = '<div class="liaddress4' + ((itemIndex%2)==0 ? '">':'w">');
        var colEndStr = '</div>';
        var textStr = (itemIndex%2)==0? this._crossBlue:this._crossWhite;
        
        if( HasValue( itemValue ) && itemValue == true )
        {
            textStr = (itemIndex%2)==0? this._markBlue:this._markWhite;
        }
        
        return colStartStr + textStr + colEndStr;
    }    
    
}