$.extend($.validator.messages, {
    required: "Required",
    remote: "Please fix this field.",
    email: "Invalid",
    url: "Please enter a valid URL.",
    date: "Please enter a valid date.",
    dateISO: "Please enter a valid date (ISO).",
    number: "Please enter a valid number.",
    digits: "Please enter only digits.",
    creditcard: "Please enter a valid credit card number.",
    equalTo: "Please enter the same value again.",
    accept: "Please enter a value with a valid extension.",
    maxlength: "Too Long", // $.validator.format("Please enter no more than {0} characters."),
    minlength: "Too Short", 
    rangelength: $.validator.format("Please enter a value between {0} and {1} characters long."),
    range: $.validator.format("Please enter a value between {0} and {1}."),
    max: $.validator.format("Please enter a value less than or equal to {0}."),
    min: $.validator.format("Please enter a value greater than or equal to {0}.")
});

var auth_token;
var random_video_id = Math.floor(Math.random()*111234);
$(document).ready(function() {
  $('a#new_video').click(function() {
    random_video_id += 1
    var video_li = $('<li id="video_'+random_video_id+'"></li>')
    var video_label = $('<label for="video_'+random_video_id+'_url">URL</label>');
    var video_url = $(' <input type="text" name="videos['+random_video_id+'][url]" id="video_'+random_video_id+'_url" size="60" />');
    var video_type = $(' <select name="videos['+random_video_id+'][video_type]">'+video_types+'</select>');
    var video_label2 = $('<label for="video_'+random_video_id+'_title">Title</label>');
    var video_title = $(' <input type="text" name="videos['+random_video_id+'][title]" id="video_'+random_video_id+'_title" size="60" />');
    var video_label3 = $('<label for="video_'+random_video_id+'_description">Description</label>');
    var video_description = $(' <input type="text" name="videos['+random_video_id+'][description]" id="video_'+random_video_id+'_description" size="100" />');
    var video_delete = $(' <a href="#" class="delete_video">Delete video</a>')
    $(video_li).append(video_label);
    $(video_li).append(video_url);
    $(video_li).append(video_type);
    $(video_li).append(video_label2);
    $(video_li).append(video_title);
    $(video_li).append(video_label3);
    $(video_li).append(video_description);
    $(video_li).append(video_delete);
    $('table.videos .single').append(video_li);
    return false;
  });
  $('a.delete_video').live('click', function() {
    $(this).parent().remove();
    return false;
  });
  $('a.add_definition').click(function() {
    var index = definition_index;
    definition_index += 1;
    var container = $('<li id="definition_'+ index +'"></ul>');
    var label = $('<label for="award_definitions_attributes_' + index + '_proc">Trigger code. (must result in true or false)</label>');
    var text_area = $('<textarea cols="40" id="award_definitions_attributes_' + index + '_code" name="award[definitions_attributes][' + index + '][code]" rows="20"></textarea>');
    var delete_link = $('<a href="#" class="delete_definition">Delete definition</a>')
    var br = $('<br/>')
    container.append(label);
    container.append(br);
    container.append(text_area);
    container.append(delete_link);
    $('#definitions').append(container);
    return false;
  })

  $('a.delete_definition').live('click', function() {
    var index = $(this).parent().find('textarea').attr('name').match(/\d+/)[0];
    $(this).parent().remove();
    $('input#award_definitions_attributes_' + index + '_id').remove();
    return false;
  });

  $('button.buildSave').click(function() {
    $(this.form).submit();
    $(this).attr('disabled', 'disabled');
    return false;
  });
  $('button.savePicks').click(function() {
    $(this.form).submit();
    $(this).attr('disabled', 'disabled');
    return false;
  });  
  $('#forumBase input[value="Submit"]').click(function() {
    $(this.form).submit();
    $(this).attr('disabled', 'disabled');
    return false;
  });
  $('.comments input[type="submit"]').click(function() {
    $(this.form).submit();
    $(this).attr('disabled', 'disabled');
    return false;
  });
  
  auth_token = $('meta[name=csrf-token]').attr('content');

  var current_user;
  if ($('meta[name=current_user]').length > 0) {
    current_user = $('meta[name=current_user]').attr('content');
  }
  var is_admin_page;
  if ($('meta[name=is_admin_page]').length > 0) {
    is_admin_page = true;
  }

  $('.disable_links').click(function(e) {
    e.preventDefault();
  });

  $('.fyeo').hide();
  if (current_user != null) {
    $('.nfye[data-nfye='+current_user+']').hide();
    $('.fyeo[data-fyeo='+current_user+']').show();
  }

  $('.flaggable').click(function(e){
    e.preventDefault();
    var el = $(this);
    $.ajax({
      url: '/flags',
      type: 'POST',
      data: {
        "flag[related_class]": el.attr('data-related_type'),
        "flag[related_id]": el.attr('data-related_id')
      }
    });
    el.attr('href','');
    el.removeClass('flaggable');
    el.html('Reported, thank you!')
    el.delay(1000).fadeOut();
  });

  $("ul.fighter_tweets_feed a[rel=nofollow]").live('click',function(e) {
    var link_href = $(this).attr('href');
    if ($(this).attr('data-method') == null) { 
      e.preventDefault();
      $("#nofollow-confirm").dialog({
        resizable: false,
        height:140,
        modal: true,
        title: 'Be careful',
        buttons: {
          'Yes': function() {
            window.location = link_href;
            $(this).dialog('close');
          },
          Cancel: function() {
            $(this).dialog('close');
          }
        }
      });
    }
  });


  if (is_admin_page != true)
    $("#flash_messages").delay(30000).fadeOut(400);

  $(".reply_link").click(function() {
    $("#post_parent_post_id").val($(this).attr("data_post_id"));
  });

  $(".thumber").click(function(){
    if (current_user) {
      var t = $(this);
      $.ajax({
        url: '/thumbs',
        type: 'POST',
        data: {
          "thumb[dir]": t.attr('data_dir'),
          "thumb[related_type]": t.attr('data_related_type'),
          "thumb[related_id]": t.attr('data_related_id')
        },
        dataType: 'json',
        success: function(r) {
          var par = t.parent();
          $('.voteUp',par).html(r['up_vote_count']);
          $('.voteDown',par).html(r['down_vote_count']);
        }
      });
    }
  });

  $("#site_search_type input[name='model']").change(function(el) {
    $('#site_search_type label').removeClass('selected');
    $(el.target).parent().addClass('selected');
  });

  $('#quick_login_link').click(function(e){
    e.preventDefault();
    $('#quick_login_link').hide();
    $('#quick_login').slideDown(120);
  });


  $('.tabbed_list a').click(function(e) {
    e.preventDefault();
    var el = $(e.target);
    el.parent().find('a').removeClass('active');
    el.addClass('active');
    var cel = el.closest('.tabbed_list').parent().find('.tabbed_content');
    cel.find('.tab').hide();
    tab_id = el.attr('href');
    cel.find(tab_id).show();
  });

  $('.collapsed_section .header a').toggle(function(){
    $(this).parent().parent().find('.content').show();
  },function(){
    $(this).parent().parent().find('.content').hide();
  });

  $('.find_post_anchor').click(function(e){
    e.preventDefault();
    var post_url = $(this).attr('href');
    var post_id = post_url.split('forum/')[1].split('-')[0];
    if ($('#post-'+post_id).length > 0) {
      $.scrollTo('#post-'+post_id);
      return false;
    }
  });

  $('.unfollow_link a').click(function(e){
    e.preventDefault();
    $.ajax({
      'url': $(this).attr('href'),
      'type': 'POST',
      'data': {
        'authenticity_token': auth_token
      }
    });
    var fl = $(this).parents('.following_links');
    var afl = $('.following_links[data-user='+$(fl).attr('data-user')+']');
    afl.find('.unfollow_link').hide();
    afl.find('.follow_link').show();
    return false;
  });

  $('form.follow_link').submit(function(e){
    e.preventDefault();
    $.ajax({
      'url': $(this).attr('action'),
      'type': 'POST',
      'data': {
        'authenticity_token': auth_token
      }
    });
    var fl = $(this).parents('.following_links');
    var afl = $('.following_links[data-user='+$(fl).attr('data-user')+']');
    afl.find('.unfollow_link').show();
    afl.find('.follow_link').hide();
  });

});

