$(document).ready(function () {
    $("input[type=radio]").css("margin-right", "5px");
    $('#listTree input[type=checkbox]').live("change",  recheckServices);

    $('input[id$=All]').bind("change", function (e) {
        calculateWeigth(false, getNumber($(this).attr("id"), true));
    });
    
    $('input[id$=Select]').bind("change", function (e) {
        calculateWeigth(true, getNumber($(this).attr("id"), false));
    });

    $('input[id$=All]').bind("click", function (e) {
        var inputId = $(this).attr("name") + "Select";
        var block = $("#" + inputId).parent();
        var select = block.find("select");
        
        if (select.length) {
            var id = $(select).attr("id");
            var objs = $('input[id^=' + id + ']');
            var s = $(select).find("option");

            objs.removeAttr("checked");
            s.removeAttr("selected");

            var text = MultipleSelectResources.Choose;
            block.find("a:first").attr('title', text).html(text);
        }
        
    });

    $("#content").show();

    if ($.multipleSelect) $.multipleSelect.initByClassName('.multipleselect', {init: true, pixelwidth: 600 });
});

function calculateWeigth(isAdd, num) {
    var weight = $("#weight");
    if (weight.length) {
        var val = parseInt(weight.val());
        var v = num;

        if (isAdd)
            val += v;
        else {
            val = (val - v) > 0? val - v: 0;
        }
        
        weight.attr("value", val.toString());
    }
}

function getNumber(id, isAll) {
    var num = 0;

   // var str = isAll ? id.substring(0, id.indexOf("All")) : id.substring(0, id.indexOf("Select"));
    //увеличение при установке
    /*
    switch (str) {
        case "ListService":
            num = 100;
            break;
        case "ListTaskType":
            num = 50;
            break;
        case "ListAssetType":
            num = 20;
            break;
        case "ListCategory":
            num = 10;
            break;
        default:
            break;
    }*/

    return num;
}

var WorkflowRule = {
    selectService: function selectService(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=WorkflowRule';

        var selServicesPath = $("ul[selectid = 'listService'] input");
        var selServicesId = new Array();
        for (i = 0; i < selServicesPath.length; i++) {
            var ids = $(selServicesPath[i]).val().split("|");
            selServicesId.push(ids[ids.length - 2]);
        }
        if (selServicesId.length > 0)
            sessionStorage.setItem('disabledServices', selServicesId);
        else
            sessionStorage.removeItem('disabledServices');

        dlg = openDialog(url, 'selectservices', { width: 900, height: 650 });
        dlg.callback = WorkflowRule.servicesCallback;
        return false;
    },
    servicesCallback: function servicesCallback(result) {
        var msName = result['msName'];
        addSelectedPaths(result, msName);
    },

    selectTaskType: function selectTaskType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=WorkflowRule';

        dlg = openDialog(url, 'selecttasktypes', { width: 900, height: 650 });
        dlg.callback = WorkflowRule.taskTypesCallback;
        return false;
    },
    taskTypesCallback: function taskTypesCallback(result) {
        var msName = result['msName'];
        addSelectedValues(result, msName);
    },

    selectAssetType: function selectAssetType(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=WorkflowRule';

        dlg = openDialog(url, 'selectassettypes', { width: 900, height: 650 });
        dlg.callback = WorkflowRule.assetTypesCallback;
        return false;
    },
    assetTypesCallback: function assetTypesCallback(result) {
        var msName = result['msName'];
        addSelectedValues(result, msName);
    },

    selectCategory: function selectCategory(url, msName) {
        if (url.indexOf('?') > -1) {
            url += "&";
        } else {
            url += "?";
        }

        url += ('msName=' + msName);
        url += '&cbmode=WorkflowRule';

        dlg = openDialog(url, 'selectcategories', { width: 990, height: 660 });
        dlg.callback = WorkflowRule.categoriesCallback;
        return false;
    },
    categoriesCallback: function categoriesCallback(result) {
        var msName = result["msName"];
        addSelectedPaths(result, msName);
    },
}