var fix_interval = null;

$(function(){
    if (!$.browser.opera && !$.browser.safari && !$.browser.msie) {
	   fix_fontsize_init('12px/12px Tahoma, "DejaVu Sans", Geneva, sans-serif', 12);
    }

    check_fontname();
});

function fix_fontsize_init(font, expectedSize, $fix)
{
    if (typeof font == 'undefined' || font == '')
        font = 'normal 16px/16px Arial, sans-serif';
    if (typeof expectedSize == 'undefined' || expectedSize == 0)
        expectedSize = 16;
    if (typeof $fix == 'undefined' || !$fix.length)
        $fix = $('body');

    $fix.prepend('<div id="fontsize_tester">A</div>');

    var $tester = $('#fontsize_tester');
    var $topmenu = $('#header_top .submenu div').eq(0);

    $tester.css({
        font:        font,
        position:   'absolute',
        left:       '-999em'
    });

    _fix_fontsize(expectedSize, $fix, $tester, $topmenu);

    fix_interval = setInterval(function(){
        _fix_fontsize(expectedSize, $fix, $tester, $topmenu);
    }, 1000);
}

function _fix_fontsize(expectedSize, $fix, $tester, $topmenu)
{
    var size = Math.round(expectedSize * expectedSize / $tester.height());

    $fix.css({ fontSize: size + 'px' });

    if ($topmenu.height() > Math.ceil($tester.height() * 1.58333))
        $('#header_top .submenu div').css({ paddingRight: '2.5em', paddingLeft: '1em'});
    else
        $('#header_top .submenu div').css({ paddingRight: '4.66em', paddingLeft: '1.66em' });
}

function check_fontname()
{
    var testsize = '4em';
    var tstr = 'àáâãäå¸æçèéêëìíîïðñòóôõö÷øùúûüýÿþ';

    $('body').append('<div id="__fonttable_tester__"></div>');
    var $tester = $('#__fonttable_tester__');

    $tester.css({
        position: 'absolute',
        top: 0,
        left: '-9999em',
        visibility: 'hidden',
        whiteSpace: 'nowrap',
        fontSize: testsize,
        lineHeight: testsize,
        letterSpacing: 'normal',
        fontWeight: 'normal',
        fontStyle: 'normal',
        height: testsize
    });

    $tester.text(tstr);

    var koeff = $tester.width() / tstr.length / $tester.height();

    $tester.remove();

    if ( koeff >= 0.15 && $('#header_top #lang').hasClass('ru') ) {
	$('body').addClass('widefont');
	$('.sidebar-banner #supporten').attr('src','/i/intinfo/supportw_en.jpg');
	$('.sidebar-banner #supportru').attr('src','/i/intinfo/supportw_ru.jpg');
	$('.sidebar-banner #nitiken').attr('src','/i/intinfo/nitik_bookw_en.jpg');
	$('.sidebar-banner #nitikru').attr('src','/i/intinfo/nitik_bookw_ru.jpg');
    }
}


