  

function rateComment(list_id, comment_id, rating) {
   
  comment_id = parseInt(comment_id);
  var comment_pts_obj = document.getElementById('comment_pts_' + comment_id);
  var old_pts = parseInt(comment_pts_obj.innerHTML);
  var new_pts = (rating == 'y') ? old_pts + 1 : old_pts - 1;
  var url = '/actions/rate_comment.php';
  var params = 'list_id=' + list_id + '&comment_id=' + comment_id + '&rating=' + rating;
  
  //$('comment_' + comment_id).hide();
   
  new Ajax.Request(url, {
        method: 'get',
        parameters: params,
        onSuccess: function(transport, json) {
          Effect.Fade($('comment_' + comment_id));
          $('comment_' + comment_id).innerHTML = 'Thanks!';
        }
  });
  
  comment_pts_obj.innerHTML = new_pts;

}

function addFavorite(list_id) {

  list_id = parseInt(list_id);

  var url = '/ajax/favorites.php';
  var params = 'action=add&yayboo_id=' + list_id;

  new Ajax.Request(url, {
        method: 'get',
        parameters: params,
        onSuccess: function(transport, json) {
          Effect.Fade($('add_favorites'));
          $('add_favorites').innerHTML = 'Saved!';
        }
  }); 

}

// get comments for a list option
function getComments(list_id, list_option_id, page) {
  list_id = parseInt(list_id);
  list_option_id = parseInt(list_option_id);
  page = parseInt(page);
  var url = '/ajax/get_comments.php';
  var params = 'list_id=' + list_id + '&list_option_id=' + list_option_id + '&page=' + page;
  var comment_loader_obj = document.getElementById('loading_comments' + list_option_id);
  var comment_form_obj = document.getElementById('comment_form' + list_option_id);
  
  // switch the option_id hidden field for adding a comment (if not logged in, there won't be a form there)
  if (comment_form_obj) {
    comment_form_obj.option_id.value = list_option_id;
  }
  
  // activate the loader animated gif
  comment_loader_obj.style.display = '';
  
  new Ajax.Request(url, {
        method: 'get',
        parameters: params,
        onSuccess: function(transport, json) {
          // remove the comment loader gif
          comment_loader_obj.style.display = 'none';
          // update the comments text
          $('comments_holder' + list_option_id).update(json.comments);
          // update the total comment numbers
          $('total_comments' + list_option_id).update('(' + json.total_comments[list_option_id] + ')');
        }
  });
}


// post a comment
function postComment(list_id, option_id, comment) {
  list_id = parseInt(list_id);
  option_id = parseInt(option_id);
  var url = '/ajax/post_comment.php';
  var params = 'list_id=' + list_id + '&option_id=' + option_id + '&comment=' + escape(comment);
  var comment_loader_obj = document.getElementById('loading_comments' + option_id);
  var comment_form_obj = document.getElementById('comment_form' + option_id);
  // clear the comment form
  comment_form_obj.comment.value = '';
  
  // activate the loader animated gif
  comment_loader_obj.style.display = '';
  
  new Ajax.Request(url, {
        method: 'get', 
        parameters: params, 
        onSuccess: function(transport, json) {
          if (json.success == 1) {
            comment_loader_obj.style.display = 'none';
            getComments(list_id, option_id, 0);
          } else {
            comment_loader_obj.style.display = 'none';
          }            
        }
  });
}

function showAllInfo() {
  var type = 'info';
  var lnks = $('list').getElementsByClassName('lnk_info');  
  var boxs = $('list').getElementsByClassName('box_info');
  for (var i=0; i<lnks.length; i++) {
    lnks[i].className = 'lnk_' + type + ' on';
  }
  for (var i=0; i<boxs.length; i++) {
    boxs[i].style.display = '';
  }
}

function toggleInfo(opt_id, type) {
  var type = (type) ? type : 'info';
  var lnk = document.getElementById('lnk_' + type + '_' + opt_id);
  var el = document.getElementById(type + '_' + opt_id);
  lnk.blur();
  if (el.style.display == 'none') {
    lnk.className = 'lnk_' + type + ' on';
    el.style.display = '';
  } else {
    lnk.className = 'lnk_' + type;
    el.style.display = 'none';
  }
}

// Arrays to hold option id's of list options
var yays = new Array();
var boos = new Array();

var submit_button = '';
var list_id = '';
var btns = new Array();

// VoteClickWatcher
//   This class will add event handlers to the vote buttons
//   and make ajax requets
var VoteClickWatcher = Class.create();

