/***
 * Foldout feature
 *
 * example: /usage: <ul id="foldout"><li><div class="block"></div><div class="foldout"></div></li></ul>/
 */
var folderInterval;
function setupFold() {
  if (!$('#foldout').size()) return;
  var nl = $('#foldout li');

  if (!nl.length) return;
  for (var i = 0; i < nl.length; i++) {
    var n = $(nl[i]);
    if (n[0].parentNode.id != "foldout") continue;

    $(n).bind("click", toggleFoldTrigger);

    // Apply JS hover solution on list items.
    $(n).bind("mouseover", toggleFoldOver);
    $(n).bind("mouseout", toggleFoldOut);

    if (ua.ie) {
      // Calculate base height of folds using canvas width.
      // When folding, we use the current canvas width to compensate for
      // resizing issues (smaller canvas = higher foldouts).
      var folder = $('.foldout:first',n);
      folder[0].oldClientHeight = (1.2 * folder.clientHeight * document.all.main.clientWidth);
      n[0].style.marginBottom = '1px';
    }
  }

  if (document.location.hash) {
    var folder = $('#'+document.location.hash.substr(2));
  } else {
    resetFold();
  }

  folderInterval = setInterval(toggleFoldOnHash, 500);

  // open first tab by default
  if (!document.location.hash || $(folder).size()==0) {
  	document.location.href = '#_art_0';
  }
}


function resetFold(excluding) {
  if (!$('#foldout').size()) return;
  if (ua.ie6 && excluding && excluding.parentNode) {
  	excluding.parentNode.style.marginBottom = '0px';

  }
  var nl = $("#foldout li");
  if (!nl.length) return;
  for (var i = 0; i < nl.length; i++) {
    var n = $(nl[i]);
    if (n[0].parentNode.id != "foldout") continue;

    var folder = $(".foldout:first",n);

    //overruled js stylesheet
    folder[0].style.height = "auto";
    folder[0].style.overflow = "";
    folder[0].style.display = "none";
    //end overrule

    if (n != excluding) {
    	$(n).removeClass('foldopen');
        $(n).removeClass('hover');
        $(folder).slideUp(400);
        n[0].style.marginBottom = '0';
      
    }
  }
}

// Set classname on mouseover and mouseout.
function toggleFoldOver() {
  $(this).addClass("hover");
}

function toggleFoldOut() {
	$(this).removeClass("hover");
}


function toggleFoldOnHash() {
  if (document.location.hash) {
    var folder = $('#'+document.location.hash.substr(2));
    if ($(folder).hasClass('foldopen')) return;
    resetFold(folder);
    toggleFold(folder);
  }
}

function toggleFoldTrigger(e) {
  toggleFold(this);
  var src = this;
  if ("li" == src.tagName) return false;
}


function toggleFold(folderParent) {
  var folder = $('.foldout:first', folderParent);
  

  if (folderInterval) clearInterval(folderInterval);

  

    
    if (!$(folderParent).hasClass("foldopen")) {
        resetFold(folderParent);
        $(folderParent).addClass("foldopen");
        if (ua.ie6) {
        	$(folder).css({visibility: 'hidden', display: 'block'});
        	var folderHeight = $(folder).height();
        	$(folder).css({display: 'none', visibility: 'visible'});
        	$('#foldout').css({marginBottom: folderHeight+'px'});
        	$(folder).css({position: 'absolute', top: ($(folderParent).height()+2)+'px'});
        }
        $(folder).slideDown(400);
        // Set hash to keep back button alive and to allow for bookmarking
        if ($(folderParent).attr('id')) document.location.hash = '_' + $(folderParent).attr('id');
      }

  folderInterval = setInterval(toggleFoldOnHash, 500);
}
