$(document).ready(function(){
  if((hash=window.location.hash+"")){
    var crumb=hash.split('!');
    if((crumb[0]=="#")&&(crumb[1])) window.location=crumb[1]; 
  }
  
  thePosts=$('#thePosts');
  theContent=$("#content");
  postsWhiteBox=$(".whitebox",thePosts);
    
  $(".votes a").live('click',function(){
    document.location=$(this).parentsUntil('li').prev('a').attr('href');
  });
  
  $(".post-list li",thePosts).live('mouseenter',function(){
    $('.voteBox',this).toggle();
  }).live('mouseleave',function(){
    $('.voteBox',this).hide();
  });
  
  $(".thumb-up").live('click',function(){
    $(this).parentsUntil('li').addClass('voted');
    $(this).parent().append('votado!');
    votar($(this).attr('_id'));
    
  });
    
  $(".thumb-down").live('click',function(){
    $("#negativepost").val($(this).attr('_id'));
    $("#negativevote-modal").modal();
  });
  
  $(".negativevote a").click(function(){
    var post_id=$('#negativepost').val();
    $("#post-"+post_id+" .voteBox").addClass('voted');
    $("#post-"+post_id+" .voteBox .vote .votelnk").append('votado!');
    votar(post_id,$(this).attr('enum'));
    $.modal.close();
  });

  $('.voteBox').live("mouseenter", function() {
    $(this).prev('a').addClass("postHover");
  }).live("mouseleave",function(){
    $(this).prev('a').removeClass("postHover");
  })
  
  
  triggerPostsOptions();
});

function triggerPostsOptions(){
    
  thePostsAjaxLinks=$("#thePosts .boxOptions a,#thePosts h2 a,#cats-modal a");
  thePostsAjaxLinks.click(function(){
    thePostsAjaxLinks.unbind('click').click(function(){ return false; });
    cargarPosts($(this).attr('href'));
    return false;
  });
  
  $('.showcats').unbind('click').click(function (e) {
    $('#cats-modal').modal();
    return false;
  });
  
  $(".showcats").show();

  posts='posts=';  
  $(".post-list li",thePosts).each(function(){
    var id=$(this).attr('id').split("-");
    posts+=id[1]+',';
  });
  
  $.ajax({
    type: "POST",
    url: voteboxurl,
    data: posts,
    dataType:'json',
    success: function(data){
      $.each(data.posts,function(i,postdata){
        postdata.user=data.user;
        $("#post-"+postdata.id).append(renderVoteBox(postdata));
      });
    }
  });  
}

function cargarPosts(url){
  window.location.hash='!'+url;
  $.modal.close();
  $("#cats-modal").remove();
  $("#postsPages").remove();
  postLists=$(".post-list",thePosts);
  postLists.html('<li style="border:none"></li>').prepend('<h2><span class="spinner"></span>Cargando posts...</h2>');
  $.getJSON(url, function(data) {
    postsWhiteBox.html(data.boxoptions).append(data.posts);
    postLists.hide().fadeIn();
    $('h4',thePosts).html(data.title);
    theContent.prepend(data.catsModal);
    $.scrollTo(thePosts, 500 );  
    triggerPostsOptions();
  });
}

function renderVoteBox(data){
  var boxclass='';
  var span='<a title="positive vote" _id="'+data.id+'" class="thumb-up"></a> ';
  if(data.user){
    span+='<a title="negative vote" _id="'+data.id+'" class="thumb-down"></a>';
  }
  
  if(data.voted){
    boxclass=' voted';
    span='votado!'
  }

  if(!data.votes_open){
    boxclass=' closed';
    span='votos cerrados';
  }
  
    
  return '<div class="voteBox'+boxclass+'">'+
  '<div class="vote publish">'+
      '<div class="votes vote-shakeit"><a>'+data.votes+'</a><br>votos</div>'+
      '<div class="votelnk thumbs">'+span+'<div style="clear: both;"></div></div>'+
  '</div></div>';
}


function votar(post_id,enumval){
  var send='data[Post][id]='+post_id;
  if (enumval) send+='&data[Vote][negative]='+enumval;
  $.ajax({
    type: "POST",
    url: addvote,
    data: send,
    dataType:'json',
    success: function(data){
      if(data.alert){
        alert(data.alert);
      }else{
        if(data.votes){
          $("#post-"+data.id+" .votes a:eq(0)").html(data.votes);
        }
        if(data.karma){
          $("#post-"+data.id+" .postItem span.karma").html(data.karma);
        }
        
      }
      
    }
  });
}
