// Begin Lazy Loading.
// 
// Lazy Loading allows us to load JS & CSS files only when the page requires them, saving bandwidth and decreasing load times.
// We get the option to inspect the mark-up and see if it's appropriate to include a particular plug-in or not automatically at runtime.
// For more detail on this technique, it's pros and cons, see: http://ajaxpatterns.org/On-Demand_Javascript#In_A_Blink
//
// NOTES:
// This file relies upon jQuery & the jquery.ondemand plugin. jQuery and the ondemand plugin MUST be included before this file.

$(document).ready(function(){
  // get the body class for the page we are on
  var thisPage = $("body").attr("class");
  
  // every page will need the jQuery core files
  $.requireJs('assets/javascript/jquery.ui/ui/ui.core.js');
  $.requireJs('assets/javascript/jquery.ui/ui/effects.core.js');
  
  // every page will require the background animation, iPhone, accordion, and media plugins
  $.requireJs('assets/javascript/jquery.backgroundPosition.js');
  $.requireJs('assets/javascript/jquery.iphone.min.js');
  $.requireJs('assets/javascript/jquery.ui/ui/ui.accordion.js');
  $.requireJs('assets/javascript/jquery.media.js');
  
  // TEST: if you are IE6 load the pngfix plugin
  if(jQuery.browser.msie == true && jQuery.browser.version == "6.0") {
    $.requireJs('assets/javascript/jquery.ifixpng2.js');
  }
  
  if($(".nav_internal").length > 0) {
    $.requireJs('assets/javascript/jquery-ui-personalized-1.6rc2.min.js');
  }
  
  // TEST: if we have an internal page anchor, load the scroller plugins.
//  if($("a[href*=#]").length > 0) {
    $.requireJs('assets/javascript/jquery.scrollTo-1.4.0-min.js');
    $.requireJs('assets/javascript/jquery.localscroll-1.2.6-min.js');
    $.requireJs('assets/javascript/jquery.serialScroll-1.2.1-min.js');
//  }
  
  // TEST: if we have a form on a page then we want to load a number of form enhancement plugins.
  if($("form").length > 0) {
    $.requireJs('assets/javascript/jquery.validate.min.js'); // jQuery form validation
    
    // TEST: if we have a date field then include the datepicker plug-in
    if ($("form input.input-date").length > 0) {
      $.requireJs('assets/javascript/jquery.ui/ui/minified/ui.datepicker.min.js'); // graphical datepicker for date inputs
      $.requireCss('assets/javascript/jquery.ui/themes/ui.datepicker.css'); // CSS for the datepicker
    }
  }
  
  // TEST: if we want a fancybox images on our site
  if($("a.fancybox").length > 0) {
    $.requireJs('assets/javascript/fancybox/jquery.fancybox-1.0.0.js');
    $.requireCss('assets/javascript/fancybox/fancy.css');
  }
  
  // we always want to load progressive enhancements at the end of the load queue
  $.requireJs('assets/javascript/progressive-enhancement.js');
  
});
