<div class="contextual">
  <% if User.current.allowed_to?(:add_subprojects, @project) %>
      <%= link_to l(:label_subproject_new), {:controller => 'projects', :action => 'add', :parent_id => @project}, :class => 'icon icon-add' %>
  <% end %>
</div>

<h2><%=l(:label_overview)%></h2>

<div class="splitcontentleft">
	<%= textilizable @project.description %>
	<ul>
	<% unless @project.homepage.blank? %><li><%=l(:field_homepage)%>: <%= link_to(h(@project.homepage), @project.homepage) %></li><% end %>
	</ul>
  <% if @subprojects.any? %>
    <% showHideOnclick = "onclick=\"showHide('#{@project.id}','#{@project.id}span')\"" %>
    <table class="list">
      <tbody>
        <tr class="open show parent subprojectRoot" id="<%= @project.id.to_s + "span" %>" >
          <td class="name" style="padding-left: 0em;">
            <span class="expander" <%=showHideOnclick%> >&nbsp; </span>
            <%= h("Sub Projects") %><span class="empty" <%=showHideOnclick%> >&nbsp;</span>
          </td>
        </tr>
<%
      # First, let's put the full project sub-tree into a nice array structure
	    projTree = [ @project ]
	    currIdx = 1
	    loop {
        currParent = projTree[currIdx - 1]
        break if(currParent.nil?)
        if(!currParent.children.empty?)
          sortedChildren = currParent.children.sort { |aa, bb| aa.name.downcase <=> bb.name.downcase }
	  sortedChildren.each_index { |ii|
            projTree[currIdx + ii,0] = sortedChildren[ii]
          }
        end
        currIdx += 1
      }

      # Next, loop over the sub-tree array elements and make the HTML rows
	    projTree.each_index { |ii|
        if(ii > 0)  # We'll skip the tree root
          rowid = classes = spanicon = openonclick = ""
          project = tmpProj = projTree[ii]
          # Getting the full ancestor of each project need only take a little iteration code
          ancestorIds = []
          (ii-1).downto(0) { |jj|
            if(tmpProj.parent_id == projTree[jj].id)  # We found the parent of tmpProj, next find ITS parent, etc
              ancestorIds << projTree[jj].id
              tmpProj = projTree[jj]
            end
          }
          # Let's only show the immediate subprojects (sub-sub projects will be hidden until user expands)
          if(project.parent_id == @project.id)
            classes += " show "
          else
            classes += " hide "
          end
          # Set up classes and such for the rows, depending on whether has children or not
          if(!project.children.empty?)
            projIdStr = project.id.to_s
            classes += " closed parent " + cycle("odd", "even")
            rowid = "id=\"#{projIdStr}span\""
            openonclick = "onclick=\"showHide('#{projIdStr}','#{projIdStr}span')\""
            spanicon = "<span #{openonclick} class=\"expander\">&nbsp; </span>"
          else
            classes += " closed child " + cycle("odd", "even")
          end
          classes += " #{ancestorIds.join(' ')} "
          baseurl = url_for(:controller => 'plugin_assets', :action => 'index')
          # Does this project have boards? If so, we'll provide a direct link to its Forums page
          if(!project.boards.empty?)
            forumsStr = "<a href=\"#{url_for({ :controller => 'projects', :action => 'show', :id => project})}/boards\"><span class=\"forumsLink\">" +
                        "<img alt=\"Forums\" title=\"Forums\" src=\"#{baseurl}/projects_tree_view/images/user_comment.png\"></span></a>"
          else
            forumsStr = ""
          end
          # Does this project have a wiki? If so, we'll provide a direct link to its main Wiki page
          if(project.wiki)
            wikiStr = "<a href=\"#{url_for({ :controller => 'projects', :action => 'show', :id => project})}/wiki\"><span class=\"wikiLink\">" +
                      "<img alt=\"Wiki\" title=\"Wiki\" src=\"#{baseurl}/projects_tree_view/images/page_white_wrench.png\"></span></a>"
          else
            wikiStr = ""
          end
%>
          <tr class="<%= classes %>" <%= rowid %> >
            <td class="name" style="padding-left: <%= 2*project.level %>em;">
              <%= spanicon %>
              <%= project.active? ? link_to(h(project.name), {:controller => 'projects', :action => 'show', :id => project}, :class => "project") : h(project.name) %>
              <span <%= openonclick %> class="empty <%=User.current.member_of?(project) ? 'my-project' : nil%>">&nbsp;</span>
              <%= forumsStr %><%= wikiStr %>
            </td>
          </tr>
<%
        end
      }
