module ApplicationHelper
  
  
  def qi_js_field(form, model, method, options = {})
    r = ""
    if options[:hidden]
      r += hidden_field_tag(method, eval("form.object.#{method}"), :class => "input_#{model}_#{method} form-control",:disabled => true)
    else
      if options[:label]
        r += label_tag(:input, options[:label])
      end
      r += text_field_tag(method, (options[:value] ? options[:value] : eval("form.object.#{method}")), :class => "input_#{model}_#{method} form-control",:disabled => true)
    end
    return raw(r)
  end

  def qi_js_field_price_line_form(form, model, method, options = {})
    r = ""
    if options[:hidden]
      r += hidden_field_tag(method, eval("form.object.#{method}"), :class => "input_#{model}_#{method} form-control",:disabled => true)
    else
      if options[:label]
        r += label_tag(:input, options[:label], class: options[:label_class])
      end
      r += text_field_tag(method, (options[:value] ? options[:value] : eval("form.object.#{method}")), :class => "input_#{model}_#{method} form-control",:disabled => true)
    end
    return raw(r)
  end

  
  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