IECompatibleHover = $.klass({
  onmouseover: function() {
    this.element.addClass('hover');
  },
  onmouseout: function() {
    this.element.removeClass('hover');
  }
});

IETextIndentMinusNinetyNineThousandNineHundredAndNinetyNine = $.klass({
  initialize: function() {
    if ($.browser.version == 6.0 || $.browser.version == 7.0)
      $(this.element).attr('value','');
  }
});

Slidable = $.klass({
  initialize: function(form, parent) {
    this.form = form;

    this.element.addClass('up');
    if (!parent.is('.nohide'))
      this.slide(0);
  },

  onclick: function() {
    this.slide('fast');
    return false;
  },

  slide: function(speed) {
    if (this.form.is(':hidden')) {
      this.element.removeClass('down');
      this.element.addClass('up');
    } else {
      this.element.removeClass('up');
      this.element.addClass('down');
    }

    this.form.slideToggle(speed);
  }
});

Popup = $.klass({
  initialize: function(poppable) {
    this.poppable = poppable;
    this.poppable.hide();

    // Add a white transparent background
    if ($.browser.msie && $.browser.version == 6.0) {
      this.cover = null;
    } else {
      $('body').prepend('<div class="cover"/>');
      this.cover = $('body > div.cover');
      this.cover.hide();
      this.cover.css({'opacity':0.2,'background':'#000','float':'left','position':'fixed','top':'0','left':'0','width':'100%','height':'100%'});
      this.poppable.css({'z-index':'9'});
    }

    // Put inside another div
    this.poppable.html('<div class="content">'+this.poppable.html()+'</div>')

    // Add heading
    this.poppable.children('.content').prepend('<h3>'+$(this.element).attr('title')+'</h3>');

    // Add close button/link
    this.poppable.prepend('<a class="closebutton" href="#">Close</a>');
    this.poppable.children('.content').append('<a class="closelink" href="#">Close</a><div class="clear"/>');

    // And the close events
    poppable = this.poppable;
    cover = this.cover;
    show = this.show;
    this.poppable.children(':first, :last').each(function(){
      $(this).click(function(){poppable.fadeOut(100);cover&&cover.fadeOut(100);return false});
    });
    this.cover&&this.cover.click(function(){poppable.fadeOut(100);cover&&cover.fadeOut(100);return false});

    // Styling
    this.poppable.addClass('poppable');

  },
  onclick: function() {
    if (this.poppable.is(':hidden') || (this.cover&&this.cover.is(':hidden'))) {
      this.poppable.fadeIn(100);
      this.cover&&this.cover.fadeIn(100);
    } else {
      this.poppable.fadeOut(100);
      this.cover&&this.cover.fadeOut(100);
    }
    return false;
  }
});

function classify_onclick() {
  var form = $(this.element).parents('form');
  if (form.find('select').val() == '') {
    form.effect('shake', {times: 3, distance: 5}, 100);
    return false;
  }
  var uri = form.attr('action');
  if (uri == '') uri = window.location+'';
  uri = uri.replace(/https?:\/\/[^\/]+\//, '/');
  var data = form.serialize()+'&json=1';
  jQuery.post(uri, data, this.callback, 'json');
  return false;
}

ClassifyAdd = $.klass({
  callback: function(data) {
    if (data['resolution'] == 'Added.') {
      $('#classifier .flash').remove();
      var val = data['label'];

      $('#classifier form.add select option').each(function() {
        if ($(this).val() == val)
          $(this).attr('disabled', 'disabled');
      });
      $('#classifier .existing').append('<form action="" method="post" class="remove"><p><span>'+val+'</span> <input type="hidden" name="action" value="Remove"/><input type="hidden" name="label" value="'+val+'"/><input type="submit" class="remove" value="Remove" title="Remove"/></p><div class="clear"/></form>');
      var form = $('#classifier .existing').children(':last');
      form.find('input[type="submit"]').attach(ClassifyRemove);
      var h = form.css('height');
      form.css({'opacity':0,'height':0});
      if (h == 'auto') h = '24px'; // IE
      form.animate({'opacity':1,'height':h}, 'slow');
    } else if (data['resolution'] == 'Already added.') {
      var val = data['label'];
      $('#classifier form.remove input[name="label"]').each(function() {
        if ($(this).val() == val) {
          $(this).parents('form').effect('shake', {times: 3, distance: 5}, 100);
        }
      });
    } else {
      alert('Error: Could not add label. Try refreshing the page.');
    }
  },
  onclick: classify_onclick
});

ClassifyRemove = $.klass({
  initialize: function() {
    $(this.element).attach(IETextIndentMinusNinetyNineThousandNineHundredAndNinetyNine);
  },
  callback: function(data) {
    if (data['resolution'] == 'Removed.') {
      $('#classifier .flash').remove();
      var val = data['label'];

      $('#classifier form.remove input[name="label"]').each(function() {
        if ($(this).val() == val) {
          form = $(this).parents('form');
          form.animate({'opacity':0,'height':0}, 'slow', function(){$(this).remove()});
        }
      });
      $('#classifier form.add select option').each(function() {
        if ($(this).val() == val)
          $(this).removeAttr('disabled');
      });
    } else {
      alert('Error: Could not remove label. Try refreshing the page.');
    }
  },
  onclick: classify_onclick
});

jQuery(function($) {
  if ($.browser.msie) { /* Let's play browser manufacturers! */
    $('body.law input.add, body.law input.remove').attach(IETextIndentMinusNinetyNineThousandNineHundredAndNinetyNine);
    if ($.browser.version == 6.0)
      $('ul.buttons li, .blogs .blog h2, input[type="submit"]').attach(IECompatibleHover);
  }

  var a=$('#advanced_search h2');
  a.html('<a href="#">'+a.html()+'</a>');
  a=$('#other_resources h2.other_resources');
  a.html('<a href="#">'+a.html()+'</a>');
  $('#advanced_search h2 a').attach(Slidable, $('#advanced_search_form'), $('#advanced_search'));
  $('#other_resources h2.other_resources a').attach(Slidable, $('#other_resources .references_citations'), $('#other_resources'));

  $('#tools').tabs();

  $('a.popup').each(function(){
    $(this).attach(Popup, $($(this).attr('href')));
  });

  $('#classifier form.add input[type="submit"]').attach(ClassifyAdd);
  $('#classifier form.remove input[type="submit"]').attach(ClassifyRemove);

  $('body.discover div.content').addClass('main');
  $('body.discover div.content.main').after('<div class="content overflow"></div>');
  $('body.discover div.content').css('float','left');
  $('body.discover div.content.main').each(function(){
    var thus = $(this);
    var nul = $(this).next();
    while (thus.children().length > nul.children().length+1) {
      var e = thus.children(':last-child');
      e.remove();
      nul.prepend(e);
    }
  });
});
