$(function() {
    if ($('#id').val() == '0') { $('#file16').attr('disabled', false); }
});

function setTab(id) {
    if (!documentReady) return;
    $('#tab').val(id);
    $('.tab').each(function(i, el) { if (el.id == id) $(el).show(); else $(el).hide(); });
    $('#tabid').val(id);
    $('#tabs>ul>li>a').each(function(i, el) { if (el.id == 'a' + id) $(el).addClass('selected'); else $(el).removeClass('selected'); });
    if (id != 'general') {
        $('.save').attr('disabled', true);
        $('.saveandreturn').attr('disabled', true);
    }
    else {
        $('.save').attr('disabled', false);
        $('.saveandreturn').attr('disabled', false);
    }
    return false;
}

function afterInitTable() {
    $('table[assettypefieldstable]').treegrid({
        treeColumn: 2
    });
}

function rowChecked(val, check) {
    var $checkbox = $('#id' + val);

    if ($checkbox.attr('field-type') == 'ElementGroup') {
        var $trs = $('input.rowid[field-path^="' + $checkbox.attr('field-path') + '"]').closest('tr');
        if (check) {
            alert('Будут удалены все элементы выбранной группы');
            $trs.addClass('selected');
        }
        else {
            $trs.removeClass('selected');
        }
    }
}

function loadFields(reload) {
    var url = base + (($('#readonly').val() == "True") ? 'assettypefield.ivp/readonly' : 'assettypefield.ivp/list');
    reload = reload || !$.trim($('#fields').html());
    $('.save').attr('disabled', true);
    $('.saveandreturn').attr('disabled', true);
    $('#fields').load(url, { tb_assettypeid: $('#id').val(), reset: true, timestamp: new Date().toString() }, function() {
        setTab('fields');
        //if (reload) {
            initTable();
        //}
    });
    return false;
}

window.designerController = 'AssetTypeDesignerField.ivp';
window.designerListParams = '?typeid=' + $('#id').val();
window.designerModalWindowWidth = 800;

function loadDesigner() {
    //var url = base + 'assettype.ivp/designer/' + $('#id').val();

    //var reload = !$.trim($('#designer').html());
        
    $('#designer').load(base + window.designerController + ' .designerfield', { reset: true, typeid: $('#id').val(), timestamp: new Date().toString() }, function () {
        setTab('designer'); 
        //if (reload)
        initDesigner();
    });
    
    return false;
}

function editField(id) {
    var url = id ? 'assettypefield.ivp/view/' + id : 'assettypefield.ivp/create/'+$('#id').val();
    openDialog(url, 'field', { width: 600, height: 490 });
    return false;
}
