/**
 * Выпадалки
 */
(function($)
{
    var s_class_open = 'show-more-open';

    var j_all = '*';
    var j_container = '.show-more';
    var j_close = '.btn-close';
    var j_title = '.show-more-title';
    var j_content = '.show-more-content';
    var j_container_close = j_container + ' ' + j_close;
    var j_container_title = j_container + ' ' + j_title;
    var j_content_all = j_content + ' ' + j_all;
    var j_is_opened = '.' + s_class_open;
    var j_container_is_opened = j_container + j_is_opened;

    // при клике закрываем открытые выпадалки кроме текущей
    $(document).bind('click', function(e)
    {
        var target = $(e.target);
        var container = target.is(j_container) ? target : target.closest(j_container);
        var containers = $(j_container_is_opened);

        containers.not(container).removeClass(s_class_open);
    });

    // открываем/закрываем выпадалку при клике по ее заголовку
    $(j_container_title).live('click', function(e)
    {
        e.preventDefault();

        var title = $(this);
        var container = title.closest(j_container);
        var content = container.children(j_content);

        if (!container.hasClass(s_class_open))
        {
            container.addClass(s_class_open);

            var widthTitle = title.width();
            var widthContent = content.width();

            if (widthTitle >= widthContent)
            {
                content.width(widthTitle + 25);
            }

            content.children('.hr').each(function()
            {
                this.style.height = this.parentNode.offsetHeight - 22 + 'px';
                this.style.width = this.parentNode.offsetWidth - 2 + 'px';
            });

            content.children('.vr').each(function()
            {
                this.style.height = this.parentNode.offsetHeight - 2 + 'px';
                this.style.width = this.parentNode.offsetWidth - 22 + 'px';
            });
        }
        else
        {
            container.removeClass(s_class_open);
        }
    });

    // закрываем выпадалку при клике по кнопке закрытия
    $(j_container_close).live('click', function(e)
    {
        e.preventDefault();

        var close = $(this);
        var container = close.closest(j_container);
        var content = container.children(j_content);
        container.removeClass(s_class_open);
    });

    $(function()
    {
        $(j_title + ' .hr').each(function()
        {
            this.style.height = this.parentNode.offsetHeight - 22 + 'px';
        });

        $(j_title + ' .vr').each(function()
        {
            this.style.width = this.parentNode.offsetWidth - 22 + 'px';
        });
    });

})(jQuery);


/**
 * Слайдер
 */
$(function()
{
    // Init menu lines with slider
    $('.slider > li a').click(function(e)
    {
        e.preventDefault();

        // Get affected elements
        var $this = $(this);
        var switcher = $(this).parents('ul');
        var target = $this.parents('li');
        var selected = switcher.children('.selected');

        if (!selected.length)
        {
            // moved from some other position, just select and go
            target.addClass('selected').addClass('rc13').addClass('rc-shape');
            window.location = $this.attr('href');
            return;
        }

        // Calculate initial and final position
        var from_x = selected.get(0).offsetLeft;
        var from_y = selected.get(0).offsetTop;
        var from_w = selected.innerWidth();
        var to_x = target.get(0).offsetLeft;
        var to_y = target.get(0).offsetTop;
        var to_w = target.innerWidth();

        // Init slider
        var slider = switcher.children('li.slider');
        if (!slider.length)
        {
            switcher.append('<li class="slider rc13 rc-shape"></li>');
            slider = switcher.children('li.slider');
            if (typeof rocon != 'undefined')
            {
                rocon.update(slider.get(0));
            }
        }

        slider.css({
            'top' : from_y,
            'left' : from_x,
            'width' : from_w,
            'height' : selected.innerHeight()
        });

        slider.show();

        selected.removeClass('selected').addClass('slide-from');
        target.addClass('slide-to');

        var time = function (from, to)
        {
            var step = switcher.children().get(0).offsetLeft - switcher.children().get(1).offsetLeft;
            var raw = Math.abs(150 * (from - to) / step);
            return raw < 80 ? 80 : raw;
        }

        if (from_y == to_y)
        {
            slider.animate({ left: to_x }, time(from_x, to_x)).animate({ width: to_w },
            {
                duration : time(from_w, to_w),
                complete : function()
                {
                    window.location = $this.attr('href');
                }
            });
        }
        else
        {
            var from_end = switcher.children().get(0).offsetLeft;
            var to_end = switcher.innerWidth() - slider.innerWidth();

            slider.animate({ left: to_end },
            {
                duration : time(from_x, to_end),
                complete : function() {
                    slider.css({
                        top: to_y,
                        left: from_end
                    });
                }
            }).
            animate({ left: to_x }, time(from_end, to_x)).animate({ width: to_w },
            {
                duration : time(from_w, to_w),
                complete : function() {
                    window.location = $this.attr('href');
                }
            });
        }
    });
});

$(".about .title").live('click', function()
{
    if ($(".about #help").hasClass('hidden'))
    {
        $('.about #help').removeClass('hidden');
    }
    else
    {
        $(".about #help").addClass('hidden');
    }
});

$("#help .close").live('click', function()
{
    $(".about #help").addClass('hidden');
});
$(".about #help").live('mouseleave', function()
{
    $(".about #help").addClass('hidden');
});


$("li .pseudo.section").live('click', function()
{
    if($("ol li.recom-ref .inner").hasClass('hidden')){
        $("ol li.recom-ref .inner").removeClass('hidden');
        $('li.recom-ref').addClass('expanded');
    } else {
        $('ol li.recom-ref .inner').addClass('hidden');
        $('li.recom-ref').removeClass('expanded');
    }

    return false;
});

$(function()
{
    $('#button-recomend').bind('click', function()
    {
        var imageObj = $('#image-recomend');

        if (imageObj.hasClass('disabled'))
        {
            active = 'true';
            imageObj.removeClass('disabled');
            $(this).text('Скрыть «Рекомендую Est!» на личном сайте');
        }
        else
        {
            active = 'false';
            imageObj.addClass('disabled');
            $(this).text('Показать «Рекомендую Est!» на личном сайте');
        }

        var url = '/est_tools/switch_vcard_recomends_button';

        $.ajax({
            url: url,
            data: {
                active: active
            },
            dataType: "json",
            success: function(data) {

            }
        });

        return false;
    });

    $('.save_realty').live('click', function ()
    {
        var obj = $(this);
        var url = obj.attr('href');
        var record_id = obj.attr('name');

        $.ajax({
            url: url,
            data: {
                sf_format: 'jsonp'
            },
            dataType: 'jsonp',
            success: function(data)
            {
                obj.find('span').text(data.text);
                obj.attr('href', data.url);

                if (data.mark == 'on')
                {
                    $('#record_id_' + record_id).css('display', 'block');
                }
                else
                {
                    $('#record_id_' + record_id).css('display', 'none');
                }
            }
        });

        return false;
    });
});

