
  
module Formtastic
    module QiImageSelect

      def self.included(base)
        base.send(:extend, ClassMethods)
      end

      module ClassMethods

        @@image_preview_sizes = [ :preview, :thumbnail, :thumb, :small ]

        def image_preview_sizes
          @@image_preview_sizes
        end

        def image_preview_sizes=(value)
          @@image_preview_sizes = value
        end

      end


      protected

      def qi_image_select_input(method, options)
	
		r = rand(1000000000000000000000000000)	
		
        html_options = options.delete(:input_html) || {}

        self.label(method, options_for_label(options)) +
        template.content_tag(:fieldset, self.manager_select_link(method, options, r) + template.tag(:br) + self.image_preview(method, options, r) + self.send(:hidden_field, method, html_options.merge!(:id => "input_"+r.to_s)))
      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}?")
          template.content_tag(:span, template.image_tag(object.image_file.file.large.medium.small.thumb.url, :id => "img_"+r.to_s), :class => "preview ")
        else
          template.content_tag(:span, template.image_tag("", :id => "img_"+r.to_s), :class => "preview ")
        end
      end

    end
  end