$(document).ready(function() {
  
  $('a[href=' + window.location.pathname + ']').parent().addClass('active');
  
  $('.itemcomment').click(function() {
    var comment = this;
    var self = $(this);
    var commentdiv = self.parent().next();
    var count = self.find('.itemcommentcount');
    var expand = self.find('.itemcommentexpand');
    if (commentdiv.html()) {
      commentdiv.html(null);
      expand.html('+');
    }
    else {
      var loc = self.attr('href')
      $.get(loc, {}, function(data){ 
        commentdiv.html(data);
        expand.html('-');
        var comm = self.parent().parent();
        comm.find('.itemcommentform').submit(function() {
          $.post(loc, {'itemcommenttext': comm.find('.itemcommenttext').val()}, function (newdata) {
            count.html((count.html()) ? parseInt(count.html()) + 1 : 1);
            self.click();
            self.click();
            commentdiv.html(newdata);
          });
          return false;
        });
      });
    }
    return false;
  });
  
  $('.itemsignup input').click(function() {
    self = this;
    info = $(this).next()
    $.post('/cal', {'eventid': this.value, 'status': this.checked}, function(data) {
      info.children('.itemsignuptext').html(self.checked ? 'Signed up' : 'Sign Up');
      count = info.children('.itemsignupspaces')
      count.html(parseInt(count.html()) + (self.checked ? -1 : 1))
    });
  });
  
  $('#loginbutton').click(function() {
    $('#darkbox').show();
    $('#login').show();
    $('#loginemail').select();
    return false;
  });
  
  $('#logincancel').click(function() {
    $('#login').hide();
    $('#darkbox').hide();
    return false;
  });
  
});

