module ApplicationHelper

  FLASH_NOTICE_KEYS = [:error, :notice, :warning]
  
  def site_valid_locales_default_head
    index = @site_valid_locales.rindex I18n.default_locale.to_s
    shift_out = @site_valid_locales.shift(index)
    @site_valid_locales += shift_out
  end
  
  def colorize_in_use_locale(locale)
    @site_in_use_locales.include?(locale)? 'green' : 'red'
  end
  
  def flash_messages
    return unless messages = flash.keys.select{|k| FLASH_NOTICE_KEYS.include?(k)}
    formatted_messages = messages.map do |type|      
      content_tag :div, :class => type.to_s do
        message_for_item(flash[type], flash["#{type}_item".to_sym])
      end
    end
    raw(formatted_messages.join)
  end
  
  def link_back
     link_to t('back'), get_go_back, :class => 'nav'
  end
  
  # Clean the link back
  def get_go_back
    begin
      if request.url.include?('locale=')
        session[:last_page]
      else
        session[:last_page] = remove_locale(request.referer)
      end
    rescue
      eval(params[:controller].split('/').join('_') << '_url')
    end
  end

  # Remove the locale but keep all the other params
  def remove_locale(url)
    target = url.split('?')
    vars = target[1].split('&') rescue []
    vars.delete_if {|var| var.include? 'locale=' }
    if vars.size > 0
      target[0].to_s + '?' + vars.join('&')
    else
      target[0].to_s
    end
  end

  def add_locale(url, locale)
    if url.include?('?')
      url + "&locale=#{locale}"
    else
      url + "?locale=#{locale}"
    end
  end

  def message_for_item(message, item = nil)
    if item.is_a?(Array)
      message % link_to(*item)
    else
      message % item
    end
  end
  
  def add_attribute(partial, f, attribute)
    new_object = f.object.send(attribute).build
    fields = f.fields_for(attribute, new_object, :child_index => "new_#{attribute}") do |f|
      render :partial => partial, :object => new_object, :locals => {:f => f}
    end
  end
  
  def active_for_controllers(*controller_names)
    controller_names.include?(controller.controller_name) ? 'active' : nil
  end

  def visible_for_controllers(*controller_names)
    controller_names.include?(controller.controller_name) ? '' : 'hide'
  end
  
  def active_for_action(controller_name, action_name)
    ((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil
  end

  def sortable(column)
    direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
    {:sort => column, :direction => direction}
  end

  def is_sort_active?(name)
    res = ''
    res << ' select' if params[:sort].eql?(name)
    res << ' active' if params[:sort].eql?(name) && params[:direction].eql?('asc')
    res
  end

  def is_sort?(name)
    ' web-symbol' if params[:sort].eql?(name)
  end

  def is_filter_active?(type, id)
    ' active' if (@filter[type].include?(id.to_s) rescue nil)
  end

  def process_page(page, id)
    parse_page_noko(page, id)
  end

  def page_metas(page)
    metas = ''
    @site.page_metas.each do |meta|
      name, content = meta.get_name_content
      metas << "<meta name=#{name} content=#{content} />\n"
    end rescue nil
    return metas
  end

  def page_title(page)
    "<title>#{page.title ? page.title[I18n.locale] : page.i18n_variable[I18n.locale]}</title>\n"
  end  

  def page_stylesheets(page)
    stylesheets = ''
    stylesheets << "<link href='/assets/bootstrap.css.erb' rel='stylesheet' type='text/css' />\n"
    stylesheets << "<link href='/assets/bootstrap-orbit.css' rel='stylesheet' type='text/css' />\n"
    stylesheets << "<link href='/assets/style.css.erb' rel='stylesheet' type='text/css' />\n"
    stylesheets << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />\n" if page.design.reset_css
    stylesheets << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' />\n" if page.design.default_css
    theme = page.design.themes.detect{ |d| d.id == page.theme_id }
    stylesheets << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />\n" if theme
    stylesheets
  end

  def page_javascripts(page)
    javascripts = ''
    javascripts << "<script type='text/javascript' src='/static/jquery.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/static/jquery.cycle.all.latest.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/static/kernel.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/bootstrap.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/jquery.tinyscrollbar.min.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/jquery.isotope.min.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/orbit-bar-member.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/orbit-bar-search.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/orbit_bar.js'></script>\n"
    javascripts << "<script type='text/javascript' src='/assets/event.js'></script>\n"
    page.design.javascripts.each do |js|
      # javascripts << "<script type='text/javascript' src='#{js.file.url}'></script>"
    end
    javascripts
  end

end
