/*
 * $Id:$
 *
 * This is a javascript file used for setting things up so that users
 * can vote as to whether something is useful or not.
 */


function choiceToDisplay(c) {
  var mapping = {useful: 'useful', not_useful: 'skip' };
  return mapping[c];
}

function votesStatus(urlId, usefulCount, notUsefulCount) {
  var percent = ((usefulCount + notUsefulCount) > 0 ? 
                 (Math.round((usefulCount / (notUsefulCount + usefulCount)) * 100)) :
                 "?");

  return $E('div', {className: 'score'},
            $E('h4', {}, percent + "%"),
            $E('div', {className: 'num-votes'}, (usefulCount + notUsefulCount) + 
               " vote" + ((usefulCount + notUsefulCount) == 1 ? "" : "s")));
}

function votingMessage(urlId, message) {
  return $E('img', {className: 'vote', src: '../shared/images/loading/gray_busy.gif'});
}

function yesNoOptions(urlId, updater) {
  return $E('div', {className: 'vote'},
            $E('a', {className: 'yes', href: '#', onclick: voteRecorder(urlId, 'useful', updater) }, "useful"),
            $E('a', {className: 'no', href: '#', onclick: voteRecorder(urlId, 'not_useful', updater) }, "skip"));
}

function alreadyVoted(urlId, choice, updater) {
  return $E('div', {className: 'vote' },
            choiceToDisplay(choice), " ",
            $E('a', {href: '#', onclick: voteRemover(urlId, updater)}, "undo"));
}

function setupBallot(positionId, urlId, usefulCount, notUsefulCount, choice) {
  var box = $('p' + positionId);
  box.appendChild(votesStatus(urlId, usefulCount, notUsefulCount));
 
  var voteBox = $E('div', {});
  var update  = makeNodeUpdater(voteBox);
  if(choice == '') {
    voteBox.appendChild(yesNoOptions(urlId, update));
  } else {
    voteBox.appendChild(alreadyVoted(urlId, choice, update));
  }
  box.appendChild(voteBox);
}

