/**
 * Athena Faculty Blog 2009 - 2010.
 * Main JavaScript module.
 *
 * (C) 2009 Wong Yong Jie. All Rights Reserved.
 * This script requires jQuery to work.
 */

/**
 * JavaScript base URL.
 */
var js_base = "http://athenablog.roregister.net/js/";

/**
 * Images base URL.
 */
var img_base = "http://athenablog.roregister.net/images/";

/**
 * CSS base URL.
 */
var css_base = "http://athenablog.roregister.net/css/";

/**
 * Blog portal URL.
 */
var blog_portal = "http://athenablog.roregister.net/portal.php";

/**
 * Page portal URL.
 */
var page_portal = "http://athenablog.roregister.net/page.php";

/**
 * Request counter.
 */
var request_counter = 0;

/**
 * Load tracker.
 */
var load_current;
var param;

/**
 * Image preloader.
 */
function preload_image(image_src){
  image = new Image();
  image.src = image_src;
  
  return true;
}

/**
 * Main module.
 * This module only executes when DOM is ready.
 */
$(document).ready(function(){
  // List of modules to load.
  var modules = [
  'blog.init.js',
  'about.init.js',
  'media.init.js',
  'people.init.js',
  'highlights.init.js'
  ];
  
  // Module debugging.
  var module_debug = false;
  
  /**
   * Load modules.
   */
  $(modules).each(function(i, module_name){
    $.getScript(js_base + "module/" + module_name, function(){
      // Notify if module debugging is enabled.
      if(module_debug){
        alert("Loaded module: " + module_name);
      }
    });
  });
  
  /**
   * AJAX loading callbacks.
   */
  $(document).bind("ajaxStart", function(){
    // Clear the existing content boxes first.
    $('#content-right').empty();
    $('#content-title-right').empty();
    
    // Tell user we are loading.
    var content_loading = $(document.createElement("div"));
    content_loading.attr("id", "content-loading");
    
    var content_loading_image = $(document.createElement("img"));
    content_loading_image.attr({
      "src": img_base + "ajax-loader.gif",
      "alt": "Loading..."
    });
    
    var content_loading_text = $(document.createElement("div"));
    content_loading_text.css({
      "fontSize": "110%",
      "paddingTop": "10px"
    });
    
    content_loading_text.append("Loading");
    content_loading.append(content_loading_image);
    content_loading.append(content_loading_text);
    $('#content-right').append(content_loading);
  });
  
  /**
   * Track URL to load the appropriate component.
   */
  setInterval(function(){
    var load_param = location.hash.substr(1).split("&");
    param = new Object();
    
    $(load_param).each(function(i, param_set){
      var param_set = param_set.split("=");
      var param_key = param_set[0];
      var param_val = param_set[1];
      
      param[param_key] = param_val;
    });
  
    if(param["load"] != null){
      var module_name = param["load"] + ".init.js";
      if(jQuery.inArray(module_name, modules) != -1 && load_current != location.hash.substr(1)){
        $.getScript(js_base + "module/" + param["load"] + ".load.js");
        load_current = location.hash.substr(1);
      }
    } else {
      location.href = "#load=highlights";
    }
  }, 500);
});
