module ApplicationHelper
  
  def edit_watcher(element_type, element_id, key='' )
    raw ('<script type="text/javascript">')+("edit_watcher('"+element_type+"', '"+element_id.to_s+"','"+key+"');")+("setInterval(function(){ edit_watcher('"+element_type+"', '"+element_id.to_s+"','"+key+"');}, 1000);")+('</script><div id="edit_watcher_result"></div>')
  end
  def hashtag_link(hash)
    link_to " ##{hash} ", hashtag_public_image_actu_path(hash.to_slug), :target => "_blank"
  end
  
  def parse_hashtag(text)
    text.gsub(Hashtag::REGEX) {hashtag_link($1)}
  
  end
  
  def flash_js
    r = raw ("$('#flashs').append('"+ escape_javascript(bootstrap_flash) +"');flash_delay();")
    flash[:notice] = nil
    flash[:error] = nil
    flash[:success] = nil
    
    r
  end
  
  
  def forum_avatar_url(user)
    if user
      user.avatar_url
    else
      "/default_avatar.png"
    
    end
  end
  
  
  def sort_link(column, title = nil)
    
    #def sortable_columns
    #  ["name", "price"]


    sort_column = (params[:column] ? params[:column] : nil)


    sort_direction = (%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc")
 
    
    
      title ||= column.titleize
      direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
      icon = sort_direction == "asc" ? "chevron-up" : "chevron-down"
      icon = column == sort_column ? icon : ""
      link_to raw("#{title} "+ic(icon).html_safe), request.query_parameters.merge({column: column, direction: direction})
  end
  
  
end
