if (typeof simulacra == 'undefined') {
  simulacra =  {};
}

// Via http://simonwillison.net/2006/Jun/25/fjax/
simulacra.ajax = {
 createXMLHttpRequest: function() {
    if (typeof XMLHttpRequest != 'undefined') {
      return new XMLHttpRequest();
    }
    try {
      return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) { }
    }
    return false;
  },
 update: function(url, id) {
    var xmlhttp = simulacra.ajax.createXMLHttpRequest();
    if (xmlhttp) {
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById(id).innerHTML = xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET", url, true);
      xmlhttp.send(null);
    }
  }
}

// Via http://simonwillison.net/2004/May/26/addLoadEvent/
simulacra.addLoadEvent = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// With help from http://unixpapa.com/js/dyna.html
// callback is an optional argument, a function that will be called when the script is loaded
simulacra.loadScript = function(url, callback) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = url;
  if (callback) {
    if (script.readyState) {
      script.onreadystatechange = function() {
        if (script.readyState == 4) {
          callback();
        }
      }
    } else {
      script.onload = function() {
        callback();
      }
    }
  }
  head.appendChild(script);
}

simulacra.randomQuote = function() {
  var e = document.getElementById('quote-div');
  if (e) {
    e.innerHTML = 'Loading&#8230;';
    simulacra.ajax.update('http://simulacra.in/code/quotes/RandomQuote.py', 'quote-div');
  }
}

simulacra.addLoadEvent(function() {
    if (document.getElementById('quote-div')) {
      simulacra.randomQuote();
    }});

// if (location.pathname == "/elsewhere/" {
//     simulacra.loadScript('http://www.google.com/jsapi?key=ABQIAAAASMN7didocRjmBh7w0xjajRRO2m9ZYEr1o9eXM7XnW6srQuCKBRS1vg_EtbQRT8KB7TjDBLo8fl12bQ');
//     simulacra.loadScript('/js/elsewhere.js');
// }
