jQuery(document).ready(function($) {
    var $liList = $('#sidebar-left.products li[tabId]');
    var $tabList = $('#content.products div[tabId]');
    $liList.find('> a').click(function(event) {
        var $content = $('#content');

        var $newA = $(this);
        var $newLi = $newA.closest('li');
        var $oldLi = $('#sidebar-left.products li[tabId].active');
        var $newTab = $tabList.filter('div[tabId="' + $newA.closest('li').attr('tabId') + '"]');
        var $oldTab = $tabList.filter('div[tabId].active');

        $content.hide();
        $oldTab.removeClass('active');
        $newTab.addClass('active');
        $oldLi.removeClass('active');
        $newLi.addClass('active');
        $content.fadeIn(500);
        
        event.preventDefault();
    });

    var $startLi = $liList.filter('li[tabId="' + location.hash.substr(1) + '"]')
    if ($startLi.length) {
        $startLi.find('> a').click();
    } else {
        $liList.filter(':eq(0)').find('> a').click();
    }

    // Слайдер на главной странице
    if (typeof $.fn.slider != 'undefined') {
        $('div.slider').slider();
    }


    // Box shadow
    if (typeof $.fn.boxShadow != 'undefined') {
        $('#content-container .blog-item > img, #content-container img[rel="shadow"]').boxShadow(1, 1, 5, '#aaaaaa');
    }


    // Cufon на index.html
    if (typeof Cufon != 'undefined') {
        Cufon.replace('#company-description span.title', {fontFamily: 'Helios'});
        Cufon.replace('#company-description span.slogan', {fontWeight: 'HeliosBold'});
        Cufon.replace('#header.slider a.button', {
            fontFamily: 'Helios',
            textShadow: '#ffe831 0 1px'
        });
    }


    // Подсказка в поле поиска
    $('#top form input[name="s"], #support-search input').each(function() {
        var value = $(this).attr('value');
        $(this).focusin(function() {
            var $this = $(this);
            if ($this.attr('value') == value) {
                $this.attr('value', '');
            }
        }).focusout(function() {
            var $this = $(this);
            if ($this.attr('value') == '') {
                $this.attr('value', value);
            }
        });
    });


    // Растяжение менюшек
    menuJustify($('#top menu'), true);
    menuJustify($('#top-menu'));
/*     menuJustify($('#footer menu.column'), true); */

    function menuJustify($menu, separator) {
        separator = separator || false;
        var menuWidth = $menu.width();
        var liSumWidth = 0;
        var liList = $menu.find('li').each(function() {
            liSumWidth += $(this).width();
        });
        var m = (menuWidth - liSumWidth - (separator ? liList.length - 1 : 0)) / (liList.length - 1);
        $menu.find('li:gt(0)').each(function(index) {
            if (!separator) {
                $(this).css({
                    marginLeft: Math.floor(m - 1) + 'px'
                });
            } else {
                $(this).css({
                    marginLeft : Math.floor((m - 1) / 2) + 'px',
                    paddingLeft: Math.floor((m - 1) / 2) + 'px',
                    borderLeft : '1px solid ' + liList.find('a').css('color')
                });
            }
        });
    }


    // Тень у заголовков и кнопок
    if (typeof $.fn.textShadow != 'undefined') {
        $('#header > h1, #header > h2, #header > h3, #content-container div[id^="sidebar"] a.question').textShadow();
    }


    // Динамический по высоте градиент
    $('#content div.blog-item').prepend('<div class="gradient"><img src="/img/blog-item-gradient.gif"/></div>');
    $('#content div.main-event').prepend('<img class="gradient" src="/img/img-17.gif"/>');


    // Стилизация select
    $('select:not(.inputselect)').each(function() {                        //
        //var $this = $(this).css({opacity: 0}).wrap('<div style="position: relative;"></div>');
        var $this = $(this);
        if ($this.closest('#custom-form').length) return;

        $this.css({opacity: 0}).wrap('<div style="position: relative;"></div>');
        var $input = $('<input class="select" type="text" value="выбрать" />');
        $this.after($input);
        $this.change(function() {
            $input.attr('value', $this.find('option:eq(' + $this.attr('selectedIndex') + ')').text());
        });
        $input.mousedown(function(e) {
            e.preventDefault();
        })
    });




    // Ширина #content в зависимости от наличия сайдбаров
    $('#content').css({width: $('#content-container').width() - ($('#content').outerWidth(true) - $('#content').width())});
    $('#content-container:has(#sidebar-left)').find('#content').width($('#content').width() - $('#sidebar-left').outerWidth(true));
    $('#content-container:has(#sidebar-right)').find('#content').width($('#content').width() - $('#sidebar-right').outerWidth(true));


    // Стилизация таблиц
    $('table tbody tr:odd').addClass('odd');

    // Стилизация кнопок
    $('#content .green-button, #header.slider a.button').wrapInner('<span class="left"></span>').children('span').append('<span class="right"></span>');


    // Словарь
    $('#content dt').click(function() {
        var $this = $(this);
        if (!$this.next('dd:animated').length) {
            $this.toggleClass('opened');
            $this.next('dd').slideToggle();
        }
    });


    // Галерея
    $('.module.gallery, #content .gallery').each(function() {
        var $this = $(this);
        var $main = $this.find('img.main');
        var $thumbList = $this.find('img:not(.main)');
        var $desc = $this.find('.photo_desc');
        $thumbList.click(function() {
            var $thumb = $(this);
            $main.stop().fadeTo(300, 0, function() {
                //$main.attr('src', $thumb.attr('src').replace(/-thumb/gi, ''));
                $main.attr('src', $thumb.attr('rel'));
                $main.fadeTo(300, 1);
            });
            
            $desc.stop().fadeTo(300, 0, function() {
                $desc.text($thumb.attr('title'));
                $desc.fadeTo(300, 1);
            });
        });
    });


    // media print
    $(function() {
        $('#company-logo').append($('<img src="/img/logo.png" />'));
        $('#top-menu-container').before($('<img id="print-line" src="/img/print-line.gif" />'))
    });


    // IE7
    if ($.browser.msie && $.browser.version == 7) {
        $('#content .blog-item div.gradient img').each(function() {
            var $this = $(this);
            $this.css({
                width: $this.parent().width(),
                height: $this.parent().height()
            });
        });
        $('#content-container > .column:last-child').css({
            marginRight: '12px',
            borderRight: 'none'
        });
        $('#content-container > .column h1:has(ul)').each(function() {
            var $h1 = $(this).css({position: 'relative'});
            var $ul = $h1.find('ul').detach();
            var $span = $('<span></span>').css({display: 'inline-block'});
            $h1.wrapInner($span).append($ul);
            $ul.css({
                position: 'absolute',
                top: '3px',
                left: $span.width + parseInt($ul.css('margin-left'))
            });
        });
        $('#content-container > .column > ul').each(function() {
            $(this).find('li:gt(0)').before('| ');
        });
        $(function() {
            var $form = $('form#kursi');
            $form.css({
                width: ($form.find('label').outerWidth(true) + $form.find('input[type="text"]').outerWidth(true)) + 'px'
            });
        });
        $('#section-search fieldset .column').each(function() {
            var $this = $(this);
            var max = 0;
            $this.find('label').each(function() {
                if ($(this).outerWidth(true) > max) {
                    max = $(this).outerWidth(true);
                }
            });
            $this.width($this.find('input[type="checkbox"]').outerWidth(true) + max);
        });
        $('#top form input[type="text"], #section-search input[type="text"]').each(function() {
            var $this = $(this);
            $this.wrap($('<div></div>').css({
                background: $this.css('background-image') + ' no-repeat left',
                'float': 'left',
                marginBottom: $this.css('margin-bottom')
            }));
            $this.css({
                background: 'transparent',
                marginBottom: 0
            });
        });
    }
    
    //ol wrapper
    $("#content-container ol").each(function() {
		$(this).find("li").wrapInner("<span>");
	});
});
