$(function()
{
    /* Make cards clickable. */
    /*$('#mainContent').on('click', '.project-card', function(e)
    {
        if($(e.target).closest('.project-actions,[data-toggle],.btn,a').length) return;
        window.location.href = $(this).data('url');
    });
    */
    $('.datasource-item.has-active-item').addClass('open');
    $('.datasource-item.has-active-item').parent().parent().addClass('open');
    $('.datasource-item:not(.has-active-item)').removeClass('open');
    $('#datasourceTree > .has-list:not(.' + source.type + '-tree)').removeClass('open');
    $('#datasourceTree').on('expand.zui.tree', function(node)
    {
        var datasourceID = $(node).attr('datasource-id');
        if(datasourceID == undefined) return;

        getDatasourceTables(datasourceID, node);
    });

    $(document).on('click', '#datasourceTree .datasource-item .table-name a', function(e)
    {
        $selector = $(e.target).closest('.table-name');
        var sourceID  = $selector.attr('source-id');
        var tableName = $selector.attr('table-name');

        var link = createLink('datasource', 'view', 'type=' + browseType + '&sourceID=' + sourceID + '&tableName=' + tableName);
        window.location.href = link;

        /*var $dataContent = $('#dataContent');
        var link = createLink('datasource', 'ajaxgetsourceinfo', 'sourceID=' + sourceID + '&tableName=' + tableName);
        $dataContent.load(link, function()
        {
            $('#datasourceTree .datasource-item .table-name').removeClass('active');
            $selector.addClass('active');
        });
        */

    });

});

/**
 * Get Datasource tables.
 *
 * @param  int    $datasourceID
 * @param  object $node
 * @access public
 * @return void
 */
function getDatasourceTables(datasourceID, node)
{
    $.post(createLink('datasource', 'ajaxGetTablesOrViews', 'datasourceID=' + datasourceID + '&type=' + browseType), function(resp)
    {
        resp = JSON.parse(resp);
        if(resp.result == 'success')
        {
            $(node).find('ul').empty();
            $(node).find('ul').append(resp.tree);
        }
        else
        {
            bootbox.alert(resp.message);
        }
    });
}

function getDatasource(datasourceID, node)
{
    var link = createLink('project', 'ajaxGetNotes', 'analysisID=' + analysisID);
    $selector.load(link, function()
    {
        resizeAnalysisReport(tabID);
        initExpand();

    });

    $.post(createLink('datasource', 'ajaxGetTablesOrViews', 'datasourceID=' + datasourceID + '&type=' + browseType), function(resp)
    {
        resp = JSON.parse(resp);
        if(resp.result == 'success')
        {
            $(node).find('ul').empty();
            $(node).find('ul').append(resp.tree);
        }
        else
        {
            bootbox.alert(resp.message);
        }
    });
}
