var ifOnlyOneValueChecked = false;
Array.prototype.lastIndexOf = function (searchElement /*, fromIndex*/) {
    "use strict";

    if (this === void 0 || this === null)
        throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
        return -1;

    var n = len;
    if (arguments.length > 1) {
        n = Number(arguments[1]);
        if (n !== n)
            n = 0;
        else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
            n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    var k = n >= 0
          ? Math.min(n, len - 1)
          : len - Math.abs(n);

    for (; k >= 0; k--) {
        if (k in t && t[k] === searchElement)
            return k;
    }
    return -1;
};

Array.prototype.filter = function (fn, obj) {
    for (var l = this.length, i = 0, a = []; i < l; i++) {
        fn.apply(obj, [this[i], i, this]) && a.push(this[i]);
    }
    return a;
}


Array.prototype.unique = function () {
    return this.filter(function (e, i, a) {
        return a.lastIndexOf(e) == i;
    });
}

$(function () {
    ifOnlyOneValueChecked = $('#isOnlyOneValueCanCheck').size() > 0;
    if (!ifOnlyOneValueChecked) {
        $('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.attr('path').substring(0, path.length) == path) { selectRow(ccb.val(), cb.checked && !ccb.attr('disabled'), false); } else return false;
            });
        });
    }    
});

function selectRow(val, check, skip) {
    var cb = $((document.getElementsByName('_id').length > 0) ? "#_id" + val : "#id" + val).get(0);
    //разрешено выбрать только одного пользователя
    if (ifOnlyOneValueChecked) {
        $("input:checked").attr('checked', false);
        $('tr.selected').removeClass('selected');
        selectedIds.length = 0;
    }
    if (check) {
        if (!skip) selectedIds.push(val);
        if (cb) {
            cb.checked = true;
            $(cb).parents('tr').addClass('selected');
        }

        //разрешено выбрать только одного пользователя
        if (ifOnlyOneValueChecked) selectedIds.reverse();
    }
    else {
        selectedIds = $.grep(selectedIds, function (i) { return i != val; });
        if (cb) {
            cb.checked = false;
            $(cb).parents('tr').removeClass('selected');
        }
    }
    try { if (rowChecked) rowChecked(val, check); } catch (e) { }
}


function addSelectedCategories(id) {
    if (id) { selectedIds.length = 0; selectedIds.push(id); rowChecked(id, true); }

    var result = new Array();
    result["selectedIds"] = selectedIds;
    result["selectedNames"] = selectedNames;
    result["selectedPaths"] = selectedPaths;

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

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

    self.close();
    return false;
}

function selectAllCategoriesToService(isChecked) {
    $('table.data input.rowid').each(function (i, el) {
        if (el.disabled)
            return true;
        if (!el.checked && !el.disabled) {
            el.checked = true;
        }
        else {
            el.checked = false;
        }
        selectRow(el.value, el.checked);
    });
    if ($('#rowids').checked == false) {
        $('#rowids').attr('checked', true);
    }
    return false;
}

//function rowChecked(id, check) {
//    if ($('#id' + id).length > 0) {
//        categoryNames['lb' + id.toString()] = "";
//        $.each($('#id' + id).attr("path").split('|'),
//            function (e, el) {
//                if (el > 0) {
//                    if (categoryNames['lb' + id.toString()] != "")
//                        categoryNames['lb' + id.toString()] += " / ";
//                    var labelname = $.trim($('#id' + el).parent().find("a").text());
//                    if(labelname == "")
//                        labelname = $.trim($('#id' + el).parent().text());
//                    categoryNames['lb' + id.toString()] += labelname;
//                }
//            }
//        );


//    }
//}