$(document).ready(function() {
  // Sidebar code
  $('.sidebar_section_title').click(function() {
    $(this).next('ul').slideToggle();
    var str = $(this).children('img').attr('src');
    if( str.match('minus') ) {
      $(this).children('img').attr('src', $(this).children('img').attr('src').replace('minus', 'plus'));
    } else if( str.match('plus') ) {
      $(this).children('img').attr('src', $(this).children('img').attr('src').replace('plus', 'minus'));
    }
  });
  
  // Comments section
  if($('.comments_count')) {
    $('.comments_count').each(function(index) {
      var cType;
      var cId = $(this).parent().attr('id');
      if( $(this).parent().hasClass('news') ) {
        cType = 'news';
      }
  
      $.getJSON('/cgi-bin/site/comment/1/'+cType+'/'+cId+'/count', function(data) {
        $('#'+cId).children('.comments_count').append('<p>Comments ('+data.count+')</p>');
      });
    
      $(this).click(function() {
        if($(this).next('.comments').children().size() == 0) {
          $(this).next('.comments').load('/cgi-bin/site/comment/1/'+cType+'/'+cId);
        } else {
          $(this).next('.comments').children().remove();
        }
      });
    });
  }

  // Charlie Sheen comment loader
  $('.sheen_btn').click(function() {
    $('.sheen').load('/cgi-bin/sheen');
  });
  
  // Cave Jonhson comment loader
  $('#cave_btn').click(function() {
    $('#cave_quote').load('/cgi-bin/cave');
  });

  // dice code
  $('#roll_em').click(function() {
    var numberOfDice = parseInt($('#number_of_dice').val());
    var typeOfDice   = parseInt($('#type_of_dice').val());
    var cumulativeAmount = 0;
    var i;
    
    for( i = 0; i < numberOfDice; i++) {
      var roll = parseInt(Math.floor(Math.random()*typeOfDice) + 1);
      cumulativeAmount += roll;
      $('#dice_result').append('Roll '+(i+1)+': '+roll+'<br />');
    }
    $('#dice_result').append('Total: '+cumulativeAmount+'<br />');
  });

  $('#clear_results').click(function() {
    $('#dice_result').html('');
  });

  //Skyrim countdown
/*  var today         = new Date();
  var release_month = 10;
  var release_year  = 2011;
  var skyrim_msg    = '';
  if( today.getMonth() == 10 && today.getFullYear() == release_year ) {
    var skyrim_count  = parseInt(12 - today.getDay());
    if( skyrim_count > 0 ) {
      skyrim_msg = skyrim_count+" days till Skyrim!";
    } else if( skyrim_count == 0 ) {
      skyrim_msg = "Skyrim released today! What are you doing here? You should be playing Skyrim!";
    } else if( skyrim_count < 0 ) {
      skyrim_msg = "Skyrim has been released, why aren't you playing it?";
    }
  } else {
    skyrim_msg = "Skyrim has been released, why aren't you playing it?";
  }
  $('.sr_countdown').html(skyrim_msg);*/
});

