$(document).ready(function() {
  $("#styleswitchers").show();
  $.stylesheetInit();
  /* taken from style_switcher.js */
  var c = readCookie('style');
  if (c) { $.highlightCurrentStylesheet(c); }
  // When one of the styleswitch links is clicked then switch the stylesheet to
  // the one matching the value of that links rel attribute.
  $('.styleswitch').bind('click', function(e) {
    rel = this.getAttribute('rel');
    $.stylesheetSwitch(rel);
    $.highlightCurrentStylesheet(rel);
    return false;
  });
  
  // NAVIGATION
  $('div#nav ul').superfish({
    dropShadows: false,
    hoverClass: 'hover',
    autoArrows: false,
    onInit: function(el) {
      $("div#nav li.current ul").css('visibility','visible').show();
    },
    onHide: function(el) {
      $("div#nav li.current ul").css('visibility','visible').show();
    }
  });
  
  // Abbreviations
  if (typeof(Abbreviations) != "undefined") {
    glossary_page = "/2009/glossary-and-abbreviations.html";
    search_page = "/2009/search.html";
    if (document.location.href.indexOf(glossary_page) == -1 && document.location.href.indexOf(search_page) == -1) {
      content = $('#main').html();
      if (content) {
        $.each(Abbreviations, function(dfn_term, dfn_value) {
          find = new RegExp("\ " + RegExp.escape(dfn_term) + "\ ", "gm")
          replace = " <a href=\"" + glossary_page + "\" class=\"abbr-link\"><abbr title=\"" + dfn_value + "\">" + dfn_term + "</abbr></a> "
          content = content.replace(find, replace);
        });
        $('#main').html(content);
      }
    }
  }

  $("td:empty").html("&nbsp;");
  $("th:empty").html("&nbsp;");

  // TABLE HOVERING
  // $('table.data').not('table.kpi').not('table.gri').tableHover({
  $('table.data').not('table.gri').tableHover({
    rowClass: 'hoverrow', 
    colClass: 'hover', 
    cellClass: 'hovercell', 
    clickClass: 'click',
    spanRows: false,
    ignoreCols: [1]
  });

  // KPI Notes
  $("table.kpi td.notes").each(function() {
    if ($(this).children("div.notes").length > 0) {
      $(this).children("div.notes").each(function(){
        $(this).wrap('<div style="position:relative"></div>');
        $(this).addClass("fancynotes").append($('<div style="text-align:right;"><a href="javascript:void(0);"><em>close</em></a></div>').click(function(){$(this).parent().fadeOut();return false;})).hide();
      });
      image = $('<img src="/images/2009/kpi/notes.png" alt="View expanded notes" style="vertical-align:top;" />');
      div = $('<div style="white-space:nowrap;"></div>').append(image).append("Expanded notes").click(function(e) {
        $(this).parent().find("div.notes").fadeIn();
      }).mouseover(function(){$(this).css("cursor", "pointer");});
      $(this).append(div);
    }
  });

  // LINKS
  $("a[rel=blank]").attr('target', '_blank');

  // zoom images
  $('a.zoom').fancybox();
});

// HIGHLIGHT MAPS
var HighlightMap = (function() {
  var current = null,
  timer = null,
  public_functions = {
    highlight: function(area) {
      if (timer) clearTimeout(timer);
      if (current == null) {
        $('#highlight-container').hide().html('<img src="/images/2009/highlights/' + area + '.gif" alt="" />').slideDown();
        current = area;
      } else if (current != area) {
        // $('#highlight-container').slideUp(200, function(){
        //   $(this).html('<img src="/images/2009/highlights/' + area + '.gif" alt="" />');
        // }).slideDown("fast");
        $('#highlight-container').css("height", "176px").html('<img src="/images/2009/highlights/' + area + '.gif" alt="" />');
        current = area;
      }
    },
    out: function() {
      timer = setTimeout(HighlightMap.attenuate, 5000);
    },
    attenuate: function() {
      $('#highlight-container').slideUp();
      current = null;
      timer = null;
    }
  }
  
  return public_functions;
})();

(function($) {
  $.highlightCurrentStylesheet = function(style_name) {
    $("#styleswitchers .styleswitch").html("A");
    $("#styleswitchers ." + style_name).html("[A]");
  }
})(jQuery);


RegExp.escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

