$(document).ready(function(){

 $("#box .box_panel>input[class=close]").live("click",function(){
   $(this).parent().parent().remove();
   $("#box>div.data").resizeX();
   $("#box>div.data>div.content").resizeY();
 });
 $("#box .box_panel>input[class=resize]").live("click",function(){
   $(this).parent().parent().resizeX();
   $(this).parent().parent().children("div.content").resizeY();
 });
 $("#box .box_panel>input[class=pushleft]").live("click",function(){
   var clone = $("<div>").append($(this).parents("div.data").clone()).html();
   var id = $(this).parents("div.data").prev().attr("id");
   $(this).parents("div.data").fadeOut().remove();
   $("#"+id).before(clone);
 });
 $("#box .box_panel>input[class=pushright]").live("click",function(){
   var clone = $("<div>").append($(this).parents("div.data").clone()).html();
   var id = $(this).parents("div.data").next().attr("id");
   $(this).parents("div.data").fadeOut().remove();
   $("#"+id).after(clone);
 });

 $("#box>div.data").each(function(){
   var box_panel = $("<div>").attr("class","box_panel").append($("<input>").attr("type","button").attr("class","close")).append($("<input>").attr("type","button").attr("class","pushleft")).append($("<input>").attr("type","button").attr("class","pushright"));
   var h = $(window).height();
   $(this).prepend(box_panel);
   $(this).children("div.content").css("height",h - 12);
 });
     
$(".editable>div").live("dblclick",function(){
 var code = $(this).html();
 $(this).before($("<div />").addClass("codeeditbox")
   .append($("<button />").text("del").addClass("deltext"))
   .append($("<button />").text("html").addClass("htmltext"))
   .append($("<button />").text("save").addClass("savetext"))
   .append($("<div />").attr("contenteditable","true").addClass("codefield").html(code))
   );
});

$(".editable button.deltext").live("click",function(event){
  $(this).parent().fadeOut().remove();
  event.preventDefault();
});

$(".editable button.htmltext").live("click",function(event){
  var name = $(this).text();

  if(name == "html"){
   var code = $(this).parent().children("div").html()
   .replace(/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"/g,'')
   .replace(/ xmlns=\"http:\/\/www.w3.org\/1998\/Math\/MathML\"/g,'')
   .replace(/< _moz_dirty=\"\"\/>/g,'')
   .replace(/ _moz_dirty=\"\"/g,'');
   $(this).parent().children("div").empty().text(code);
   $(this).next("button.savetext").attr("disabled","disabled");
   $(this).text("text");
  }
  if(name == "text"){
   var code = $(this).parent().children("div").text().replace(/<math/g,'<math xmlns="http://www.w3.org/1998/Math/MathML"');
   $(this).parent().children("div").empty().html(code);
   $(this).next("button.savetext").removeAttr("disabled");
   $(this).text("html");
  }
  event.preventDefault();
});

$(".editable button.savetext").live("click",function(event){
  var code = $(this).parent().children("div").html()
  .replace(/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"/g,'')
  .replace(/< _moz_dirty=\"\"\/>/g,'')
  .replace(/ _moz_dirty=\"\"/g,'');
  $(this).parent().next("div").html(code);
  $(this).parent().fadeOut().remove();
  event.preventDefault();
});

$(".source>div").live("dblclick",function(){
 $(this).before($("<div />").addClass("commentbox")
   .append($("<button />").text("del").addClass("delcomment"))
   .append($("<button />").text("post").addClass("postcomment"))
   .append($("<div />").attr("contenteditable","true").addClass("commentfield"))
   );
});

$(".source button.delcomment").live("click",function(event){
  $(this).parent().fadeOut().remove();
  event.preventDefault();
});

$(".source button.postcomment").live("click",function(event){
  var url = "/save/comment";
  var text = $(this).parent().next("div").text();
  var info = $(".contenttext input[name='course_id']").val()+", "+$(".contenttext input[name='chapter_id']").val()+", "+$(".contenttext input[name='template_id']").val();
  var comment = $(this).next("div").text();
   $.post(url,{selected_text:text,pageinfo:info,comment:comment});
  $(this).parent().fadeOut().remove();
  event.preventDefault();
});

 $("#menus>ul>li>a").live("click",function(event){
  var href = "/forms/" + $(this).attr("href");
  var klass = $(this).attr("href");
  if(klass == "exit") { 
    $("#menus>div").empty().removeClass().load(href,function(){
      location.reload();
    });
  } else {
      $("#menus>div").empty().removeClass().append($("<p>").addClass("loading")).addClass(klass).toggle().load(href,function(){
      $("#menus>div").removeClass("loading");
    });
  }
  event.preventDefault();
 });

// $("#menus").hover(function(){},function(){
//  $("#menus>div").hide();   
// });
 
/* feedback */

 $(".feedback_button").live("click",function(event){
  var url = "/save/feedback";
  var temp = $(this).parents("div.data").attr("id");
  var text = $("#"+temp).find("textarea").val();
  $("#menus>div").load(url,{feedback:text},function(data){
    $("#"+temp).find(".feedback_button").after($("<div>").attr("class","temp_feedback").append(data));
    $("#"+temp).find(".temp_feedback").delay("slow").fadeOut("slow");
  });
  event.preventDefault();
 });
 
 /* survey */

 $("#survey_button").live("click",function(event){
  var url = "/save/survey";
  var num = $("#survey input[name='this_survey_number']").val();
  var val = $("#survey input[name='survey_answer']:checked").val();
  $("#survey").load(url,{this_survey_number:num,survey_answer:val});
  event.preventDefault();
 });

 /* crossdomain link*/
 
 $("#nav a").live("click",function(event){
  var href = $(this).attr("href");
  var temp = "id" + Math.floor(Math.random() * 1000000000);
  var box_panel = $("<div>").attr("class","box_panel").append($("<input>").attr("type","button").attr("class","close")).append($("<input>").attr("type","button").attr("class","pushleft")).append($("<input>").attr("type","button").attr("class","pushright"));
  $("#box").prepend($("<div>").attr("class","data").attr("id",temp).prepend(box_panel).fadeIn().append($("<div>").attr("class","content").append($("<p>").addClass("loading")).load(href)));
  $("#box>div.data").resizeX();
  $("#box>div.data>div.content").resizeY();
  event.preventDefault();
 });
 
 /* check data at login/register/recover password */
 

 $("#check input[type='text'],#check input[type='password']").live("blur",function(){
  var url = "/check/"+$(this).attr('name');
  var val = $(this).val();
  $(this).next().empty().load(url,{field:val});
  $("#regbutton").next().empty();
 });

 $("#check a").live("click",function(event){
  var href = "/forms/" + $(this).attr("href");
  //$(this).parents(".login").empty().addClass("loading");
  $(this).parents(".login").load(href);
  event.preventDefault();
 });

 $("#regbutton").live("click",function(event){
  var url = "/forms/process";
  var name = $("#check input[name='reguser']").val();
  var pass = $("#check input[name='pass']").val();
  var mail = $("#check input[name='email']").val();
  $(this).next().load(url,{user:name,pass:pass,email:mail,subjoin:1},function(data){
   if(data == "Validated!"){
	window.location = "/";
   }else{}
  });
  event.preventDefault();
  });
  
 $("#forgotbutton").live("click",function(event){
  var url = "/forms/process";
  var name = $("#check input[name='user']").val();
  $(this).next().load(url,{user:name,subforgot:1},function(data){
   if(data == "Validated!"){
	$(">div:first-child").empty().append("New password is generated for you at your request, please check your email!");
   }else{}
  });
  event.preventDefault();
  });
 
 /* edit user info form */

 $("#save input,#save textarea,#save select").live("change",function(){
  var url = "/save/"+$(this).attr('name');
  var val = $(this).val();
  $(this).next().empty().load(url,{field:val});

 });

  /* courses accordion + load user answers*/

 $("#box>div.data .accordion a").live("click",function(event){
  var href = $(this).attr("href");
  $(this).parents("div.data").find("div.contenttext").empty().append($("<p>").addClass("loading"));
  var temp = $(this).parents("div.data").attr("id");
  $("#"+temp).children("div.content").load(href,function(){
   var klass = $("#"+temp).find(".contenttext").children("div").attr("class");
   if(klass == "source"){


    $("#"+temp).find("div.source").attr("title","double-click to add comment");
     
    // $("#"+temp+" div.question").each(function(){
    //  var box_solution = $("<div>").attr("class","div_solution_chat").append($("<h3>").attr("class","toggle_solution_chat").text("discuss solutions"))
    //                                                                 .append($("<div>").css("display","none")
    //                                                                                   .attr("class","solution_chat")
    //                                                                                   .append($("<div>").attr("class","solution_chat_body"))
    //                                                                                   .append($("<div>").attr("contenteditable","true").text("type"))
    //                                                                                   .append($("<button>").text("save").attr("class","save_solution")));


    // $(this).prepend($("<div />").addClass("problemrating"));
    //  var course_id = $("#"+temp).find("input[name='course_id']").val();
    //  var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
    //  var template_id = $("#"+temp).find("input[name='template_id']").val();
    //  var num = $("#"+temp).find("div.question").index($(this)) + 1;
    //     $.post("/check/rating",{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num},function(data){
    //      $("#"+temp).find("div.problemrating").eq(num-1).html(data);
    //      $("input.star").rating();
    //     });


    //  $(this).after(box_solution);
    // });
     
     $("#"+temp).find("input[name='answer']").each(function(){
      $(this).parent().append($("<span>").addClass("Send"));
      $(this).parent().css("text-align","center");
      var course_id = $("#"+temp).find("input[name='course_id']").val();
      var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
      var template_id = $("#"+temp).find("input[name='template_id']").val();
      var num = $("#"+temp).find("div.question").index($(this).parent().parent()) + 1;
      var subnum = $("#"+temp).find("input[name='answer']").index(this) + 1;
      $.post("/check/answer",{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,answer_number:subnum},function(data){
       $("#"+temp).find("input[name='answer']").eq(subnum-1).val(data);
      });
    });
   }
   if(klass == "editable"){
    $("#"+temp).prepend($("<button />").text("save changes").addClass("savechanges"));
     $("#"+temp).find("input[name='answer']").each(function(){
      $(this).parent().append($("<span>").addClass("Send"));
      $(this).parent().css("text-align","center");
      var course_id = $("#"+temp).find("input[name='course_id']").val();
      var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
      var template_id = $("#"+temp).find("input[name='template_id']").val();
      var num = $("#"+temp).find("div.question").index($(this).parent().parent()) + 1;
      var subnum = $("#"+temp).find("input[name='answer']").index(this) + 1;
      $.post("/check/answer",{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,answer_number:subnum},function(data){
       $("#"+temp).find("input[name='answer']").eq(subnum-1).val(data);
      });
     });
   }	
  },"xml")
  event.preventDefault();
 });

$("h3.toggle_solution_chat").live("click",function(event){
 var temp = $(this).parents("div.data").attr("id");
 var url = "/check/studentcomment";
 var course_id = $("#"+temp).find("input[name='course_id']").val();
 var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
 var template_id = $("#"+temp).find("input[name='template_id']").val();
 var num = $("#"+temp).find("div.question").index($(this).parents("div.div_solution_chat").prev()) + 1;
 $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num},function(data){
  $("#"+temp).find("div.solution_chat_body").html(data);
 });
 $(this).next("div.solution_chat").toggle();
 event.preventDefault();
});

$("button.save_solution").live("click",function(event){
 var temp = $(this).parents("div.data").attr("id");
 var url = "/save/studentcomment";
 var course_id = $("#"+temp).find("input[name='course_id']").val();
 var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
 var template_id = $("#"+temp).find("input[name='template_id']").val();
 var num = $("#"+temp).find("div.question").index($(this).parents("div.div_solution_chat").prev()) + 1;
 var student_comment = $(this).prev("div").html();
 alert(student_comment);
 $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,student_comment:student_comment},function(data){
  alert(data);
 });
 event.preventDefault();
});




$("#box>div.data button.savechanges").live("click",function(event){
  var temp = $(this).parents("div.data").attr("id");
  var url = "/save/content";
  var course_id = $("#"+temp).find("input[name='course_id']").val();
  var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
  var template_id = $("#"+temp).find("input[name='template_id']").val();
  var content = $("#"+temp).find(".editable").html().replace(/ xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"/g,'');
  $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,content:content});
  event.preventDefault();
});
 
$('#box>div.data button.clearanswer').live("click",function(){
 $(this).parent().children("div.answerfield").empty().attr("contenteditable","true").html(" ");
});
 
$('#box>div.data div.answerfield').live("paste",function(){
 $(this).one("keyup",function(){
  var code = $(this).text().replace(/(>)\s+/g,'>')
  .replace(/&PlusMinus;/g,'&#x000B1;')
  .replace(/&times;/g,'&#x000D7;')
  .replace(/&lowast;/g,'&#x02217;')
  .replace(/&divide;/g,'&#x000F7;')
  .replace(/&CirclePlus;/g,'&#x02295;')
  .replace(/&CircleTimes;/g,'&#x02297;')
  .replace(/&plus;/g,'+')
  .replace(/&minus;/g,'&#x02212;')
  .replace(/&sdot;/g,'&#x022C5;')
  .replace(/&bull;/g,'&#x02022;')
  .replace(/&lang;/g,'&#x02329;')
  .replace(/&rang;/g,'&#x0232A;')
  .replace(/&harr;/g,'&#x02194;')
  .replace(/&rarr;/g,'&#x02192;')
  .replace(/&larr;/g,'&#x02190;')
  .replace(/&varr;/g,'&#x02195;')
  .replace(/&uarr;/g,'&#x02191;')
  .replace(/&darr;/g,'&#x02193;')
  .replace(/&hArr;/g,'&#x021D4;')
  .replace(/&rArr;/g,'&#x021D2;')
  .replace(/&lArr;/g,'&#x021D0;')
  .replace(/&vArr;/g,'&#x021D5;')
  .replace(/&uArr;/g,'&#x021D1;')
  .replace(/&dArr;/g,'&#x021D3;')
  .replace(/&nearr;/g,'&#x021D7;')
  .replace(/&swarr;/g,'&#x021D9;')
  .replace(/&searr;/g,'&#x021D8;')
  .replace(/&nwarr;/g,'&#x021D6;')
  .replace(/&rlarr;/g,'&#x021C4;')
  .replace(/&rlhar;/g,'&#x021CC;')
  .replace(/&map;/g,'&#x02905;')
  .replace(/&alpha;/g,'&#x003B1;')
  .replace(/&beta;/g,'&#x003B2;')
  .replace(/&gamma;/g,'&#x003B3;')

  .replace(/&delta;/g,'&#x003B4;')
  .replace(/&epsiv;/g,'&#x003B5;')
  .replace(/&zeta;/g,'&#x003B6;')
  .replace(/&eta;/g,'&#x003B7;')
  .replace(/&theta;/g,'&#x003B8;')
  .replace(/&iota;/g,'&#x003D9;')
  .replace(/&kappa;/g,'&#x003BA;')
  .replace(/&lambda;/g,'&#x003BB;')
  .replace(/&mu;/g,'&#x003BC;')
  .replace(/&nu;/g,'&#x003BD;')
  .replace(/&xi;/g,'&#x003BE;')
  .replace(/&ogr;/g,'&#959;')
  .replace(/&pi;/g,'&#x003C0;')
  .replace(/&rho;/g,'&#x003C1;')
  .replace(/&sigmav;/g,'&#x003C2;')
  .replace(/&sigma;/g,'&#x003C3;')
  .replace(/&tau;/g,'&#x003C4;')
  .replace(/&upsilon;/g,'&#x003C5;')

  .replace(/&phi;/g,'&#x003C6;')
  .replace(/&phiv;/g,'&#x003C6;')
  .replace(/&chi;/g,'&#x003C7;')
  .replace(/&psi;/g,'&#x003C9;')
  .replace(/&omega;/g,'&#x003C9;')
  .replace(/&Agr;/g,'&#913;')
  .replace(/&Bgr;/g,'&#914;')
  .replace(/&Gamma;/g,'&#x00393;')
  .replace(/&Delta;/g,'&#x00394;')
  .replace(/&Egr;/g,'&#917;')
  .replace(/&Zgr;/g,'&#918;')
  .replace(/&EEgr;/g,'&#919;')
  .replace(/&Theta;/g,'&#x00395;')
  .replace(/&Igr;/g,'&#921;')
  .replace(/&Kgr;/g,'&#922;')
  .replace(/&Lambda;/g,'&#x0039B;')
  .replace(/&Mgr;/g,'&#924;')
  .replace(/&Ngr;/g,'&#925;')
  .replace(/&Xi;/g,'&#926;')
  .replace(/&Ogr;/g,'&#927;')
  .replace(/&Pi;/g,'&#928;')
  .replace(/&Rgr;/g,'&#929;')
  .replace(/&Sigma;/g,'&#x003A3;')
  .replace(/&Tgr;/g,'&#932;')
  .replace(/&Upsi;/g,'&#933;')
  .replace(/&Phi;/g,'&#934;')
  .replace(/&KHgr;/g,'&#935;')
  .replace(/&Psi;/g,'&#936;')

  .replace(/&Omega;/g,'&#x003A9;')
  .replace(/&le;/g,'&#x02264;')
  .replace(/&ge;/g,'&#x02265;')
  .replace(/&Lt;/g,'&#x0226A;')
  .replace(/&Gt;/g,'&#x0226B;')
  .replace(/&Precedes;/g,'&#x00227A;')
  .replace(/&Succeeds;/g,'&#x0227B;')
  .replace(/&vltri;/g,'&#x022B2;')
  .replace(/&vrtri;/g,'&#x022B3;')
  .replace(/&ne;/g,'&#x02260;')
  .replace(/&equiv;/g,'&#x02261;')
  .replace(/&sim;/g,'&#x0223C;')
  .replace(/&ap;/g,'&#x02248;')
  .replace(/&sime;/g,'&#x02243;')
  .replace(/&cong;/g,'&#x02245;')
  .replace(/&bsim;/g,'&#x0223D;')
  .replace(/&bcong;/g,'&#x0224C;')
  .replace(/&prop;/g,'&#x0221D;')
  .replace(/&there4;/g,'&#x02234;')
  .replace(/&exist;/g,'&#x02203;')
  .replace(/&forall;/g,'&#x02200;')
  .replace(/&not;/g,'&#x000AC;')
  .replace(/&and;/g,'&#x02227;')
  .replace(/&or;/g,'&#x02228;')
  .replace(/&trie;/g,'&#x0225C;')
  .replace(/&wedgeq;/g,'&#x02259;')
  .replace(/&DotEqual;/g,'&#x02250;')
  .replace(/&nbsp;/g,'&#x000A0;')
  .replace(/&hairsp;/g,'&#x0200A;')
  .replace(/&thinsp;/g,'&#x02009;')
  .replace(/&ThickSpace;/g,'&#x02009;&#x0200A;&#x0200A;')
  .replace(/&emsp;/g,'&#x0203;');
  $(this).empty().attr("contenteditable","false").append(code);
  });
});

$('#box>div.data button.postanswer').live("click",function(event){
  var url = "/save/solution";
  var course_id = $(".contenttext input[name='course_id']").val();
  var chapter_id = $(".contenttext input[name='chapter_id']").val();
  var template_id = $(".contenttext input[name='template_id']").val();
  var num = $(".contenttext div.question").index($(this).parent()) + 1;
  var subnum = $(".contenttext button.postanswer").index(this) + 1;
  var html = $(this).parent().children("div.answerfield").html();
    $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,answer_number:subnum,answer_solution:html},function(){
	 $(this).parent().children("div.answerfield").attr("contenteditable","false");
	});
  event.preventDefault();
});

$('#box>div.data button.copyanswer').live("click",function(event){

  var html = $(this).parent().children("div.answerfield").html();
  $(this).parent().children("textarea.answerhtmlfield").val(html).fadeIn();
  $("textarea.answerhtmlfield").live("copy",function(){
    $(this).fadeOut();
  });
  event.preventDefault();
});

  /* submit problems answers */
 
 $(".contenttext input[name='answer']").live("change",function(){
  var url = "/save/"+$(this).attr("name");
  var course_id = $(".contenttext input[name='course_id']").val();
  var chapter_id = $(".contenttext input[name='chapter_id']").val();
  var template_id = $(".contenttext input[name='template_id']").val();
  var num = $(".contenttext div.question").index($(this).parent().parent()) + 1;
  var subnum = $(".contenttext input[name='answer']").index(this) + 1;
  var text = $(this).val();
  $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,answer_number:subnum,answer_text:text},function(data){
   $(".contenttext input[name='answer']").eq(subnum-1).next().removeClass().addClass(data);
  });
 });
 
   /* submit problem rating */
 
 $(".contenttext span.beauty div.star-rating").live("click",function(){
  var url = "/save/rating";
  var course_id = $(".contenttext input[name='course_id']").val();
  var chapter_id = $(".contenttext input[name='chapter_id']").val();
  var template_id = $(".contenttext input[name='template_id']").val();
  var num = $(".contenttext div.problem").index($(this).parent().parent().parent().parent().parent()) + 1;
  var val = $(this).children("a").text();
  $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,beauty:val});
 });
 
  $(".contenttext span.difficulty div.star-rating").live("click",function(){
  var url = "/save/rating";
  var course_id = $(".contenttext input[name='course_id']").val();
  var chapter_id = $(".contenttext input[name='chapter_id']").val();
  var template_id = $(".contenttext input[name='template_id']").val();
  var num = $(".contenttext div.problem").index($(this).parent().parent().parent().parent().parent()) + 1;
  var val = $(this).children("a").text();
  $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,difficulty:val});
 });

  /* edit student answer */

 $("#results tbody td span.right").live("click",function(){
  var comment = $(this).next(".comment").html();
  var answer = $(this).text();
  var points = $(this).attr("title");
  $(this).parent().append($("<div />").addClass("editAnsForm")
    .append($("<input />").addClass("studAnswer").attr("type","text").attr("value",answer))
	.append($("<input />").addClass("studPoints").attr("type","text").attr("value",points))
	.append($("<input />").addClass("insComment").attr("type","text").attr("value",comment))
	.append($("<button />").text("submit").addClass("submit"))
	.append($("<button />").text("cancel")));
  });

 $("#results tbody td span.wrong").live("click",function(){
  var comment = $(this).next(".comment").html();
  var answer = $(this).text();
  var points = $(this).attr("title");
  $(this).parent().append($("<div />").addClass("editAnsForm")
    .append($("<input />").addClass("studAnswer").attr("type","text").attr("value",answer))
	.append($("<input />").addClass("studPoints").attr("type","text").attr("value",points))
	.append($("<input />").addClass("insComment").attr("type","text").attr("value",comment))
	.append($("<button />").text("submit").addClass("submit"))
	.append($("<button />").text("cancel")));
  });
  
  $("#results tbody td button").live("click",function(){
  var url = "/save/result";
  var course_id = $(".contenttext input[name='course_id']").val();
  var chapter_id = $(".contenttext input[name='chapter_id']").val();
  var template_id = $(".contenttext input[name='template_id']").val();
  var trnum = $("#results>tbody>tr").index($(this).parent().parent().parent());
  var ansnum = $($(this).parent().parent().parent()).children().index($(this).parent().parent())-1;
  var studName = $(this).parent().parent().prevAll(".name").children(".username").text();
  var studAnswer = $(this).prevAll(".studAnswer").val();
  var studPoints = $(this).prevAll(".studPoints").val();
  var insComment = $(this).prevAll(".insComment").val();
  var klass = $(this).attr("class");
  if(klass == "submit"){
  $(this).parent().empty().load(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,answer_number:ansnum,student_name:studName,answer_text:studAnswer,answer_point:studPoints,answer_comment:insComment}).fadeOut("slow").remove();
  }else{$(this).parent().fadeOut("slow").remove()}
  event.preventDefault();
  });

