//----------------------------------------------
// addonSiteTOC.js
// 
// Version 2012-02-24 001 
//
// 2012-02-25 GW Added expandall and collapseall
//
// 2012-02-24 GW Replace ids with classes for
//               gwsitetoc, gwsitetoc_ul
//
// 2011-11-04 GW In gwsitetoc_checker_docready
//               Mods so comparison works with relative vs absolute hrefs
//               Also fix Home comparison
//
// 2011-01-30 GW In Expand TOC Items section:
//               1. wikispaces_page --> gwws_page_url
//               2. Fixup for home page
//
//----------------------------------------------

if (addonsEnable && ( typeof(ready_addonSiteTOC) == 'undefined' ) ) {
  var ready_addonSiteTOC = 0;

  //----------------------------
  jQuery(function($){
  //----------------------------

    //----------------------------
    // Modify TOC list for open/shut/click
    //----------------------------
    $('.gwsitetoc > ul').addClass('gwsitetoc_ul');

    $('.gwsitetoc li:has(ul)')
      .addClass('gwsitetoc_shut')
      .click( function(event) {
        if (this == event.target) {
          if ($(this).hasClass('gwsitetoc_shut')) {  // open it
            $(this)
              .removeClass('gwsitetoc_shut')
              .addClass('gwsitetoc_open');
          }
          else {  // shut it
            $(this)
              .removeClass('gwsitetoc_open')
              .addClass('gwsitetoc_shut');
          }
        }
        // return false;
      });
      
    $('.gwsitetoc li:not(:has(ul))').addClass('gwsitetoc_nokids');

    //-------------------------------------
    // Wrap spans around TOC item free text 
    //-------------------------------------
    $(".gwsitetoc_ul li").each( function(linum)  {
      if ( $(this).children.length > 0) {
        var node = this.childNodes[0];
        if (node.nodeType === 3) {
          var text = "";
          text = $.trim(node.nodeValue);
          if (text.length > 0) { 
            $(node).wrap('<span class="gwsitetoc_itemtext"><\/span>');
          }
        }
      }
    } );

    //----------------------------
    // Expand all TOC items on path to current page
    //----------------------------
    $('.gwsitetoc li').addClass('gwsitetoc_notpath');
 
    // Use template variable to get page
//  var myurl = '/' + wikispaces_page + '/';
//  myurl = myurl.replace(/ /g,"+");

    // More-reliable template variable to get page
    var myurl = gwws_page_url;

    // Wrap urls with slashes so as to avoid matching partial matches
    myurl = '/' + myurl + '/';

    $('.gwsitetoc li a').filter(function() {
        var testurl = $(this).attr('href') +'/';
        // Fixup for home page. (Page = home, but actual url = /
        if (testurl == '//') testurl = '/home/';
        return testurl == myurl;
      } )
      .addClass('gwsitetoc_currentlink');

    $('.gwsitetoc li:has(a.gwsitetoc_currentlink)')
      .addClass('gwsitetoc_currentpath')
      .removeClass('gwsitetoc_notpath');

    $('.gwsitetoc li.gwsitetoc_currentpath.gwsitetoc_shut')
     .removeClass('gwsitetoc_shut')
     .addClass('gwsitetoc_open');

    //-----------------------------------
    // Prev/Next feature
    //-----------------------------------
    var all_links = $('.gwsitetoc li>a');

    var this_link      = $("<span>[None]</span>");
    var prev_link_temp = $("<span>[None]</span>");
    var prev_link      = $("<span>[None]</span>");
    var next_link      = $("<span>[None]</span>");
    var currentlink_found = false;

    for (var ix = 0; ix < all_links.size(); ix++) {
      prev_link_temp = this_link;
      this_link = $(all_links.get(ix));

      if (currentlink_found) {
        next_link = this_link;
        break;
      } 

      if (this_link.hasClass('gwsitetoc_currentlink') ) {
        currentlink_found = true;
        prev_link = prev_link_temp;
      }
    }
    
    var prev_target = $('td.gwsitetoc_navprev');
    var next_target = $('td.gwsitetoc_navnext');
    
    prev_target.append(prev_link.clone());
    next_target.append(next_link.clone());

    //--------------------------------
    // ( CSS is loaded by theme )
    //--------------------------------

    // test expandall
    // gwsitetoc_expandall();
  }); // jQuery doc ready  
}

