﻿var actionLink = "";

function initialiseGrid(
    name,
    addRows,
    columnSorted)
{
    $(function() {
        var grid = $("#" + name);
        window[name] = grid;
        setupGrid(
            grid,
            columnSorted);
        addRows(grid);
        initialiseSort(grid, name);
    });
}

function setDefaultColumnHeaderLabels(grid)
{
    grid.setLabel('indicatorName','','',{'title':'Click to sort by Indicator ascending'});
    grid.setLabel('latestValue','','',{'title':'Click to sort by Latest Value ascending'});
    grid.setLabel('financialopportunity', '', '', { 'title': 'Click to sort by Financial Opportunity ascending' });
    grid.setLabel('volumeOpportunity','','',{'title':'Click to sort by Volume Opportunity ascending'});
    grid.setLabel('rank', '', '', { 'title': 'Click to sort by Rank ascending' });
    grid.setLabel('category', '', '', { 'title': 'Click to sort by Category' });
}

function setupGrid(
    grid,
    columnSorted)
{
    grid.jqGrid({
        autowidth: true,
        colModel: [
          { name: 'indicatorID', index: 'indicatorID', hidden: true },
          { name: 'indicatorName', index: 'indicatorName', label: 'Indicator', width: 190, resizable: false, sorttype: 'text', formatter: indicatorLinkFormatter },
          { name: 'latestValue', index: 'latestValue', label: 'Latest Value', width: 300, align: 'center', resizable: false, sorttype: 'float' },
          { name: 'financialOpportunity', index: 'financialOpportunity', label: 'Financial Opportunity', width: 155, align: 'center', resizable: false, sorttype: 'float', formatter: opportunityFormatter },
          { name: 'volumeOpportunity', index: 'volumeOpportunity', label: 'Volume Opportunity', width: 255, align: 'center', resizable: false, sorttype: 'float' },
          { name: 'rank', index: 'rank', label: 'Rank', width: 60, align: 'center', resizable: false, sorttype: 'int' },
          { name: 'category', index: 'category', label: 'Category', width: 100, align: 'center', resizable: false, sorttype: 'text' }
        ],
        datatype: 'clientSide',
        gridview: true,
        height: 'auto',
        onSortCol: columnSorted
    });

    setDefaultColumnHeaderLabels(grid);
}

function indicatorLinkFormatter(cellvalue, options, rowObject)
{
    return actionLink
        .replace(/__INDICATOR_NAME_PLACEHOLDER__/g, cellvalue)
        .replace(/__INDICATOR_ID_PLACEHOLDER__/g, rowObject.indicatorID);
}

function initialiseSort(grid, cookiePrefix)
{
    var defaultSortField = "category";
    var defaultSortDirection = "asc";

    var sortField = $.cookie(cookiePrefix + "-dashboard-list-sort-field") || defaultSortField;
    var sortOrder = $.cookie(cookiePrefix + "-dashboard-list-sort-order") || defaultSortDirection;

    // Sorts ascending by default.
    grid.sortGrid(sortField, false);

    // Calling this method again sorts descending
    // (as if the user has clicked the col heading twice)
    if (sortOrder == 'desc')
        grid.sortGrid(sortField, false);
}
