﻿var _mouseElement;

function FlexDataList(tableId)
{
    var instance = new Object();
    
    instance.table = ESWebForm_GetElementById(tableId);
    instance.tableRows = instance.table ? ESWebForm_GetTableRows(instance.table) : null;
    
    instance.pageIndex = -1;
    instance.pageCount = 0;
    
    instance.pageRowCount = 0;
    instance.pageChangeInterval = 0;
    
    instance.pagingEnabled = false;
    instance.intervalHandler = null;
    
    instance.initialize = FlexDataList_Initialize;
    instance.startPaging = FlexDataList_StartPaging;
    instance.stopPaging = FlexDataList_StopPaging;
    instance.incrementPage = FlexDataList_IncrementPage;
    instance.updatePage = FlexDataList_UpdatePage;
    instance.handleMouseOver = FlexDataList_HandleMouseOver;
    instance.handleMouseOut = FlexDataList_HandleMouseOut;
    
    if (instance.table)
    {
        if (instance.table.addEventListener)
        {
            instance.table.addEventListener('mouseover', FlexDataList_HandleMouseOver, false);
            instance.table.addEventListener('mouseout', FlexDataList_HandleMouseOut, false);
        }
        else if (instance.table.attachEvent)
        {
            instance.table.attachEvent('onmouseover', FlexDataList_HandleMouseOver);
            instance.table.attachEvent('onmouseout', FlexDataList_HandleMouseOut);
        }
    }
    
    return instance;
}

function FlexDataList_Initialize(pageRowCount, pageChangeInterval)
{
    this.pageRowCount = pageRowCount;
    this.pageChangeInterval = pageChangeInterval;
    
    if (this.tableRows && (this.pageRowCount > 0))
    {
        this.pageIndex = 0;
        this.pageCount = Math.ceil(this.tableRows.length / this.pageRowCount);
        this.updatePage();
    }
}

function FlexDataList_StartPaging()
{
    this.pagingEnabled = true;
    
    if ((this.pageRowCount > 0) && (this.pageChangeInterval > 0))
    {
        var instance = this;
        var code = function() { instance.incrementPage(); };
        this.intervalHandler = window.setInterval(code, this.pageChangeInterval);
    }
}

function FlexDataList_StopPaging()
{
    this.pagingEnabled = false;
    
    if (this.intervalHandler)
    {
        window.clearInterval(this.intervalHandler);
        this.intervalHandler = null;
    }
}

function FlexDataList_IncrementPage()
{
    if (_mouseElement && this.table && ((_mouseElement == this.table) || ESWebForm_ElementContains(this.table, _mouseElement)))
        return;
    
    var activeElement = ESWebForm_GetActiveElement();
    
    if ((this.table != null) && (activeElement != null) && 
        ((this.table == activeElement) || ESWebForm_ElementContains(this.table, activeElement)))
        return;
    
    this.pageIndex++;
    
    if (this.pageIndex >= this.pageCount)
        this.pageIndex = 0;
    
    this.updatePage();
}

function FlexDataList_UpdatePage()
{
    if (!this.tableRows)
        return;
    
    for (var i = 0; i < this.tableRows.length; i++)
    {
        var row = this.tableRows[i];
        var rowPageIndex = this.pageRowCount > 0 ? Math.floor(i / this.pageRowCount) : rowPageIndex;
        row.style.display = (rowPageIndex == this.pageIndex) || (this.pageIndex == -1) ? '' : 'none';
    }
}

function FlexDataList_HandleMouseOver(e)
{
    if (!e) e = window.event;
    if (!e) return;
    
    _mouseElement = ESWebForm_GetEventToElement(e);
}

function FlexDataList_HandleMouseOut(e)
{
    if (!e) e = window.event;
    if (!e) return;
    
    _mouseElement = ESWebForm_GetEventToElement(e);
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();