function gwsitetoc_expandall(addclass) {
var jqarg = '.gwsitetoc' + addclass + ' li.gwsitetoc_shut';
//   jQuery('.gwsitetoc li.gwsitetoc_shut')
   jQuery(jqarg)
     .removeClass('gwsitetoc_shut')
     .addClass('gwsitetoc_open');
}

function gwsitetoc_collapseall(addclass) {
var jqarg = '.gwsitetoc' + addclass + ' li.gwsitetoc_open';
//   jQuery('.gwsitetoc li.gwsitetoc_open')
   jQuery(jqarg)
     .removeClass('gwsitetoc_open')
     .addClass('gwsitetoc_shut');
}


//------------------------------------------
// For site TOC checker page
//------------------------------------------

function gwsitetoc_checker_docready($) {
  // cnm_ means classname
  var cnm_toc_pagefound    = "gwsitetoc_check_toc_pagefound"
  var cnm_toc_pagenotfound = "gwsitetoc_check_toc_pagenotfound"
  var cnm_page_tocfound    = "gwsitetoc_check_page_tocfound"
  var cnm_page_tocnotfound = "gwsitetoc_check_page_tocnotfound"

//  var tocchecker_out = $("div#tocchecker_out");
//  tocchecker_out.append("Hello<br>");
  
  //-------------------------------------
  // Default items in both lists to "not found"
  //-------------------------------------  
  
  // Items from list of all pages generated by wikispaces
  var jq_a_pagelist  = $("#gwsitetoc_check_pagelist li>a");
  var li_pagelist    = jq_a_pagelist.parent();  
  jq_a_pagelist.addClass(cnm_page_tocnotfound); 
  
  // fixup for the "Home" entry
  var a_home        = jq_a_pagelist.get(0);
  var homehref      = a_home.href;
  a_home.href       = homehref + "Home";  
  
  // Items from TOC created manually by site maintainer
  var jq_a_toclist  = $("#gwsitetoc_check_toc li>a");
  var li_toclist    = jq_a_toclist.parent();
  jq_a_toclist.addClass(cnm_toc_pagenotfound);
  
  //-------------------------------------
  // Compare each href in the page list to each
  // href in the TOC list.  If matched, 
  // mark both as "found-in-other-list"
  //-------------------------------------  
  jq_a_pagelist.each( function() {
    var a_page      = this;
    var jq_a_page   = $(this);     
    var a_page_href = a_page.href.toLowerCase();
    // var jq_li_page  = jq_a_page.parent();
    
    jq_a_toclist.each( function() {
      var a_toc      = this;
      var jq_a_toc   = $(this);
      var a_toc_href = a_toc.href.toLowerCase();

      if (a_page_href == a_toc_href) {
        // var jq_li_toc  = jq_a_toc.parent();
        
        jq_a_page.removeClass(cnm_page_tocnotfound)
                 .addClass(cnm_page_tocfound);  
        
        jq_a_toc.removeClass(cnm_toc_pagenotfound)
                .addClass(cnm_toc_pagefound);    
      }     
    
    } )

  } )

} // end gwsitetoc_checker_docready

function gwsitetoc_checker_colorenable(enable) {
  if (enable) {
    jQuery("div#gwsitetoc_check_toc"     ).addClass("gwsitetoc_check_colorenable");
    jQuery("div#gwsitetoc_check_pagelist").addClass("gwsitetoc_check_colorenable");
  } else {
    jQuery("div#gwsitetoc_check_toc"     ).removeClass("gwsitetoc_check_colorenable");
    jQuery("div#gwsitetoc_check_pagelist").removeClass("gwsitetoc_check_colorenable");
  }
}


