$(document).ready(
    function()
    {
        $("#header li:last-child a").css({'background':'none', 'border':'none'});
        $("#special dl:nth-child(odd)").after('<hr />');
       // $("#about dd").wrapInner('<div><p></p></div>').prepend('<img src="./i/scroll.gif" />');
        $("#big_banner").prepend('<img src="./i/corner.gif" style="left:0; top:0;" />');

        $('#image a').click(
            function()
            {
                var link = $(this);

                if ($('div.darkbox-frame').length == 0)
                {
                    // Если попап прежде не вызывался,
                    // создаём его и цепляем к BODY
                    $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><a class="next">Следующая</a><a class="prev">Предыдущая</a><div class="darkbox-button"></div></div></div>').appendTo('body');

                    $('div.darkbox-frame a.next').click(
                        function(e)
                        {
                            var list = $('#image a');
                            var index = list.index(link);

                            if (index < list.length - 1)
                            {
                                link = list.eq(index + 1);

                                refreshNextPrevPopup(link);
                                $('div.darkbox-canvas img').attr({ 'src': link.attr('href'), 'alt': link.attr('title') });
                            }
                        });
                    $('div.darkbox-frame a.prev').click(
                        function(e)
                        {
                            var list = $('#image a');
                            var index = list.index(link);

                            if (index > 0)
                            {
                                link = list.eq(index - 1);

                                refreshNextPrevPopup(link);
                                $('div.darkbox-frame img').attr({ 'src': link.attr('href'), 'alt': link.attr('title') });
                            }
                        });
                }

                // Клонируем попап,
                // прицепляем клон к BODY и показываем его
                var frame = $('div.darkbox-frame');
                frame.addClass('darkbox-frame-on');

                var shadow = frame.find('div.darkbox-shadow').animate({ opacity: 0.6 }, 300);
                var canvas = frame.find('div.darkbox-canvas');
                var button = frame.find('div.darkbox-button');

                refreshNextPrevPopup(link);

                // Цепляем к попапу картинку и ждём её загрузки
                var image = $('<img src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');

                $('img', canvas).remove();
                image.appendTo(canvas);
                image.load(
                    function()
                    {
                        var imageWidth  = image.width();
                        var imageHeight = image.height();
                        var frameWidth  = frame.width() - 40;
                        var frameHeight = frame.height() - 40;

                        if (imageWidth > frameWidth || imageHeight > frameHeight)
                        {
                            var resize = ((imageWidth - frameWidth) > (imageHeight - frameHeight)) ?
                                imageWidth - frameWidth :
                                imageHeight - frameHeight;

                            imageWidth -= resize;
                            imageHeight -= resize;

                            image.width(imageWidth);
                            image.height(imageHeight);
                        }

                        // Анимируем загрузчик до размеров картинки
                        // и одновременно смещаем к центру
                        canvas.addClass('darkbox-canvas-load').animate({
                                'width': imageWidth,
                                'marginLeft': -imageWidth / 2,
                                'height': imageHeight,
                                'marginTop': -imageHeight / 2 },
                            500,
                            function()
                            {
                                // После завершения анимации показываем кнопку и картинку
                                canvas.addClass('darkbox-canvas-done');
                                button.addClass('darkbox-button-on');
                                button.addClass(navigator.platform.toLowerCase().indexOf('mac') + 1 ? 'darkbox-button-left' : 'darkbox-button-right');

                                image.animate({opacity: 1 },500,
                                    function()
                                    {
                                        // Вешаем обработчики закрытия
                                        shadow.click(closer);
                                        button.click(closer);

                                    });
                            });
                    }); // end of image.load

                // Функция закрытия попапа
                var closer = function()
                    {
                        canvas.remove();
                        shadow.animate({ opacity: 0 }, 300,
                            function()
                            {
                                frame.remove();
                            });
                    };

                // Внимательно слушаем клавишу Esc
                $(document).keydown(
                    function(e)
                    {
                        if (e.which == 27)
                            closer();
                    });

                return false;
            }); // end of image.a.click

        $("#hotel div.button").click(
            function()
            {
                $(this).hide()
                $("#hotel form").slideDown();
            });

        prependComboBox('#search');
    });
function prependComboBox(parentId)
{
    $('ul', $(parentId)).each(
        function(index, item)
        {
            var list = $(item);
            var selectedValue = list.prev().val();

            list.prepend('<li class="first" value="0"></li><li class="arrow" value="0"></li>');

            $('li.first, li.arrow', list).click(
                function(e)
                {
                    var sender = $(this);
                    var ul = sender.parent();

                    if ($('li[value!=0]', ul).length > 0)
                    {
                        if (ul.hasClass('open') != true)
                        {
                            ul.addClass('open');
                        }
                        else
                        {
                            ul.removeClass('open');
                        }
                    }
                });

            $('li[value]', list).each(
                function(index, item)
                {
                    var li = $(item);

                    if (li.hasClass('first') || li.hasClass('arrow'))
                    {
                        return;
                    }

                    li.click(
                        function(e)
                        {
                            var t = $(this);
                            var ul = t.parent();

                            t.hide().siblings().show();
                            ul.removeClass('open');
                            ul.children('.first').show().text(t.text());
                            ul.prev().val(t.attr('value'));
                        });

                    if (li.attr('value') == selectedValue)
                    {
                        li.click();
                    }
                });

        });
}
function refreshNextPrevPopup(item)
{
    var list  = $('#image a');
    var index = list.index(item);
    var prev  = $('div.darkbox-frame a.prev');
    var next  = $('div.darkbox-frame a.next');

    if (list.length > 1)
    {
        if (index > 0)
        {
            prev.show();
        }
        else
        {
            prev.hide();
        }

        if (index < list.length - 1)
        {
            next.show();
        }
        else
        {
            next.hide();
        }
    }
    else
    {
        prev.hide();
        next.hide();
    }
}