/* Answersheet */

$("#box>.data .sButtons>button").live("click",function(event){
  $(this).parent().parent().children(".sInput").children("ul").append($("<li>").attr("contenteditable","true"));
    event.preventDefault();
});

$("#box>.data button.sent_answersheet").live("click",function(event){
  var temp = $(this).parents("div.data").attr("id");
  var url = "/save/studentcomment";

  var currentDate = new Date();
  var day = currentDate.getDate();
  var month = currentDate.getMonth() + 1;
  var year = currentDate.getFullYear();
  var hour = currentDate.getHours();
  var minute = currentDate.getMinutes();
  var second = currentDate.getSeconds();
  var time = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
  var comment = $(this).parent().children(".sInput").html();
  var post = $("<div>").attr("class","sPost").append($("<div>").attr("class","sInfo")
                               .append($("<div>").text(time).attr("class","sTime")))
                       .append($("<div>").attr("class","sText").html(comment));
  $(this).parent("div.sMessage").find(".sInput").before(post);
  $(this).parent().children(".sInput").empty().append($("<ul>").append($("<li>").attr("contenteditable","true")));   
  var course_id = $("#"+temp).find("input[name='course_id']").val();
  var chapter_id = $("#"+temp).find("input[name='chapter_id']").val();
  var template_id = $("#"+temp).find("input[name='template_id']").val();
  var num = $("#"+temp).find("button.sent_answersheet").index(this) + 1;
  $.post(url,{course_id:course_id,chapter_id:chapter_id,template_id:template_id,problem_number:num,student_comment:comment},function(data){});
  event.preventDefault();
});

$("#box>div.data div.sPost.unread").live("click",function(event){
  var id = $(this).attr("id");
  var url = "/save/supervisorcommentid";
  $.post(url,{id:id},function(data){
    $(this).removeClass().attr("class","sPost");
  });
  event.preventDefault();
});

$("#box>div.data div.sPost.unread").live("dblclick",function(event){
  var id = $(this).attr("id");
  var field = $("<textarea>").attr("id","textarea"+id);
  $(this).after(field);
  event.preventDefault();
  $("#box>div.data #textarea"+id).live("dblclick",function(event){
    var url = "/save/supervisorcomment";
    var comment = $(this).val();
    $.post(url,{id:id,supervisor_comment:comment},function(data){
      //$(this).remove();
    });
  });
});

//Fn11 key press
$(document).keypress(function(event){
  if (event.keyCode == '122'){
    window.setTimeout(function(){$("#box>div.data>div.content").resizeY()}, 3000);
  }
});

(function($) {
 $.fn.resizeX = function() {
   var n = $("#box>div.data").size();
   var w = $(window).width() / n;
   $(this).css("width",w);
 };
 $.fn.resizeY = function() {
   var h = $(window).height();
   $(this).css("height",h - 12);
 };
})(jQuery);

});  

