(function($) {
  $.fn.gc_tree = function() {

    adjustIslands = function(leaves) {
      var marginWidth = -10;
      var paddingWidth = 30;
    
      $(leaves).each(function() {
        if(/Island$/.test($(this).attr("class"))) {
          $(this).css("padding-left", paddingWidth.toString() + "px").css("margin-left", marginWidth.toString() + "px");
          var childLeaves = $(this).find("div[class*='Level_']");
          adjustIslands(childLeaves);
        } else {
          var childSpans = $(this).children("span");
          var childImg = $(this).children("img").get(0);
          
          if(childImg) {
            // IE
            $(childImg).attr("style", null).css("display", "none");
            
            var childSpan = $(childSpans).get(0);
            if(childSpan) {
              $(childSpan).css("margin-left", marginWidth.toString() + "px");
            }
          } else {
            var firstChild = $(childSpans).get(0);
            if(firstChild) {
              $(firstChild).attr("style", null);
            }
            
            var secondChild = $(childSpans).get(1);
            if(secondChild) {
              $(secondChild).css("margin-left", marginWidth.toString() + "px");
            }
          }
        }
      });
    };
    
    return this.each(function() {
      $(this).css("display", "none");
      $(this).find("span").css("font-size", "10pt");
      
      $("#" + this.id + " div > div").css("overflow", "visible").css("outline", "0px none");
      
      var rootNodes = $(this).find("div[class*='Level_0_']");
      $(rootNodes).each(function() {
        var childSpans = $(this).children("span");
        if(childSpans.length > 1) {
          // Remove the first, creates an artificial padding
          $($(childSpans)[0]).remove();
        }
      });
      
      var nodes = $(this).find("div[class*='Level_1_']");
      adjustIslands(nodes);
      
      $(this).css("display", "block");
    });
  }
})(jQuery);
