// 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 UI/Effects core files (bundled into one file here)
  $.requireJs('/assets/javascript/jquery-ui-1.8.6/js/jquery-ui-1.8.6.custom.min.js'); // includes everything
  $.requireCss('/assets/javascript/jquery-ui-1.8.6/css/ui-lightness/jquery-ui-1.8.6.custom.css'); // CSS for jQuery UI
  
  // every page will require the form labelize plugin
  // $.requireJs('/assets/javascript/jquery.InputHelper.min.js');
  
  /* custom fonts
     if CSS3's @font-face is not the prefered solution then every page will require Cufon related files
  $.requireJs('/assets/javascript/cufon-yui.js'); // this is not a jQuery file!
  $.requireJs('/assets/fonts/love-ya-like-a-sister_400.font.js'); // this is not a jQuery file
  $.requireJs('/assets/javascript/jquery.fontavailable-1.1.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.2-min.js');
    $.requireJs('/assets/javascript/jquery.localscroll-1.2.7-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) {
    
    // 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-1.8/ui/minified/jquery.ui.datepicker.min.js'); // graphical datepicker for date inputs
    //    $.requireCss('/assets/javascript/jquery-ui-1.8/themes/base/jquery.ui.datepicker.css'); // CSS for the datepicker
    //}
  //}
  
  // TEST: if we want a carousel on the page
  if($("div.carousel-1").length > 0) {
    $.requireJs('/assets/javascript/jcarousellite_1.0.1.min.js');
  }
  
  // TEST: if we want a colourbox images on our site
  if($("a.fancybox,a.colorbox").length > 0 || $("body.my.landing").length > 0) {
    $.requireJs('/assets/javascript/colorbox/colorbox/jquery.colorbox-min.js');
    $.requireCss('/assets/javascript/colorbox/example1/colorbox.css');
  }
  
  // TEST: if we need a starrating on the page
  if($(".widget_rate form").length > 0) {
    $.requireJs('/assets/javascript/jquery.starrating/jquery.rating.js');
    $.requireCss('/assets/javascript/jquery.starrating/jquery.rating.css');
  }
  
  // TEST: if we are on the food selector page we need the isotope plugin
  if($("body.foodselector").length > 0) {
    $.requireJs('/assets/javascript/jquery.isotope/jquery.isotope.min.js');
    // $.requireCss('/assets/javascript/jquery.starrating/jquery.rating.css');
  }
  
  // TEST: if we want a tablesorter
  if($("table.tablesorter").length > 0) {
    $.requireJs('/assets/javascript/jquery.tablesorter/jquery.tablesorter.min.js');
    $.requireCss('/assets/javascript/jquery.tablesorter/blue/style.css');
  }
  
  // we always want to load progressive enhancements at the end of the load queue
  $.requireJs('/assets/javascript/progressive-enhancement.js');
  
});
