class QiImageSelectInput < Formtastic::Inputs::TextInput
  include FormtasticBootstrap::Inputs::Base
  
  def to_html
    r = rand(1000000000000000000000000000)  


    
    label_html +
    template.content_tag(:fieldset, image_preview(method, options, r) + remove_image_link(method, options, r) + builder.hidden_field(method.to_s, input_html_options.merge!(:id => "input_"+r.to_s))   )
  
    
  end

  def remove_image_link(method, options, r)
    template.content_tag(:a, "Supprimer", :href => "#", :onclick => "remove_image_cible('"+r.to_s+"');return false;")
      
  end

  def manager_select_link(method, options, r)
    template.content_tag(:a, "Modifier cette image", :href => "#", :onclick => "select_image_from_manager('"+r.to_s+"');return false;")
  end

  def image_preview(method, options,r)
    if object.send("#{method}?") and ImageFile.exists?(object.send("#{method}"))
      template.content_tag(:span, template.image_tag(object.image_file.file.large.medium.small.thumb.url, :id => "img_"+r.to_s, :onclick => "select_image_from_manager('"+r.to_s+"');return false;"), :class => "preview ")
    else
      template.content_tag(:span, template.image_tag("admin/default_image.png", :id => "img_"+r.to_s, :onclick => "select_image_from_manager('"+r.to_s+"');return false;"), :class => "preview ")
    end
  end

end






