<script>
    $(document).ready(function() {
        $('.dropdown-menu li a').each(function() {
            if ($(this).hasClass('current')) {
                $(this).parents('.dropdown').find('a.dropdown-toggle').css('color', '#e00000');
            };
        })
    })
</script>

<div id="pw_header">
  <%= link_to image_tag(get_home_page_logo, title: I18n.t('home'), alt: pp_title.partition('-')[0]), home_logo_url, id: "pw_logo" %>
  <%= link_to image_tag("pp2.png", title: I18n.t('goto_pixpalace2'), alt: I18n.t('pixpalace2')), pp2_url, id: "pw_logo_2", target: "PP2" if Server.itself? PIXADMIN_SERVER_NAME %>
  <div id="navigation1">
    <nav class="navbar">
      <li class="flash_msg">
        <% flash.each do |k,v| %>
          <span id="pw_flash-<%= k %>"><%= v %></span>
        <% end %>
      </li>
      <% if session[:pixtech] && session[:pixtech].include?('vitrine') %>
        <li>
          <%= link_to("<span class='glyphicon glyphicon-user' style='float:right'></span>#{I18n.t('log_in')}".html_safe, '#', class: 'login_modal_link', data: {toggle: 'modal', target: '#login-modal'}) %>
        </li>
      <% else %>
        <li style="padding-top:0.25em;">
          <% if current_user.is_superadmin? %>
            <%= %x[hostname]%>
            <span class="pw_separator">|</span>
          <%end%>
          <% unless current_user.title.name.blank? || (session[:pixtech] && !session[:pixtech].blank?) %>
            <%= "#{current_user.title.name}" %>
            <span class="pw_separator">|</span>
          <%end%>
          <%= "#{current_user.first_name} #{current_user.last_name}" %>
          <span class="pw_separator" style="margin-right:30px;"></span>
        </li>
        <% if Server.itself?(VINGTMINUTES_SERVER_NAME) || Server.itself?(PTREF_SERVER_NAME) %>
          <li>
            <%= link_to "<span class='glyphicon glyphicon-upload'></span>".html_safe, new_uploads_photo_path, title: "Upload", target: :blank %>
            <span class="pw_separator" style="margin-right:10px;"></span>
          </li>
        <% end %>
        <% unless session[:pixtech] && !session[:pixtech].blank? %>
          <li>
            <%= link_to "<span class='glyphicon glyphicon-question-sign'></span>".html_safe, pdf_file, target: :blank,title: I18n.t('help')  %>
            <span class="pw_separator" style="margin-right:10px;"></span>
          </li>
        <% end %>
        <% unless Server.itself?(PP2_SERVER_NAME) %>
          <li>
            <%= link_to("<span class='glyphicon glyphicon-cog'></span>".html_safe, edit_setting_path(current_user.setting.id),remote: true, title: I18n.t('settings.settings')) %>
            <span class="pw_separator" style="margin-right:10px;"></span>
          </li>
        <% end %>
        <li>
          <%= link_to("<span class='glyphicon glyphicon-off colored'></span>".html_safe, :logout, id: "pw_logout", title: I18n.t('logout'), onclick: 'signOut();') %>
          <span class="pw_separator" style="margin-right:10px;"></span>
        </li>
      <% end %>
    </nav>
  </div>

  <% if session[:pixtech] && Pixways.get_pixtech_param_value(session[:pixtech],'show_main_menu') == 'false' %>
    <div style='height:45px'></div>
  <% else %>
    <div id="navigation2">

      <ul class="nav nav-tabs">
        <li><%= link_to_unless_current(I18n.t('home'), :home){|link| content_tag 'a', link, class: 'current'} %>
        </li>
        <% unless Server.itself?(PTREF_SERVER_NAME) || Server.type_is?('PIXTECH') %>
          <% unless (!session[:pixtech_author].blank? && Pixways.get_pixtech_param_value(session[:pixtech],'show_menu_agencies_contacts') == 'false') || (session[:pixtech] && !session[:pixtech].blank? && Pixways.get_pixtech_param_value(session[:pixtech],'pixtech_only')=='true') %>
            <li>
              <% if session[:pixtech] && !session[:pixtech].blank? %>
                <% agencies_contacts_value = Pixways.get_pixtech_param_value(session[:pixtech],'agencies_contacts_text') %>
                <% if agencies_contacts_value.blank?  %>
                  <%= link_to_unless_current(I18n.t('agencies_contacts', provider_word: I18n.t(session[:provider_word], count: 2)).capitalize, :providers){|link| content_tag 'a', link, :class => 'current'} %>
                <% else %>
                  <%= link_to_unless_current( I18n.t(agencies_contacts_value), :providers){|link| content_tag 'a', link, :class => 'current'} %>
                <% end %>
              <% else %>
                <%= link_to_unless_current(I18n.t('agencies_contacts', provider_word: I18n.t(session[:provider_word], count: 2)).capitalize, :providers){|link| content_tag 'a', link, :class => 'current'} %>
              <% end %>
            </li>
          <% end %>
        <% end %>
        <% if session[:pixtech] && !session[:pixtech].blank? && Pixways.get_pixtech_param_value(session[:pixtech],'show_creators_list')=='true' %>
          <li><%= link_to_unless_current(I18n.t(:creator_list,creator_word: I18n.t(session[:creator_word])), :authors_list){|link| content_tag 'a', link, class: 'current'} %></li>
        <% end %>
        <% if (can? :manage, Statistic) && session[:pixtech_author].blank? && Pixways.get_pixtech_param_value(session[:pixtech],'show_menu_customers_contacts') != 'false' && !Server.itself?(VINGTMINUTES_SERVER_NAME) && !Server.itself?(PTREF_SERVER_NAME) %>
          <li><%= link_to_unless_current(I18n.t('menu_titles'), :titles){|link| content_tag 'a', link, class: 'current'} %></li>
        <% end %>
        <% if current_user.is_superadmin? %>
          <li><%= link_to_unless_current(I18n.t('admin.admin'), :admin){|link| content_tag 'a', link, class: 'current'} %></li>
        <% end %>
        <% if can? :manage, Statistic %>
          <% unless !session[:pixtech_author].blank? && Pixways.get_pixtech_param_value(session[:pixtech],'show_menu_statistics') == 'false' %>
            <li><%= link_to_unless_current(I18n.t('statistics.statistics'), :searchstats){|link| content_tag 'a', link, :class => 'current'} %></li>
          <% end %>
        <% end %>
        <% if (can? :manage, Statistic) && Server.itself?(VINGTMINUTES_SERVER_NAME) %>
          <li><%= link_to_unless_current("Uploads", uploads_photos_path){|link| content_tag 'a', link, :class => 'current'} %></li>
        <% end %>
        <% if ( (can? :manage, Statistic) || (!session[:pixtech_author].blank?) ) && ( Server.itself?(PIXADMIN_SERVER_NAME) || Server.itself?(VINGTMINUTES_SERVER_NAME) || Server.itself?(TEST_SERVER_NAME) ) %>
          <li class="dropdown">
            <% if session[:pixtech] && !session[:pixtech].blank? %>
              <a data-toggle="dropdown" <%= "class=dropdown-toggle" if current_page?(:pictures_control)||current_page?(:search_stats)||current_page?(:rtp)||current_page?(:admin_reps)||current_page?(:new_reportage)  %> id="item_adm_agency" onclick="void(0)"><%= Pixways.get_pixtech_param_value(session[:pixtech],'text_admin_agency') %><span class="caret"></span></a>
            <% else %>
              <a data-toggle="dropdown" <%= "class=dropdown-toggle" if current_page?(:pictures_control)||current_page?(:search_stats)||current_page?(:rtp)||current_page?(:admin_reps)||current_page?(:new_reportage)  %> id="item_adm_agency" onclick="void(0)"><%= I18n.t('adm_agency', provider_word: (I18n.t(session[:provider_word], count: 1)).capitalize) %><span class="caret"></span></a>
            <% end %>
            <ul class="dropdown-menu">
              <% if Server.itself?(PIXADMIN_SERVER_NAME) || Server.itself?(TEST_SERVER_NAME) %>
                <% if (current_user.is_provider_admin?) || (current_user.is_superadmin?) %>
                  <li><%= link_to_unless_current(I18n.t('dashboard.dashboard_title'), :dashboard){|link| content_tag 'a', link, :class => 'current'} %></li>
                  <li><%= link_to_unless_current(I18n.t(:new_creator_account,creator_word: I18n.t(session[:creator_word])), new_user_path){|link| content_tag 'a', link, :class => 'current'} if Pixways.get_pixtech_param_value(session[:pixtech],'show_new_author') == 'true' %></li>
                  <li><%= link_to_unless_current(I18n.t('pictures_control'), :pictures_control){|link| content_tag 'a', link, :class => 'current'} %></li>
                  <li><%= link_to_unless_current(I18n.t('top'), :refresh_providers){|link| content_tag 'a', link, :class => 'current'} %></li>
                  <li><%= link_to_unless_current(I18n.t('features_admin'), admin_reps_path){|link| content_tag 'a', link, :class => 'current',:offres_reportages =>1} %></li>
                  <li><%= link_to_unless_current(I18n.t('search_stats'), :search_stats){|link| content_tag 'a', link, :class => 'current'} %></li>
                  <li class="last_item_menu"><%= link_to_unless_current(I18n.t('admin.providers.requests'), :rtp){|link| content_tag 'a', link, :class => 'current'} %></li>
                <% elsif !session[:pixtech_author].blank? %>
                  <li><%= link_to_unless_current(I18n.t('pictures_control'), :pictures_control){|link| content_tag 'a', link, :class => 'current'} %></li>
                  <li class="last_item_menu"><%= link_to_unless_current(I18n.t('search_stats'), :search_stats){|link| content_tag 'a', link, :class => 'current'} %></li>
                <% else %>
                  <li><%= link_to_unless_current(I18n.t('features_admin'),admin_reps_path){|link| content_tag 'a', link, :class => 'current',:offres_reportages =>1} %></li>
                  <li class="last_item_menu"><%= link_to_unless_current(I18n.t('search_stats'), :search_stats){|link| content_tag 'a', link, :class => 'current'} %></li>
                <% end %>
              <% elsif Server.itself?(VINGTMINUTES_SERVER_NAME) %>
                <li class="last_item_menu"><%= link_to_unless_current(I18n.t('pictures_control'), :pictures_control){|link| content_tag 'a', link, :class => 'current'} %></li>
              <% end %>
            </ul>
          </li>
        <% end %>
        <% if current_user.is_superadmin? && Server.itself?(PTREF_SERVER_NAME) && Server.itself_production? %>
          <% if current_page?(controller: '/users', action: 'new') %>
            <li><%= content_tag 'a', "PT User", class: 'current'  %></li>
          <% else %>
            <li><%= link_to("PT User", new_user_path) %></li>
          <% end %>
        <% end %>
      </ul>
    </div>
  <% end %>

