$(document).ready(function() {
  $(".logout").click(function(){
    e = $(this);
    FB.getLoginStatus(function(response) {
      if (response.session) {
        FB.logout();
      }
      else
      { 
        window.location=e.attr('href');
      }
    });
    return false;
  });

  $('nav ul ul').hide();
  $('nav ul ul').find('li.active').parent().show();
  $('nav ul ul').find('li.active').parent().parent().addClass('expanded');
  $('nav ul li a').click(function() {
    var checkElement = $(this).next();
    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
      $('nav ul ul:visible').slideUp('normal');
      $('nav ul ul:visible').parent().removeClass('expanded');
      return false;
    }
    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
      $('nav ul ul:visible').slideUp('normal');
      $('nav ul ul:visible').parent().removeClass('expanded');
      checkElement.slideDown('normal');
      checkElement.parent().addClass('expanded');
      return false;
    }
  });

/*  $('#featuredNews article div').hide();
  $('#featuredNews article div').first().addClass('expanded');
  $('#featuredNews article div').first().show();
  var animating = 0;
  $('#featuredNews article').mouseover(function() {
    e = $(this).find('div');
    if(!e.hasClass('expanded') && animating==0) {
      animating = 1;
      $('#featuredNews article div').removeClass('expanded');
      $('#featuredNews article div').slideUp('normal');
      e.addClass('expanded');
      e.slideDown('normal',function(){animating=0;});
    }
    return false;
  });*/
  
  if (navigator.appVersion.indexOf("Mac")==-1) {    
    $('.fb_login').html('<fb:login-button perms="email,user_birthday"></fb:login-button>'); }
    
  $("#postComment").hide();
  $("#postCommentLink").toggle(function(){ 
    $("#postComment").show('slow');
  }, function(){
    $("#postComment").hide('slow');
  });
  
  $("#postThread").hide();
  $("#postThreadLink").toggle(function(){ 
    $("#postThread").show('slow');
  }, function(){
    $("#postThread").hide('slow');
  });

  $("#postReply").hide();
  $("#postReplyLink").toggle(function(){ 
    $("#postReply").show('slow');
  }, function(){
    $("#postReply").hide('slow');
  });

  $(".deleteComment").click(function(){
    e = $(this);
    var id = e.parent().parent().attr('id').replace('comment','');
    var language = e.attr('class').replace('deleteComment ','');
    if(!confirm("Are you sure you wish to remove comment #" + id + "?")) { return false; }
    e.html('<img alt="Loading..." src="/assets/img/common/ajax-loader.gif" /> Loading...');
    $.post('/'+language+'/community/ajax',
      {fn:'delete_comment',id:id},
      function(data){
        if(data.success==1) {
          e.parent().parent().remove();}          
        else { 
          alert('An error occurred; the comment was not removed.');}
      },'json');
    return false;
  });
    
  $(".deleteThread").click(function(){
    e = $(this);
    var id = e.parent().parent().attr('id').replace('thread','');
    var language = e.attr('class').replace('deleteThread ','');
    if(!confirm("Are you sure you wish to remove the thread '" + e.parent().parent().find('a').first().text() + "'?")) { return false; }
    e.html('<img alt="Loading..." src="/assets/img/common/ajax-loader.gif" /> Loading...');
    $.post('/'+language+'/community/ajax',
      {fn:'delete_thread',id:id},
      function(data){
        if(data.success==1) {
          e.parent().parent().remove();}          
        else { 
          alert('An error occurred; the thread was not removed.');}
      },'json');
    return false;
  });
  
  $(".deletePost").click(function(){
    e = $(this);
    var id = e.parent().parent().attr('id').replace('post','');
    var language = e.attr('class').replace('deletePost ','');
    if(!confirm("Are you sure you wish to remove post #" + id + "?")) { return false; }
    e.html('<img alt="Loading..." src="/assets/img/common/ajax-loader.gif" /> Loading...');
    $.post('/'+language+'/community/ajax',
      {fn:'delete_post',id:id},
      function(data){
        if(data.success==1) {
          e.parent().parent().remove();}          
        else { 
          alert('An error occurred; the post was not removed.');}
      },'json');
    return false;
  });
});
