var SPINNER_SMALL = $('<img>').attr('src', '/images/spinner_small.gif').addClass('spinner small');
var SPINNER_MED = $('<img>').attr('src', '/images/spinner_medium.gif').addClass('spinner medium');

$.fn.closeTo = function(element) {
  var element = $(element);
  $(this).css({
    'top'  : element.position().top,
    'left' : (element.position().left + element.width() + 10)
  });
  return $(this);
}

var makeFieldWithHintText = function(field) {
  field.
    css('color', '#999').
    css('font-style', 'italic').
    val($(field).attr('hinttext')).
    one('click focus keydown', function() {
      $(this).css('color', '#333').css('font-style', 'normal').val('').select();
    });

  field.parents('form:first').submit(function(e) {
    if (jQuery.trim(field.val()) == jQuery.trim(field.attr('hinttext')))
      field.val('');
  });
}

$.fn.linqia_tooltip = function() {
  return this.each(function(index){
    $(this).attr('id', 'tooltip-'+index);

    $(this).mouseenter(function() {
      $(this).find('.tooltip-message:first').clone().
        attr('id', 'tooltip-message-'+$(this).attr('id')).
        appendTo('body').
        css({
          'zIndex' : 123,
          'top'    : $(this).offset().top + 24,
          'left'   : $(this).offset().left
        }).fadeIn();
    });

    $(this).bind('mouseout', function() {
      $('#tooltip-message-'+$(this).attr('id')).fadeOut('fast', function() { $(this).remove(); });
    });
  });
}

$(document).ready(function() {
  $('#login').click(function(e) {
    var login_label_link = $(this).children('a');
    $('#login-bar #upper').slideToggle('fast', function() {
      $(this).find('input:not([type=hidden]):first').select();
      if (login_label_link.text() == 'Close')
        login_label_link.text('Login');
      else
        login_label_link.text('Close');
    });
    e.preventDefault();
  });

  //
  // in-field hints for login bar
  //
  $('#username').click(function() { $(this).val(''); });
  if (!$('#username').val())
    $('#username').val('Username');

  var hint_password_field = $('<input>').
    attr({
      'type'  : 'text',
      'class' : 'text',
      'tabindex' : $('#password').attr('tabindex')
    }).val('Password').
    focus(function() {
      $(this).hide().prev().show().val('').focus();
    }).hide();

  // setup
  $('#password').after(hint_password_field);
  if (!$('#password').val())
    $('#password').hide().next().show();

  $('#password').blur(function() {
    if (!$(this).val())
      $(this).hide().next().show();
  });
  //eof: in-field hints for login bar

  $('#logout').click(function(e) {
    window.location.href = $(this).find('a:first').attr('href');
    e.preventDefault();
  });

  $('.tooltip').linqia_tooltip();

  $('#flashes .closing-handle').click(function(e) {
    e.preventDefault();
    $(this).parent().fadeOut('fast');
  });

  if ($('#flashes')) {
    $('#flashes').
      animate({ opacity: 1.0 }, 2000).
      fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
  }

  // Setup form fields containing placeholder (hint) text
  $('[hinttext]').each(function(e) {
    if ($(this).val() == '' || $(this).val() == $(this).attr('hinttext'))
      makeFieldWithHintText($(this));

    $(this).blur(function() {
      if ($(this).val() == '')
        makeFieldWithHintText($(this));
    });
  });

  // every INPUT with class 'number' won't accept
  // users inserting nothing but numbers
  $('input.number').keypress(function(e) {
    if ((e.keyCode != 9 && e.keyCode != 8 && e.keyCode != 37 && e.keyCode != 39) && (e.charCode < 48 || e.charCode > 57))
      e.preventDefault();
  });
});

