module Padrino::Helpers::AssetTagHelpers

def flash_tag(kind, options={})

flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
Creates a div to display the flash of given type if it exists
def flash_tag(kind, options={})
  flash_text = flash[kind]
  return '' if flash_text.blank?
  options.reverse_merge!(:class => 'flash')
  content_tag(:div, flash_text, options)
end

def image_path(src)

Returns the path to the image, either relative or absolute
def image_path(src)
  src.gsub!(/\s/, '')
  src =~ %r{^\s*(/|http)} ? src : uri_root_path('images', src)
end

def image_tag(url, options={})

image_tag('icons/avatar.png')
Creates an image element with given url and options
def image_tag(url, options={})
  options.reverse_merge!(:src => image_path(url))
  tag(:img, options)
end

def javascript_include_tag(*sources)

javascript_include_tag 'application', 'special'
def javascript_include_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |script| javascript_tag(script, options) }.join("\n")
end

def javascript_path(source)

Returns the javascript_path appending the default javascripts path if necessary
def javascript_path(source)
  return source if source =~ /^http/
  source.gsub!(/\.js$/, '')
  source_name = source; source_name << ".js" unless source =~ /\.js\w{2,4}$/
  result_path = source_name if source =~ %r{^/} # absolute path
  result_path ||= uri_root_path("javascripts", source_name)
  stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
  "#{result_path}?#{stamp}"
end

def javascript_tag(source, options={})

javascript_tag 'application', :src => '/javascripts/base/application.js'
def javascript_tag(source, options={})
  options = options.dup.reverse_merge!(:src => javascript_path(source), :type => 'text/javascript', :content => "")
  tag(:script, options)
end

def link_to(*args, &block)

parameters: name, url='javascript:void(0)', options={}, &block
link_to('/dashboard', :class => 'blocky') do ... end
link_to 'click me', '/dashboard', :class => 'linky'
Creates a link element with given name, url and options
def link_to(*args, &block)
  options = args.extract_options!
  if block_given?
    url = args[0] || 'javascript:void(0);'
    options.reverse_merge!(:href => url)
    link_content = capture_html(&block)
    result_link = content_tag(:a, link_content, options)
    block_is_template?(block) ? concat_content(result_link) : result_link
  else
    name, url = args[0], (args[1] || 'javascript:void(0);')
    options.reverse_merge!(:href => url)
    content_tag(:a, name, options)
  end
end

def mail_to(email, caption=nil, mail_options={})

mail_to "me@demo.com", "My Email" => My Email
mail_to "me@demo.com" => me@demo.com
Creates a mail link element with given name and caption
def mail_to(email, caption=nil, mail_options={})
  html_options = mail_options.slice!(:cc, :bcc, :subject, :body)
  mail_query = Rack::Utils.build_query(mail_options).gsub(/\+/, '%20').gsub('%40', '@')
  mail_href = "mailto:#{email}"; mail_href << "?#{mail_query}" if mail_query.present?
  link_to (caption || email), mail_href, html_options
end

def meta_tag(content, options={})

meta_tag "text/html; charset=UTF-8", :http-equiv => "Content-Type" =>
meta_tag "weblog,news", :name => "keywords" =>
Creates a meta element with the content and given options
def meta_tag(content, options={})
  options.reverse_merge!("content" => content)
  tag(:meta, options)
end

def stylesheet_link_tag(*sources)

stylesheet_link_tag 'style', 'application', 'layout'
Returns a stylesheet link tag for the sources specified as arguments
def stylesheet_link_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
end

def stylesheet_path(source)

Returns the stylesheet_path appending the default stylesheets path if necessary
def stylesheet_path(source)
  return source if source =~ /^http/
  source.gsub!(/\.css$/, '')
  source_name = source; source_name << ".css" unless source =~ /\.css$/
  result_path = source_name if source =~ %r{^/} # absolute path
  result_path ||= uri_root_path("stylesheets", source_name)
  stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
  "#{result_path}?#{stamp}"
end

def stylesheet_tag(source, options={})

stylesheet_tag('style', :media => 'screen')
def stylesheet_tag(source, options={})
  options = options.dup.reverse_merge!(:href => stylesheet_path(source), :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
  tag(:link, options)
end

def uri_root_path(*paths)

Other tags:
    Example: uri_root('javascripts') -
def uri_root_path(*paths)
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
  File.join(root_uri || '/', *paths)
end