// Shrink the sidebar recursively
subcat = document.getElementById('subcategories');
if (subcat) {
  var subCatHeight = document.getElementById('subcategories').clientHeight;
  var heightStep = (subCatHeight-45)/10;
  document.getElementById('subcategories').style.height = subCatHeight + 'px';
}

function shrinkSidebar(step, toSize) {
  side = document.getElementById('sidebar');
  page = document.getElementById('pagecontent');
  subcat = document.getElementById('subcategories');

  width = side.style['width'].replace(/px/,"");
  if (width > toSize) {
    width = width - step;
    if (width < toSize) {
      width = toSize;
    }
    
    if (subcat) {
      height = subcat.style['height'].replace(/px/,"");
      height = height - heightStep;
      if (height < 45) {
        height = 45;
      }
      subcat.style.height = height + 'px';
    }
    
    side.style['width'] = width + 'px';
    page.style['marginRight'] = width + 'px';
    setTimeout("shrinkSidebar(" + step + "," + toSize +")",10);
  } else {
    filter = document.getElementById('filters');
    subbox = document.getElementById('subbox');
    if (subcat) {
      document.getElementById("categoryTree").style['display'] = "none";
      document.getElementById("subcatheadertext").style.display = "none";
      document.getElementById("subcatheader").style.borderBottom = "none";
      document.getElementById("toolsheader").style['display'] = "none";
    } else {
      document.getElementById("toolsheadertext").style.display = "none";
//      document.getElementById("toolsheader").style.borderBottom = "none";
    }
    if (subbox) {
      subbox.style.display = 'none';
    }
    if (filter) {
      filter.style.display = 'none';
      document.getElementById("addFilter").style.display = 'none';
    }
    document.images['arrow'].src = '/images/left.png';
    
    createCookie('sidebar_collapsed','true',0);
  }
}

function growSidebar(step, toSize) {
  side = document.getElementById('sidebar');
  page = document.getElementById('pagecontent');
  subcat = document.getElementById('subcategories');
  
  width = side.style['width'].replace(/px/,"");
  if (width < toSize) {
    width = width - 10 + 10 + step;
    if (width > toSize) {
      width = toSize;
    }
    
    if (subcat) {
      height = subcat.style.height.replace(/px/,"");
      height = height - 10 + 10 + heightStep;
      if (height > subCatHeight)
      {
        height = subCatHeight;
      }
      subcat.style.height = height + 'px';
    }
    
    side.style['width'] = width + 'px';
    page.style['marginRight'] = width + 'px';
    setTimeout("growSidebar(" + step + "," + toSize + ")",10);
  } else {
    document.images['arrow'].src = '/images/right.png';
    showByTag('toolscontent','p');
    if (document.getElementById('subtool')) {
      document.getElementById('subtool').style.display = 'block';
    }
    eraseCookie('sidebar_collapsed');
  }
}

function resizeSidebar() {
  side = document.getElementById('sidebar');
  subtool = document.getElementById('subtool');

  if (side.style.width.replace(/px/,"") - 10 > 65) {
    shrinkSidebar(20,65);
    hideByTag("toolscontent",'p');
    if (subtool) {
      subtool.style.display = 'none';
    }
  } else {
    if (document.getElementById('subcategories')) {
      document.getElementById("categoryTree").style['display'] = "block";
      document.getElementById("subcatheadertext").style.display = "inline"
      document.getElementById("toolsheader").style.display = 'block';
      document.getElementById("subcatheader").style.borderBottom = "1px dashed gray";
    } else {
      document.getElementById('toolsheadertext').style.display = 'block';
      document.getElementById('toolsheader').style.borderBottom = '1px dashed gray';
    }
    if (document.getElementById('filters')) {
      document.getElementById('filters').style.display = 'block';
      document.getElementById('addFilter').style.display = 'inline';
    }
    if (document.getElementById('subbox')) {
      document.getElementById('subbox').style.display = 'block';
    }
    growSidebar(20,290);
  }
}

// hide subelments of parent id of a particular tag type
function hideByTag(parent_id,tag) {
  items = document.getElementById(parent_id).getElementsByTagName(tag);
  for(var i = 0; i < items.length; i++) {
    items[i].style['display'] = 'none';
  }
}
	  
// show subelements of parent id of a particular tag type
function showByTag(parent_id,tag) {
  items = document.getElementById(parent_id).getElementsByTagName(tag);
  for(var i = 0; i < items.length; i++) {
    items[i].style['display'] = 'block';
  }
}
		    
