//var COLLAPSABLE_EXPAND_IMG = "/common/icons/tree/arrow-close-blue.gif";
//var COLLAPSABLE_SHRINK_IMG = "/common/icons/tree/arrow-open-orange.gif";
var COLLAPSABLE_EXPAND_IMG = "/common/icons/tree/plus-sign.gif";
var COLLAPSABLE_SHRINK_IMG = "/common/icons/tree/minus-sign.gif";
var COLLAPSABLE_START_TYPE = "li";
var COLLAPSABLE_PARENT_TYPE = "div";

var COLLAPSABLE_PARENT_NAME_CATEGORY = "collapsable-category"; 
var NON_COLLAPSABLE_PARENT_NAME = "noncollapsable";
var NON_COLLAPSABLE_IMG = "/common/template/04/images/spacer.gif"; // "/common/icons/tree/arrow-white.gif";
var NON_COLLAPSABLE_START_TYPE = "li";

twistycollapsableInit=function(){
	if (document.getElementById && document.createTextNode) { 
		// --- Initialize the screen display when the page is first loaded ---
		document.getElementById("hideBlock").style.display = "inline";
		document.getElementById("showBlock").style.display = "inline";
		
	    // returns an array of elements with a name attribute whose value matches specified classname parameter
        var entries = document.getElementsByTagName(COLLAPSABLE_PARENT_TYPE);  
        for(i=0;i<entries.length;i++) {
			if (entries[i].className==COLLAPSABLE_PARENT_NAME || entries[i].className==COLLAPSABLE_PARENT_NAME_CATEGORY || entries[i].className==NON_COLLAPSABLE_PARENT_NAME) 
				//assignCollapseTxt(entries[i]); // --- Used with text-based icons ---
				assignCollapseImg(entries[i]); // --- Used with image-based icons ---
		}
    } 
}

function switchLink(action) {
	//document.getElementById("hideBlock").style.display = (document.getElementById("hideBlock").style.display=="none") ? "inline" : "none";
	//document.getElementById("showBlock").style.display = (document.getElementById("hideBlock").style.display=="none") ? "inline" : "none";
	document.getElementById("hideBlock").style.display = "inline";
	document.getElementById("showBlock").style.display = "inline";
	changeLinkImg(action); 
	//changeLinkCategory(action);
}

/**
 * This function overwrites the changeLink in /js.lib/common.js.
 */
changeLinkCategory=function(setting){ 
	var tempstatus = "";
	var alink = "";
	if (document.getElementById && document.createTextNode) { 
        var aentries = document.getElementsByTagName(COLLAPSABLE_PARENT_TYPE);
		for (var i=0; i<aentries.length; i++){
			if (aentries[i].className==COLLAPSABLE_PARENT_NAME || aentries[i].className==COLLAPSABLE_PARENT_NAME_CATEGORY)  {
				alink = aentries[i].getElementsByTagName("a")[0];
				if (setting=="expand")
					twisty(alink, 1); 
				else if (setting=="collapse")  
					twisty(alink, -1); 
			}
		}
	}
}



changeLinkImg=function(setting) { 
	var tempstatus = "";
	var alink = "";
    // --- if there are switch contents defined on the page --- 
	 if(document.getElementById && document.createTextNode) { 
        var aentries = document.getElementsByTagName(COLLAPSABLE_PARENT_TYPE);
		for (var i=0; i<aentries.length; i++){
			if (aentries[i].className==COLLAPSABLE_PARENT_NAME || aentries[i].className==COLLAPSABLE_PARENT_NAME_CATEGORY)  {
				alink = aentries[i].getElementsByTagName("img")[0];   // --- if use image as collapse/expand icon ---
				if (setting=="expand") 
					twistyImages(alink, 1);
				else if (setting=="collapse")  
					twistyImages(alink, -1);
			}
		}
	}
}



/**
 * Assign collapse/expand text to each block, based on the tag's class name.
 */
