// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(document).ready(function() {
  $('#slider').slider();

  $('a.TK').click(function() { alert('Not yet implemented.'); return false; });
  
  $('.new_comment .labeled').autoResize().blur(function() {
    if ($(this).val() == '') {
      $(this).css('height','18px');
    }
  });

  $('.labeled').labelIntoInput({'color':'#FFF'});

  $('input[type="text"], input[type="password"]')
    .focus(function() {
      $(this).css({'background-color' : '#FFF'});
    })
    .blur(function() {
      if ($(this).val() == '') {
        $(this).css({'background-color' : '#FDE100'});
      }
    });

  var tooLong = function(t_obj, char_area, c_settings, char_rem){
    $(t_obj).css('color','#FDE100')
      .closest('form').find('[type=submit]').attr('disabled', true);
  }, notTooLong = function(t_obj, char_area, c_settings, char_rem){
    $(t_obj).css('color','inherit')
      .closest('form').find('[type=submit]').attr('disabled', false);
  };

  $('#new_message textarea').NobleCount('#messagetextcounter', {
    on_positive: notTooLong, on_negative: tooLong
  });

  $('.new_comment textarea').NobleCount('', {
    max_chars: 750, on_positive: notTooLong, on_negative: tooLong
  });

  $('.flash').prepend(
    $('<a class="close">X</a>')
      .click(function() { 
        $(this).parent().fadeOut(function() { $(this).detach(); });
      }));

  $('.docomments')
    .click(function() {
      $(this).closest('.dogood')
        .find('.dotalk').slideToggle()
        .find('input[type=text]').focus()
      ;
      return false;
    });

  $('.on_the_spot_editing').closest('.dogood').find('.dowhen:first')
    .after(
      $('<a href="#">edit</a>').click(function(event) {
        event.preventDefault();
        $(this).closest('.dometa').prevAll('.body')
          .find('.on_the_spot_editing').click();
      })
    )
    .after(' &bull; ')
  ;

  $('a.dobackpats')
    .cluetip({
      cluetipClass: 'altrooist',
      dropShadow: false,
      closePosition: 'title',
      closeText: 'X',
      width: 225,
      local: true,
      activation: 'click',
      sticky: true,
      onShow: function(ct, c) { 
        var $container = $(this).closest('.dogood').find('img');
        var offset = $container.offset();
        ct.css({
          left: 'auto',
          right: $('body').width() - offset.left + 12,
          top: offset.top
        });
      }
    });
  $('.dobackpatters')
    .find('h3').remove();
  $('.dotalk')
    .filter(':not(:has(.dogood))').hide().end()
    .find('[type=submit]').hide().end()
    .find('[type=text]')
    .bind('focus', function() {
      $(this).siblings('[type=submit]').slideDown(); 
    });
});

