var hints;
var changed;


$(document).ready(function(){
    /* Set up field hints */
    hints = {
        title:      "Enter a title to identify this card - your name and "  +
                    "the name of your lens maybe? Enter a space to leave "  +
                    "it blank",
        eye:        "Measure the distance between the centers of your "     +
                    "eyes in cm.",
        arm:        "Extend your arm holding a card vertically between "    +
                    "your thumb and forefinger. Measure the distance in "   +
                    "cm between your eye and the card.",
        dis:        "A comma separated list of distances you want for "     +
                    "rangefinder scale. These should coincide with the "    +
                    "scale on your lens. Infinity will be added "           +
                    "automatically. Distances shorter than your arm will "  +
                    "not work.",
        units:      "Enter the units used for the distances on your lens",
        font:       "Enter the font size for distance indexes",
        foclen:     "Enter the focal length of your lens in mm.",
        maxf:       "Enter the widest (small no.) aperture of your lens.",
        minf:       "Enter the narrowest (large no.) aperture of your lens.",
        film:       "Select your film size: Minox, 110, 135 (35mm), etc.",
        coc:        "Optionally, you may enter a custom circle of "         +
                    "confusion in mm if you don't want to use the d/1500 "  +
                    "values for common film sizes from above.",
        hfd:        "Check here to generate a card with a hyperfocal "      +
                    "distance chart specific to your lens"
    }

    changed = new Object;

    for ( var f in hints ){
        changed[f] = false;
        $('#' + f).focus(function(){
            name = $(this).attr('id');
            $('#hint').html(hints[name]);
            if (!changed[name] && name != 'hfd'){
                $(this).val('');
                changed[name] = true;
            }
        });
        $('#' + f).blur(function(){
            $('#hint').empty();
        });
        $('#' + f).change(change_card);
    }

    $('#hfd').click(function(){$('#hfdinfo').slideToggle();});

    $('#submit').hide();
    $('#hfdinfo').hide();
    $('#hints').hide();
    $('#hint').show();
    $('#card_dl_t').show();
    $('#card_dl').show();

    change_card();


});

function change_card(){
    var src = 'app/?width=300';
    var uri = 'app/?width=2100';
    for (var f in hints){
       if(f == 'hfd' && !$('#hfd').attr('checked')){
         continue;
       }
       src += '&' + f + '=' + $('#' + f).val();
       uri += '&' + f + '=' + $('#' + f).val();
    }
    $('#card_prev').attr('src', src);
    $('#card_dl').attr('href', uri);
}

