module BlogHelper
  # header on "parent" entries: don't duplicate the first entry
  def parent_header
    if @entries.length==0 or 
       @parent.updated.to_date != @entries[0].updated.to_date
      "<h2>#{ @parent.updated.strftime '%a, %d %b %Y' }</h2>"
    end
  end

  # header on "normal" entries: don't repeat
  def entry_header entry
    if entry.updated.to_date != @last_date
      @last_date = entry.updated.to_date
      "<h2>#{ entry.updated.strftime '%a, %d %b %Y' }</h2>"
    end
  end

  # entry titles: don't repeat the parent's title
  def entry_title entry, parent=nil
    if entry.title and entry.title != ""
      if not parent or parent.title != entry.title
        "<h3>#{ entry.title }</h3>"
      end
    end
  end

  # signature lines: method of post and author related info
  def sigline entry
    if (author = entry.author) != nil
      sig = case entry['method']
        when 'trackback': 'Trackback from '
        when 'excerpt': 'Excerpt from '
        else 'Posted by '
      end
  
      if author.uri
        sig << link_to(author.name, author.uri, :title=>author.ipaddr)
      elsif author.ipaddr
        sig << "<a title=\"#{author.ipaddr}\">#{author.name}</a>"
      else
        sig << author.name
      end
    else
      sig = "Posted"
    end

    sig << " at "
    sig << link_to(entry.updated.strftime('%H:%M'), entry.by_date)

    if not entry.parent
       sig << " | Comments (#{entry.children.size})"
    end

    return '<div class="posted">' + sig + '</div>'
  end

  # calendar for a month: used by archive pages
  def monthcalendar(year,month)
    require 'date'
    date = Date.new(year,month,1)
    rows = []
  
    while date.month == month
      rows << [0]*7 if rows.length==0 or date.wday==0
      rows[-1][date.wday] = date.day
      date = date.succ
    end
  
    return rows
  end

end
