var notice_anim = false;
var notice_current = 1;
var notice_paused = false;

$(document).ready(function(){
  // Make boxes clickable.
  $(".post-item").each(function(){
    $(this).click(function(){
      var postItemId = this.id.replace("post-item-", "");
      location.href = "?p=" + postItemId;
    });
    
    $(this).css("cursor", "pointer");
  });

  // Notices.
  var notice_count = $('.selector').length;
  var notice_next = notice_current + 1;

  $('.selector').bind('click', function(){
    // Read selector index.
    var notice_id = this.id.replace(/\D/g, '');
    
    if((!notice_anim) && (notice_id != notice_current)){
      if(notice_id > notice_current){
        var notice_hide_direction = "left";
        var notice_show_direction = "right";
      } else {
        var notice_hide_direction = "right";
        var notice_show_direction = "left";
      }
      
      notice_anim = true;
      $('.notice-active').hide("slide", { direction: notice_hide_direction }, 1000);
      $('#notice-' + notice_id).show("slide", { direction: notice_show_direction }, 1000, function(){
        // Set active class.
        $('.notice-active').removeClass('notice-active');
        $(this).addClass('notice-active');
        
        // Replace the dots with correct ones.
        var selector_new = document.getElementById('selector-' + notice_id).src.replace("-inactive", "-active");
        var selector_old = document.getElementById('selector-' + notice_id).src.replace("-active", "-inactive");
        $('#selector-' + notice_id).attr("src", selector_new);
        $('#selector-' + notice_current).attr("src", selector_old);
        
        notice_current = notice_id;
        notice_anim = false;
        notice_next = parseInt(notice_current) + 1;
        
        if(parseInt(notice_current) >= notice_count){
          notice_next = 1;
        }
      });
    }
  });
  
  // Number of images.
  function noticeScroll(){
    $('#selector-' + notice_next).trigger('click');
  }
  
  setInterval(noticeScroll, 12000);
});