var currcard = 0;
var sessionactive = true;var flipped = false;var edit_mode = false;
var total_num_of_cards = theCardset.length - 1;
var total_correct = 0;var total_incorrect = 0;var total_unanswered = 0;

studystart();

function studystart() {
  next_card();
  Element.show('card_front');
  Element.show('study_controls');
  if (!em) {observekeys()};
  google.load("visualization", "1", {packages:["piechart"]});
  update_cardset_status();
}

function observekeys() {
  new Event.observe(document, 'keydown', function(event){ 
   if(event.keyCode == Event.KEY_DOWN && srs == 0 && !edit_mode && cap) {mark_incorrect();Event.stop(event);}
   if(event.keyCode == Event.KEY_UP && srs == 0 && !edit_mode && cap) {mark_correct();Event.stop(event);}
   if(event.keyCode == Event.KEY_LEFT && !edit_mode && cap) {prev_card();Event.stop(event);}
   if(event.keyCode == Event.KEY_RIGHT && !edit_mode && cap) {
     if (theCardset[currcard].answered || flipped) {
       next_card();
     } else {
       flip();
     }
   }
  });
  new Event.observe(document, 'keydown', function(event){ 
   if(event.keyCode == 49 && srs == 1 && !edit_mode && cap) {mark_repetition(1);Event.stop(event);}
   if(event.keyCode == 50 && srs == 1 && !edit_mode && cap) {mark_repetition(2);Event.stop(event);}
   if(event.keyCode == 51 && srs == 1 && !edit_mode && cap) {mark_repetition(3);Event.stop(event);}
   if(event.keyCode == 52 && srs == 1 && !edit_mode && cap) {mark_repetition(4);Event.stop(event);}
   if(event.keyCode == 53 && srs == 1 && !edit_mode && cap) {mark_repetition(5);Event.stop(event);}
   if(event.keyCode == 70 && srs == 1 && !edit_mode && cap) {flip();Event.stop(event);}
   if(event.keyCode == 102 && srs == 1 && !edit_mode && cap) {flip();Event.stop(event);}
  });
}

function drawChart() {
  var pie_data = new google.visualization.DataTable();
  var min_data = Math.round(total_num_of_cards*0.02);
  var pie_options = {width: 530, height: 270, backgroundColor: '#c3d9ff', borderColor: '#c3d9ff', focusBorderColor: '#fff', legend: 'right', legendBackgroundColor: '#efefef', enableTooltip: false};
  var piec_pc = Math.round(total_correct/total_num_of_cards*1000)/10; var piei_pc = Math.round(total_incorrect/total_num_of_cards*1000)/10; var pieu_pc = Math.round(total_unanswered/total_num_of_cards*1000)/10;
  if (total_unanswered==0 && total_incorrect==0) {
    pie_options.colors = ['#66cc66'];
  } else if (total_correct==0 && total_incorrect==0) {
    pie_options.colors = ['#fad163'];
  } else if (total_unanswered==0 && total_correct==0) {
    pie_options.colors = ['#ff3333'];
  } else if (total_unanswered==0) {
    pie_options.colors = ['#66cc66','#ff3333'];
  } else if (total_correct==0) {
    pie_options.colors = ['#ff3333','#fad163'];
  } else if (total_incorrect==0) {
    pie_options.colors = ['#66cc66','#fad163'];
  } else {
    pie_options.colors = ['#66cc66','#ff3333','#fad163'];
  }
  pie_data.addColumn('string', 'Card Status');
  pie_data.addColumn('number', 'Number of Cards');
  pie_data.addRows(3);
  pie_data.setValue(0, 0, 'Correct ' + piec_pc + '%');
  pie_data.setValue(0, 1, total_correct);
  pie_data.setValue(1, 0, 'Incorrect ' + piei_pc + '%');
  pie_data.setValue(1, 1, total_incorrect);
  pie_data.setValue(2, 0, 'Unanswered ' + pieu_pc + '%');
  pie_data.setValue(2, 1, total_unanswered);
  if (sessionactive) {
  var pie = new google.visualization.PieChart(document.getElementById('pie_div'));
  pie.draw(pie_data, pie_options);
  }
}

function next_card() {
  if (currcard < total_num_of_cards)
    {
    currcard++;
    display_card(false);
    }
}

function prev_card() {
  if (currcard > 1)
    {
    currcard--;
    display_card(false);
    }
}

function display_card(with_delay) {
  flipped=false;
  show_hide_card_back();
  update_card_content();
  if (with_delay) {
    setTimeout(update_card_status, 200);
  } else {  
    update_card_status();
  }
  update_cardset_status();
}

function show_hide_card_back() {
  if (theCardset[currcard].answered) {
    $('card_back').show();
  } else {
    $('card_back').hide();
  }
}

function update_card_content() {
    $('card_front').update(theCardset[currcard].card_front);
    $('card_back').update(theCardset[currcard].card_back);
    $('card_position').update(currcard + ' / ' + total_num_of_cards);
}

