module StaticMatic
  module Helpers
    self.extend self
    
    # Generates links to all stylesheets in the source directory
    def stylesheets(options = {})
      stylesheet_dir = "#{@staticmatic.base_dir}/src/stylesheets"
      
      stylesheet_directories = Dir["#{stylesheet_dir}/**/*.sass"]
      
      # Bit of a hack here - adds any stylesheets that exist in the site/ dir that haven't been generated from source sass
      Dir["#{@staticmatic.base_dir}/site/stylesheets/*.css"].each do |filename|
        search_filename = File.basename(filename).chomp(File.extname(filename))
        already_included = false
        stylesheet_directories.each do |path|
          path = File.basename(path)
          if path.include?(search_filename)
            already_included = true
          end
        end
        
        stylesheet_directories << filename if !already_included
      end

      output = ""
      stylesheet_directories.each do |path|
        filename_without_extension = File.basename(path).chomp(File.extname(path))
        href = "stylesheets/#{filename_without_extension}.css"
        
        if !options[:relative]
          href = "/#{href}"
        end
        output << tag(:link, :href => href, :rel => 'stylesheet', :media => 'all')
      end
      
      output
    end
    
    # Generate javascript source tags for the specified files
    #
    # javascripts('test')   ->   <script language="javascript" src="javascripts/test.js"></script>
    #    
    def javascripts(*files)
      output = ""
      files.each do |file|
        if !file.match(/^http\:\/\//)
          output << tag(:script, :language => 'javascript', :src => "javascripts/#{file}.js") { "" }
        end
      end
      output
    end

    # Generates a form text field
    #
    def text_field(name, value)
      tag(:input, :type => "text", :name => name, :value => value)
    end
    
    # Generate an HTML link
    #
    # If only the title is passed, it will automatically
    # create a link from this value:
    #
    # link('Test')  ->  <a href="test.html">Test</a>
    #
    def link(title, page = "", options = {})
      if page.blank?
        page = urlify(title) + ".html"
      end
      
      tag(:a, :href => page) { title }
    end
    
    # Generates an image tag
    def img(name, options = {})
      options[:src] = "/images/#{name}"
      options[:alt] ||= name.split('.').first.capitalize.gsub(/_|-/, ' ')
      tag :img, options
    end
  
    # Generates HTML tags:
    #
    # tag(:br)   ->   <br/>
    # tag(:a, :href => 'test.html') { "Test" }    ->    <a href="test.html">Test</a>
    #
    def tag(name, options = {}, &block)
      output = "<#{name}"
      options.each do |attribute, value|
        output << " #{attribute}=\"#{value}\""
      end
      
      if block_given?
        output << ">"
        output << yield
        output << "</#{name}>\n"
      else
        output << "/>\n"
      end
      output
    end
    
    # Generates a URL friendly string from the value passed:
    #
    # "We love Haml"  ->  "we_love_haml"
    # "Elf & Ham"     ->  "elf_and_ham"
    # "Stephen's gem" ->  "stephens_gem"
    #
    def urlify(string)
      string.tr(" ", "_").
             sub("&", "and").
             sub("@", "at").
             tr("^A-Za-z0-9_", "").
             sub(/_{2,}/, "_").
             downcase
    end
    
    # Include a partial template
    def partial(name)
      @staticmatic.generate_partial(name)
    end
  end
end