




/* Javascript for Coffee Recommender */

// load the questions array and declare other global vars
var questions = loadCSVdata( "/media/document/1/questions.csv" );
var tmp       = questions.shift();  // get rid of the header row to put us sync with the DOM "rows"
tmp.shift();

var content;
var rows;
var totals    = new Array();
for ( var i=0; i<tmp.length; i++ ) {
  totals[i] = 0;
}

window.onload = function () {
  if ( !document.getElementsByTagName("a") || !document.getElementById ) 
    return false;

  content   = document.getElementById("content");
  rows      = content.getElementsByTagName("ul");

  // the div elements match up to the rows in the CSV
  var h2Elems   = content.getElementsByTagName("h2");
  for ( var i=0; i<rows.length; i++ ) {
    h2Elems[i].firstChild.nodeValue = questions[i].shift();
    setRow( rows[i], i );
  }

  

  // add recommender event to the See My Results button
  var imgs        = content.getElementsByTagName("img");
  var recbtn      = imgs[imgs.length-1];
  recbtn.onclick  = function () { recommend(); return false; }

  return;
}

// *******************
// setRow
// populate the h2, li, a and img elems with data from the CSV
// *******************
function setRow ( row, r ) {
  var rowData = questions[ r ];

  // the li elements match up to the cells in the CSV
  var cell = row.getElementsByTagName("li");
  for ( var i=0; i<5; i++ ) {
    var aElem = cell[i].getElementsByTagName("a")[0];
    var title = rowData[i];
    addEvent(aElem, r, i);
    if ( i < 4 ) {
      aElem.firstChild.nodeValue = title;
      var img = cell[i].getElementsByTagName("img")[0];
      img.setAttribute("alt", title);
    } else {
      cell[i].setAttribute((document.all ? "className" : "class"),"selected");
      var nothanks                  = cell[i].getElementsByTagName("p")[0];
      title                         = "No Thanks";
      nothanks.firstChild.nodeValue = rowData[i];
      aElem.firstChild.nodeValue    = title;
    }
    aElem.setAttribute("title", title);
  }
  return;
}

// *******************
// addEvent
// add onclick event to an element
// *******************
function addEvent(el, q, a) {
  el.onclick = function() {
    vote(q, a);
    return false;
  }
  return;
}

// *******************
// loadCSVdata
// csv -> js array 
// *******************
function loadCSVdata( url ) {
  var http  = new JKL.ParseXML.CSV( url );
  var data  = http.parse();
  return data;
}

// *******************
// vote for a profile by clicking
// *******************
function vote( q, a ) {
  var answers   = rows[q].getElementsByTagName("li");
  // the last answer "no thanks" decrements the scores for selected answers on that question
  if ( a == 4 ) {
    for ( var i=0; i<4; i++ ) {
      if ( totals[i] > 0 && answers[i].className == "selected" ) 
        totals[i]--;
      answers[i].className = "answer";
    }
  // any other answer will increment the score for that profile
  } else if ( answers[a].className != "selected" ) {
    answers[4].className = "noThanks";
    totals[a]++;
  }
  answers[a].className = "selected";
  
  return;
}

// *******************
// getProfile
// function to calculate which profile to choose
// *******************
function getProfile () {
  var highscore = 0;
  var profiles  = new Array();

  // first get the high score
  for ( var i=0; i<totals.length; i++ ) {
    if ( totals[i] > highscore ) 
      highscore = totals[i];
  }

  // find which answers had the high score
  for ( var i=0; i<totals.length; i++ ) {
    if ( totals[i] == highscore ) 
      profiles.push(i);
  }

  // if there was only one answer with the high score, cool, that's our profile
  if ( profiles.length == 1 ) {
    return profiles[0];

  // otherwise refer to the "tiebreak" question (the first one) to choose
  } else {
    var tb;
    for ( var i=0; i<questions.length; i++ ) {
      tb = tiebreak(i, profiles);
      if ( typeof tb != 'undefined' ) 
        break;
    }
    return tb;
  } 
}

// *******************
// tiebreak
// breaks a profile tie
// *******************
function tiebreak (q, profiles) {
  var answers   = rows[q].getElementsByTagName("li");
  var tiebreak;

  // minor problem if the tiebreak question has both highscore answers selected, but hey we're just gonna choose the darker one
  for ( var i=0; i<profiles.length; i++ ) {
    if ( answers[profiles[i]].className == "selected" ) {
      //alert ("using answer " + answers[profiles[i]].innerHTML + " as tiebreak");
      tiebreak = profiles[i];
    }
  }
  return tiebreak;
}

// *******************
// getAnswers
// turn answers for a given question (q) into an array
// *******************
function getAnswers (q) {
  var answers = rows[q].getElementsByTagName("li");
  var params  = new Array();
  for ( var i=0; i<4; i++ ) {
    if ( answers[i].className == "selected" ) 
      params.push(i);
  }
  return params;
}

// *******************
// recommend
// runs getProfile and redirect to the coffee recommender results page
// *******************
function recommend () {
  var myProfile = getProfile() + 1;
  var myUrl     = "/page/1/coffee-recommender-profiles.jsp?profile=" + myProfile;
  // urlify the answers for the questions
  for ( var i=0; i<5; i++ ) {
    var params = getAnswers(i);
    myUrl += "&q" + i + "=" + params.join();
  }
  //alert ("my profile is " + myProfile);
  window.location = myUrl;
  return;
}

