$(function() {
    // Document is ready

    $('#menu>li.has_submenu').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    var fontSize = parseInt($.cookie('font-size'));

    if (fontSize > 0) {
        $('.article .text').css('font-size', fontSize + 'px');
    }

    var text = $(".marquee").html();

    $(".marquee").html('<div>' + text + '</div><div>' + text + '</div>');

    textWidth =  $(".marquee div").width();

    firstDiv = $('.marquee div:first-child')[0];
    secondDiv = $('.marquee div:last-child')[0];

    firstLeftPosition = 0;
    lastLeftPosition = textWidth;

    setInterval('moveMarquee()', 25);

    $('body').click(
        function () {
            $('#popup_calendar').hide();
        }
    );

    $('#popup_calendar').click(
        function(event) {
            event.stopPropagation();
        }
    );

    $('td.search a.archive').click(
        function (event) {
            event.stopPropagation();
            popupCalendar.redraw();
            $('#popup_calendar').toggle();
            return false;
        }
    );

    // Conferences
    $('#all_answers a, #all_questions a').click(Conferences.switchTab);
    $('#add_conference_question form').submit(Conferences.checkForm);
});


var Conferences = {};
Conferences.switchTab = function() {
    var $parent = $(this).parent();
    Conferences.makeInactive($('#conference_tabs_wrapper div.active'));
    Conferences.makeActive($parent);
    if ( 'all_answers' == $parent.attr('id') ) {
        $('#all_questions_content').addClass('hidden');
        $('#all_answers_content').removeClass('hidden');
    }
    else {
        $('#all_answers_content').addClass('hidden');
        $('#all_questions_content').removeClass('hidden');
    }
    return false;
}

Conferences.makeActive = function($el) {
    var cnt = $('span.count:first', $el).html();
    var title = $('a:first', $el).html();
    $el.addClass('active').html(title + ' <span class="count">'+ cnt +'</span>');
}

Conferences.makeInactive = function($el) {
    var cnt = $('span.count:first', $el).html();
    var title = $el.html().replace(/\s+<span.*/i, '').replace(/^\s*/, '');
    $el.removeClass('active').html('<a href="#">' + title + '</a> <span class="count">'+ cnt +'</span>');
    $('a:first', $el).click(Conferences.switchTab);
}

Conferences.checkForm = function() {
    var frm = this;
    if (!frm['name'].value.match(/[^\s]/)) {
        alert('Имя пользователя не может быть пустым');
        frm['name'].focus();
        frm['name'].select();
        return false;
    }

    if (!frm['question'].value.match(/[^\s]/)) {
        alert('Текст вопроса не может быть пустым');
        frm['question'].focus();
        frm['question'].select();
        return false;
    }

    if ((frm['email'].value.match(/[^\s]/)) &&
        (!frm['email'].value.match(/^[a-z0-9_\-\.]+@([a-z0-9_\-]+\.)+[a-z]{2,5}$/i))) {
        alert('Неправильный ввод в поле \"E-mail\".');
        frm['email'].focus();
        frm['email'].select();
        return false;
    }

    return true;
}


var firstLeftPosition;
var lastLeftPosition;

var firstDiv;
var secondDiv;

var textWidth;

function moveMarquee() {
    firstDiv.style.left = firstLeftPosition + 'px';
    secondDiv.style.left = lastLeftPosition  + 'px';

    firstLeftPosition -= 1;
    lastLeftPosition -= 1;

    if (firstLeftPosition <= -textWidth) {
        firstLeftPosition = textWidth;
    }

    if (lastLeftPosition <= -textWidth) {
        lastLeftPosition = textWidth;
    }
}