function changeServiceAjax() {
    $("body").append('<div id="bodyoverlay"></div><div id="dialog" style="width:100%; height:90%;z-index:9999; position:absolute; top:120px; left:20px"></div>');
    var oldtypeid = $("#typeid").val();
    var oldserviceid = $("#serviceid").val();
    $("#dialog").load(base + 'task.ivp/selectservice?nolayout=true',
        { serviceid: null, taskid: $("#id").val(), oldserviceid: $("#serviceid").val(), oldtypeid: $("#typeid").val() },
        function () {
            $("#servicecontainer").height($("#dialog").height() - 100);
            $("a#back").remove();
            $("#dialog form").prepend('<a style="float:right;" id="back" href="#" onclick="return false;"><img src="img/close.gif"/></a>');
            $('#serviceholder ul').first().css('z-index', 30).show();
            $('#dialog li[data-service-id=' + oldserviceid + '][data-type-id=' + oldtypeid + ']').remove();

            $('a#back').attr("href", "#").click(function () {
                $("#dialog").remove();
                $("#bodyoverlay").remove();
                return false;
            });
            $('#btnback').click(_backOneLevel);
            $('#serviceholder li').click(_selectServiceAjax);
            _columnWidth = $("#serviceholder ul").first().width() + 1;
            $(window).resize(_resizeContainer);
            _resizeContainer();
          
            _restorePosition();
        });

    return false;
}


function _restorePosition() {
    if ($('#serviceid').val()) {
        var $li = $('li.service[data-service-id="' + $('#serviceid').val() + '"]');
        $($li.parents('li').get().reverse()).trigger('click');
        if (!$li.hasClass('endpoint'))
            $li.trigger('click');
    }
}

function _selectServiceAjax(event) {
    var $li = $(this);
    var $parent = $li.parents('ul:first');
    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').length == 0) {
        $('#taskform #serviceid').val($li.attr('data-service-id'));
        $('#taskform #typeid').val($li.attr('data-type-id'));
        if ($('#typeid').val() != '' && $('#tariff').val() != 'SaaSPlus' && $('#tariff').val() != '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;
        }
        //$li.parents('form').submit(function () { data: $("#taskform").serialize(); });

        $("#taskform").attr("action", $li.parents('form').attr("action"));
        $("#taskform").submit();

        //$("form#taskform").submit();
        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);
}

function _scrollContainer() {
    if (_currentLevel > _columns) {
        var offset = (_columns - _currentLevel) * _columnWidth;
        $('#serviceholder').animate({ left: offset }, 'fast');
        return true;
    }
    else if (_currentLevel == _columns) {
        $('#serviceholder').animate({ left: 0 }, 'fast');
    }
    return false;
}

function _resizeContainer() {
    _columns = Math.round(($('#servicecontainer').width() - 70) / _columnWidth);
    _scrollContainer();
}

function _setRolloutUlPosition($ul, $li) {
    $ul.css('left', 0);
    var windowHeight = $(window).height();
    var liTop = $li.offset().top - $(window).scrollTop();
    var windowScrollTop = $(window).scrollTop();
    var freeTop = $li.offset().top - windowScrollTop - (windowScrollTop < $('#serviceholder').offset().top ? $('#serviceholder').offset().top - windowScrollTop : 0);
    var freeBottom = $(window).height() - freeTop;

    if ($ul.height() <= freeBottom) {
        $ul.css('top', $li.position().top).show();
    }
    else if ($ul.height() <= freeTop) {
        $ul.css('top', $li.position().top + $li.height() - $ul.height() + 2).show();
    }
    else {
        $ul.css('top', windowScrollTop).show();
    }
}

function _setHeight() {
    var height = 0;
    $('#serviceholder ul:visible').each(function (i, el) { var $el = $(el); if ($el.height() + $el.position().top > height) height = $el.position().top + $el.height(); });
    $('#serviceholder').height(height);
}

function _backOneLevel() {
    var $parent = _currentLI.parents('li:first');
    if ($parent.length > 0) {
        $parent.trigger('click');
    }
    else {
        _currentLevel = 1;
        $('#serviceholder ul:first li h3').removeClass('selected');
        $('#serviceholder ul:first li ul').hide();
        _scrollContainer();
    }
}
