if (typeof(iTriage) == "undefined") {
  iTriage = {}
}

iTriage.user = {}
iTriage.user.currentLocation = {}

iTriage.setLocation = function(location) {
  iTriage.user.currentLocation = location;
  document.fire("itriage:location_loaded");
  return true;
}

// Load User's current location over AJAX call. 
// just loads a hash
iTriage.loadLocation = function() {
  new Ajax.Request('/location', {
    method:'get',
    onSuccess: function(transport){
      iTriage.user.currentLocation = transport.responseText.evalJSON();
      document.fire("itriage:location_loaded");
    }
  });
}

iTriage.onLocationChange = function(location) {
  iTriage.setLocation(location);
  $('location').setValue(iTriage.user.currentLocation.display).highlight();
  iTriage.afterLocationChange();
  return true;
}

iTriage.afterLocationChange = function(location){
  return;
}


// Commenting 

iTriage.showLogin = function(){
  // show form
  //
}

iTriage.isLoggedIn = function(){
  
}

iTriage.loadComments = function(comment_id) {
  //gather comment ids
  var comment_ids = $$("#comments .comment").map(function(e){ return e.id.replace(/comment_/, "") });
  
  // retreive votes
  new Ajax.Request("/community/comments/votes_for_comments", {
    method: 'post',
    parameters: {comment_ids:comment_ids.toJSON()},
    onSuccess: function(r) {
      iTriage.updateCommentVotes(r.responseText.evalJSON());
    }
  });
  // mark votes
}

iTriage.updateCommentVotes = function(comment_votes) {
  comment_votes.each(function(comment_vote){
    var voting = $$("#comment_" + comment_vote.comment_id + " .voting").first();
    if (comment_vote.vote == 1) {
      var button = voting.down('.vote_up');
    } else {
      var button = voting.down('.vote_down');
    }
    button.addClassName("selected");
    button.update('');
  });
}

iTriage.replyToComment = function(comment_id) {
  //alert("replying to comment");
  var replyDiv = $$("#comment_" + comment_id + " .reply").first();
  new Ajax.Updater(replyDiv, '/community/comments/new?parent_id=' + comment_id, {
    
  });
  
}

iTriage.voteForComment = function(comment_id, vote) {
  var url = '/community/comments/' + comment_id + '/vote'
  new Ajax.Request(url, {
    method: 'post',
    parameters: {vote:vote}
  });
}

// handle event 'itriage:location_set', thrown after
// location has been set
Event.observe(document, 'itriage:location_loaded', function() {
  $$('.current_location').each(function(e){
    if (e.tagName == "INPUT") {
      e.value = iTriage.user.currentLocation.display;
    } else if (e.tagName == "SPAN"){
      e.update(iTriage.user.currentLocation.display);
    }
  });
});

// add onload observers
Event.observe(window, 'load', function() {
  // if items are classed 'current_location', fetch location
  // and fill them in
  if ($$('.current_location').size() > 0) {
    iTriage.loadLocation();
  }
  
  if ($$('#comments').size() > 0) {
    iTriage.loadComments();
  }
  
  
  // autofocus
  $$(".autofocus").map(function(e){ e.focus(); });
  
});



