# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  @@tabs = [
            { :label => 'About', :redirect => { :controller => 'home', :action => nil, :id => nil } },
            { :label => 'Resources', :redirect => { :controller => 'resource', :action => nil, :id => nil } },
            { :label => 'Organizations', :redirect => { :controller => 'org', :action => nil, :id => nil  } },
            { :label => 'Search', :redirect => { :controller => 'search', :action => nil, :id => nil } },
            { :label => 'Help', :redirect => { :controller => 'help', :action => nil, :id => nil } }
           ]

  # See which controller we're in, then emit a different tag for that tab
  def display_tabs
    # Add tabs depending on permission of user
    if current_user
      @@tabs += [  { :label => 'Action', :redirect => { :controller => 'admin' } } ]
    end   
    if permit?('site_admin')
      @@tabs += [  { :label => 'Admin', :redirect => { :controller => 'site_admin' } } ]  
    end 

    # Construct the choice list
    html = <<-HTML
			<ul>
				<li class="browse_category">Select An Area:</li>
	HTML
	
    @@tabs.each do |tab|
      if @controller.controller_name == tab[:redirect][:controller]
        html << '<li class="active">' + link_to( tab[:label], tab[:redirect] ) + '</li>'
      else
        html << '<li>' + link_to( tab[:label], tab[:redirect] ) + '</li>'
      end
    end
    html << '</ul>'
  end
  
  def client_side_list_with_autocomplete( *args )
    stem = (args.last.is_a?(Hash) and args.last[:naming]) ? args.last[:naming].to_s : 'entry'
    choices = (args.last.is_a?(Hash) and args.last[:choices]) ? args.last[:choices].to_s : stem.pluralize
    lookup_div = "#{stem}_lookup"
    selected_entries = "selected_#{stem}"
    required_entries = (args.last.is_a?(Hash) and args.last[:require_choice_in_table]) ? "#{choices}" : 'null'
    html = <<-HTML_CODE
  		<div id="#{stem}_list"></div>
      <input type="text" id="#{lookup_div}" name="#{lookup_div}" />
      <a href="##{stem}_list" onclick="csl_add_to_list(#{selected_entries}, '#{stem}', #{required_entries})">Add</a>
      <div class="auto_complete" id="#{lookup_div}_auto_complete"></div>
    HTML_CODE
    js_code = <<-JS_CODE
      new Autocompleter.Local(
        '#{lookup_div}', '#{lookup_div}_auto_complete', csl_remove_from_array(#{selected_entries}, #{choices}),
        { 
          fullSearch: true, 
          frequency: 0.3, 
          minChars: 1 
        }
      );
    JS_CODE
    html << javascript_tag(js_code)
  end
  
  def init_ratings_bar
    # Add AJAX calls on selection of star rating and comment toggling.
    js_code = <<-JS_CODE
      <script type="text/javascript">
        var wt_init_actions = {
          '.star-rating a' : function(el) {
            el.onclick = function() {
              new Ajax.Request(el.href, {asynchronous:true, evalScripts:true, onComplete:wt_ajax_refresh }); 
              return false;
            }
          }
        };
        Behaviour.register(wt_init_actions);
        
        // This should be called whenever updating DOM that has dynamically attached actions.
        function wt_ajax_refresh() {
          Behaviour.apply(wt_init_actions);
        }
      </script>                                                                     
    JS_CODE
    @dynamic_code[:ratings_bar] = js_code
  end
												
  # Help Links
  def help_link( arg )
"
<span id=\"help\">
#{link_to '?', :controller => 'help', :action => arg } 
</span>
"

# this could be an image
#    #{link_to image_tag('help.gif'), :controller => 'help', :action => arg  } 
  end


  def cloud_size ( arg )

    s = [ "s1", "s2", "s3", "s4", "s5" ]

    if arg <= 0 then
      arg = 0
    end
    
    if arg >= 4 then
      arg = 4
    end
    
    s[arg]

  end 

  # Check if we have a valid entry for a list
  def is_specified?( arg )
    not ( arg.nil? or ( arg.class == String and arg.empty? ) )
  end

end
