/***
 * Foldout feature
 *
 * example: /nieuw-bij-ah/
 */
var folderInterval;

function setupFold() {
  var nl = $('#nieuwbijah li');
  
  if (!nl.length) return;
  for (var i = 0; i < nl.length; i++) {
    var n = nl[i];
    if (n.parentNode.id != "nieuwbijah") continue;
    
    $(n).click(function(){toggleFoldTrigger(this)}).hover(function(){
    	$(this).addClass("hover");
    },function(){
    	$(this).removeClass("hover");
    });
    
    
    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)[0];
      folder.oldClientHeight = (1.2 * folder.clientHeight * document.all.main.clientWidth);
      n.style.marginBottom = '1px';
    }
  }
  
  if (document.location.hash) {
    var folder = $('#'+document.location.hash.substr(1));
  } else {
    resetFold();
  }
  
  folderInterval = setInterval(toggleFoldOnHash, 500);
}


function resetFold(excluding) {
  var nl = $("#nieuwbijah li");
  if (!nl.length) return;
  for (var i = 0; i < nl.length; i++) {
    var n = nl[i];
    if (n.parentNode.id != "nieuwbijah") continue;

    var folder = $(".foldout:first",n)[0];
    
    //overruled js stylesheet
    folder.style.height = "auto";
    folder.style.overflow = "";
    folder.style.display = "none";
    //end overrule
    
    if (n != excluding) {
      if (ua.ie6) {
        $(n).removeClass('foldopen');
        $(n).removeClass('hover');
        folder.style.visibility = "hidden";
         $(folder).slideUp(400);
        n.style.marginBottom = '0';
      } else if (ua.ie) {
       $(n).removeClass('foldopen');
        $(folder).slideUp(400);
        n.style.marginBottom = '0';
      } else {
        $(n).removeClass('foldopen');
        $(folder).slideUp(400);
      }
    }
  }
}


function toggleFoldOnHash() {
  if (document.location.hash) {
    var folder = $('#'+document.location.hash.substr(1));
    if (!folder.size()) return;
    if ($(folder).hasClass('foldopen')) return;
    resetFold(folder);
    toggleFold(folder);
  }
}


function toggleFoldTrigger(obj) {
  toggleFold(obj);
  var src = obj;
  if ("li" == src.tagName) return false;
}


function toggleFold(folderParent) {
  var folder = $('.foldout:first', folderParent)[0];
  
  if (folderInterval) clearInterval(folderInterval);
  
  if (ua.ie) {
    // For IE6/7
    var sizeXXL = $('body').hasClass("size_xxl");
    
    var folderLastItem = $('ul#nieuwbijah .nieuw').size() - 1;
    
    var folderParentId = parseInt($(folderParent).attr('id').substr(4));
    var folderItemsBefore = (sizeXXL == true) ? (folderParentId % 4) : (folderParentId % 3);
    var folderItemsAfter = (sizeXXL == true) ? (3 - folderItemsBefore) : (2 - folderItemsBefore);
    var rowLastItemId = folderParentId + folderItemsAfter;

    /*
    var winStatus = "rowLastItemId: " + rowLastItemId + " -- " +
                    "folderLastItem: " + folderLastItem + " -- " +
                    "sizeXXL: " + sizeXXL;
    */
    
    var nl = $("#nieuwbijah .nieuw");
    
    
    
    if (!$(folderParent).hasClass("foldopen")) {
      resetFold(folderParent);
      if (ua.ie6) {
        if (typeof (nl[rowLastItemId]) != 'undefined') {
          $(nl[rowLastItemId]).animate({marginBottom: (parseInt(folder.oldClientHeight / document.all.main.clientWidth) + 15)+'px'},400);
        }
        folder.style.top = folderParent.clientHeight + folderParent.offsetTop + 9 + 'px';
        folder.style.visibility = "visible";
      } else if (folderParentId != rowLastItemId) { 
        // window.status = "Normaal item: margin morph -- " + winStatus;
        if ((typeof (nl[rowLastItemId]) != 'undefined') &&
            (rowLastItemId != folderLastItem)) {
        	$(nl[rowLastItemId]).animate({marginBottom: (parseInt(folder.oldClientHeight / document.all.main.clientWidth) + 15)+'px'},400);
        }
      } else if (rowLastItemId != folderLastItem) {
        // window.status = "Laatste item: margin 0 -- " + winStatus;
        nl[rowLastItemId].style.marginBottom = '0';
      }
      $(folderParent).addClass("foldopen");
      $(folder).slideDown(400);

      if(ua.ie6) {
        var compactbanners = $('#compactbanners');
        if(compactbanners.size()) {
          compactbanners.css({
            'margin-top': $(folder).height() + 15 + 'px'
          });
        }
      }
      
      // Set hash to keep back button alive and to allow for bookmarking
      document.location.hash = $(folderParent).attr('id');
    }
    
    // Clean up
    sizeXXL = null; folderParentId = null; folderItemsBefore = null;
    folderItemsAfter = null; rowLastItemId = null; nl = null; folder = null;

  } else {

    // For Firefox & Safari.
    if (!$(folderParent).hasClass("foldopen")) {
      resetFold(folderParent);
      $(folderParent).addClass("foldopen");
      $(folder).slideDown(400);
      
      // Set hash to keep back button alive and to allow for bookmarking
      
      document.location.hash = $(folderParent).attr('id');
    }
  }

  folderInterval = setInterval(toggleFoldOnHash, 500);
}


// Loader moved to /rev05/ui/default.js to launch setupFold 
// before shoppinglist initiation.
// loader.schedule("foldout", setupFold, Loader.priority.HIGH);

