$(function(){
  $('form.opinion').submit(function(){
    if($(this).find('textarea:first').is(':blank'))
    {
      $(this).find('textarea').show()
      return false
    }
  })
  
  $('a.vote-form-no-toggle').click(function(){
    form = $(this).parents('div#opinion-forms').find('form.opinion')
    if (form.is(':hidden'))
    {
      form.slideToggle()
    }
    form.find('textarea:first').focus()
    form.find('input#opinion_state').attr('value', 'no')
    form.find('label').text('Please explain why you are saying it does not work:')
    $(this).toggleClass('selected')
  })
  
  $('a.vote-form-yes-toggle').click(function(){
    form = $(this).parents('div#opinion-forms').find('form.opinion')
    if (form.is(':hidden'))
    {
      form.slideToggle()
    }
    form.find('textarea:first').focus()
    form.find('input#opinion_state').attr('value', 'yes')
    form.find('label').text('Please explain why you are saying it works:')
    $(this).toggleClass('selected')
  })

  // Plugins opinions page - forms.  Allows you to click on the comments
  // list anywhere to show and hide it.
  $('li.opinion-item').click(function(){
    empty = $(this).find('.comments-toggle-empty');
    if (empty) {
      submit = empty.parents('li:first').find('span.comment-submit');
      submit.toggle();
      submit.find('textarea:first').focus();
    };
    $(this).find('div.comments-toggle').parents('li:first').find('ul.comment-list').slideToggle()
  });

  // Show or hide the leave comment box
  $('form.comment-form p.toggle').click(function(){
    $(this).parents('form:first').find('span.comment-submit').toggle()
    $(this).parents('form:first').find('textarea:first').focus()
    $(this).toggle();
    return false;
  })

  // Prevent default bubbling on these three actions to make sure the whole
  // comments list doesn't toggle
  $('li.opinion-item textarea').click(function(){
    $(this).focus();
    return false;
  });
  
  $('li.opinion-item input[type=submit]').click(function(){
    $(this).parents('form').submit();
    return false;
  });
  
  $('li.opinion-item a').click(function(){
    window.location = $(this).attr('href')
    return false;
  })

  // Makes all elements on the plugins#index page have a hover to show all states
  $('ul.plugin-list.index li.plugin-item').livequery(function(){
    $(this).bind('mouseover',function(){
      $('a.hidden').hide();
      $(this).children('a.hidden').show();
      $(this).addClass('hovering');
    })
  });
  
  // Pulls in the next lot of pagination, and also sets up the hover state for the new elements
  $('div.pagination a').livequery(function(){
    $(this).click(function(){
      link = $(this)
      $(this).text('loading...')
      $.getJSON($(this).attr('href'), function(data){
        link.parents('div.pagination:first').siblings('ul.plugin-list:first').children('li:last').after(data.plugins)
        link.parents('div.pagination:first').replaceWith(data.pagination)
      })
      return false
    })
  })

  // Returns true if the parent form from the referenced element is a "new record" false otherwise
  $.fn.newRecord = function(){
    if (this.parents('form').find('input').is('[value=put]')) {
      return false;
    } else {
      return true;
    }
  }

  // Hides the URL Check box on the my/plugins/name/edit form if it is linked to RubyGems
  if ($('input#plugin_link').is(':checked')) {
    $('div#RubyGemsUrl').hide();
  }

  // Hides the URL Check box my/plugins/name/edit and new forms if clicking link to RubyGems
  $('input#plugin_link').click(function(){
    if ($(this).newRecord()) {
      $('div#NoRubyGems').slideToggle(500);
    } else {
      $('div#RubyGemsUrl').slideToggle(500);
    }
    $('div#RubyGems').slideToggle(500);
  });

  // Search Box
  $(function() {
    $("#search").autocomplete({
      source: '/plugins/search',
      delay: 200,
      select: function(event, ui) { window.location = '/plugins/'+ui.item.id; },
    });
    
    $('#search').click(function(){
      if($(this).val() == 'Search Plugins') {
        $(this).removeClass('empty').val('');
      }
    });

    $('#search').blur(function(){
      if($(this).val() == '') {
        $(this).addClass('empty').val('Search Plugins');
      }
    });
    
  });

  // Login Page - Hides or shows the login, clearing and storing previous values in 
  // the process to make sure we don't submit the wrong thing
  $('a#openid-link').click(function(event){
    event.preventDefault();
    $('#login').hide().data('login', $('input#user_session_login').val());
    $('input#user_session_openid_identifier').val($('#openid').data('login'));
    $('#openid').show();
    $('input#user_session_login').val('');
  })

  $('a#login-link').click(function(event){
    event.preventDefault();
    $('#openid').hide().data('login', $('input#user_session_openid_identifier').val());
    $('input#user_session_login').val($('#login').data('login'));
    $('#login').show();
    $('input#user_session_openid_identifier').val('');
  })

  // Login Page - Hides or shows the login, clearing and storing previous values in 
  // the process to make sure we don't submit the wrong thing
  $('a#show-comments').click(function(event){
    event.preventDefault();
    $('ul.comments-present').show();
  })



})