// Used for keeping current card state so can flip
var cardSideUp = 'bottom';

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
window.onload = function () {
  flipCard();
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function flipCard() {

  // Reference the side of card to show
  var topSide = document.getElementById('top');
  var bottomSide = document.getElementById('bottom');

  if (cardSideUp == 'top') {
     topSide.style.display = 'none';
     bottomSide.style.display = 'block';
     cardSideUp = 'bottom';
  } else if (cardSideUp == 'bottom') {
     topSide.style.display = 'block';
     bottomSide.style.display = 'none';
     cardSideUp = 'top';
  } else {
    alert('Programmer error: unrecognized state: '+cardSideUp);
  } 
  
  return false;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function help() {
  alert('Lots of features, but let\'s keep it simple:\n\n1. Select a deck\n2. You\'ll see a card\n3. Ask yourself, do I know what this means?\n4. Flip card over and verify your guess. See "FLIP CARD OVER" at bottom of the page?\n4. If you got it right, select "CORRECT". Otherwise, select "WRONG".\n5. You will be given another card. Repeat.\n\nIf using adaptive mode, the app will use your responses to help you learn by replaying the words it believes are causing you difficulty.');
  return false;
}

function aboutDecks() {
  alert('A study aid. If you have write permissions, you can create decks of vocabulary cards.\n\nTry to recall the definitions of words or the words from the definitions.\n\nLearn vocabulary, countries, presidents, etc.');
  return false;
}

function aboutAdaptive() {
  alert('Adaptive mode means that the most "troublesome" words will be favored. The algorithm used to decide what is "troublesome" factors in the number of correct versus incorrect responses, with more weight placed on the total incorrect responses, to facilitate retention.\n\nThe algorithm uses all responses by all users, not just you. As these words are answered correctly enough times, they will be supplanted with unanswered words or other missed words.\n\nYou should not see the same word twice in a row, but you may see it every two or more cards if missed frequently enough.\n\nAn admin may reset the record or correct and incorrect responses at any time. Tada.'); 
  return false;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
 * Modified from: http://www.webmasterworld.com/forum91/3429.htm
 */
function resize(){  
  var frame = document.getElementById("wrapper");  
  
  var htmlheight = document.body.parentNode.clientHeight;  
  if (!htmlheight) {
    htmlheight = 0;
  }
  
  // The actual 
  var windowheight = window.innerHeight; 
  if (!windowheight) {
    windowheight = document.body.clientHeight;
  }
  if (!windowheight) {
    windowheight = 0;
  }
  
  // How far down is user scrolled?
  var height = getScrollHeight();
  var width = getScrollWidth();
  
  // Now add greatest value to it!
  if ( htmlheight >= windowheight ) {  
    height+=htmlheight;
  } else { 
    height+=windowheight;
  } 
  
  frame.style.height = height + "px";
  
  // Update the collab bar
  var collabBar = document.getElementById('footer');
  collabBar.style.position = 'absolute';
  collabBar.style.bottom = '0px';
  
  //var collabBarHeight = collabBar.clientHeight;
  var collabBarHeight = 18;
  
  if (!collabBarHeight) {
    collabBarHeight = 0;
  }
  
  //pause(250);
  
  // 
  var adjustedTopMargin = height - collabBarHeight - 15;
  var adjustedBottomMargin = height - adjustedTopMargin - collabBarHeight;
  
  if (adjustedBottomMargin < 15) {
    adjustedBottomMargin = 15;
  }
      
  //alert("adjustedTopMargin<"+adjustedTopMargin+"> = height<"+height+"> - collabBarHeight<"+collabBarHeight+"> - 10");
  
  collabBar.style.top = adjustedTopMargin+"px";
  collabBar.style.bottom = adjustedBottomMargin+"px";
  collabBar.style.height = collabBarHeight+'px';
} 

/**
 * Source: unknown
 */
function getScrollWidth() {
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
}

/**
 * Source: unknown
 */
function getScrollHeight() {
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
 * Five DOM elements:
 * 1. edit-card-is-noun
 * 2. edit-card-is-verb
 * 3. edit-card-is-adjective
 * 4. edit-card-is-adverb
 * 5. edit-card-is-other
 *
 * action must equal 'edit' or 'create'
 */
function uncheckPartsOfSpeech(action) {
  document.getElementById(action+'-card-is-noun').checked = false;
  document.getElementById(action+'-card-is-verb').checked = false;
  document.getElementById(action+'-card-is-adjective').checked = false;
  document.getElementById(action+'-card-is-adverb').checked = false;
  document.getElementById(action+'-card-is-other').checked = false;
  
  return false;
}

function checkPartsOfSpeech(action) {
  document.getElementById(action+'-card-is-noun').checked = true;
  document.getElementById(action+'-card-is-verb').checked = true;
  document.getElementById(action+'-card-is-adjective').checked = true;
  document.getElementById(action+'-card-is-adverb').checked = true;
  document.getElementById(action+'-card-is-other').checked = true;
  
  return false;
}

function chooseJustNoun(action) {
  uncheckPartsOfSpeech(action);
  document.getElementById(action+'-card-is-noun').checked = true;
  return false;
}

function chooseJustVerb(action) {
  uncheckPartsOfSpeech(action);
  document.getElementById(action+'-card-is-verb').checked = true;
  return false;
}

function chooseJustAdjective(action) {
  uncheckPartsOfSpeech(action);
  document.getElementById(action+'-card-is-adjective').checked = true;
  return false;
}

function chooseJustAdverb(action) {
  uncheckPartsOfSpeech(action);
  document.getElementById(action+'-card-is-adverb').checked = true;
  return false;
}

function chooseJustOther(action) {
  uncheckPartsOfSpeech(action);
  document.getElementById(action+'-card-is-other').checked = true;
  return false;
}