if (!RB.constants) {
  RB.buildImageTag = function(name) {
    return "<img src='<%= image_path('') %>"+ name + "'/>";
  };

  RB.constants = {
    protect_against_forgery: <%= protect_against_forgery? ? 'true' : 'false' %>,
    request_forgery_protection_token: <%= request_forgery_protection_token.to_json.html_safe %>,
    form_authenticity_token: <%= form_authenticity_token.to_json.html_safe %>,
    <%
      autorefresh_wait = Backlogs.setting[:autorefresh_wait].to_s
      autorefresh_wait = '1000' if autorefresh_wait.blank?
      autorefresh_wait = '1000' unless autorefresh_wait =~ /^[0-9]+$/
      autorefresh_wait = Integer(autorefresh_wait)
      autorefresh_wait = 1000 if autorefresh_wait < 1000
    %>
    autorefresh_wait: <%= autorefresh_wait.to_json %>,
    new_story_position: <%= Backlogs.setting[:new_story_position].to_s.to_json.html_safe %>
  };

  RB.constants.locale = {
    _: function(s) {
      if (RB.constants.locale[s])
        return RB.constants.locale[s]; 
      return s;
    },
    'Filter tasks': "<%= l(:label_filter_tasks)%>",
    'Filter tasks: my tasks': "<%= l(:label_filter_tasks_my_tasks)%>",
    'All tasks': "<%= l(:label_all_tasks) %>",
    'My tasks': "<%= l(:label_my_tasks) %>",
    'View options': "<%= l(:label_view_options) %>",
    'No data to show': "<%= l(:label_no_data_to_show) %>"
  };

  RB.routes = {
    create_sprint: <%= url_for(:controller => 'rb_sprints', :action => 'create').to_json.html_safe %>,
    update_sprint: <%= url_for(:controller => 'rb_sprints', :action => 'update', :sprint_id => ':id').to_json.html_safe %>,
    update_release: <%= url_for(:controller => 'rb_releases', :action => 'update', :release_id => ':id').to_json.html_safe %>,

    create_story: <%= url_for(:controller => 'rb_stories', :action => 'create').to_json.html_safe %>,
    update_story: <%= url_for(:controller => 'rb_stories', :action => 'update', :id => ':id').to_json.html_safe %>,

    create_task: <%= url_for(:controller => 'rb_tasks', :action => 'create').to_json.html_safe %>,
    update_task: <%= url_for(:controller => 'rb_tasks', :action => 'update', :id => ':id').to_json.html_safe %>,

    create_impediment: <%= url_for(:controller => 'rb_impediments', :action => 'create').to_json.html_safe %>,
    update_impediment: <%= url_for(:controller => 'rb_impediments', :action => 'update', :id => ':id').to_json.html_safe %>,

    show_updated_items: <%= url_for(:controller => 'rb_updated_items', :action => 'show', :project_id => ':id').to_json.html_safe %>,
    show_burndown_chart: <%= url_for(:controller => 'rb_burndown_charts', :action => 'show', :sprint_id => ':id').to_json.html_safe %>,
    show_burndown_embedded: <%= url_for(:controller => 'rb_burndown_charts', :action => 'embedded', :sprint_id => ':id').to_json.html_safe %>,
    show_tooltip: <%= url_for(:controller => 'rb_stories', :action => 'tooltip', :id => ':id').to_json.html_safe %>,
    home: <%= url_for(:home).to_json.html_safe %>
  };

  RB.urlFor = function(route_name, options) {
    route = RB.routes[route_name];
    if(options != null && options['id'] != null){
      route = route.replace(':id', options['id']);
    }
    return route;
  };
}

<% if @project %>
  RB.constants.project_id = <%= @project.id %>;
  RB.permissions = {
      create_stories: <%= User.current.allowed_to?(:create_stories, @project) ? 'true' : 'false' %>,
      update_stories: <%= User.current.allowed_to?(:update_stories, @project) ? 'true' : 'false' %>,
      create_sprints: <%= User.current.allowed_to?(:create_sprints, @project) ? 'true' : 'false' %>,
      update_sprints: <%= User.current.allowed_to?(:update_sprints, @project) ? 'true' : 'false' %>,
      create_releases: <%= User.current.allowed_to?(:modify_releases, @project) ? 'true' : 'false' %>,
      update_releases: <%= User.current.allowed_to?(:modify_releases, @project) ? 'true' : 'false' %>,
      create_tasks: <%= User.current.allowed_to?(:create_tasks, @project) ? 'true' : 'false' %>,
      update_tasks: <%= User.current.allowed_to?(:update_tasks, @project) ? 'true' : 'false' %>,
      create_impediments: <%= User.current.allowed_to?(:create_impediments, @project) ? 'true' : 'false' %>,
      update_impediments: <%= User.current.allowed_to?(:update_impediments, @project) ? 'true' : 'false' %>
  };
<% end %>

<% if @sprint %>
  RB.constants.sprint_id = <%= @sprint.id %>;
  RB.constants.project_id = <%= @sprint.project_id %>;
  <% if @context == 'taskboard' %>
  RB.constants.task_states = <%= workflow_transitions(RbTask).to_json.html_safe %>;
  <% end %>
<% end %>

<%
  if @context == 'backlogs'
=begin
 project_versions maps all projects to those versions which can receive the projects issues.
 project_versions are used by the js user interface to determine to which backlogs a dragged story can be dropped to.
 It uses the project_id attached to the story and checks all visible sprints if it is in the array hashed to the project_id here.
 We need to provide maps to at lest all projects which we find in visible versions AND all self_and_descendants for the product backlog. 
 An approximation would be all projects which all visible sprints are shared in plus self_and_descendants.
 Most simple way: use all projects the user can see, trading some bytes of bandwidth against many server cycles of comparison and filtering.
=end
    project_versions = Hash[Project.visible.find(:all).map{|project|
      [
        project.id.to_s,
        project.open_shared_sprints.collect{|v| v.id.to_s }
      ] if project
    }]

=begin
 a release can contain stories of all projects which it is visible
 but droppables are only self_and_descendants of the current view to avoid user confusion by vanishing stories
=end
    project_releases = Hash[Project.visible.find(:all).map{|project|
      [
        project.id.to_s,
        project.shared_releases.open.visible.reject{|r|
          !@project.projects_in_shared_product_backlog.include? project
          }.collect{|v| v.id.to_s }
      ] if project
    }]

#project_self_and_descendants is used by the js ui to determine if a dragged story can be dropped onto the product backlog.
    projects_in_product_backlog = Hash[@project.projects_in_shared_product_backlog.map{|p| [p.id.to_s, true]}]
  %>
  RB.constants.projects_in_product_backlog = <%= projects_in_product_backlog.to_json.html_safe %>;
  RB.constants.project_versions = <%= project_versions.to_json.html_safe %>;
  RB.constants.project_releases = <%= project_releases.to_json.html_safe %>;
  RB.constants.story_states = <%= workflow_transitions(RbStory).to_json.html_safe %>;
  RB.routes.backlog_menu = <%= url_for(:controller => 'rb_master_backlogs', :action => 'menu', :project_id => @project.identifier, :only_path => true).to_json.html_safe %>;
  RB.routes.closed_sprints = <%= url_for(:controller => 'rb_master_backlogs', :action => 'closed_sprints', :project_id => @project.identifier, :only_path => true).to_json.html_safe %>;
<% end %>
