include("js/jquery.ba-hashchange.min.js");

//Set up a fake console.log if firebug isn't around.
if (!console || !console.log) {
  var console = new Array();
  console.log = function() {}
}

var loaded = false;
var templated = false;

function loadHandler() {
  loaded = true;
  initPage();
}

function initPage() {
  if (loaded && templated) {
    fadeIn();
  }
}

function fadeIn(callback) {
    if (typeof callback === 'undefined') {
    callback = function() { return false; };
  }
  $("#menu").parent().show();
  $(".template").fadeTo(1000,1,function() {
    callback();
  });
}

(function() {
  var cache = {};
  
  function applyTemplates() {
    console.log("Applying templates now.");
//     var reg = new RegExp(/%([\w-]*)%/g);
    
    $(".template").each(function(index, item) {
      var templateString = $(item).attr("template");
      var scratch = cache[templateString];
      $(item).children("div").each(function(index1, item1) {
	var class1 = $(item1).attr("class");
	var needle = "%" + class1 + "%";
// 	console.log($(item1).attr("class"));
	scratch = scratch.replace(new RegExp(needle, 'g'), $(item).children("." + class1).html());
      });
//       console.log(scratch);
      $(item).html(scratch);
//       var m;
//       var matches = [];
//       while (m = reg.exec(cache[templateString])) {
// 	matches.push(m);
//       }
// //       console.log("Matches in template " + templateString + ":");
// //       console.log(matches);
//       var scratch = cache[templateString];
//       for (match in matches) {
// 	scratch = scratch.replace(matches[match][0], $(item).children("." + matches[match][1]).html());
//       }
//       $(item).html(scratch);
    });
    
    templated = true;
    //initPage();
    if (typeof setupGrowBoxes !== 'undefined') {
      setupGrowBoxes();
    }
    
    if (typeof setupCarousel !== 'undefined') {
      setupCarousel();
    }
      
    fadeIn(function() { 
      $("#template-scratch").remove();
      if (typeof setupMenu !== 'undefined') {
	setupMenu();
      }
      
    });
    
  }

  $().ready(function() {
//     $("body").append('<div id="template-scratch"></div>');
//     $("#template-scratch").hide();
    var templateCount = $(".template").size();
    var templatesLoaded = 0;
    $(".template").each(function(index, item) {
      //Load templates used on this page
      var templateString = $(item).attr("template");
      var inputString = $(item).html();
      
      if (cache[templateString] == undefined) {
	cache[templateString] = "loading";
 	console.log("Loading " + templateString);
	$.ajax({url: templateString, cache: true, complete: function(data) {
// 	$("#template-scratch").load(templateString, function() {
	  cache[templateString] = data.responseText;
// 	  console.log(data.responseText);
	  templatesLoaded++;
	  if (templatesLoaded == templateCount) {
	    applyTemplates();
	  }
	} });
	
      } else {
	templatesLoaded++;
	if (templatesLoaded == templateCount) {
	  applyTemplates();
	}
      }
    });
  });
})();

function include(filename) {
  var head = document.getElementsByTagName('head')[0];
  script = document.createElement('script');
  script.src = filename;
  script.type = 'text/javascript';
  
  head.appendChild(script);
}


