var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-325299-4']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();

var Engine = {
  start: function() {
    this.enhanceSource();
  },
  enhanceSource: function() {
    
    $('figure > pre').each(function() {
      
      var $pre = $(this);
      var $code = $pre.children('code');
      var $annotations = $pre.parent().nextAll('ol:first');
      var $annotations_items = $annotations.children('li');
      
      var annotations = [];
      var annotations_index = $annotations.attr('start');
      
      if (annotations_index === -1) {
        // if `start` is not specified, firefox will report -1
        annotations_index = 1;
      }
      
      $annotations.addClass('annotations');
      
      var notes_markers = '➊ ➋ ➌ ➍ ➎ ➏ ➐ ➑ ➒ ➓'.split(' ');
      var current_marker = 0;
      
      var make_marker = function() {
        var $mark = $(document.createElement('span'));
        $mark.text(notes_markers[current_marker]).addClass('marker');
        return $mark;
      };
      
      // get annotations’ indexes
      $annotations_items.each(function() {
        var value = $(this).attr('value');
        var item = { element: this };
        value = (value === -1) ? 0 : value;
        if (value) {
          item.value = value;
          annotations.push(item);
          annotations_index = value;
        } else {
          item.value = annotations_index;
          annotations.push(item);
        }
        annotations_index = annotations_index + 1;
      });
    
      // wrap each line in span      
      var code_lines = $code.html().split('\n');
      $code.empty();
      
      for (var i=0; i < code_lines.length; i++) {
        var $span = $(document.createElement('span'));
        $span.html(code_lines[i] + '\n').appendTo($code);
      }
      
      // 
      var $code_lines = $code.children('span');
      $code_lines.each(function(index) {
        var $code_item = $(this);
        var $annotation_item = false;
        
        for (var i=0; i < annotations.length; i++) {
          if (annotations[i].value === (index + 1)) {
            $annotation_item = $(annotations[i].element);
            break;
          }
        }
        
        if ($annotation_item) {
          
          $code_item.addClass('mark').append(make_marker());
          $annotation_item.prepend(make_marker());
          current_marker += 1;
        
          $code_item.hover(function() {
            $annotation_item.addClass('hover');
          }, function() {
            $annotation_item.removeClass('hover');
          });
          $annotation_item.hover(function() {
            $code_item.addClass('hover');
          }, function() {
            $code_item.removeClass('hover');
          });
        
        }
      });

    });
    

  }
};

$(function() {
  Engine.start();
});
