module HtmlHelper
  include ActionView::Helpers::AssetTagHelper
  include ActionView::Helpers::TagHelper
  
  def doctype(type, subset)
    "<!DOCTYPE html PUBLIC \"-//W3C//DTD #{dtd_type(type, subset)}//EN\"\n" + 
      "\"http://www.w3.org/TR/#{dtd_url(type, subset)}.dtd\">"
  end
  
  def document_header(opts={}, &content)
    doctype_opt = opts[:doctype] || :xhtml
    subset_opt = opts[:subset] || :strict
    language_opt = opts[:language] || :en
    contenttype_opt = opts[:content_type] || 'iso-8859-1'
    concat(doctype(doctype_opt, subset_opt) + "\n", content.binding)
    concat(html_tag(doctype_opt, language_opt) + "\n", content.binding)
    concat("<head>\n\t#{content_type(contenttype_opt)}", content.binding)
      yield
    concat("</head>", content.binding)
  end
  
  def html_tag(type=:xhtml, language=:en)
    tag = "<html "
    tag << "xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"#{language.to_s}\" " if type == :xhtml
    tag << "lang=\"#{language.to_s}\">"
  end
  
  def end_html_tag
    "</html>"
  end
  
  def content_type(charset='iso-8859-1')
    "<meta http-equiv=\"content-type\" content=\"text/html; charset=#{charset}\" />"
  end
  
  def stylesheet_include_tag(*sources)
    if sources.include?(:controller)
      sources.delete(:controller)
      sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
    end
    if sources.include?(:action)
      sources.delete(:action)
      sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
    end
    if sources.include?(:defaults)
      sources.delete(:defaults)
      sources.unshift('application')
      sources.push(controller_stylesheet_source) if stylesheet_exists(controller_stylesheet_source)
      sources.push(action_stylesheet_source) if stylesheet_exists(action_stylesheet_source)
    end
    sources.collect { |source|
      path = "/stylesheets/#{source}.css"
      tag('link', { 'type' => 'text/css', 'rel' => 'stylesheet', 'href' => path})
    }.join("\n")
  end
  
  protected
    def dtd_type(type, subset)
      case type
        when :html
          return "HTML 4.01" if subset == :strict
          "HTML 4.01 #{subset.to_s.capitalize}"
        when :xhtml
          "XHTML 1.0 #{subset.to_s.capitalize}"
        else
          "XHTML 1.0 #{subset.to_s.capitalize}"
      end
    end
    
    def dtd_url(type, subset)
      case type
        when :html
          return "html4/loose" if subset == :transitional
          "html4/#{subset.to_s}"
        when :xhtml
          "xhtml1/DTD/xhtml1-#{subset.to_s}"
        else
          "xhtml1/DTD/xhtml1-#{subset.to_s}"
      end
    end
    
    def controller_stylesheet_source
      params[:controller]
    end
    
    def action_stylesheet_source
      [params[:controller], params[:action]].join("_")
    end
    
    def stylesheet_path(source)
      "#{RAILS_ROOT}/public/stylesheets/#{source}.css"
    end
    
    def stylesheet_exists(source)
      File.exists?(stylesheet_path(source))
    end
end