function closeTab(chapter)
{
  var list = chapter.find(".list");
  var f = function(){
    chapter.removeClass("openedtab");
    chapter.addClass("closedtab");
  };
  
  if(list.length > 0) {
    list.slideUp("fast", f);
  } else {
    f();
  }
}

function openTab(chapter)
{
  var list = chapter.find(".list");
  var f    = function(){
    chapter.addClass("openedtab");
    chapter.removeClass("closedtab");
  };
  
  if(list.length > 0) {
    list.slideDown("fast", f);
  } else {
    f();
  }
}



$(function(){
  
//  $("body").delegate("a", "click", function(){
//    var href    = $(this).attr("href");
//    var oldHash = window.location.hash; 
//    
//    if(typeof href == "string" && 
//       (href.indexOf("/html/") == 0 ||
//        href.indexOf("/documentation/") == 0 ||
//        href.indexOf("/api/") == 0)) {
//      window.location.hash = "!" + href;
//      return false;
//    } else if(href.indexOf("#") == 0) {
//      if(oldHash.indexOf("!") != -1) {
//        window.location.hash = href.substring(1) + oldHash.substring(oldHash.indexOf("!"));
//      } else {
//        window.location.hash = href.substring(1); 
//      }
//      return false;
//    }
//  });
  
  //TODO: if only the local hash changes, don't reload
 // $(window).bind('hashchange', function(){
 //
 //   var hash      = window.location.hash;
 //   var markIndex = hash.indexOf('!');
 //   var newHash   = hash.substring(markIndex + 1);
 //   var localHash = hash.substring(0, markIndex);
 //
 //   if(newHash){
 //     if(newHash == "/html/index") {
 //       newHash = "/html/home";
 //     }
 //     
 //     if(newHash.indexOf("/api/") == 0) {
 //  
 //       $.get(newHash, function(data){
 //         var content = $('<div />').append(data);
 //         var container = $("article.xqdoc");
 //         if(container.length == 1) {
 //           var html = content.find("article#content").html();
 //           container.replaceWith($('<article id="content" class="xqdoc" />').append(html));
 //         } else {
 //           $("#main-wrapper > *").replaceWith(content.html());
 //         }
 //         
 //         if(localHash && $(localHash).length == 1) {
 //           $.scrollTo(localHash);
 //         }
 //       });
 //       
 //       $("nav .active").removeClass("active");
 //       $("#dev-center").addClass("active");
 //                 
 //     } else if(newHash.indexOf("/documentation/") == 0) {
 //       
 //       $.get(newHash, function(data){
 //         var content = $('<div />').append(data);
 //         var container = $("article.doc");
 //         if(container.length == 1) {
 //           var html = content.find("article#content").html();
 //           container.replaceWith($('<article id="content" class="doc" />').append(html));
 //         } else {
 //           $("#main-wrapper > *").replaceWith(content.html());
 //         }
 //       });
 //       
 //       $("nav .active").removeClass("active");
 //       $("#dev-center").addClass("active");
 //       
 //     } else {
 //       
 //       $.get(newHash, function(data){
 //         $("#main-wrapper > *").replaceWith(data);
 //       });
 //     
 //       var id = newHash.substring(newHash.indexOf("/html/") + 6);
 //       var index = id.indexOf("/");
 //       if(index != -1) {
 //         id = id.substring(0, index);
 //       }
 //       
 //       $("nav .active").removeClass("active");
 //       $("#"+id).addClass("active");
 //       
 //     }

 //   }
 // });
 
//  $(window).trigger('hashchange');
  
  //Documentation handling
  $("body").delegate(".chapter", "click", function(){
    $(".active-section").removeClass("active-section");
    var chapter = $(this).parent();
    if(chapter.hasClass("closedtab")) {
      closeTab($(".openedtab"));
      openTab(chapter);
    }
  });
    
  $("body").delegate(".section", "click", function(){
    $(".active-section").removeClass("active-section");
    $(this).addClass("active-section");
  });
  
  //API handling
  $("body").delegate(".subsection", "click", function(){
    $(".active-section").removeClass("active-section");
    $(this).addClass("active-section");
  });
  
  $("body").delegate(".expand", "click", function(){
    //var path = $(this).attr("href");
    $(".side-nav").append('<li id="loading"><img src="/images/ajax-loader.gif" alt="Loading" style="margin-left: 96px;" /></li>');
    //$.get("/api/menu?path="+path, function(data){
    closeTab($(".openedtab"));
    //  var loading = $("#loading");
    //  loading.replaceWith(data);
    //});
  });
  
  $("body").delegate(".top-section", "click", function(){
    $(".active-section").removeClass("active-section");
    $(this).addClass("active-section");
    var parent = $(this).parent();
    if(parent.hasClass("closedtab")) {
      parent.find("~ li").remove();
      openTab(parent);
    }
  });
  
  $("body").delegate("#show-other-platforms", "click", function(){
    var con = $(".other-platforms");
    if(con.is(':visible')) {
      con.slideUp();
      $(this).find("span").text("Show downloads for other platforms");
    } else {
      con.slideDown();
      $(this).find("span").text("Hide downloads for other platforms");
    }
  });
  
  $("ol").each(function(i, n)
  {
    $(n).children().each(function(k, v){
      $(v).css('background-image', 'url(/images/list/'+ (k + 1) +'.png)');
      $(v).css('background-repeat', 'no-repeat');
    });
  });
});

