jQuery(document).ready(function($) {
    /* Cufon **********************************************************************/
    if (typeof Cufon != 'undefined') {
        Cufon.replace('#top-menu > li > a', {
            hover:      true,
            fontFamily: 'DINCyr-Medium'
        });

        Cufon.replace('body:not(#page-index) #header h1', {fontFamily: 'DINCyr-Medium'});
        Cufon.replace('#page-index #header h1 > span', {fontFamily: 'DINCyr-Medium'});

        Cufon.replace('#page-index #header .quote q', {fontFamily: 'DINCyr-Light'});
        Cufon.replace('#page-index #header .quote cite', {fontFamily: 'DINCyr-Medium'});

        Cufon.replace('#page-index #header .block h3', {
            fontFamily: 'DINCyr-Medium',
            textShadow: '#759490 0 -1px'
        });

        Cufon.replace('#page-index #sidebar .contacts h1 span', {fontFamily: 'DINCyr-Medium'});
        Cufon.replace('#page-index #sidebar .contacts a', {fontFamily: 'DINCyr-Medium'});
        Cufon.replace('#page-index #sidebar .contacts > span:not(.tel)', {fontFamily: 'DINCyr-Medium'});
        Cufon.replace('#page-index #sidebar .contacts > span.tel', {
            fontFamily: 'DINCyr-Medium',
            textShadow: '#859196 0 -1px'
        });

        Cufon.replace('body:not(#page-headquarters) #content h1', {fontFamily: 'DINCyr-Light'});
        Cufon.replace('#content h1 > span', {fontFamily: 'DINCyr-Light'});
        Cufon.replace('#content h2', {fontFamily: 'DINCyr-Medium'});

        Cufon.now();
    }

    /* Search Form ****************************************************************/
    $('#top form input[name="s"]').each(function() {
        var $this = $(this);
        var text = $this.attr('value');

        $this.focusin(function() {
            if ($this.attr('value') == text) {
                $this.attr('value', '');
            }
        }).focusout(function() {
            if ($this.attr('value') == '') {
                $this.attr('value', text);
            }
        });
    });

    /* Top Menu *******************************************************************/
    $(function() {
        var $topMenu = $('#top-menu');
        $topMenu.find('li.online-shop').prepend('<i></i>')

        $topMenu.children('li:has(menu)').each(function() {
            var $this = $(this);
            var $menu = $this.children('menu');
            var $liList = $menu.children('li');

            var i = 0;
            while($liList.slice(5 * i, 5 * (1 + i++)).wrapAll('<div class="column">').length);

            var sumWidth = 0;
            $menu.find('div.column').each(function() {
                sumWidth += $(this).outerWidth(true);
            });
            $menu.width(sumWidth);

            var $top = $('#top');
            var topX = $top.offset().left;
            var menuX = $menu.offset().left;

            if (menuX - topX + $menu.innerWidth() > $top.innerWidth()) {
                $menu.addClass('right');
            }

            if (!($.browser.msie && $.browser.version < 9)) {
                $this.hover(function() {
                    $menu.stop().fadeTo(250, 1);
                }, function() {
                    $menu.stop().fadeTo(250, 0, function() {
                        $(this).css({display: 'none'});
                    });
                });
            } else {
                $this.hover(function() {
                    $menu.css({display: 'block'});
                }, function() {
                    $menu.css({display: 'none'});
                });
            }

            $menu.css({
                visibility: 'visible',
                display:    'none'
            });
        });
    });

    /* Breadcrumbs ****************************************************************/
    $(function() {
        var $bc = $('#breadcrumbs a');
        if ($bc.length) {
            $bc.after(' / ')
        }
    });

    $('#content dl.spoiler dt').wrapInner('<span></span>');

    $('#content dl.spoiler dt').click(function() {
        var $this = $(this);
        if (!$this.next('dd:animated').length) {
            $this.toggleClass('opened');
            $this.next('dd').slideToggle();
        }
    });

    /* Content Tabs ***************************************************************/
    $('#content dl.tab').each(function() {
        var $dl = $(this);
        var $dtList = $dl.children('dt');
        $dtList.wrapInner('<span class="left"></span>').append('<span class="right"></span>');

        var dtLeftPos = 0;
        $dtList.each(function() {
            var $dt = $(this);
            $dt.css({left: dtLeftPos + 'px'});
            dtLeftPos += $dt.outerWidth(true);
            $dt.click(function() {
                $dtList.removeClass('active');
                $dt.addClass('active');
            });
        });
        $dtList.css({position: 'absolute'});

        $($dtList.get(0)).click();
    });

    /* Index Slider ***************************************************************/
    $(function() {
        if ($('#page-index').length && typeof $.fn.jCarouselLite != 'undefined') {
            $('#slider .block div a').append('<i></i>')
            $('#slider .block').wrapAll('<ul></ul>').wrap('<li></li>');

            $('#slider').jCarouselLite({
                btnPrev: '.prev',
                btnNext: '.next',
                visible: 2,
                easing:  'easeInQuart',
                speed:   700
            });
        }
    });

    /* IE 7-8 *********************************************************************/
    $(function() {
        if ($.browser.msie) {
            var $cufon = $('cufon.cufon-vml').css({position: 'relative'});
            switch (Math.floor($.browser.version)) {
            case 7:
                $cufon.each(function() {
                    $(this).css({top: '-' + Math.floor($(this).height() * 0.18) + 'px'});
                });
                $('#page-about #content div.signature').css({marginBottom: 0});
                $('#content, #main').append($('<div></div>').css({
                    height: '1px',
                    clear: 'both'
                }));
                break;
            case 8:
                $cufon.each(function() {
                    $(this).css({top: '-' + Math.floor($(this).height() * 0.08) + 'px'});
                });
                break;
            }
        }
    });

    /* Скрытие шапки <h1> на внутренних страницах *********************************/
    if($('#content h1:first').html() == "") $('#content h1:first').hide();
});
