$(function() {

    $('.js-selectChildService').on('click', function () {
        selectServiceDilog(true);
    });

    $('.js-selectParentService').on('click', function () {
        selectServiceDilog(false);
    });
});


function selectServiceDilog(isChildTask) {
    $("body").append('<div id="bodyoverlay"></div><div id="changeservicedialog" style="width:100%; height:90%;z-index:9999; position:absolute; top:120px; left:20px"></div>');

    $.get('Task/SelectService',
        function (data) {
            var bindingType = isChildTask ? "ParentId" : "ChildId";
            var url = base + "Task/Create?" + bindingType + "=" + $('#id').val();
            $("#changeservicedialog").html(data);
            $("#changeservicedialog").offset({ top: ($(window).scrollTop()) });
            $("#servicecontainer").height($("#changeservicedialog").height() - 100);

            $("#changeservicedialog form").prepend('<a style="float:right;" class="noul" id="back" href="#" onclick="return false;"><img src="img/close.gif"/></a>');

            $('#serviceholder ul').first().css('z-index', 30).show();

            $('#changeservicedialog li').attr('data-url', url);

            $('a#back').attr("href", "#").click(function () {
                $("#changeservicedialog").remove();
                $("#bodyoverlay").remove();
                return false;
            });

            $('#btnback').click(_backOneLevel);
            $('#serviceholder li').click(function (event) {
                _selectService(this, event, function () {
                    $("#changeservicedialog").remove();
                    $("#bodyoverlay").remove();
                    return false;
                });
            });

            _columnWidth = $("#serviceholder ul").first().width() + 1;
            $(window).resize(_resizeContainer);
            _resizeContainer();

            _restorePosition();
        }
    );
    $('input,select', $('#addexpenses')).attr('disabled', true);
    $('#addexpenses').hide('fast', function () { $('#btnaddexpenses').show(); });
    return false;
}

function _selectService(that, event, callback) {
    var tariff = $('#tariff').val().toUpperCase();
    var $li = $(that);
    var $ul = $li.find('ul:first');

    event.stopPropagation();
    $li.siblings().removeClass('selected').find('h3').removeClass('selected');
    $li.children().removeClass('selected').find('h3').removeClass('selected');

    $('ul:visible', $li.siblings()).hide();
    $('ul:visible', $li.children()).hide();
    $li.addClass('selected').find('h3:first').addClass('selected');


    //Если мы на последнем уровне - отпавляем запрос на создание заявки
    if (!$li.find('ul').size()) {
        //$('#serviceid').val($li.attr('data-service-id'));
        

        //$('#typeid').val($li.attr('data-type-id'));
        //if ($('#typeid').val() != '' && tariff != 'SAASPLUS' && tariff != 'ENTERPRISE') {
        //    alert(TaskResources.DisabledInThisTariff);
        //    return;
        //}

        if ((!$('#taskid').val() || $('#taskid').val() == '0') && $('#oldtypeid').val() != '1001' && $('#oldserviceid').val() != 0 && $li.attr('data-type-id') == '1001') {
            alert(TaskResources.NoChangeToMeetingRoom);
            $('#typeid').val('');
            return;
        }

        var taskTypeAction = $li.attr('data-url');
        var taskType = "&TypeId=" + $li.data("type-id");
        var taskService = "&ServiceId=" + $li.data("service-id");

        if (typeof taskTypeAction !== 'undefined' && taskTypeAction !== false) {
            var uri = taskTypeAction + taskType + taskService;

            window.open(uri, '_blank');
            if (typeof callback === 'function') {
                callback();
            }

        }
        return;
    }

    _currentLevel = $li.parents('ul').length + 1;
    _currentLI = $li;
    $('#btnback').attr('disabled', _currentLevel <= _columns);
    if ((_currentLevel <= _columns) == true) $('#btnback').hide();
    else $('#btnback').show();

    var scrolled = _scrollContainer();

    _setRolloutUlPosition($ul, $li);
    _setHeight();

    if (!scrolled)
        $ul.animate({ 'left': _columnWidth }, 'fast');
    else
        $ul.css('left', _columnWidth);
}