module MuckEngineHelper

  # Outputs a small bit of javascript that will enable message 
  # output to jgrowl or to the given message dom id
  #
  # message_dom_id: The dom id of the element that will hold messages.
  #                 This element can have display:none by default.
  def jquery_message(message_dom_id)
    if GlobalConfig.growl_enabled
      "jQuery('##{message_dom_id}').html(json.message);"
      "jQuery('##{message_dom_id}').show();"
    else
      "jQuery.jGrowl.info(json.message);"
    end
  end
  
  def custom_form_for(record_or_name_or_array, *args, &proc) 
    options = args.detect { |argument| argument.is_a?(Hash) } 
    if options.nil? 
      options = {:builder => MuckCustomFormBuilder} 
      args << options 
    end 
    options[:builder] = MuckCustomFormBuilder unless options.nil? 
    form_for(record_or_name_or_array, *args, &proc) 
  end
  
  def custom_remote_form_for(record_or_name_or_array, *args, &proc) 
    options = args.detect { |argument| argument.is_a?(Hash) } 
    if options.nil? 
      options = {:builder => MuckCustomFormBuilder} 
      args << options 
    end 
    options[:builder] = MuckCustomFormBuilder unless options.nil? 
    remote_form_for(record_or_name_or_array, *args, &proc) 
  end
  
  def output_flash(options = {})
    output_errors('', options)
  end
  
  def output_errors(title, options = {}, fields = nil)
    fields = [fields] unless fields.is_a?(Array)
    flash_html = render(:partial => 'shared/flash_messages')
    flash.clear
    field_errors = render(:partial => 'shared/field_error', :collection => fields)
    css_class = "class=\"#{options[:class]}\"" unless options[:class].nil?
    
    if !flash_html.empty? && field_errors.empty?
      # only flash.  Don't render the 
      render(:partial => 'shared/flash_error_box', :locals => {:flash_html => flash_html, :css_class => css_class})
    elsif !field_errors.empty?
      # field errors and/or flash
      render(:partial => 'shared/error_box', :locals => {:title => title, 
        :flash_html => flash_html, 
        :field_errors => field_errors,
        :css_class => css_class,
        :extra_html => options[:extra_html]})
    else
      #nothing
      ''
    end
  end
  
  # generates javascript that will hide a link or button
  # when click and then show a different dom object
  def show_hide_on_click(id_to_show, id_to_hide)
    %Q{jQuery(document).ready(function() {
      jQuery('##{id_to_hide}').click(function(){
        jQuery(this).hide();
        jQuery('##{id_to_show}').show();
      });
    });}
  end
  
  # Render a photo for the given object.  Note that the object will need a 'photo' method
  # provided by paperclip.
  # size is commonly one of:
  # :medium, :thumb, :icon or :tiny but can be any value provided by the photo object
  def icon(object, size = :icon)
    return "" if object.blank?
    image_url = object.photo.url(size) rescue '/images/profile_default.jpg'
    link_to(image_tag(image_url, :class => size), object, { :title => object.full_name })
  end
  
  def secure_mail_to(email)
    mail_to email, nil, :encode => 'javascript'
  end
  
  # Used inside of format.js to return a message to the client.
  # If jGrowl is enabled the message will show up as a growl instead of a popup
  def page_alert(page, message, title = '')
    if GlobalConfig.growl_enabled
      page << "jQuery.jGrowl.error('" + message + "', {header:'" + title + "'});"
    else
      page.alert(message)
    end
  end
  
  # Override link_to_remote so that instead of '#' the proper url is rendered
  # This makes the link usable even if javascript is disabled
  # See: http://www.intridea.com/2007/11/21/link_to_remote-unobtrusively
  def link_to_remote(name, options = {}, html_options = {})  
    html_options.merge!({:href => url_for(options[:url])}) unless options[:url].blank?
    super(name, options, html_options)  
  end
  
  def locale_link(name, locale)
    parts = request.host.split('.')
    first_subdomain = parts.first
    request_uri = request.request_uri
    if first_subdomain == 'www' or Language.supported_locale?(first_subdomain)
      link_to name, request.protocol + (locale == I18n.default_locale.to_s ? 'www' : locale) + '.' + parts[1..-1].join('.') + request.port_string + request_uri
    elsif /^localhost/.match( request.host ) or /^(\d{1,3}\.){3}\d{1,3}$/.match( request.host )
      if request_uri.include?('?')
        if request_uri.include?('locale')
          link_to name, request.protocol + request.host_with_port + request_uri.sub(/locale=.*/, 'locale=' + locale)
        else
          link_to name, request.protocol + request.host_with_port + request_uri + '&locale=' + locale
        end
      else
        link_to name, request.protocol + request.host_with_port + request_uri + '?locale=' + locale
      end  
    else
      link_to name, request.protocol + (locale == I18n.default_locale.to_s ? 'www' : locale) + '.' + request.host_with_port + request_uri
    end
  end

  # Generate parameters for a url that refer to a given object as parent.  Useful
  # for comments, shares, etc
  def make_muck_parent_params(parent)
    return if parent.blank?
    { :parent_id => parent.id, :parent_type => parent.class.to_s }
  end
  
  # Take a block and renders that block within the context of a partial.
  # from http://snippets.dzone.com/posts/show/2483
  def block_to_partial(partial_name, options = {}, &block)
    options.merge!(:body => capture(&block))
    concat(render(:partial => partial_name, :locals => options))
  end
    
  # Take a block and renders that block within the context of a partial.
  # Passes the block to the partial.  The partial is then responsible for
  # capturing and rendering the block.
  # from http://snippets.dzone.com/posts/show/2483
  def raw_block_to_partial(partial_name, options = {}, &block)
    options.merge!(:block => block)
    concat(render(:partial => partial_name, :locals => options))
  end
  
  # Summarize html content by removing html
  # tags and truncating at a given number of words.
  # Truncation will occur at word boundries
  # Parameters:
  #   text    - The text to truncate
  #   length  - The desired number of words
  #   omission  - Text to add when the text is truncated ie 'read more'
  def html_summarize(text, length = 30, omission = '...')
    snippet(strip_tags(text), length, omission)
  end
  
  # Truncates text at a word boundry and provides a 
  # parameter for a 'more link'
  # Parameters:
  #   text      - The text to truncate
  #   wordcount - The number of words
  #   omission  - Text to add when the text is truncated ie 'read more'
  def snippet(text, wordcount, omission)
   text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "")
  end

  def round(flt)
    return (((flt.to_f*100).to_i.round).to_f)/100.0
  end

  def truncate_on_word(text, length = 270, end_string = ' ...')
    if text.length > length
      stop_index = text.rindex(' ', length)
      stop_index = length - 10 if stop_index < length-10
      text[0,stop_index] + (text.length > 260 ? end_string : '')
    else
      text
    end
  end
  
  def truncate_words(text, length = 40, end_string = ' ...')
    words = text.split()
    words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
  end
  
  # Outputs a javascript include localized for specific time actions
  def time_scripts(locale)
    render :partial => 'scripts/time_scripts', :locals => {:locale => locale}
  end

  # Outputs include script for google ajax api
  def google_ajax_api_scripts
    return '' if defined?(@google_ajax_api_scripts_included)
    script = '<script type="text/javascript" src="http://www.google.com/jsapi'
    script << "?key=#{GlobalConfig.google_ajax_api_key}" if GlobalConfig.google_ajax_api_key
    script << '"></script>'
    @google_ajax_api_scripts_included = true
    script
  end
  
  # Output include script for google slideshows
  def google_ajax_slideshow_scripts
    return '' if defined?(@google_ajax_slideshow_scripts_included)
    @google_ajax_slideshow_scripts_included = true
    '<script src="http://www.google.com/uds/solutions/slideshow/gfslideshow.js" type="text/javascript"></script>'
  end
  
  # Output include script for google feeds
  def google_load_feeds
    return '' if defined?(@google_load_feeds_included)
    @google_load_feeds_included = true
    google_ajax_api_scripts + '<script type="text/javascript">google.load("feeds", "1");</script>'
  end
  
  # Output include script for google search
  def google_load_search
    return '' if defined?(@google_load_search_included)
    @google_load_search_included = true
    google_ajax_api_scripts + '<script type="text/javascript">google.load("search", "1");</script>'
  end
  
  # Output include script to load jquery from google
  def google_load_jquery
    return '' if defined?(@google_load_jquery_included)
    @google_load_jquery_included = true
    google_ajax_api_scripts + '<script type="text/javascript">google.load("jquery", "1");</script>'
  end

  # Output include script to load jquery ui from google
  def google_load_jquery_ui
    return '' if defined?(@google_load_jquery_ui_included)
    @google_load_jquery_ui_included = true
    google_ajax_api_scripts + '<script type="text/javascript">google.load("jqueryui", "1");</script>'
  end
  
  # Outputs a snippet of javascript that can parse uris
  # http://blog.stevenlevithan.com/archives/parseuri
  def parse_uri_script
    render :partial => 'scripts/parse_uri'
  end
  
end