$(function () {
    var isSingle = (getURLParameter('selectmode') != null && getURLParameter('selectmode') != '' && getURLParameter('selectmode') == "SINGLE");
    if (!isSingle) {
        $('input.rowid').live('click', function () {
            var cb = this;
            var path = $(cb).attr('path');
            $(cb).parents('tr').nextAll().each(function () {
                var tr = this;
                var ccb = $('input.rowid', tr);
                if (ccb.length == 0 || ccb.length == null)
                    return true;
                if (ccb.attr('path').substring(0, path.length) == path && !ccb.is('[disabled]')) {
                    selectRow(ccb.val(), cb.checked, false);
                }
                else
                    return false;
            });
        });
    }

    if($('#mode').size() > 0 && $('#mode').val() == 'KBFOLDER')
        $("#wrapper").css("min-width", "1024px");
});

function addSelectedCompanies(id) {
    if (id) { selectedIds.length = 0; selectedIds.push(id); rowChecked(id, true); }
    if (selectedIds.length == 0) { alert(CompanyResources.NoCompanySelected); return false; }
    var pid = $('#permissionid').length > 0 ? $('#permissionid').val() : 0;
    //if (getURLParameter('callback') != null && getURLParameter('callback') != '') {
    //    var sid = id ? id : selectedIds[0];
    //    eval('opener.' + getURLParameter('callback'))(selectedIds.join(','), $('span.id' + sid).first().text());
    //}
    //else {
        //if (self.callback == null) self.callback = opener.companiesSelected;
        //if (self.callback == null && $('#mode').size() > 0 && $('#mode').val() == 'KBFOLDER') self.callback = opener.addSelectedCompanies;
    var result = new Array();
    result["selectedIds"] = selectedIds;
    result["selectedNames"] = selectedNames;
    result["selectedPaths"] = selectedPaths;

    if (getURLParameter('msName') != null && getURLParameter('msName') != '') {
        result["msName"] = getURLParameter('msName');
    }

    if ($('#permissionid').length > 0)
        result["permissionId"] = $('#permissionid').val();

    if (self.callback == null) {
        if (getURLParameter('cbmode') != null && getURLParameter('cbmode') != '') {
            eval('opener.' + getURLParameter('cbmode') + '.companiesCallback(result)');
        } else {
            self.callback = opener.companiesCallback;
            self.callback(result);
        }
    } else {
        
        self.callback(result);
    }

    self.close();
    return false;    
}

function rowChecked(id, check) {
    if ($('#id' + id).length > 0) {        
        selectedNames['id' + id] = $('#id' + id).attr('fullname');
        selectedPaths["id" + id] = $("#id" + id).attr('path');
    }
}
/**************KB permissions***********************/

function changePermission(link) {
    var link = $(link);
    var parent = link.parent();
    $('.popupwrapper').hide();
    if (parent.find('.popupwrapper').length > 0)
        parent.find('.popupwrapper').show();
    else {
        var html = '<div class="popupwrapper"><div class="popup">' + $('#selectpermissiontemplate').html() + '</div></div>';
        parent.append(html).find('.popupwrapper').css({ left: link.position().left, top: link.position().top + 10 }).show();
    }
    return false;
}

function hidePermission(link) {
    $(link).parents('.popupwrapper').hide();
    return false;
}

function setPermission(link, permissionid) {
    var parent = $(link).parents('h1');
    $(link).parents('.popupwrapper').remove();
    $('#permissionid').val(permissionid);
    parent.find('a#kbpermissionlink:first').text($(link).text());
    return false;
}