</div>
<div id="pw_contextual_header">
  <%= yield :contextual_header %>
</div>

<script>
    (function ($, window, delay) {
        // http://jsfiddle.net/AndreasPizsa/NzvKC/
        var theTimer = 0;
        var theElement = null;
        var theLastPosition = {x:0,y:0};
        $('[data-toggle]')
            .closest('li')
            .on('mouseenter', function (inEvent) {
                if (theElement) theElement.removeClass('open');
                window.clearTimeout(theTimer);
                theElement = $(this);

                theTimer = window.setTimeout(function () {
                    theElement.addClass('open');
                }, delay);
            })
            .on('mousemove', function (inEvent) {
                if(Math.abs(theLastPosition.x - inEvent.ScreenX) > 4 ||
                    Math.abs(theLastPosition.y - inEvent.ScreenY) > 4)
                {
                    theLastPosition.x = inEvent.ScreenX;
                    theLastPosition.y = inEvent.ScreenY;
                    return;
                }

                if (theElement.hasClass('open')) return;
                window.clearTimeout(theTimer);
                theTimer = window.setTimeout(function () {
                    theElement.addClass('open');
                }, delay);
            })
            .on('mouseleave', function (inEvent) {
                window.clearTimeout(theTimer);
                theElement = $(this);
                theTimer = window.setTimeout(function () {
                    theElement.removeClass('open');
                }, delay);
            });
    })(jQuery, window, 200); // 200 is the delay in milliseconds
</script>