/**
 * ClientNavigator Javascript object
 * Author: Marc Trudel, Philippe Milot
 *
 * This object is used to simulate a standard website navigation without using page loads.
 * Each new content load triggers a URL change and updates the browser's history.
 * Requires the jQuery library.
 *
 * Usage:
 * var myNavigator = new ClientNavigator(onURLChange, "#myContentHolder");
 * myNavigator.start();
 * function onURLChange(URLArray, callback) {
 *   //Analyze the URL array and retrieve your new content
 *   //DON'T insert the content yourself!  Instead, do this:
 *   callback(newHTMLContent, newtitle);
 * }
 *
 * It is also possible to use only the ClientNavigator's URL handling
 * and let you update the page content yourself.  To do so, simply 
 * instantiate a ClientNavigator without providing a content ID:
 *
 * var myNavigator = new ClientNavigator(onURLChange);
 * myNavigator.start();
 * function onURLChange(URLArray) {
 *   //Analyze the URL array and retrieve your new content
 *   //Then update the page yourself.
 *   //Don't forget to call this after!!
 *   myNavigator.isLoading = false;
 * }
 */
 
var ClientNavigator = function(onURLChange, contentSelector)
{
  this.refreshSpeed = 100;
  this.onURLChange = onURLChange;
  this.currentURL = location.href;
  this.isLoading = false;
  this.firstLoad = true;
  this.isContentManager = typeof(contentSelector) != "undefined";
  if (this.isContentManager)
    this.contentArea = this.jq(contentSelector);
};

ClientNavigator.prototype.initializeLinks = function(classname)
{
  var tempCN = this;
  this.jq("." + classname).each(function(i)
  {
    this.href = tempCN.parseURL(this.href);
  });
};

ClientNavigator.prototype.setRefreshSpeed = function(speed)
{
  this.refreshSpeed = speed;
};

ClientNavigator.prototype.jq = function(item)
{
  return jQuery(item);
};

ClientNavigator.prototype.start = function()
{
  this.currentURL = location.href;
  var tempCN = this;
  this.masterInterval = setInterval(function() { tempCN.updateContent(); } , this.refreshSpeed);
};

ClientNavigator.prototype.stop = function()
{
  clearInterval(this.masterInterval);
};

ClientNavigator.prototype.updateContent = function()
{
  if (this.currentURL == location.href) {
    if (this.firstLoad) {
      //Special "first page load" case,  only do something if there is anchor info
      if (this.currentURL.indexOf('#') >= 0)
        this.loadFromURL();
      this.firstLoad = false;
    }
    
    return; //No changes, don't do a thing
  }
  
  var el1 = this.currentURL.split("#");
  var el2 = location.href.split("#");
  if (el1[1] != el2[1] && !this.isLoading)
  {
    this.currentURL = location.href;
    this.loadFromURL();
  }
};

ClientNavigator.prototype.loadFromURL = function()
{
  if(!this.isLoading)
  {
    this.isLoading  = true;
    var el;
    
    var poundIndex = this.currentURL.indexOf("#");
    if(poundIndex != -1 && this.currentURL.substring(poundIndex).length > 1)
    {
      var url = this.currentURL.substr(poundIndex+1);
      el = url.split("/");
      if (el[0] == '') el.shift();
    }
		else
    {
      el = new Array();
    }
    
    if (this.isContentManager) {
      var tempCN = this;
      this.onURLChange(el, function(content, title) { tempCN.onLoadEnd(content, title); });
    } else {
      this.onURLChange(el);
    }
  }
};

ClientNavigator.prototype.onLoadEnd = function(content, title)
{
  if (title) this.setTitle(title);
  this.contentArea.html(content);
  this.isLoading = false;
};

ClientNavigator.prototype.setTitle = function(title)
{
  document.title = title;
};

ClientNavigator.prototype.parseURL = function(url)
{
  if(typeof(url) == "undefined" || url == '')
  {
    url = "/";
  }
  else if(url.indexOf("http://") != -1)
  {
    url = url.substr(url.indexOf("/",8));
  }
  
  var poundIndex = url.indexOf("#");
  if (poundIndex < 0)
    url = '#' + url;
  else if (poundIndex > 0)
    url = url.substr(poundIndex, url.length - poundIndex);
  
  return url;
};

ClientNavigator.prototype.URL = function(url)
{
  url = this.parseURL(url);
 
  location.href = url;
};

