function current_path() {
  var match_from_hash = window.location.hash.match(/^#(\/.*)/);
  if (match_from_hash != null)
    return match_from_hash[1];
  else
    return window.location.pathname;
}

// taken from jquery.trackpageview
function external_link( link ) {
  var href = $(link).attr('href');
  if (href == null) return false; // no href

  var protocol_match = href.match(/(\w+):.*/);
  if (protocol_match == null) return false; // doesn't start with abc:

  var protocol = protocol_match[1];
  if (protocol == null) return false; // just incase (doesn't start with abc:)

  if (protocol == 'http' || protocol == 'https') {
    if (window.location.host == '') return true; // can't be relative to nothing
    var is_relative = new RegExp("https?://" + window.location.host).test(href);
    return ! is_relative; // not relative
  } else {
    return true; // if it's not http || https, it can't be a relative link
  }
}

function relative_link( link ) {
  return ! external_link(link);
}

function shouldnt_be_loaded_via_ajax( link ) {
  var href = $(link).attr('href');
  if (/\.(png|jpg|ico|css|gif|js)$/.test(href)) return true;
  if ($(link).hasClass('noajax')) return true;
  return false;
}

function make_relative( href ) {
  if (/^http/.test(href))
    return href.match(/^https?:\/\/[^\/]+(.*)/)[1];
  else
    return href;
}

function page_name_from_href( href ) {
  var page = make_relative(href).replace(/^\//, '').replace('/', '-');
  if ( page == '' )
    page = 'home';
  return page;
}

function load_page( link ) {
  if (typeof(link) == 'string') {
    var href = link;
    document.title = 'Dev Fu!';

  } else {
    var href       = $(link).attr('href')
    var separator  = $.browser.msie ? ' - ' : ' » '
    var page_title = $(link).attr('rel');
    if (page_title == '') page_title = $(link).text();
    document.title = 'Dev Fu!' + separator + page_title;
  }

  $('#content').animate({ opacity: 0 }, function(){
    $('#content *').remove();
    $('#content').append($('<div id="inner-content"></div>'));
    $('#inner-content').load(href + ' #content>*', function(){
      $('body').removeClass( $('body').attr('class') );
      $('body').addClass(page_name_from_href(href));
      window.location.hash = make_relative(href);
      dom_ready();
      $('#content').animate({ opacity: 1 });
    });
  });
}

function dom_ready() {

  if (/#disqus_thread$/.test(window.location.hash))
    $('#comments').show();
  else
    $('#comments').hide();

  $('a').do_once('ajaxified').each(function(){
    if (relative_link(this) && ! shouldnt_be_loaded_via_ajax(this)) {

      $(this).click(function(){
        load_page(this);
        return false;
      });

    } else {
      $(this).attr('target', '_blank');
    }
  });

  $('#disqus_thread').each(function(){
      $(this).disqus({ 
        domain: 'devfu', 
        url:    'http://devfu.com' + current_path()
      })
    });

  $.disqusLinks('devfu')

  $('a.comments.noajax').click(function(){ $(this).next('#comments').toggle(); return false; });

}

$(function(){

  // if there's a # in the url and it starts with a / that means we want to 
  // go to a page that was loaded via ajax ... so go there
  //
  // we don't re-run this whenever ajax is called, so it's not in dom_ready
  //
  if ( /^#\//.test(window.location.hash) )
    load_page(window.location.hash.replace(/^#/, ''));

  dom_ready();

});
