var COLLAPSABLE_EXPAND_IMG = "/common/icons/tree/arrow-close-blue.gif";
var COLLAPSABLE_SHRINK_IMG = "/common/icons/tree/arrow-open-orange.gif";
var COLLAPSABLE_START_TYPE = "li";

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";

collapsableInit=function(){
	if (document.getElementById && document.createTextNode) { 
		// --- Initialize the screen display when the page is first loaded ---
		document.getElementById("hideBlock").style.display = "none";
		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 ---
			/* --- Original ---
			if (entries[i].className==COLLAPSABLE_PARENT_NAME)  {
                assignCollapseImg(entries[i]);	
			} else if (entries[i].className==NON_COLLAPSABLE_PARENT_NAME)  {
				assignNonCollapseImg(entries[i]);	
			}
			*/
    } 
}

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";
	// 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); 
			}
		}
	}
}


/**
 * 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';
	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.setAttribute('state', -1); 
	div.insertBefore(collapsableIcon, div.getElementsByTagName(COLLAPSABLE_START_TYPE)[0]);
	if (div.className==COLLAPSABLE_PARENT_NAME) {
		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", collapsableInit, false);
} else {
	//window.onload=collapsableInit;
	window.onload=initializePageEventsIE;
}

function initializePageEventsIE() {
	collapsableInit();
	initalizeTemplateEventsIE(); // -- defined in common2008.js
}

