$(function () {
    font.setup();
    fontcheck();
    
    $('.hidden').hide();
    $('a.reveal').click(function () {
        $(this.hash).slideToggle(250);
    });
    
    // select nav
    var url = window.location.pathname.substring(1) || '/';
    $('#navigation a[href$="' + url + '"]').attr('class', 'selected');
    $('#render').click(runComparison);
});

function fontcheck() {
    $('span.font.check').each(function () {
        var $font = $(this);
        var f = $font.html();
        
        if (!font.isInstalled(f)) {
            $font.addClass('missing');
        }
    });
}

function runComparison() {
    var i, 
        ff = $('#fonts').val(),
        result = $('#result'),
        styleText = '',
        html = [],
        c = $('input[name=case]:checked').val(),
        text = $('input[name=text]').val();
        tt = (c != 'normal' ? '; text-transform: ' + c : '');
    
    result.empty();
    $('#renderStyle').remove();
    ff = ff.replace(/^\s*font\-family:\s*/, '').replace(/;$/, '').replace(/,\s+/g, ',').replace(/\n/g, ',').split(',');
    
    for (i = 0; i < ff.length; i++) {
        styleText += 'span.f' + i + ' { font-family: ' + ff[i] + ', "Comic Sans MS"; ' + tt + '} ' + "\n";
    }
    $('head').append('<style id="renderStyle">' + styleText + '</style>');
    
    
    for (i = 0; i < ff.length; i++) {
        html.push('<p><span class="f' + i + '">' + text + '</span> <span class="font check">' + ff[i] + '</span></p>');
    }
    
    result.append(html.join(''));
    fontcheck();
    
    return false;
}