﻿function ui2_sortDataPagerColumn_jquery(elementWrapper, columnNameInputFieldID, sortDirectionInputFieldID, columnName, sortDirection)
{
    $('#'+columnNameInputFieldID).val(columnName);
    $('#'+sortDirectionInputFieldID).val(sortDirection);

    ui2_submitForm_jquery(elementWrapper);
}

function ui2_redirectOnClick_jquery(elementWrapper, url)
{
    window.location.href = url;
}

function ui2_expandColumnOnClick_jquery(wrappedElement, maxColumnSize, dataPagerTag) {

    var lastcolumn;
    columncount = $('th').length;   //number of column's (headers) that the DataPager is displaying
    expandedcolumn = maxColumnSize;            //%age width of the column which will be expanded
    smallcolumn = (100 - expandedcolumn) / (columncount - 1);   //calculated %age width for the remaining columns
    var widths = new Array();       //Array to store the default widths of the DataPager's column's for resetting to normal
    
    //First we need to collect the default width of each column so that we can reset columns to normal when collapsing the expanded column
    i = 0;
    $('th').each(function() {
        widths[i] = $(this).css("width");
        i++;
    });
    
    //whenever the 'Expand Column' button is clicked
    $(wrappedElement).click(function(event) {
        //If the currently clicked Expand Column is for an already expanded column we need to reset all the columns to default widths
        if ((lastcolumn != null) && ($(lastcolumn).attr("id") == $(parentcolumn).attr("id"))) {     //Double check this - Is it doing what i think it is??
            x = 0;
            $('th').each(function() {
                $(this).width(widths[x]);
                x++
            });
            lastcolumn = null;
            $(this).toggleClass("collapse_button");
            $('td.disable_wrapping').toggleClass("disable_wrapping");
        }
        //If the currently clicked Expand Column button is for a non expanded column we need to expand the column while setting the other columns
        //to the pre-calculated "smallcolumn" width, along with resetting any buttons currently displaying the "collapse column" image.
        else {
            parentcolumn = $(wrappedElement).parent().parent().parent();
            $('th').css("width", smallcolumn + "%");
            $(parentcolumn).css("width", expandedcolumn + "%");
            
            //Do some CSS class toggling for white-space wrapping and button images.
            var columnClass = dataPagerTag + "_dp_col_" + (parseInt($(parentcolumn).attr("id").substr($(parentcolumn).attr("id").length - 1, $(parentcolumn).attr("id").length)) + 1);
            $('td:not(.disable_wrapping)').toggleClass("disable_wrapping");
            $('td.' + columnClass).toggleClass("disable_wrapping");
            $('.collapse_button').toggleClass("collapse_button");
            $(this).toggleClass("collapse_button");

            lastcolumn = parentcolumn;
        }
        //Stop the click event propagating up the DOM so that if the column is sortable the click event isnt fired on the <th> that the 
        //expand button is contained in.
        event.stopPropagation();
    });
}
