$(document).ready(function() {
  window.calc_ready = true;
  $(".section").corner(
      {
      tl: { radius: 8 },
      tr: { radius: 8 },
      bl: { radius: 8 },
      br: { radius: 8 }
      }
    );
  $(".formula_wrap").corner(
      {
      tl: { radius: 5 },
      tr: { radius: 5 },
      bl: { radius: 5 },
      br: { radius: 5 }
      }
    );
  $(".section h2").corner(
      {
      tl: { radius: 8 },
      tr: { radius: 8 },
      bl: false,
      br: false
      }
    );
    function hide_options() {
  $(".options_wrap div.formula_wrap, .options_wrap input, .options_wrap label").hide();
  }
  
  
    $('#function_list_a').bind('click',function() {
      $('#function_list').slideToggle("slow");
    });
    
    $("input").focus(function () {
         $(this).addClass("highlight");
          if(this.value == this.defaultValue){
            this.select();
          }
    });
    $("input").blur(function () {
         $(this).removeClass("highlight");
    });
    
    // Display options for radio buttons:
    change_options(false);
$("input[name='radio']").change(function(){
  change_options(false); 
});
$("input[name='radio']").click(function(){
  change_options(false); 
});

$("#submit").click(function() {
  if(window.calc_ready==true) {
  $("#return").slideDown();
  radio_value = $("input[@name='radio']:checked").val();
  if(radio_value == "plot" || radio_value == "integrate_def" || radio_value =="plot3d") {
    return_height(620);
  } else {
  return_height(220);
  }
  window.calc_ready = false;
  $("#patience").hide();
  setTimeout("window.calc_ready=true", 5000);
  }
  else {
  window.calc_ready = false;
  setTimeout("window.calc_ready=true", 5000);
  $("#patience").show();
  return false;
  }

});




function return_height(height) {
   heightplus = height + 50;
      $("#return_frame").animate({ 
        height: height
      }, 1500 );
       $("#return").animate({ 
        height: heightplus
      }, 1500 );
   


}    

function change_options(demo) {
hide_options();
  radio_value = $("input[@name='radio']:checked").val();
  switch(radio_value) {
    case "evaluate":
      $("#i_x_min").show();
      $("#l_x_min").show();
      if(!demo)
      $("#i_x_min").val("0");
      $("#l_x_min").html("Evaluate at <i>x=</i>");      
    break;
    case "integrate_def":
      $("#i_x_min").show();
      $("#l_x_min").show();
      $("#i_x_max").show();
      $("#l_x_max").show();
      if(!demo)
      $("#i_x_min").val("-10");
      $("#l_x_min").html("Integrate between");
      if(!demo)
      $("#i_x_max").val("10");
      $("#l_x_max").html("and");     
    break;
    case "differentiate":
      $("#i_n").show();
      $("#l_n").show();
      if(!demo)
      $("#i_n").val("1");
      $("#l_n").html("Differentiate how many times?");      
    break;
    case "plot":
      $("#i_x_min").show();
      $("#l_x_min").show();
      $("#i_x_max").show();
      $("#l_x_max").show();
      if(!demo)
      $("#i_x_min").val("-3");
      $("#l_x_min").html("Plot between <i>x=</i>");
      if(!demo)
      $("#i_x_max").val("3");
      $("#l_x_max").html("and <i>x=</i>");     
    break;
    case "solve":
      $("div.formula_wrap").show();
      $("#formula2").show();
      $("#l_formula2").show();
      $("#l_formula2").html("Solve equal to:");  
    break;
    case "plot3d":
      $("#i_x_min").show();
      $("#l_x_min").show();
      $("#i_x_max").show();
      $("#l_x_max").show();
      $("#i_y_min").show();
      $("#l_y_min").show();
      $("#i_y_max").show();
      $("#l_y_max").show();
      if(!demo)
      $("#i_x_min").val("-3");
      $("#l_x_min").html("Plot between <i>x=</i>");
      if(!demo)
      $("#i_x_max").val("3");
      $("#l_x_max").html("and <i>x=</i>");     
      if(!demo)
      $("#i_y_min").val("-3");
      $("#l_y_min").html("And between <i>y=</i>");
      if(!demo)
      $("#i_y_max").val("3");
      $("#l_y_max").html("and <i>y=</i>");     
    break;
    case "limit":
      $("#i_x_min").show();
      $("#l_x_min").show();   
      if(!demo)
      $("#i_x_min").val("0");
      $("#l_x_min").html("Take the limit as <i>x</i> tends to:");   
    break;
    case "powerseries":
      $("#i_x_min").show();
      $("#l_x_min").show();   
      if(!demo)
      $("#i_x_min").val("0");
      $("#l_x_min").html("Find the taylor series about x=");   
    break;

    }
}

function show_example(formula1, radio, formula2, n, x_min, x_max, y_min, y_max) {
  $("#formula1").val(formula1);
  $("#formula2").val(formula2);
  $("#i_n").val(n);
  $("#i_x_min").val(x_min);
  $("#i_x_max").val(x_max);
  $("#i_y_min").val(y_min);
  $("#i_y_max").val(y_max);
  $(radio).attr("checked", true);
  change_options(true);
  $("#submit").click();
}

function do_example(example_number) {
  switch(example_number) {
    case "1":
      show_example("sin(x)","#r6","","","-2*PI","2*PI", "", "");            
    break;
    case "2":
      show_example("E^(-x^2)","#r3","","","-INF","INF", "", "");            
    break;
    case "3":
      show_example("(a+x)/(b+x^2)","#r4","","1","","", "", "");            
    break;
    case "4":
      show_example("E^(I*PI*x)+1","#r1","","","1","", "", "");            
    break;
    case "5":
      show_example("x^3-6*x^2+11*x-6","#r7","0","","","", "", "");            
    break;
    case "6":
      show_example("(x+1)^5","#r5","","","","", "", "");            
    break;
    case "7":
      show_example("1/(1+x^2)","#r2","","","","", "", "");            
    break;
    case "8":
      show_example("exp(sin(x^2+y^2))","#r8","","","-1","1","-1","1");            
    break;
    case "9":
      show_example("sin(x)*cos(2*x)","#r3","","","0","PI", "", "");         
    break;
    case "10":
      show_example("(1+1/x)^x","#r9","","","INF","", "", "");         
    break;
    case "11":
      show_example("sin(a*x)/x","#r10","","","0","", "", "");         
    break;
    
  }
}

$("#examples a").each(function() {
  $(this).click(function(){
    do_example($(this).attr("title"));
  });
}
);
    
});
