var navBarTimeout = false;
var navBarFitting = false;
var navBarMoreHoverTimer;

var userMenuHoverTimer;

$(document).ready(function() {

  // add index and settings links
  $("#_SearchForm").append("<a class='head-link' href='/_Index'>Index</a>");
  if (isAdmin) {
    $("#_SearchForm").append("<span class='head-link'>|</span><a class='head-link' href='/_Settings'>Settings</a>");
  }
  
  // hide the Pages in this Section box if there's nothing to list
  if ($("#section_treenav .tree_nav").children().length==0) {
    $("#section_treenav").hide();
  }

  // events for the nav bar extention drop down menu
  $(".navbar .more, #navbarext").live('mouseenter',function(){
    if (typeof navBarMoreHoverTimer!='undefined') {
      clearTimeout(navBarMoreHoverTimer);
      navBarMoreHoverTimer = undefined;
    }
    showNavBarExt();
  });
  $(".navbar .more, #navbarext").live('mouseleave',function(){
    navBarMoreHoverTimer = setTimeout('hideNavBarExt()',500);
  });

  // events for the user drop down menu
  $("#user-menu-trigger, #user-menu").bind('mouseenter',function(){
    if (typeof userMenuHoverTimer!='undefined') {
      clearTimeout(userMenuHoverTimer);
      userMenuHoverTimer = undefined;
    }
    showUserMenu();
  });
  $("#user-menu-trigger, #user-menu").bind('mouseleave',function(){
    userMenuHoverTimer = setTimeout('hideUserMenu()',500);
  });

  // build the navbar
  autoFitNavBar();
  // rejigger on resize for everyone but IE
  if (typeof jQuery.browser.msie == 'undefined') {
    $(window).resize(function() {
      if (navBarTimeout) return;
      setTimeout('autoFitNavBar()',100);
      navBarTimeout = true;
    });
  }
  
  // build the sidebar
  populateSidebar();
  
  //add attachment icons
  addAttachmentIcons();
});

function autoFitNavBar() {
  navBarTimeout = false;
  if (navBarFitting) return;
  navBarFitting = true;
  if (typeof navbar != 'undefined') {
    var nb = $(".navbar"); // nav bar 
    var ext = $("#navbarext"); // extension drop down
    var full = false; // are we out of room on the nav bar?
    var more = '<li class="more"><a href="#">More <img src="/img/settings_button_open.png" border="0"></a></li>';
    nb.html('');
    ext.html('');
    var maxw = nb.parent().width()-110;
    var width = 0;
    for (var i in navbar) {
      var p = navbar[i];
      var cls = p.active?' class="current"':'';
      var item = '<li'+cls+'><a href="'+p.url+'">'+p.title+'</a></li>';
      if (!full) {
        nb.append(item);
        var last = nb.find('> li:last');
        width += last.width();
        if (width>maxw) { 
          last.remove();
          nb.append(more);
          full = true;
        }
      }
      // needs to run in addition to above - don't use else
      if (full) {
        ext.append(item);
      }
    }
  }
  navBarFitting = false;
}

function hideNavBarExt() {
  $("#navbarext").hide();
}
function showNavBarExt() {
  var ext = $("#navbarext");
  var last = $(".navbar .more");
  ext.css({
   "top": last.offset().top+last.height()+1,
    "right": $(window).width() - (last.offset().left + last.width())
  }); 
  ext.show();
}

function hideUserMenu() {
  $("#user-menu").hide();
}
function showUserMenu() {
  var menu = $("#user-menu");
  var trigger = $("#user-menu-trigger");
  menu.css({
   "top": trigger.offset().top + trigger.height()+7,
    "left": trigger.offset().left,
    "width": trigger.width()
  }); 
  menu.find("a").css({"width":trigger.width()-12, "padding":"6px"});
  menu.show();
}

function populateSidebar() {
  var sb = sidebar.split(','); // sidebar defined in Layout
  var container = $("#active_sidebar");
  for (var i in sb) {
    var item = $("#"+sb[i]);
    item.detach();
    container.append(item);
  }
}

function addAttachmentIcons() {
	$("#attachmentTable td a").each(function() {
		var $link = $(this);
		
		var path = $link.attr("href");
		var extension = /[^.]+$/.exec(path);

		//Why doesn't this work?
		//$link.addClass(extension);
		$link.attr("class", extension);
	});
}