%>
	  </tbody>
	</table>
  <% end %>
	<% @project.custom_values.each do |custom_value| %>
	<% if !custom_value.value.empty? %>
	   <li><%= custom_value.custom_field.name%>: <%=h show_value(custom_value) %></li>
	<% end %>
	<% end %>

  <% if User.current.allowed_to?(:view_issues, @project) %>
  <div class="box">
    <h3 class="icon22 icon22-tracker"><%=l(:label_issue_tracking)%></h3>
    <ul>
    <% for tracker in @trackers %>
      <li><%= link_to tracker.name, :controller => 'issues', :action => 'index', :project_id => @project,
                                                :set_filter => 1,
                                                "tracker_id" => tracker.id %>:
					<%= l(:label_x_open_issues_abbr_on_total, :count => @open_issues_by_tracker[tracker].to_i,
																										:total => @total_issues_by_tracker[tracker].to_i) %>
			</li>
    <% end %>
    </ul>
    <p><%= link_to l(:label_issue_view_all), :controller => 'issues', :action => 'index', :project_id => @project, :set_filter => 1 %></p>
  </div>
  <% end %>
  <%= call_hook(:view_projects_show_left, :project => @project) %>
</div>

<div class="splitcontentright">
<% if users_by_role_implemented %>
	<% if @members_by_role.any? %>
	  <div class="box">
		<h3 class="icon22 icon22-users"><%=l(:label_member_plural)%></h3>
		<p><% @members_by_role.keys.sort.each do |role| %>
		<%= role.name %>:
		<%= @members_by_role[role].collect(&:user).sort.collect{|u| link_to_user u}.join(", ") %>
		<br />
		<% end %></p>
	  </div>
	<% end %>
<% else %>
	<% if @users_by_role.any? %>
	  <div class="box">
		<h3 class="icon22 icon22-users"><%=l(:label_member_plural)%></h3>
		<p><% @users_by_role.keys.sort.each do |role| %>
		<%=h role %>: <%= @users_by_role[role].sort.collect{|u| link_to_user u}.join(", ") %><br />
		<% end %></p>
	  </div>
	<% end %>
<% end %>

  <% if @news.any? && authorize_for('news', 'index') %>
  <div class="box">
    <h3><%=l(:label_news_latest)%></h3>
    <%= render :partial => 'news/news', :collection => @news %>
    <p><%= link_to l(:label_news_view_all), :controller => 'news', :action => 'index', :project_id => @project %></p>
  </div>
  <% end %>
  <%= call_hook(:view_projects_show_right, :project => @project) %>
</div>

<% content_for :sidebar do %>
    <% planning_links = []
      planning_links << link_to_if_authorized(l(:label_calendar), :controller => 'issues', :action => 'calendar', :project_id => @project)
      planning_links << link_to_if_authorized(l(:label_gantt), :controller => 'issues', :action => 'gantt', :project_id => @project)
      planning_links.compact!
      unless planning_links.empty? %>
    <h3><%= l(:label_planning) %></h3>
    <p><%= planning_links.join(' | ') %></p>
    <% end %>

    <% if @total_hours && User.current.allowed_to?(:view_time_entries, @project) %>
    <h3><%= l(:label_spent_time) %></h3>
    <p><span class="icon icon-time"><%= l_hours(@total_hours) %></span></p>
    <p><%= link_to(l(:label_details), {:controller => 'timelog', :action => 'details', :project_id => @project}) %> |
    <%= link_to(l(:label_report), {:controller => 'timelog', :action => 'report', :project_id => @project}) %></p>
    <% end %>
<% end %>

<% content_for :header_tags do %>
<%= auto_discovery_link_tag(:atom, {:action => 'activity', :id => @project, :format => 'atom', :key => User.current.rss_key}) %>
<% end %>

<% html_title(l(:label_overview)) -%>