VoteClickWatcher.prototype = {

  initialize: function() 
  {
    //var cookie_auth = getCookie("yayboo_auth");
    var auth = $('authenticated').value;
    this.setup_vote_events_auth();
  },
  
  setup_vote_events_auth: function() 
  {
    submit_button = $('submit_votes');
    list_id = $('list_id').value;
    code = $('code').value;
    btns = $('list').getElementsByClassName('yb_button');
    
    for (var i=0; i<btns.length; i++) 
    {
      Event.observe(btns[i], 'click', this.vote_handler.bindAsEventListener(btns[i]));
    }
    
    Event.observe(submit_button, 'click', this.vote_submitter.bindAsEventListener(submit_button));
    
  }, // end setup_vote_events_auth

  vote_handler: function(e) 
  {
    var elm = Event.element(e);
    var button_div = elm.parentNode;
    var vote_type = elm.id.substring(0, 1);
    var vote_option_id = parseInt(elm.id.substring(1));
    var in_yay = yays.inArray(vote_option_id);
    var in_boo = boos.inArray(vote_option_id);
    
    // Remove this option from both yays and boos (right before we add it back to one of them)
    yays = yays.without(vote_option_id);
    boos = boos.without(vote_option_id);
    
    // Add this option to proper vote array
    if(vote_type == 'y') 
    {
      // if we're changing the vote
      if (in_boo) 
      {
        button_div.style.backgroundPosition = '0 -53px';
      } 
      else 
      {
        button_div.style.backgroundPosition = '0 -26px';
        yays.push(vote_option_id);
      }
    }
    else if(vote_type == 'b') 
    {
      // if we're changing the vote
      if (in_yay) 
      {
        button_div.style.backgroundPosition = '0 -53px';
      } 
      else 
      {
        button_div.style.backgroundPosition = '0 0px';
        boos.push(vote_option_id);
      }
    }
  }, // end vote_handler 
  
  vote_submitter: function(e) 
  {
    var url = '/ajax/vote.php?list_id=' + list_id + '&code=' + code;
    var vote_data = '';
    
    // Strip out any duplicate votes
    yays = yays.uniq();
    boos = boos.uniq();
    
    // build vote_data string (will be yays|boos 123,423,124|732,829,391)
    yays.each(function(el) {
      vote_data += el + ','; 
    });
    
    vote_data += '|';
    
    boos.each(function(el) {
      vote_data += el + ','; 
    });
        
    new Ajax.Request(url, {
      method: 'post',
      parameters: {vote_data: vote_data},
      onSuccess: function(transport, json) 
      {
        // TODO: figure out errors if (json.errors) 
        
        var list_update = eval('(' + transport.responseText + ')');
        
        // Update the points for every option
        // el is an object containing el.id = option id, el.y = number of yays, el.b = number of boos
        list_update.each(function(el) {
          try {
            var score = parseInt(el.y) - parseInt(el.b);
            var sign = (score >= 0) ? '+' : '';
            $('calc_score_' + el.id).update(sign + score + ' pts');
            $('total_votes_' + el.id).update(parseInt(el.y) + parseInt(el.b) + ' votes');
          } catch(e){ }
        });
        
        // TODO: Figure out a standar way to display errors  if(json.php_error)
      }
      // TODO: Figure out a standar way to display errors  onFailure: function(transport) {  notice.update('Ruh Roh!').setStyle({ background: '#0f0' }); notice.show(); }
    });
    
    // Clear buttons
    // we want to cascade from the bottom up, so, we need indexes to put these at..
    // so create an array map of id's from bottom to top
    // then we place each of the yays and boos into a queue by looking up which index the id should be placed at from vote_queue_ids
    
    // Using getElementsByClassName to get the option_id's will provide us with an array of elements in the order the page presents them visually (not the order of option_id's which would be scrambled)
    document.vote_queue_elms = document.getElementsByClassName('yb_button');
    document.vote_queue_ids = new Array(); // key = 0-10...  value = yayboo option id
    document.vote_queue = new Array();
    
    // strip the id number from:  id="yb_button72827"
    var pref = 'yb_button';
    var id_num = '';
    var output = '';
    for (var i=0; i<document.vote_queue_elms.length; i++) 
    {
      id_num = document.vote_queue_elms[i].id;
      document.vote_queue_ids.push(id_num.substring(pref.length));
    }
     
    document.votes_timeout = 50;
    
    yays.each(function(id) 
    {
      try 
      {
        // j should be 0-10...  and document.vote_queue_ids[j] should be the yayboo option id
        for (var j=0; j<document.vote_queue_ids.length; j++) 
        {
          // save the id in the vote queue in the right order
          if (document.vote_queue_ids[j] == id) 
          {
            document.vote_queue[j] = id;
          } else if (!document.vote_queue[j]) 
          {
            document.vote_queue[j] = '';
          }
        }
      } 
      catch(e) { alert(e); }
    });
    
    boos.each(function(id) 
    {
      try 
      {
        // j should be 0-10...  and document.vote_queue_ids[j] should be the yayboo option id
        for (var j=0; j<document.vote_queue_ids.length; j++) 
        {
          // save the id in the vote queue in the right order
          if (document.vote_queue_ids[j] == id) 
          {
            document.vote_queue[j] = id;
          } else if (!document.vote_queue[j]) 
          {
            document.vote_queue[j] = '';
          }
        }
      } 
      catch(e) { alert(e); }
    });
    
    for (var k=(document.vote_queue.length - 1); k>=0; k--) 
    {
      if (document.vote_queue[k]) 
      {
        this.yb_opt_id = document.vote_queue[k];
        
        var eval_string = '';
        eval_string += "var points = (parseInt($('calc_score_" + this.yb_opt_id + "').innerHTML.sub('pts', ''))) + 1;";
        eval_string += "var css_class = (points < 0) ? 'net_points_red_dark' : 'net_points_green_dark';";
        eval_string += "$('yb_button" + this.yb_opt_id + "').hide();";
        eval_string += "$('calc_score_" + this.yb_opt_id + "').hide();";
        eval_string += "$('calc_score_" + this.yb_opt_id + "').addClassName(css_class);";
        eval_string += "Effect.Appear($('calc_score_" + this.yb_opt_id + "'));";
        
        setTimeout(eval_string, document.votes_timeout);
        document.votes_timeout += 100;
      }
    }
    
    // Clear votes
    yays = new Array();
    boos = new Array();
    
    // show the refresh link
    Effect.Appear('refresh_list');
  } // end vote_submitter
};
    
