// see extensions.js for more goodies

$(document).ready(function() {
  $(':input#terms').friendlyTextBox('Enter Your Search')
  
  // activate all facebox links
  $('a[rel*=facebox]').facebox()
  
  // hide all flash messages if they are present
  setTimeout("$('p.notice, p.error').fadeOut(1500)", 10000)
  
  // inserts things into the post body text area
  $('.textile').click(function() {
    var title = $(this).attr('title'),
        text_area = document.getElementById('post_body')
    
    var startPos = text_area.selectionStart,
        endPos = text_area.selectionEnd
    
    text_area.value = (text_area.value.substring(0, startPos) + title
      + text_area.value.substring(endPos, text_area.value.length))
    
    text_area.focus()
    text_area.setSelectionRange(startPos + title.length, endPos + title.length)
  })
  
  // twitter
  $('#twitter').tweet({ username: "rpheath", count: 5 })
  
  // set the focus to the username field upon
  // return from the ajax call to load login form
  $('a#admin-link').click(function() {
    $.get(this.href, function(data) {
      $.facebox(data)
      $('input#user_name').focus()
    })
    return false
  })
  
  $('a.cp').click(function() {
    $.get(this.href, function(data) {
      $.facebox(data)
    })
    return false
  })
  
  // handle the github project overflow
  $('a#github-link').click(function() {
    $('#more-github-projects').show()
    $(this).hide()
  })
  
  // show my skills
  $('a#skills').click(function() {
    //$.get(this.href, function(html) { $.facebox(html) })
    //return false
    $.ajax({
      type: 'GET',
      url: this.href,
      beforeSend: $.facebox.loading(),
      success: function(html) {
        $.facebox(html)
      }
    })
    return false
  })
  
  // add toggle behavior to admin tags
  $('div#admin-tags a').toggleTags()
  
  // ajaxify the comments preview
  $('input#comment_preview').ajaxPreview()
  
  // ajaxify the comments form
  $('#post-comments').ajaxify()
    
  // IE6 notification
  if ($.browser.msie && $.browser.version == '6.0') {
    $.facebox(
      '<h1>It is time to upgrade your browser!</h1>' +
      '<p>You are currently using Internet Explorer v6.0, which is extremely outdated. ' +
      'This site has not been optimized (at all) for IE6, and it is strongly advised ' +
      'that you upgrade your browser.</p><br />' +
      '<p>Options: <a href="http://getfirefox.com">Firefox</a>, ' +
      '<a href="http://www.apple.com/safari">Safari</a>, or even the latest ' + 
      '<a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a>.</p><br />' +
      '<p>Do it for the good of the web. Consider yourself warned!</p>'
    )
  }
  
  // add Accept: text/javascript header to jQuery ajax requests
  $.ajaxSetup({ 
    'beforeSend': function(xhr) { 
      xhr.setRequestHeader('Accept', 'text/javascript')
      $('#loading').show()
    },
    'complete': function(xhr) { $('#loading').hide() }  
  })
})