$(document).ready(function() {
    /* Opera overflow hack */
    if ($.browser.opera) {
        $("body, html").css({'position':'absolute', 'overflowX':'auto', 'overflowY':'auto', 'height':'auto'});
    }

    /* Carousel */
    var html = $('#carousel').html();
    $('#carousel').append(html);
    $('#carousel').append(html);
    $('#carousel').append(html);
    $('#carousel').append(html);

    /* Carousel Animation */
    function mycarousel_initCallback(carousel) {
        jQuery('.jcarousel-item').bind('click', function() {
            var item_index = $(this).index(); // Получаем индекс эл-та по которому кликнули
            el = $('#carousel li.curent .item');
            eimg = $('#carousel li.curent .item img');

            /* Скукоживаем букет */
            eimg.animate({
                bottom: 54+'px',
                width: 60+'%',
                left: 0+'%'
            }, 120, function(){
                el.find('.descr').hide(); // Скрываем описание букета
                el.animate({
                    height: 260+'px',
                    left: 35+'%',
                    width: 98+'%'
                }, 20, function(){
                    el.parent().animate({}, 'fast', function(){
                        el.parent().removeClass('curent');
                    });

                    /* Двигаем карусель */
                    carousel.scroll($.jcarousel.intval(item_index));

                    /* Раскукоживаем букет */
                    $('#carousel li').eq(item_index).find('.item').animate({
                        height: 589+'px',
                        width: 100+'%'
                    }, 400, function(){
                        $('#carousel li').eq(item_index).animate({}, 'fast', function(){
                            $('#carousel li').eq(item_index).find('img').animate({
                                bottom: 14+'px',
                                width: 140+'%',
                                left: -40+'%'
                            }, 1000);
                        });
                        $('#carousel li').eq(item_index).find('.descr').show(); // Показывем описание букета
                        $('#carousel li').eq(item_index).addClass('curent');
                        resDescr();
                    });
                });
            });
            return false;
        });
    };

    $('#carousel').jcarousel({
        wrap: 'circular',
        buttonNextHTML: null,
        buttonPrevHTML: null,
        animating: 5000,
        start:Math.ceil($('#carousel li').length/2),
        initCallback: mycarousel_initCallback
    });

    $('#carousel li').eq(Math.ceil($('#carousel li').length/2)).addClass('curent');


    /* Resizing Flower Desription */
    function resDescr(){
        if($('body').width() < 1100){
            $('#carousel li.curent .descr').animate({
                left: 100+'%',
                width: 120+'%'
            });
        }
        else if($('body').width() > 1100){
            $('#carousel li.curent .descr').animate({
                left: 120+'%',
                width: 150+'%'
            });
        }
    }

    $(window).resize(function(){
        resDescr();
    });
    resDescr();


    /* Styling Images */
    $('.about-w img').each(function(){
        $(this).wrap('<div class="img-bw"></div>');
    });
    $('.serv-list img').each(function(){
        $(this).wrap('<div class="img-mw"></div>');
    });
    $('.cel-list img').each(function(){
        $(this).wrap('<div class="img-w"></div>');
    });

    /* Show Popup Window */
    $('.show').click(function(){
        $('.popup').hide(); /* Hide All Popups Windows */
        $('.fixed').show(); /* Show Popups Container */
        $(".fixed .p-"+$(this).attr('class').replace('show ','')+"").show(); /* Get Window Id And Show It By Clicking */
    });

    /* Close Popup Window */
    $('.close').click(function(){
        $(this).parent('.popup').hide();
        $('.fixed').hide();
    });

    $('.fancy').fancybox({
        titlePosition:'inside',
        transitionIn:'elastic',
        transitionOut:'elastic',
        overlayColor: '#262226',
        showCloseButton: true,
        showNavArrows: true
    });
    $('.fancy').hover(
        function(){
            $(this).find('.img-w').css({'backgroundPosition':'0 -156px'});
        },
        function(){
            $(this).find('.img-w').css({'backgroundPosition':'0 0'});
        }
    );
});