//// CORE EXTENSIONS

// String extensions
$.extend(String.prototype, {
  /* easier way to check if something is
   * contained within a string
   *
   * Ex:
   *  if ('web, design, development'.include('design')) {
   *    // do something
   *  }
   */
  include: function(s) {
    return (this.toString().indexOf(s) > -1)
  }
});

//// PLUGIN EXTENSIONS

// tagFunctions namespace
$.extend({
 tagFunctions: {
   // removes a tag from the value of a textbox
   // - tb => textbox in question
   // - tag => tag to remove
   remove: function(tb, tag, separator) {
     var tags = $.grep(tb.val().split(separator), function(t) { return (t != tag) })
     tb.val(tags.join(separator))
   },
   // appends a tag to the value of a textbox (comma-delimited)
   // - tb => textbox in question
   // - tag => tag to remove
   append: function(tb, tag, separator) {
     if (tb.val().length > 0 && tb.val().charAt(tb.val().length - 1) != separator) {
       tb.val(tb.val() + separator) 
     }
     tb.val(tb.val() + tag)
   }
 }
})

// comments namespace
$.extend({
  comments: {
    // loads form for page cached pages
    loadForm: function(path) {
      $.get(path, function(data) {
        $('#comment-form-wrapper').html(data)
      })
    },
    // appends a new comment and highlights the containing div
    addNewComment: function(html, e) {
      $(e).animate({ marginLeft: '0px' }).html(html)
    },
    // clears the textarea of the comments form
    clearForm: function(e) { 
      $(e).val('') 
    },
    // updates the latest comment in the header
    updateLatest: function(html, e) {
      $(e).html(html)
    },
    // updates comment count in sidebar
    updateCommentCount: function(e) {
      var current_count = Number($(e).html().match(/\d*/))
      $(e).html((current_count == 0 ? '1 comment' : String(current_count + 1) + ' comments'))
    },
    // supports ajax comment previews
    preview: function(e1, e2) {
      $.ajax({
        type: 'POST',
        url: '/comments/preview',
        data: $(e1).serialize(),
        success: function(html) {
          $(e2).html(html).animate({ marginLeft: '15px' })
        }
      })
    },
    // error message when comments can't be saved
    errorMessage: function() {
      return '<h1>Ooooops! Something went wrong...</h1>' + 
        '<p>Things to check:</p><ul>' +
        '<li>Make sure you fill in all required fields</li>' +
        '<li>No malicious or profane comments allowed</li></ul>' 
    }
  }
})

//// PLUGINS

$.fn.friendlyTextBox = function(msg) {
  var regex = new RegExp('^' + msg + '$')

  $(this).focus(function() {
    if ($(this).val().match(regex)) $(this).val('')
    $(this).addClass('active')
  }).blur(function() {
    if ($(this).val() == '') $(this).removeClass('active').val(msg)
  })
}

// http://github.com/rpheath/tag-toggler/tree/master
$.fn.toggleTags = function(options) {
  // defaults
  var settings = $.extend({
   text_box: 'input#tags',
   separator: ', '
  }, options)

  // when a tag is clicked...
  this.click(function() {
    // vars
    var tb = $(settings.text_box),
       tag = this.innerHTML

    // is it already in the list of tags?
    // if so, remove it; if not, add it
    if (tb.val().include(tag)) {
      $.tagFunctions.remove(tb, tag, settings.separator)
    } else {
      $.tagFunctions.append(tb, tag, settings.separator)
    }
  })

  return $(this)
}

// comments: ajax preview
$.fn.ajaxPreview = function(options) {
  // defaults
  var settings = $.extend({
    form_id: '#post-comments',
    preview: '#new-comment'
  }, options)
  
  this.live('click', function() {
    $.comments.preview(settings.form_id, settings.preview)
    return false
  })
  
  return $(this)
}

// comments: ajax submit
$.fn.ajaxify = function() {
  // catch the form submission
  this.live('submit', function() {
    $.ajax({
      type: 'POST',
      url: this.action,
      data: $(this).serialize(),
      dataType: "script",
      error: function() { $.facebox($.comments.errorMessage()) }
    })
    return false
  })
  
  return $(this)
}