<% @title = "Welcome, "+current_user.name+"." %>
<% @description = "Your Dashboard" %>
<div class="container">
  <div class="row">
  <div class="span4">
    <div class="whiteSpace"></div>
    <h2 class="span12">Leagues</h2>
    <div class="searchWhiteSpace span12"></div>
    <div class="span4">
      <%= link_to "Create a league",new_league_path, :class=>"btn btn-primary btn-league" %>
    </div>
    <div class="whiteSpace span12"></div>
    <h1 class="span4 or-text"><center>or</center></h1>
    <div class="whiteSpace span12"></div>
    <div class="span4 searchDashboard">
      <div class="input-append">
        <%= form_tag("/leagues", :method => "get") do %> 
        <%= text_field_tag :filter,nil, :placeholder => "Search for a league", :class=>"long" %>
        <input name="commit" class="btn" type="submit" value="Go!" />
        <% end %>
      </div> <!-- span5 -->
      <div class="whiteSpace"></div>
      <div class="activityContainer">
        <h1 class="shiftLeft">Activity stream</h1>
        <div class="innerActivity">
        <% for comment in Comment.all.reverse[0,20]%>
          <% if comment.comment_type == 2 %>
            <div class="commentsFix"><div><a href="<%= user_path(comment.user_id) %>"><%=User.find(comment.user_id).name%></a> posted a comment on <a href="<%= user_path(comment.location_id) %>"><%= User.find(comment.location_id).name%>'s</a> wall:</div> <span style="font-weight:600;"><%=comment.comment%></span></div>
          <% else %>
            <div class="commentsFix"><div><a href="<%= user_path(comment.user_id) %>"><%=User.find(comment.user_id).name%></a> posted a comment on <a href="<%= league_path(comment.location_id) %>"><%= League.find(comment.location_id).name%></a>:</div> <span style="font-weight:600;"><%=comment.comment%></span></div>
          <% end %>
        <% end %>
        </div> 
      </div>
    <div class="whiteSpace"></div>
    </div> <!-- whiteSpace --> 
  </div>
  <div class="whiteSpace"></div>
  <h2 class="fixTextAlign">Your performance</h2>
  <div class="whiteSpace"></div>
  <div class="span8">
    <% portfolios = Portfolio.where("user_id = ?",current_user.id) %>
    <% x = 1 %>
    <% unless portfolios.nil? %>

    <div class="accordion" id="basic-accordion">
    <% for portfolio in portfolios %>
      <% positions = Order.find_positions(portfolio.id)%>
                  <div class="accordion-group open">
                    <div class="accordion-heading">
                      <a class="accordion-toggle" data-toggle="collapse" data-parent="#basic-accordion" href="#collapse<%=x%>">
                        <%=League.find(portfolio.league_id).name%> </br>
                      </a>
                    </div>
                    <div id="collapse<%=x%>" class="accordion-body <%if x==1%>in<%end%> collapse">
<%x=x+1%>
                      <div class="accordion-inner">
      <table>
        <tr>
          <td style="font-weight:bold">Capital:</td> <td>$<%= number_with_precision(Performance.today(portfolio.id),:precision => 2) %></td>
        </tr>
        <% @total_worth =Performance.today(portfolio.id) %>
        <% positions.each do |position| %>
        <tr class="green_table_row" style="background-color: #D2E8CF;">
          <% @total_worth += YahooFinance::get_standard_quotes(position[0])[position[0]].lastTrade*position[1] %>
          <td style="font-weight:bold"> <%= position[0]%>:</td> <td><%= position[1] %> shares worth $<%= number_with_precision(YahooFinance::get_standard_quotes(position[0])[position[0]].lastTrade*position[1], :precision => 2) %></td>
        </tr>
        <% end %>
        <tr>
          <td style="font-weight:bold">Total Value:</td> <td>$<%= number_with_precision(@total_worth, :precision => 2) %></td>
        </tr>
      </table>
                        <div id="simple_graph_dashboard<%= x %>"></div>

                      </div>
                    </div>
                  </div>

        <script type="text/javascript" charset="utf-8">
          $(function() {
            var chart<%= x %> = new Highcharts.StockChart({
              chart: {
                renderTo: 'simple_graph_dashboard<%= x %>'
                // alignTicks: true // by default
              },

              yAxis: {
                title: {
                  text: 'Your performance'
                }
              },

              rangeSelector: {
                selected: 1
              },

              series: [{
                name: 'Your performance',
                data: <%= Order.find_history(portfolio.id)  %>
              }]
            });
          });
        </script>
    <% end %>
  <% end %>
</div>

  </div>
  </div>




               <script>
                 function switchGraph(){
                    if($("#simple_graph_dashboard").is(":visible")){
                        $("#simple_graph_dashboard").hide();
                        $("#candlestick_dashboard").show();
                    }
                    else{
                        $("#simple_graph_dashboard").show();
                        $("#candlestick_dashboard").hide();
                    }

                 }
               </script>
</div> <!-- container --> 
