/* pangram.js
 * Pangram-maker web application.
 * Depends on: jQuery 1.3.2
 * Copyright 2009 Jason Stitt. All rights reserved.
 */

$(function() {
    $('#main-app').show();
    updateLetters();
    addKnownPangramButtons();
    randomizeTagline();
    $('#pangram').bind('keyup', updateLetters);
    $('#pangram-tagline').bind('mouseover', randomizeTagline);
});

const LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const TAGLINES = [
    "boxes five jolly quick hot wizards",
    "finds quartz glyph in jowl of vexed cab",
    "quit job with galvanized xylophones cafe",
    "to quiz jocks, vegans before wooly lynx death",
    "requires fez worn by jailed TV box chump",
    "undervotes oxalic jolly quiz show fob",
    "has zootaxy covered in squawk job flow",
    "stiffs exoenzyme by withholding quick verjuice",
    "bejewels feverish zygotic quadruplex",
    ];

/*
 * Randomly change the tagline in the site title
 */
function randomizeTagline() {
    if(TAGLINES.length > 1) {
        var current = $('#tagline').text();
        var line = current;
        while(line == current) {
            var i = Math.floor(Math.random() * TAGLINES.length);
            line = TAGLINES[i];
        }
        $('#pangram-tagline').text(line);
    }
}

/* 
 * Add a link/button to the beginning of each list item in the list of
 * known pangrams that places that pangram into the main text box, replacing
 * its existing contents.
 */
function addKnownPangramButtons() {
    $('#known-pangrams li').each(function() {
        var item = $(this);
        var button = $('<a href="#" class="btn">(Try it)</a>');
        var text = item.text();
        item.prepend(button);
        button.bind('click', function() {
            $('#pangram').val(text);
            updateLetters();
            return false;
        });
    });
}

/*
 * Count the uses of each letter of the English alphabet in the main text
 * box, then update the 'remaining' and 'overused' indicators.
 */ 
function updateLetters() {
    var i;
    
    /* Count letters used */
    var usage = new Object();
    for(i = 0; i < LETTERS.length; i++) {
        usage[LETTERS[i]] = 0;
    }
    var text = $('#pangram').val();
    for(i = 0; i < text.length; i++) {
        usage[text.charAt(i).toUpperCase()]++;
    }
    
    /* For each letter, update the remaining & overused indicator */
    var anyRemaining = false;
    var anyOverused = false;
    for(i = 0; i < LETTERS.length; i++) {
        var c = LETTERS[i];
        var remaining = $("#letter-" + c);
        var overused = $("#over-" + c);
        var count = usage[c];
        if(0 == count) {
            anyRemaining = true;
            remaining.show();
            overused.hide();
        } else {
            remaining.hide();
            if(1 == count) {
                overused.hide();
            } else {
                anyOverused = true;
                overused.text(c + ' ' + count).show();
            }
        }
    }
    
    /* Show/hide entire divs so that headers don't show up if there's nothing
    under them */
    $('#remaining').toggle(anyRemaining);
    $('#overused').toggle(anyOverused);
    $('#perfect').toggle(!anyOverused && !anyRemaining);
}

