/* POLL OBJECT
 * Requires: HttpRequest.js
 * Arguments:
 * id    - the id of the container
 * path  - the path of the poll
 * [tmpl] - the template for display poll and results
 * [lang] - the language to use
 */

function Poll(id, path, tmpl, lang) {
  p_ = this;
  tmpl = tmpl || 'default';
  lang = lang || 'en';
  var q, a, haveResults;
  var url = '/ncp_polls/xml/display.do';
  this.url = url;
  var msgs = new Object();
  if (lang == 'fr') {
    msgs.loading = 'Chargement...';
    msgs.voted = 'Vous avez d'+String.fromCharCode(233)+'j'+String.fromCharCode(224)+' vot' + String.fromCharCode(233);
    msgs.select = 'Vous devez d\'abord s'+String.fromCharCode(233)+'lectionner un choix pour voter';
  } else {
    msgs.loading = 'Loading...';
    msgs.voted = 'You have already voted';
    msgs.select = 'To vote, please select an answer';
  }

  var container = document.getElementById(id);

  var pollBox = document.createElement('div');
  pollBox.id = id+"Box";
  pollBox.style.display = 'none';
  container.appendChild(pollBox);

  var pollResults = document.createElement('div');
  pollResults.id = id+"Results";
  pollResults.style.display = 'none';
  container.appendChild(pollResults);

  var pollLoading = document.createElement('div');
  pollLoading.style.display = 'none';
  pollLoading.innerHTML = '<div style="color:#999999;">' + msgs.loading + '</div>';
  container.appendChild(pollLoading);

  Poll.prototype.setUrl = function(newUrl) {
      this.url = newUrl;
  }

  Poll.prototype.display = function() {
    displayLoading();
    var data = 'path=' + path + '&tmpl=' + tmpl + '&lang=' + lang;
    var http = new HttpRequest();
    http.getXml('post', this.url, displayContent, data);
  }

  function displayContent(xml) {
    // Check for Error
    if (xml.getElementsByTagName('error').length > 0) {
      var errMsg = xml.getElementsByTagName('error').item(0).firstChild.nodeValue;
      pollLoading.innerHTML = errMsg;
      return;
    }
    q = getQuestion(xml);
    a = getAnswers(xml);
    pollBox.innerHTML = xml.getElementsByTagName('content').item(0).firstChild.nodeValue;
    pollResults.innerHTML = xml.getElementsByTagName('results').item(0).firstChild.nodeValue;

    // Add Event Listener
    document.getElementById('poll'+q.id+'Vote').onclick = submit;
    document.getElementById('poll'+q.id+'Results').onclick = displayResults;
    document.getElementById('poll'+q.id+'Back').onclick = displayPoll;
    (q.canVote == 1) ? displayPoll() : displayResults();
    // Calculate Height
    pollLoading.style.height = pollBox.scrollHeight + "px";
  }

  function submit(e) {
    if (q.canVote == 0) { alert(msgs.voted); return false; }
    var answerId
    var f = document.getElementById('poll'+q.id+'Form');
    var inputs = f.answerId;
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked == true) { answerId = inputs[i].value; break; }
    }
    if (answerId == null) { alert(msgs.select); return false; }
    displayLoading();
    var data = 'path=' + path + '&QId=' + q.id + '&AId=' + answerId + '&tmpl=' + tmpl + '&lang=' + lang;
    var http = new HttpRequest();
    http.getXml('post', p_.url, displaySubmit, data);

    function displaySubmit(xml) {
      displayContent(xml);
      displayResults();
      return false;
    }
    return false;
  }
  function displayLoading() {
    pollBox.style.display = 'none';
    pollResults.style.display = 'none';
    pollLoading.style.display= 'block';
  }
  function displayPoll(e) {
    pollBox.style.display = 'block';
    pollResults.style.display = 'none';
    pollLoading.style.display= 'none';
    return false;
  }
  function displayResults(e) {
    pollBox.style.display = 'none';
    if (q.canVote == 0) document.getElementById('poll'+q.id+'Back').style.display = 'none';
    pollResults.style.display = 'block';
    pollLoading.style.display= 'none';
    return false;
  }
  function getQuestion(xml) {
    var question = new Object();
    var qNode = xml.getElementsByTagName('question').item(0);
    question.id = qNode.getAttribute('id');
    question.value = getChildValue(qNode, 'value');
    question.voteOnce = getChildValue(qNode, 'voteonce');
    question.canVote = getChildValue(qNode, 'canvote');
    return question
  }
  function getAnswers(xml) {
    var answers = new Array();
    var totalVotes = 0;
    var aNodes = xml.getElementsByTagName('answer');
    for (var i = 0; i < aNodes.length; i++) {
      var answer = new Object();
      answer.id = aNodes[i].getAttribute('id');
      answer.value = getChildValue(aNodes[i], 'value');
      answer.votes = getChildValue(aNodes[i], 'votes');
      answers.push(answer);
      totalVotes += parseInt(answer.votes);
    }
    for (var i = 0; i < answers.length; i++) {
      answers[i].percent = Math.round(answers[i].votes/totalVotes*100);
    }
    return answers;
  }
  function getChildValue(xmlNode, name) {
    try { return xmlNode.getElementsByTagName(name).item(0).firstChild.nodeValue; }
    catch(e) { return null; }
  }
}