function assignCollapseTxt(div) { 
	// --- Creates an instance of the element object with tag name "img" ---
	var collapsableIcon = document.createElement('a');
    collapsableIcon.style.cursor='pointer'; 
    collapsableIcon.style.color='#0000ff'; 
    collapsableIcon.setAttribute('expand', COLLAPSABLE_EXPAND); 
    collapsableIcon.setAttribute('shrink', COLLAPSABLE_SHRINK);
	collapsableIcon.className = 'twisty';
	
	// -- default to collapse all 
	//collapsableIcon.setAttribute('state', -1); 
	
	// 2008-0730: Based on BO request to default all 1st level listing to expanded
	if (div.getElementsByTagName(COLLAPSABLE_START_TYPE)[0].className=='list-category') {
		collapsableIcon.setAttribute('state', 1); 
	} else {
    	collapsableIcon.setAttribute('state', -1); 
	}
	
	
	div.insertBefore(collapsableIcon, div.getElementsByTagName(COLLAPSABLE_START_TYPE)[0]);
	if (div.className==COLLAPSABLE_PARENT_NAME || div.className==COLLAPSABLE_PARENT_NAME_CATEGORY) {
		collapsableIcon.style.cursor='pointer'; 
		collapsableIcon.onclick=function() { twisty(collapsableIcon, 0); }               
		collapsableIcon.onclick(); 
	} else if (div.className==NON_COLLAPSABLE_PARENT_NAME) {
		collapsableIcon.style.cursor='pointer'; 
		collapsableIcon.onclick=function() { twisty(collapsableIcon, 0); }               
		collapsableIcon.onclick(); 
	    collapsableIcon.style.color='#ffffff';  // --- use white font if non-collapsable ---
	}
}

/**
 * Assign collapse/expand image to each block, based on the tag's class name.
 */
function assignCollapseImg(div) { 
	// --- Creates an instance of the element object with tag name "img" ---
	var collapsableIcon = document.createElement('img');
	collapsableIcon.className = 'twisty';
	collapsableIcon.setAttribute('align', 'bottom');
	collapsableIcon.style.color='#0000ff'; 
    collapsableIcon.setAttribute('expand', COLLAPSABLE_EXPAND); 
    collapsableIcon.setAttribute('shrink', COLLAPSABLE_SHRINK);
	
	//collapsableIcon.setAttribute('state', -1); 
	// 2008-0730: Based on BO request to default all 1st level listing to expanded
	if (div.getElementsByTagName(COLLAPSABLE_START_TYPE)[0].className=='list-category') {
		collapsableIcon.setAttribute('state', 1); 
	} else {
    	collapsableIcon.setAttribute('state', -1); 
	}
	
	div.insertBefore(collapsableIcon, div.getElementsByTagName(COLLAPSABLE_START_TYPE)[0]);
	if (div.className==COLLAPSABLE_PARENT_NAME || div.className==COLLAPSABLE_PARENT_NAME_CATEGORY) {
		collapsableIcon.style.cursor='pointer'; 
		collapsableIcon.onclick=function() { twistyImages(collapsableIcon, 0); }               
		collapsableIcon.onclick(); 
	} else {
		collapsableIcon.src = NON_COLLAPSABLE_IMG;
		collapsableIcon.setAttribute('height', '10');
		collapsableIcon.setAttribute('width', '10');
	}
}

/**
 * Assign non-collapsable image to desginated block 
 */ 
function assignNonCollapseImg(div) { 
	var nonCollapsableIcon = document.createElement('img');  
	nonCollapsableIcon.style.display = 'inline'; 
	nonCollapsableIcon.style.cursor = 'default';
	nonCollapsableIcon.className = 'twisty';
	nonCollapsableIcon.src = NON_COLLAPSABLE_IMG;
	nonCollapsableIcon.setAttribute('align', 'bottom');
	div.insertBefore(nonCollapsableIcon, div.getElementsByTagName(NON_COLLAPSABLE_START_TYPE)[0]);
}


if (document.addEventListener) {    
	document.addEventListener("DOMContentLoaded", twistycollapsableInit, false);
} /*else {
	window.onload=collapsableInit;
}*/

/* for Internet Explorer */
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      twistycollapsableInit(); // call the onload handler
    }
  };
/* end */
