# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  # user-centric actions #############################
  def logged_in?
    session[:user_id].nil? ? false : true
  end
  
  def login_link
    "<a href='#login'>Login</a>"
  end
  
  def you?(user)
    user.id == session[:user_id]
  end
  
  def full_name(f)
    h(f.firstname) + ' ' + h(f.lastname)
  end
  
  def display_name(user)
    (you? user) ? "You" : "#{user.username}"
  end
  
  # event-centric actions #############################  
  def output_date(date)
    date.strftime("%I:%M %p - %A %m/%d/%Y") if date
  end
  
  def simple_date(date)
    date.strftime("%m/%d/%Y")
  end
  
  # generic actions #############################
  def refresh_messages
    page[:messages].replace :partial => "layouts/messages"
    page[:messages].visual_effect :highlight
  end
  
  def thumbnail(img, link = nil, size = :normal)
    # the width and height of the following background MUST be smaller than 
  	# the thumbnail generated by attachment_fu (see Image model)  	
  	size = (size == :small) ? '90px' : '150px'
    width = height = size
    
    if img.nil?
      "<div style='width: #{width}; height: #{height}; border: 1px solid #CCC; color: #666; text-align: center; vertical-align: middle; display: table-cell;'>No image</div>"
    else
      link ||= img.public_filename
    	link_to '', link, 
    	        :style => "display: block; width: #{width}; height: #{height}; background: url(#{img.public_filename(:thumb)}) no-repeat center center;"
    end
  end
  
  def get_image(obj)
    obj.image
  end
  
  # EventsController#tag_cloud never even called so I'm just taking this out -Hubert
  #depends on calling controller having a tag_cloud action, defaults to events_controller
  #def link_to_tag_cloud(target_controller = 'events')
  #  return link_to( 'tag cloud', {:controller => target_controller, :action => 'tag_cloud'} )
  #end
  
  #depends on calling controller having a tagged action, defaults to events_controller
  def tag_cloud_items(tags, target_controller = 'events')
    links = []
    tag_cloud_font_sizes(tags) do |name, font_size|
      link = link_to name, {:controller => target_controller, :action => 'tagged', :tag => name}, {:style => font_size}
      links << link
    end
    return links
  end
  
  def tags_for(acts_as_taggable_model)
    send = ""
    if acts_as_taggable_model.tags.size.nonzero?
      send += render :partial => 'tag', :collection => acts_as_taggable_model.tags
    else
      send += 'no tags <br />'
    end
    send
  end
  
  private ######################################################
  
  def tag_cloud_font_sizes(tags, base=1.0)
    max, min = 0, 1_000_000_000
    tags.each { |t|
      max = t[:count].to_i if t[:count].to_i >= max
      min = t[:count].to_i if t[:count].to_i <= min
    }
    
    if max == min
      divisor = 1.0
    else
      divisor = (max - min) #* (tags.size)
    end
    
    tags.each do |t|
      size = base + t[:count].to_f / divisor 
      yield t[:name], "font-size: #{size}em;"
    end
  end
  
end
