$(function(){
    // добавление класса к четным строкам таблиц
    $("#content table:not(.nostripe) tbody tr:odd").addClass("odd");

    // FAQ: скрыть/показать ответ при клике на вопрос
    $("#faqList")
        .find('dd').hide().end()
        .find('dt').click(function(){$(this).next().toggle();});

    // Фокус при перехоже на форму
    $('a[href=#form]').click(function(){
        $('#_form textarea:first').focus();
    });
    if (location.hash == '#form') {
        $('#_form textarea:first').focus();
    }
    $('a[href=#leavecomment]').click(function(){
        $('#_leavecomment textarea:first').focus();
    });
    if (location.hash == '#leavecomment') {
        $('#_leavecomment textarea:first').focus();
    }


    // Форма добавления вопроса
    $('#faq-submit').expandButton({'disabled' : true});
    $('#faq').activeSubmit({
        fields : ['#faq-message', '#faq-name', '#faq-email'],
        submit : '#faq-submit'
    });

    // Форма Контакты
    $('#contact-submit').expandButton({'disabled' : true});
    $('#contact').activeSubmit({
        fields : ['#contact-message', '#contact-name', '#contact-email'],
        submit : '#contact-submit'
    });


    // Табы на странице с описанием товара
    $('.description-gallery').tabs(
        '.description-gallery .images img',
        {
            current : 'active',
            tabs : 'li'
        }
    );    
    
    // Стилизация кнопок
    $('#form-comment-submit, #form-faq-submit, #form-contact-submit').expandButton({
        'disabled' : true
    });
    $('#dropshipping-about-join').expandButton({
        'disabled' : false
    });

    // Активация кнопки submit в форме с полями or
    $.fn.formOr = function(button){
        var form = this;
        $('.form-required-or', this).bind('keyup', activeButton);
        function activeButton() {
            var complete = false;
            if ($(button).size()) {
                $('.form-required-or', form).each(function(){
                    if ($("input", this).val() || $("textarea", this).val()) {
                        complete = true;
                    }
                });
                complete ? $(button, form).expandButton('enable') : $(button, form).expandButton('disable');
            }
        }
        activeButton();
    }
    /*$('#actions-quest').formOr('#actions-quest-submit');
    $('#actions-mention').formOr('#actions-mention-submit');*/
    $('#form-comment').formOr('#form-comment-submit');
    $('#form-faq').formOr('#form-faq-submit');
    $('#form-contact').formOr('#form-contact-submit');

    // переключение сертификатов
    $('.certificates-gallery').each(function(gi, go){
        var galery  = $(this);
        var pages   = $(this).find('.certificates-gallery-page');
        var height  = 0;

        // get max height of pages in gallery
        pages.each(function(pi, po){
            $(this).css('border','1px solid #fff');
            height = Math.max(height, $(this).outerHeight());
        });

        // place only pages in gallery, without any other html
        galery.html(pages.height(height-2)).css('overflow', 'hidden');

        // more then 1 page
        if (1 < pages.length) {
            // create gallery navi and append to gallery
            var galery_navi = $('<ul/>').addClass('certificates-gallery-navi').addClass('pages-numbers').appendTo(galery);

            // create navi items
            pages.each(function(pi, po){
                galery_navi.append(
                    $('<li/>').html(
                        $('<a/>')
                            .attr('index', pi)
                            .attr('href', '#' + (pi + 1))
                            .text(pi + 1)
                            .click(function(e){
                                e.preventDefault();
                                if ($(this).parents('li').hasClass('active')) {return;}
                                if (!pages.eq($(this).attr('index')).length) {return;}
                                galery_navi.find('li').removeClass('active');
                                $(this).parents('li').addClass('active');
                                pages.hide().eq($(this).attr('index')).show();
                            })
                    )
                );
            });

            // reset gallery height
            height += galery_navi.outerHeight();

            // simulate 1-st page click
            galery_navi.find('a:first').click();
        }

        // set gallery height
        galery.height(height);
    });
    if (window.location.hash) {
        var certhash    = window.location.hash.replace('#','').split(':');
        var certid      = 'certificate-' + certhash[0];
        var certnum     = certhash[1] ? parseInt(certhash[1]) - 1 : 0;
        if ($('#' + certid).length) {
            $(document).scrollTop( $('#' + certid).offset().top );
            if ($('#' + certid).find('.certificates-gallery-navi').find('a').eq(certnum).length) {
                $('#' + certid).find('.certificates-gallery-navi').find('a').eq(certnum).click();
            }
        }
    }
});
