/* fix IE background image flicker */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err) {};

$(document).ready(function() {
  
  $('li.nav_tab').hover(
    function()  {
      $(this).children('a').addClass('active').siblings('div').show();
    },
    function()  {
      $(this).children('a').removeClass('active').siblings('div').hide();
    });
  
  $('li#signin a').click(
    function(){
      $(this).siblings().show();
      $('input#email').focus();
  });
  
  $('li#signin a.cancel').click(
    function(){
      $(".signin_form").hide();
  });
  
  $('li.system_announcement a.cancel').click(
    function(){
      $(this).parent(".system_announcement").hide('blind', {}, 'fast');
  });
    
  $('#show_post_form').click(function() {
    $('div#post_form_wrapper').slideDown('fast');
    $(this).removeClass('inactive');
  });
  
  $('#cancel_new_post').click(function() {
    $('div#post_form_wrapper').slideUp('fast');
    $('#post_title').val('');
    $('#post_body').val('');
    $('#show_post_form').addClass('inactive');
    return false;
  });
  
  $('.show_full_post').click(function() {
    $(this).hide().siblings('.truncated_post').hide();
    $(this).siblings('.hide_full_post').show().siblings('.full_post').show();
  });
  
  $('.hide_full_post').click(function() {
    $(this).hide().siblings('.full_post').hide();
    $(this).siblings('.full_post').find('textarea').val('');
    $(this).siblings('.show_full_post').show().siblings('.truncated_post').show();
  });
  
  $('#expand_posts').click(function() {
    $(this).hide().siblings('#minimize_posts').show();
    $('.show_full_post').hide().siblings('.hide_full_post').show();
    $('.truncated_post').hide().siblings('.full_post').show();
  });
  
  $('#minimize_posts').click(function() {
    $(this).hide().siblings('#expand_posts').show();
    $('.hide_full_post').hide().siblings('.show_full_post').show();
    $('.full_post').hide().siblings('.truncated_post').show();
  });

  $('.expand_post').click(function() {
    $(this).parents('.truncated_post').hide()
    .siblings('.full_post').show()
    .siblings('.truncated_post').hide()
    .siblings('.hide_full_post').show()
    .siblings('.show_full_post').hide();
  }); 

  $('.cancel_new_comment').click(function() {
    $(this).parents('.full_post').hide()
    .siblings('.truncated_post').show()
    .siblings('.hide_full_post').hide()
    .siblings('.show_full_post').show();
    $(this).parent().siblings().find('textarea').val('');
  });
  
  $('.comments_count').click(function() {
    $(this).parent().parent().hide().siblings('.full_post').show().siblings('.truncated_post').hide().siblings('.hide_full_post').show().siblings('.show_full_post').hide();
  });

  $(".date_picker").datepicker({ 
      dateFormat: "DD, MM d, yy",
      mandatory: true,
      showOn: "button", 
      showAnim: "slide", 
      showOptions: { 
        direction: "up" 
      },
      buttonImage: "/images/calendar.gif", 
      buttonImageOnly: true 
  });
  
  $(".birthdate_picker").datepicker({ 
       dateFormat: "DD, MM d, yy",
       yearRange: '-20:+0',
       showOn: "button", 
       showAnim: "slide", 
       showOptions: { 
         direction: "up" 
       },
       buttonImage: "/images/calendar.gif", 
       buttonImageOnly: true 
   });
   
  $("#meeting_repeat_false").click(function() {
   $('#meeting_fields').hide();
   $('#meeting_frequency').val('');
   $('#meeting_end_date').val('');
  });

  $("#meeting_repeat_true").click(function() {
   $('#meeting_fields').show();
  });
  
  $('#meeting_host_false').click(function() {
    $('#hosting_fields').hide();
    $('#location_fields').show();
    $('#hosting_fields select').val('');
  });
  
  $('#meeting_host_true').click(function() {
    $('#hosting_fields').show();
    $('#location_fields').hide();
    $('#location_fields input, #location_fields textarea').val('');
  });
   
  $("#contact_by_network").click(function(){
   $('#email_contact_import').show().find('select, input:not(.button)').val('');
   $('#email_invite_form').hide();
   $('#email_contact_list_container').html('');
   $('#network_invite_form').show();
  });

  $("#contact_by_email").click(function(){
   $('#email_invite_form').show();
   $('#network_invite_form').hide().find('input').attr('checked', '');
  });
   
  $('#activity_add_leader').click(function(){
   $('#add_leader_form').show();
   $('#manage_members_form, #add_members_form').hide().find('input:not(.button)', 'textarea', 'select').val('').attr('checked', '');
  });

  $('#activity_manage_members').click(function(){
   $('#manage_members_form').show();
   $('#add_leader_form, #add_members_form').hide().find('input:not(.button)', 'textarea', 'select').val('').attr('checked', '');
  });

  $('#activity_add_members').click(function(){
   $('#add_members_form').show();
   $('#manage_members_form, #add_leader_form').hide().find('input:not(.button)', 'textarea', 'select').val('').attr('checked', '');
  });
   
  $('ul.tabs input:checked').parent('li').addClass('selected');
  $('ul.tabs .radio').click(function(){
    $(this).parent('li').addClass('selected');
    $(this).parent('li').siblings().removeClass();
  });
  
  $('#agenda_day').accordion({
    header: 'dt',
    active: false,
    animated: false,
    alwaysOpen: false
  });
  
  $('.sidebar_entry').accordion({
    header: 'h4',
    animated: false,
    alwaysOpen: false
  });
    
  $('#receive_updates :checkbox').attr('checked');
  $('#receive_updates :checkbox').click(function(){
    $('#receive_updates ul').toggle();
  });
  
  showFeedLink = function(){
      window_w    = $(window).width();
    $('.popup').click(function(e){
      caption     = $(this).siblings(".feed_link_caption");
      $("body").append(caption);
      caption_w   = $(caption).width();
      $(caption)
        .css("left",(window_w - caption_w)/2 + "px")
        .fadeIn("fast");
      },
      
    $('.feed_link_caption a.cancel').click(function(){
      caption = $(this).parent(".feed_link_caption");
      $(caption).fadeOut('fast');
      $("li."+$(caption).attr('title')).append(caption);
      })
    )
  };
  showFeedLink();
  
  $('li.print a').click(function(){
   window.print(); 
  });
});


