// Copyright Tor Brede Vekterli 2008 (vekterli@arcticinteractive.com)
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

function enumerate_code_styles()
{
  var tag_matches = document.getElementsByTagName('pre');
  var styles = [];
  // Check the style of each <pre> element
  for (var i = 0; i < tag_matches.length; ++i)
  {
    var classname = tag_matches[i].className;
    var matches = classname.match(/^syntax-highlight:(.*)$/i)
    if (matches != null)
    {
      styles[matches[1]] = matches[1];
    }
  }
  
  return styles;
}

function string_capitalize(str)
{
  return str.charAt(0).toUpperCase() + str.substr(1).toLowerCase();
}

// TODO: fix for CSharp etc. Use a lookup map for special cases
function auto_include_styles(scripts_path)
{
  var styles = enumerate_code_styles();
  var special = {
    'xhtml' : 'Xml',
    'jscript' : 'JScript',
    'csharp' : 'CSharp'
  };

  // Generate a set of javascript elements with URLs based on the style
  for (var style in styles)
  {
    // If there's a special entry for the style, use that, otherwise use the
    // the value of the style name Capitalized
    var filename = special[style.toLowerCase()] || string_capitalize(style);
    document.write('<script type="text/javascript" src="' + scripts_path
        + 'shBrush' + filename + '.js"></script>');
  }
}
