﻿/// <reference name="Telerik.Web.UI.Common.Core.js" />
/// <reference name="Telerik.Web.UI.Common.jQuery.js" />
/// <reference name="Telerik.Web.UI.Common.jQueryInclude.js" />

/**************************************************************************

    This file should only contain generic functions that can be used by
    multiple object instances.  Do not put control specific functions
    in this file.

    The reference tags at the top are included to help provide javascript
    intellisense support to VS2008.
    
    Any variables added should be included before any of the functions.

    If you need to get a handle to the BaseRadAjaxManager, call the
    GetBaseAjaxManager() function that exists in the Default.Master or
    GetDialogAjaxManager() function for pages using the Dialog.Master.

    Contents:
     - All variables
     - RowCreated()
     - AutoFitGridHeight()
     - SetClientDropDownTextEmpty()
     - SetDropDownTextOnClientItemsRequesting()
     - IsScrolledToBottom()
     - HandleGridScrolling()
     - ScrollGridTop()
     - RebindGridByClientId()
     - ExpandOnSingleClick()
    
**************************************************************************/



function RowCreated() {
    /// <summary>
    /// To enable the ability to reference the client object of a grid row inside a client
    /// event handler of RadGrid, this RowCreated function in the ClientSettings
    /// ClientEvents-OnRowCreated property, otherwise eventArgs.get_gridDataItem() will 
    /// return null.
    ///</summary>
}

function AutoFitGridHeight(sender, args) 
{
    /// <summary>
    /// This function should be assigned to the ClientEvents OnGridCreated property
    /// to enable automatic resizing the scrollArea height
    /// of a RadGrid based on on the number of rows.  If the grid
    /// contains no data, the scrollArea height will be set to 70px.
    /// <summary>
    /// <param name="sender">The RadGrid to be resized</param>
    /// <param name="args"></param>
    var grid = sender;
    var scrollArea = sender.GridDataDiv;
    var headerArea = sender.GridHeaderDiv;
    var mtv = grid.get_masterTableView();
    var dataItems = mtv.get_dataItems();
    var newHeight = "70px";
    
    if (dataItems.length > 0) 
    {
        newHeight = ( mtv.get_element().clientHeight + headerArea.clientHeight ) + 'px';
    }
    
    scrollArea.style.height = newHeight;
}

function SetClientDropDownTextEmpty(sender, args) {
    /// <summary>
    /// Sets the text value of the sending RadComboBox to an empty string.
    /// Assign to the RadComboBox OnClientDropDownOpening property.
    /// </summary>
    sender.set_text("");
}

function SetDropDownTextOnClientItemsRequesting(sender, args) {
    /// <summary>
    /// Sets the CustomText property to an empty string when appending items
    /// or to the Text property when not.
    /// Assign to the RadComboBox OnClientItemsRequesting property.
    /// </summary>
    if (sender.get_appendItems()) {
        args.get_context().CustomText = "";
    } else {
        args.get_context().CustomText = sender.get_text();
    }
}


function IsScrolledToBottom(scrollArea) {
    /// <summary>
    /// This method calculates whether you have reached the bottom when dragging the vertical grid scroll.
    /// </summary>
    var currentPosition = scrollArea.scrollTop + scrollArea.clientHeight;
    return currentPosition == scrollArea.scrollHeight;
}

function HandleGridScrolling(sender, args, requestKey) {
    /// <summary>
    /// This is used to fire requests to the sending RadGrid when used with 
    /// continuous virutal scrolling.
    /// </summary>
    var gridId = sender.get_id();
    if (null === gridId) {
        return; 
    }
    var grid = $find(gridId);
    if (null === grid) {
        return; 
    }
    var scrollArea = document.getElementById(gridId + '_GridData');
    var masterTableView = grid.get_masterTableView();
    if (null === masterTableView) {
        return;
    }
    var manager = GetBaseAjaxManager();
    if (null === manager) {
        return;
    }
    if (IsScrolledToBottom(scrollArea)) {
        manager.ajaxRequest(requestKey);    //"LoadMoreRecords"
    }
}

function ScrollGridTop(sender, args) {
    /// <summary>
    /// Scrolls to the first row in the sending RadGrid
    /// </summary>
    var gridId = sender.get_id();
    if (null === gridId) {
        return;
    }
    var grid = $find(gridId);
    if (null === grid) {
        return;
    }
    var scrollArea = document.getElementById(gridId + '_GridData');
    var masterTableView = grid.get_masterTableView();
    var topRow = masterTableView.get_dataItems()[0];
    if (null === topRow) {
        return;
    }
    setTimeout(function() {
        var topRowElement = topRow.get_element();
        scrollArea.scrollTop = topRowElement.offsetTop;
    }, 200);
}

function RebindGridByClientId(gridClientId) {
    /// <summary>
    /// Causes the RadGrid to rebind based on the RadGrid.ClientID
    /// value passed in.
    /// </summary>
    var grid = $find(gridClientId);
    var mainTableView = $find(gridClientId).get_masterTableView();
    mainTableView.rebind();
    AutoFitGridHeight(grid, null);
}


// RadTreeView
function ExpandOnSingleClick(sender, args) {
    var node = args.get_node();
    if (node.get_level() > 0) {
        node.toggle();
    }

}