-function renameMemberTitle(title, count)
  if title.indexOf("ethods") >= 0
    span Functions (#{count})
  else if title.indexOf("ropert") >= 0
    span Properties (#{count})   
  else
    span #{title} (#{count})

mixin makeHierarchy(obj)
    div#inheritance
        h6 Hierarchy
            span#inheritance_arrow
        ul#hierarchyTree
          li
            strong #{obj.id}
          ul
            for h in obj.hierarchy
              li
                mixin hierarchyTree(h)

mixin hierarchyTree(hierarchyItem)
  mixin link(hierarchyItem)
  -if (hierarchyItem.parents)
    for p in hierarchyItem.parents
      if p.parents
        ul  
         li
            mixin hierarchyTree(p)
      else
        li
          mixin hierarchyTree(p)

mixin makeTag(obj, attributes)
  -xmlBlobs = "<pre><code class='language-xml'><span class='tag'>&lt;<span class='title'>a:" + obj.define + "</span>"

  -for k in obj.children.filter(function(x){return x.type === 'attribute'})
    -xmlBlobs += "\n     <span class='" + (k.inheritedFrom ? "isInheritedAttribute" : " ") + "'><span class='attribute'>" + k.name + "</span>=";
    -types = [];
    -moreThanOneType = k.signatures[0].returns.length > 1;
    -k.signatures[0].returns.forEach(function (r, idx, arr) {
          -if (r.type == "Boolean")
                -types.push('<span class="value">"' + 'true | false' + '"')
          -else
                -if (moreThanOneType)
                    -types.push('<span class="value">' + r.type)
                -else
                    -types.push('<span class="value">"' + r.type + '"')
    -})
    -xmlBlobs += (moreThanOneType ? '"' + types.join(' | ') + '"</span>' : types) + "</span></span>";
  != xmlBlobs + "></span></code></pre>"
                     
mixin commonDescription(obj, parents)
  if typeof obj === 'string'
    obj = list[obj]

  if (obj.private !== true && obj.name.charAt(0) !== "$")
    title = obj.id + (obj.type ? ' (' + obj.type + ')' : '')
    -var inheritedClass = obj.inheritedFrom ? "isInherited" : ""
    article(id=obj.path, data-title=title, class="article #{inheritedClass}")
      .section.method
        .memberContent
          if obj.signatures
            div.title
              ul.signatures
                -var loops = 0;
                for sig in obj.signatures
                  li.signature
                    ul
                      li.signature-call!= signature(obj, sig, ["methodClicker"])
                        if sig.returns
                          li.signature-returns
                            ul.argument-types
                              for ret in sig.returns
                                li.argument-type!= returnLink(obj, ret, [])
                    -if (loops == 0)
                      -loops = 1 // ensure that we only print ONE meta info UL per signature (some methods have multiple signatures)
                      ul.metaInfo
                          if obj.incomplete
                            li
                              span.label.incomplete Incomplete
                          if obj.experimental
                            li
                              span.label.experimental Experimental
                          if obj.readonly
                            li
                              span.label.read-only Read-Only
                          if obj.chainable
                            li
                              span.label.chainable Chainable
                          if obj.internal
                            li
                              span.label.internal Internal
                          if obj.deprecated
                            li
                              span.label.deprecated
                                | Deprecated
                                if obj.deprecated.from
                                  | &nbsp; (since #{obj.deprecated.from})
                                  if obj.deprecated.off
                                    | &nbsp;and will be removed on #{obj.deprecated.off}
                          if obj.alias_of
                            li
                              span.label.alias.single
                                | Aliased as: 
                                != link(obj.alias_of)
                          else if obj.aliases.length
                            li
                              span.label.alias.multiple
                                | Aliased as:
                                ul.alias
                                  for alias in obj.aliases
                                    li
                                      mixin link(alias)

                          if obj.related_to
                            li
                              span.label.related-to
                                | Related to 
                                mixin link(obj.related_to, ["relatedToLink"])  
                                  
          div.sideToggler

            div(id='ellipsis_#{obj.id}', class='ellipsis_description')
                mixin markdown(obj.short_description)

            h3(id='#{obj.id}', class='methodToggle methodClicker inactive')
              &nbsp;&nbsp;&nbsp;
            div.description
        
              if obj.inheritedFrom
                span.inheritedFrom
                    | Inherited from: 
                    != link(obj.inheritedFrom)
              mixin markdown(obj.description)

              if obj.bound && ~obj.bound.indexOf('#')
                p.note.methodized
                  | This method can be called <em>either</em> as an 
                  != link(obj.bound, ['link-short'], 'instance method')
                  |  <em>or</em> as a generic method. If calling as generic, pass the instance in as the first argument.
              else if obj.bound && !~obj.bound.indexOf('#')
                p.note.functionalized
                  | This method can be called <em>either</em> as an instance method <em>or</em> as a 
                  != link(obj.bound, ['link-short'], 'generic method')
                  |. If calling as generic, pass the instance in as the first argument.

              if obj.arguments && obj.arguments.length > 0
                h4 Arguments
                != argumentTable(obj.arguments, ["argument-list", "table", "table-striped", "table-bordered"])

              if obj.returns
                h4 Returns
                != returnTable(obj.returns, ["return-list", "table", "table-striped", "table-bordered"])

mixin article(obj, methods, events, properties)
  h4 Members
  #tabbable
    ul.nav.nav-tabs
      -attributes = obj.children.filter(function(x){return x.type === 'attribute'})
      -if (attributes.length > 0)
        li
          a(href="#attributeSection", data-toggle="tab") Attributes

      -if (methods.length > 0)
        li
          a(href="#methodSection", data-toggle="tab") Methods

      -if (events.length > 0)
        li
          a(href="#eventSection", data-toggle="tab") Events

      -if (properties.length > 0)
        li
          a(href="#propertySection", data-toggle="tab") Properties

    div.tab-content
      .tab-pane.active#attributeSection
        for child in attributes
          mixin commonDescription(child, [].concat(obj))

      .tab-pane#methodSection
        for child in methods
          mixin commonDescription(child, [].concat(obj))

      .tab-pane#eventSection
        for child in events
          mixin commonDescription(child, [].concat(obj))

      .tab-pane#propertySection
        for child in properties
          mixin commonDescription(child, [].concat(obj))


mixin api()
  -pos = 0
  for obj in tree.children
    -if (obj.private !== false)
      .classContent
        .membersBackground
        div(class=' members pos#{pos}')
          div(class=' membersContent pos#{pos}')
              h1.memberHeader
                -var heading = obj.path
                span.name #{heading}
             // 
              -if (true || obj.filename.indexOf("index") < 0)
                ul(class='nav tabs pos#{pos}', data-tabs='tabs')
                  for selector, title in {'Events': ['event', 'events'], 'Constructors': ['constructor', 'constructors'], 'Class methods': ['class method', 'class_methods'], 'Class properties': ['class property', 'class_properties'], 'Instance methods': ['instance method', 'instance_methods'], 'Instance properties': ['instance property', 'instance_properties'], 'Constants': ['constant', 'constants']}
                    members = obj.children.filter(function(x){return x.type === selector[0]})
                      li(class="dropdown", data-dropdown="dropdown")
                        if members.length
                           a(href="\#", class="dropdown-toggle", data-toggle="dropdown")
                              != renameMemberTitle(title, members.length)
                              b.caret
                          ul.dropdown-menu
                            for m in members
                              li(data-id='#{m.id}', class='memberLink')
                                mixin link(m, ["tabLink"], true)
         -pos++
          if obj.type === 'section' || obj.type === 'namespace' || obj.type === 'class' || obj.type === 'mixin'
            if obj.stability
              mixin markdown(">" + obj.stability)

            if obj.hierarchy
                 mixin makeHierarchy(obj)

            if obj.description
              .section.description
                .memberContent
                  mixin markdown(obj.description)
            
            if obj.define && obj.children
              h4 XML Definition
              div#xmlBlob
               mixin makeTag(obj)
            
            mixin article(obj, obj.children.filter(function(x){return x.type === 'class method'}), obj.children.filter(function(x){return x.type === 'event'}), obj.children.filter(function(x){return x.type === 'class property'}))

mixin short_description_list(collection)
  ul.method-details-list
    for obj in collection
      if typeof obj === 'string'
        obj = list[obj]
      li.method-description
        h4
          mixin link(obj)
        if obj.short_description
          mixin markdown(obj.short_description)

mixin link(obj, classes, short)
  l = link(obj, classes, short)
  != l

mixin links(collection)
  ul.method-list
    for obj in collection
      li
        mixin link(obj)
