/**
 * Project Loader script
 *
 * Allows for project html details to be loaded asyncronously after the main
 * webpage data is loaded.
 *
 * This is deaigned to be used with a scrollTo menu such that all the data is
 * eventually loaded into one gigantic webpage that can be zoomed and scrolled
 * and navigated, but still allow the basic information of the page to be loaded
 * lightning fast.
 */

var scrollduration = 1000;

// if the url contains a hash then scroll to that id
$.localScroll.defaults = {
    duration: scrollduration,
    axis: 'xy', // Which of top and left should be modified.
    stop: true, // Avoid queuing animations
    target: window, // What to scroll (selector or element). The whole window by default.
    reset: true, // Used by $.localScroll.hash. If true, elements scroll is resetted before actual scrolling
    margin: true
    // lock:false, // ignore events if already animating
    // lazy:false, // if true, links can be added later, and will still work.
    // filter:null, // filter some anchors out of the matched elements.
    // hash: false // if true, the hash of the selected link, will appear on the address bar.
};

function replace_links_with_scrolling() {

  // check the url for a hash to scroll to
  $.localScroll.hash();

  // turn pages into anchors: http://site/projectname into http://site/#projectname
  $('#header #project_nav a').each( function() {
    var last_slash = this.href.lastIndexOf('/');
    var project = this.href.substr(last_slash+1);
    this.href = this.href.substr(0, last_slash) + '#' + project;

    $(this).bind('click', {destination: "#" + project},  function(event) {
      // prevent default event (but not bubbling)
      event.preventDefault();
      // scrollTo div with project id
      $.scrollTo($(event.data.destination), scrollduration, {margin: true});
      // set url in location bar
      window.location.hash = event.data.destination;
      return false;
    });
  });

  // change home button to a scrollTo
  $('#header h1').bind('click', function(event) {
    event.preventDefault();
    $.scrollTo( { top: 0, left:0 }, scrollduration);
    // remove hash anchor from url
    window.location.hash = "";
    return false;
  });

}

