/* ---------------------------------------------
Nested Accordion v.1.4
Script to create 'accordion' functionality on a hierarchically structured content.
http://adipalaz.awardspace.com/experiments/jquery/nested_accordion.html
Requires: jQuery v1.3+
Copyright (c) 2009 Adriana Palazova
Dual licensed under the MIT (http://adipalaz.awardspace.com/docs/mit-license.txt) and GPL (http://adipalaz.awardspace.com/docs/gpl-license.txt) licenses.
------------------------------------------------ */

(function($j) {
//$j.fn.orphans - http://www.mail-archive.com/jquery-en@googlegroups.com/msg43851.html
$j.fn.orphans = function(){
var txt = [];
this.each(function(){$j.each(this.childNodes, function() {
  if (this.nodeType == 3 && $j.trim(this.nodeValue)) txt.push(this)})}); return $j(txt);};
  
$j.fn.accordion = function(options) {
    var defaults = {
        obj : 'ul', //the element that contains the accordion - 'ul', 'ol', 'div' 
        objClass : '.accordion', //the class name of the accordion
        objID : '', //the ID of the accordion (optional)
        wrapper :'li', //the common parent of 'a.trigger' and 'o.next' - 'li', 'div'
        el : 'li', //the parent of 'a.trigger' - 'li', '.h'
        head : '', //the headings that are parents of 'a.trigger' (if any)
        next : 'ul', //the collapsible element - 'ul', 'ol', 'div'
        initShow : '', //the initially expanded section (optional)
        showMethod : 'slideDown', //'slideDown', 'show', 'fadeIn', or custom
        hideMethod : 'slideUp', //'slideUp', 'hide', 'fadeOut', or custom
        showSpeed: 400,
        hideSpeed: 800,
        activeLink : true // 'true' if the accordion is used for site navigation
    };
    
    var o = $j.extend({}, defaults, options);
    
   
    return this.each(function() {
        var containerID = '#' + this.id,
          Obj = containerID + ' ' + o.obj + o.objID + o.objClass,
          El = Obj + ' ' + o.el,
          loc = window.location.href;

        $j(Obj).find(o.head).addClass('h');
                         
        $j(El).orphans().wrap('<a class="trigger" style="display:block" href="javascript:doNothing()" title="open/close" />');
         
		$j(this).removeClass('active');
		
		listItem = document.getElementById( 'level1_' + level2 );
		
		if( listItem == null )
			listItem = document.getElementById( 'root_' + level2 );
			
		if( listItem != null )
		{
			listItemLink = listItem.getElementsByTagName( 'a' )[0];
			listItemLink.className = "activeCategory";
			listItemLink.style.color = "#fd6617";
		}
		
        $j(El + '+ div:not(".outer")').wrap('<div class="outer" />'); 

        $j(Obj + ' :header.h').each(function(){
          var $jthis = $j(this);
          if (o.wrapper == 'div') $jthis.add( $jthis.next('div.outer') ).wrapAll('<div class="new"></div>');
        }); 
        
        $j(El + ' a.trigger').closest(o.wrapper).find('> ' + o.next).hide();

        if (o.activeLink) $j(Obj + ' a[href=' + loc + ']').addClass('active').closest(o.next).addClass('current');

        $j(Obj).find(o.initShow).show()
          .parents(o.next).show().end()
          .parents(o.wrapper).find('> a.trigger, > ' + o.el + ' a.trigger').addClass('open');
        
        $j(El).find('a.trigger').click(function() {
            var $jnextEl = $j(this).closest(o.wrapper).find('> ' + o.next),
                $jsiblings = $j(this).closest(o.wrapper).siblings(o.wrapper);
                
            if(($jnextEl).length && ($jnextEl.is(':visible'))) {
              $j(this).removeClass('open');
              $jnextEl.filter(':visible')[o.hideMethod](o.hideSpeed);
			 return false;
            };
            
            if(($jnextEl).length && ($jnextEl.is(':hidden'))) {
              $jsiblings.find('> a.open, >'+ o.el + ' a.open').removeClass('open').end()
                .find('> ' + o.next + ':visible')[o.hideMethod](o.hideSpeed);
			 
			  ulAccordion = $j('.accordion')[0];
			  activeDivs = ulAccordion.getElementsByTagName('a');
			   
			  /*for( i = 0; i < activeDivs.length; i++ )
			  {
				  if( activeDivs[i].className == "active" )
				  	  activeDivs[i].className = "lowest";
			  }*/
			  
			  $j(this).addClass('open');
			  $jnextEl[o.showMethod](o.showSpeed);
              n = $j(this).parent('li').attr('id').slice(1);
			  
			  listId = $j(this).parent('li').attr('id');
			  
			  array = listId.split('_');
			  n = array[1];
			  
			  if( listId.indexOf( 'root' ) != -1 || listId.indexOf( 'level1' ) != -1 )
			  {
				  divs = $j(".activeCategory");
		
				  for( i = 0; i < divs.length; i++ )
				  {
			          divs[i].className = "lowest";
			          divs[i].style.color = "#ffffff";
				  }
				  
				  getNextCategories( absolutePath, webRoot, n, '0' );
			  }
			  else
			  {
			      getNextProducts( absolutePath, webRoot, n, '0' );
			  }
            };
          });
    });
};
})(jQuery);
///////////////////////////
// The plugin can be invoked, for example, like this:
/* ---
$j(function() {
/// Standard nested lists:
  $j('#container1').accordion();
  // this will expand the first sub-list, when the accordion is initialized:
  $j('#container1').accordion({initShow : ">li>ul:eq(0)"});
  // this will expand the second sub-list, when the accordion is initialized:
  $j('#container1').accordion({initShow : ">li>ul:eq(1)"});
  // this will expand the sub-list with "class=current", when the accordion is initialized:
  $j('#container1').accordion({initShow : "ul.current"});
  
/// Nested Lists + Headings + DIVs:
  $j('#container2').accordion({el: '.h', head: 'h4, h5', next: 'div'});
  $j('#container2').accordion({el: '.h', head: 'h4, h5', next: 'div', initShow : 'div.outer:eq(1)'});
  
/// Nested DIVs + Headings:
  $j('#container2').accordion({obj: 'div', wrapper: 'div', el: '.h', head: 'h4, h5', next: 'div.outer'});
  $j('#container2').accordion({objID: '#acc2', obj: 'div', wrapper: 'div', el: '.h', head: 'h4, h5', next: 'div.outer', initShow : 'div.outer:eq(0)'});
});
--- */
