/* Loader for SHJS, enabling activation from body with sh_highlightBody()
 *
 * As described at http://khopesh.com/blog/adam-katz-musings/lifetype-highlight
 *
 * Example usage:

    <script type="text/javascript" src="/shjs-0.6/sh_main.min.js"></script>
    <script type="text/javascript" src="/sh_load.js"></script>
    <script type="text/javascript">
      sh_highlightBody("/shjs-0.6/lang", "/shjs-0.6/css", "desert", true);
    </script>

 *
 * Copyright 2010 by Adam Katz <http://khopis.com/scripts>, GPL v2+
 *
 */

var sh_theme; // global variable storing the <style> object for the CSS theme


function sh_setTheme(css) {

  if (sh_theme && (sh_theme.hasChildNodes() || sh_theme.styleSheet) ) {
    sh_theme.removeChild(sh_theme.firstChild); // already there, remove theme
  } else {
    sh_theme = document.createElement("style");
    sh_theme.type = "text/css";

    // place it in the head if we can find it, otherwise the body
    var head = document.getElementsByTagName("head");
    if (head && head[0]) { head = head[0]; } else { head = document.body; }
    head.appendChild(sh_theme);
  }

  var cssText = '@import url("' + css + '");';
  if (sh_theme.styleSheet) { // IE
    sh_theme.styleSheet.cssText = cssText;
  } else { // Everybody else
    sh_theme.appendChild( document.createTextNode(cssText) );
  }

}

// Set an argument to an unquoted number to use the default.  min is a boolean.
// 0 args: langPrefix=lang, themePrefix=css, theme=nedit, min=true
// 1 arg:  arg1=langPrefix, themePrefix=langPrefix/../css, theme=nedit, min=true
// 2 args: arg1=langPrefix, arg2=themePrefix, theme=nedit, min=true
// 3 args: arg1=langPrefix, arg2=themePrefix, arg3=themeName, min=true
function sh_highlightBody(langPrefix, themePrefix, themeName, min) {

  if (typeof(langPrefix) != "string") { langPrefix = "lang"; }
  if (langPrefix[langPrefix.length-1] != "/") { langPrefix += "/"; }
  if (typeof(themePrefix) != "string")
    { themePrefix = langPrefix.replace(/\/*[^\/]+\/*$/,"/css"); }
  if (typeof(themeName) != "string") { themeName = "nedit"; }
  if (typeof(min) == "undefined" || min) { min = ".min"; } else { min = ""; }

  sh_setTheme(themePrefix + "/sh_" + themeName + min + ".css");

  try {

    var sh_go = function() { sh_highlightDocument(langPrefix, min + ".js"); }

    if (typeof(document.addEventListener) == "undefined") { // IE
      document.body.attachEvent("onload", sh_go() );
    } else { // Everybody else
      document.body.addEventListener("load", sh_go(), false);
    }

  } catch(e) { /* Ignore firefox's harmless error */  }

}
