if(RB==null){
  var RB = {};
}

RB.constants = {
  <% if @section == 'backlogs'

    default_status = IssueStatus.default
    default_status = default_status.id.to_s if default_status

    roles = User.current.roles_for_project(@project)
    transitions = {:states => {}, :transitions => {} , :default => default_status }

    RbStory.trackers.each {|tracker_id|
      tracker = Tracker.find(tracker_id)
      tracker_id = tracker_id.to_s

      transitions[:transitions][tracker_id] = {}

      tracker.issue_statuses.each {|status|
        status_id = status.id.to_s

        transitions[:states][status_id] = {:name => status.name, :closed => (status.is_closed? ? l(:label_closed_issues) + ' ' : "")}

        [[false, false], [true, true], [false, true], [true, false]].each{|creator, assignee|
          key = "#{creator ? '+' : '-'}c#{assignee ? '+' : '-'}a"

          transitions[:transitions][tracker_id][key] ||= {}

          begin
            allowed_statues = status.new_statuses_allowed_to(roles, tracker, creator, assignee)
          rescue #Workaround in order to support redmine 1.1.3
            allowed_statues = status.new_statuses_allowed_to(roles, tracker)
          end
          
          allowed = allowed_statues.collect{|s| s.id.to_s}
          
          transitions[:transitions][tracker_id][key][:default] ||= allowed[0]

          allowed.unshift(status_id)

          transitions[:transitions][tracker_id][key][status_id] = allowed.compact.uniq
        }
      }
    }
  %>
  story_states: <%= transitions.to_json %>,
  <% end %>

  <% if @section == 'taskboard'
      transitions = User.current.admin?
      if !transitions && RbTask.tracker
        roles = User.current.roles_for_project(@project)
        tracker = Tracker.find(RbTask.tracker)

        catch (:done) do
          [[false, false], [true, true], [false, true], [true, false]].each{|creator, assignee|
            tracker.issue_statuses.each {|status|
              status.new_statuses_allowed_to(roles, tracker, creator, assignee).each{|s|
                transitions = true
                throw :done
              }
            }
          }
        end
      end %>
  workflow: <%= transitions.to_json %>,
  <% end %>
  project_id: <%= @project.id %>,
  sprint_id: <%= @sprint ? @sprint.id : "null" %>,
  protect_against_forgery: <%= protect_against_forgery? ? "true" : "false" %>,
  request_forgery_protection_token: '<%= request_forgery_protection_token %>',
  form_authenticity_token: '<%= form_authenticity_token %>',
  <%
    autorefresh_wait = Setting.plugin_redmine_backlogs[: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 %>
}

RB.buildImageTag = function(name){
  return "<img src='<%= Engines::RailsExtensions::AssetHelpers.plugin_asset_path('redmine_backlogs', 'images', nil) %>" + name + "'/>"
}

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;
}

RB.routes = {
  create_sprint: '<%= url_for(:controller => 'rb_sprints', :action => 'create') %>',
  update_sprint: '<%= url_for(:controller => 'rb_sprints', :action => 'update', :sprint_id => ":id") %>',
  
  create_story: '<%= url_for(:controller => 'rb_stories', :action => 'create') %>',
  update_story: '<%= url_for(:controller => 'rb_stories', :action => 'update', :id => ":id") %>',
  
  create_task: '<%= url_for(:controller => 'rb_tasks', :action => 'create') %>',
  update_task: '<%= url_for(:controller => 'rb_tasks', :action => 'update', :id => ":id") %>',

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

  show_updated_items: '<%= url_for(:controller => 'rb_updated_items', :action => 'show', :project_id => ":id") %>',
  show_burndown_chart: '<%= url_for(:controller => 'rb_burndown_charts', :action => 'show', :sprint_id => ":id") %>',
  show_burndown_embedded: '<%= url_for(:controller => 'rb_burndown_charts', :action => 'embedded', :sprint_id => ":id") %>',

  backlog_menu: '<%= url_for(:controller => 'rb_master_backlogs', :action => 'menu', :project_id => @project.identifier, :format => :json, :only_path => true) %>'
}

if(typeof console != "undefined" && console != null) console.log('*** server variables loaded ***');
