$(function(){
    var timeoutId;
    $('div.language div.select').click(function(){
        if ($(this).next().hasClass('open'))
        {
            $(this).next().removeClass('open');
        }
        else
        {
            $(this).next().addClass('open');
        }
    }).next().andSelf().hover(function(){
        clearTimeout(timeoutId);
    },
    function(){
        var obj = this;
        timeoutId = setTimeout(function(){$(obj).removeClass('open');}, 1000);
    });
    $('div.language li').hover(function(){
        $(this).addClass('hover');
    },
    function(){
        $(this).removeClass('hover');
    }).click(function(){
        var frm = $(this).parent().prev().val($(this).attr('class').substr(0,3)).parent();
        frm.attr('action', frm.attr('action') + 'lang=' + $(this).attr('val')); 
        frm.submit();
    });
    
    $('#modalDialog').dialog({
        autoOpen: false,
        draggable: true,
        resizable: false,
        modal: true,
        show: 'fade',
        width: 470,
        minHeight: 300
    });
    
    $('.mail').click(function(event){
        var parser = function(data){
            $('#modalDialog').dialog('option', {
                'title': $(data).find('title').text(),
                buttons: {
                    "Отправить": function() {
                        var $form = $(this).find('form');  
                        $(this).dialog('option', 'title', 'Подождите пожалуйста...');
                        $.ajax({
                            type: $form.attr('method'),
                            url: $form.attr('action'),
                            dataType: "xml",
                            data: $form.serialize(),
                            success : parser
                        }); 
                    },
                    "Закрыть": function() { $(this).dialog("close"); }
                }
            });
            $('#modalDialog').html($(data).find('message').text()).dialog('open');
            $('#modalDialog').parent().find('.ui-dialog-buttonpane').find('button:first').trigger('focus');
        };
        
        $.ajax({
            type: "POST",
            url: "/mail.php",
            dataType: "xml",
            success: parser
        });
    });
    
    $('<div class="bgBottom centralMenu' + nextId(mrcore_image) + '"></div>').prependTo($('.centralMenu'));
    $('.centralMenu').find('span').hide().filter('.text' + mrcore_image).show();
    
    setInterval(imagesChange, 7000);
});

function nextId(id, max)
{
    if (typeof(max) == 'undefined')
    {max = mrcore_images_max;}
    return (id + 1 >= max)? 0 : id + 1;
}

function prevId(id, max)
{
    if (typeof(max) == 'undefined')
    {max = mrcore_images_max;}
    return (id <= 0)? max-1 : id - 1;
}

function imagesChange(){
    var $top = $('.bgTop');
    var $bottom = $('.bgBottom');
    
    $top.add('.centralMenu span.text' + mrcore_text).fadeOut(2000);
    $bottom.add('.centralMenu span.text' + nextId(mrcore_text, mrcore_text_max)).fadeIn(2000);
    mrcore_text = nextId(mrcore_text, mrcore_text_max);
    mrcore_image = nextId(mrcore_image);
    $bottom.queue(function(){
        $bottom.removeClass('centralMenu' + mrcore_image).addClass('centralMenu' + nextId(mrcore_image)).hide();
        $bottom.dequeue();
    });
    $top.queue(function(){
        $top.removeClass('centralMenu' + prevId(mrcore_image)).addClass('centralMenu' + mrcore_image).show();
        $top.dequeue();
    });    
}