function update_card_status() {
  show_hide_card_back();
  if (theCardset[currcard].answered && theCardset[currcard].correct)
    {
    $('card_status').update('CORRECT');
    $('card_status').style.color='#66cc66';
    $('card_status').show;
    show_marking_buttons();
    }
    else if(theCardset[currcard].answered)
    {
    $('card_status').update('INCORRECT');
    $('card_status').style.color='#ff3333';
    $('card_status').show;
    show_marking_buttons();
    }
    else
    {
    $('card_status').update('');
    $('card_status').style.background='';
    $('card_status').hide;
    show_flip_button();
    }
}

function update_cardset_status() {
  var i=0;
  total_correct=0;total_incorrect=0;total_unanswered=0;
  for (i=1;i<=total_num_of_cards;i++) { 
    if (theCardset[i].answered && theCardset[i].correct)
    {
    total_correct++;
    }
    else if (theCardset[i].answered)
    {
    total_incorrect++;
    }
    else
    {
    total_unanswered++;
    }
  }
  $('totalc').update(total_correct);
  $('totali').update(total_incorrect);
  $('totalu').update(total_unanswered);
  $('resultst').update(total_num_of_cards);
  $('resultsc').update(total_correct);
  $('resultsi').update(total_incorrect);
  $('resultsu').update(total_unanswered);
  }

function flip() {
  Element.show('card_back');
  show_marking_buttons();
  flipped=true;
}

function show_flip_button() {
  Element.show('flip');
  Element.hide('mark');
}

function show_marking_buttons() {
  Element.show('mark');
  Element.hide('flip');
}

function mark_correct() {
  theCardset[currcard].answered = true;
  theCardset[currcard].correct = true;
  if (li && ucs && !sa) {new Ajax.Request('/results/mark_correct?card_id=' + theCardset[currcard].card_id, {asynchronous:true, evalScripts:true});}
  next_unanswered_card_or_end_session();
}

function mark_incorrect() {
  theCardset[currcard].answered = true;
  theCardset[currcard].correct = false;
  if (li && ucs) {new Ajax.Request('/results/mark_incorrect?card_id=' + theCardset[currcard].card_id, {asynchronous:true, evalScripts:true});}
  next_unanswered_card_or_end_session();
}

function mark_repetition(the_grade) {
  theCardset[currcard].answered = true;
  if (the_grade < 3)
    {
      theCardset[currcard].correct = false;
    } else {
      theCardset[currcard].correct = true;
    }
  if (li && ucs && !sa) {new Ajax.Request('/results/mark_repetition?card_id=' + theCardset[currcard].card_id + '&grade=' + the_grade, {asynchronous:true, evalScripts:true});}
  next_unanswered_card_or_end_session();
}

function find_next_unanswered_card() {
  next_unanswered_card=0;
  var i=1;
  do
  {
    if (!theCardset[i].answered)
      {
      next_unanswered_card=i;
      }
      i++;
  }
  while ((i<=currcard) && (next_unanswered_card==0));
  i=currcard;
  i++;
  while ((i <= total_num_of_cards) && (next_unanswered_card <= currcard))
  {
    if (!theCardset[i].answered)
      {
      next_unanswered_card=i;
      }
      i++;
  }
}

function next_unanswered_card_or_end_session() {
  update_card_status();
  update_cardset_status();
  find_next_unanswered_card();
  if (!next_unanswered_card==0)
    {
    currcard=next_unanswered_card;
    display_card(true);
    }
  else
    {
    end_session();
    }
}

function shuffle() {
  var i = theCardset.length;
  if (i == 0) return false;
  if (edit_mode) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     if (j==0) j++;
     var tempi = theCardset[i];
     var tempj = theCardset[j];
     theCardset[i] = tempj;
     theCardset[j] = tempi;
   }
   new Effect.Pulsate('study_flashcards', {duration:.3});
   currcard=1;
   display_card(false);
   if (theCardset[currcard].answered) next_unanswered_card_or_end_session();
}

function reverse() {
  if (edit_mode) return false;
  for (var i=1;i<=total_num_of_cards;i++) {
    var tempf = theCardset[i].card_front;
    var tempb = theCardset[i].card_back;
    theCardset[i].card_front = tempb;
    theCardset[i].card_back = tempf;
  }
  display_card(false);
  new Effect.Highlight('studysession' , {duration:.5});
}

function study_edit() {
  if (edit_mode) return false;
  new Ajax.Request('/cards/studyedit/' + theCardset[currcard].card_id, {asynchronous:true, evalScripts:true});
  edit_mode = true;
}

function study_update(newfront,newback) {
  theCardset[currcard].card_front = newfront;
  theCardset[currcard].card_back = newback;
  update_card_content();
  edit_mode = false;
}

function study_edit_cancel() {
  $('study_flashcards_edit_form').remove();
  $('study_flashcards').show();
  $('card_status').show();
  $('study_controls').show();
  edit_mode = false;
}

function end_session() {
  drawChart();
  $('flip').hide();
  $('mark').hide();
  if (sessionactive) {
    new Effect.BlindDown('study_stats', {duration:.5});
  }
  sessionactive = false;
}
