$(document).ready(function(){  
    toefl_score(); 
    $('#id_toefl_score')[0].selectedIndex = 0;      
    $("#lexile_help").dialog({title:'Lexile Measures',
			      buttons: {
			      OK: function() {
				  $(this).dialog('close');
			      }},
			      autoOpen:false});
    $("#lexile_help_link").click(function() {
	$("#lexile_help").dialog('open');
    });
});

function toefl_score() {
    $("#id_toefl_score").change(function () {
	    $.post("/toefl/convert/", 
	        {'toefl_score': this.value}, 
	        function(data) {
	            $("#lexile_score").html(data.lexile + "L");
		    if(data.lexile > 100){
			var min = data.lexile-100;
			var max = data.lexile+50;
			var fab_link = '/fab/?lexile_m='+data.lexile;
			$("#lexile_min").html(min + "L");
			$("#lexile_max").html(max+"L");
			$('#find_books_link').attr('href', fab_link);
			$('#find_books_link').show();
		    } else 
		    {
		        $("#lexile_min").html(0);
			$("#lexile_max").html(0);		    
		    }
	            map(data.lexile);		
	        }, 
	        'json');
    })
}

function map(lex) {    
    var b = 432.2432;
    var m = -0.2108;
    var top = m*lex + b;

    $("#graph_overlay").css('top',top + "px");
    $("#graph_overlay").show();
}

