
var foldedContentHeaderTagName = 'h2' ;

var foldedContents = new Array() ;

var foldedContentsLoaded = false ;

function initFoldedContent() {
	if (foldedContentsLoaded) {
		return ;
	}
	
	foldedContentsLoaded = true ;

	// find headers
	var headings = document.getElementsByTagName(foldedContentHeaderTagName) ;

	for (var i = 0; i < headings.length; i++) {
		if (isNaN(i)) {
			continue ;
		}
		var h = headings[i] ;

		// check class name whether it's a folded content header
		if (!h.className || h.className.indexOf('folded_content') == -1) {
			continue ;
		}

        // Next index of folded content
        var idx = foldedContents.length ;
        
        // Add class name to the heading
		h.className = h.className + ' folded_content_header folded_content_header_'+idx ;
		// Set the onclick event for the heading
		h.onclick = function() { toggleFoldedContent(this); }

        // folded content item for global array
		var fc = {'header':h,'contents':[],'visible':h.className.indexOf('folded_content_header_active') != -1} ;

        // find elements which belong to the current heading
        var nextElem = h ;
		if (!nextElem.nextSibling) {
			nextElem = h.parentNode ;
		}

		while (nextElem = nextElem.nextSibling) {
			if (nextElem.nodeType != 1) {
				continue ;
			}

			// Check class name
			if (!nextElem.className || nextElem.className.indexOf('folded_content') == -1) {
				break ;
			}

            // Check tag name (if it's a heading, it's a new folded content)
			if (nextElem.tagName.toLowerCase() == foldedContentHeaderTagName) {
				break ;
			}

            // Add class names
			nextElem.className = nextElem.className + ' folded_content_'+idx ;
			if (!fc.visible) {
				nextElem.className = nextElem.className + ' folded_content_hidden' ;
			}

			// Add the element
			fc.contents.push(nextElem) ;
		}
		delete nextElem ;

        // Save the folded content item
		foldedContents.push(fc) ;

		// Cleanup
		delete fc ;
		delete h ;
		delete idx ;
	}
	delete headings ;
}

function toggleFoldedContent(elem) {
	// Find idx in class name of the element
	if (!elem.className || !(/folded_content_header_(\d+)/.exec(elem.className))) {
		return ;
	}
	var idx = RegExp.$1 ;

    // Check whether the contents should be shown or hidden
    var show = !foldedContents[idx].visible ;

    // Toggle class name of heading
	foldedContents[idx].header.className = show ?
	   foldedContents[idx].header.className + ' folded_content_header_active' :
	   foldedContents[idx].header.className.replace('folded_content_header_active', '') ;

    // Toggle class name of contents
	for (i = 0; i < foldedContents[idx].contents.length; i++) {
		foldedContents[idx].contents[i].className = show ?
			foldedContents[idx].contents[i].className.replace('folded_content_hidden', '') :
			foldedContents[idx].contents[i].className + ' folded_content_hidden' ;
	}

    // Set visible state
	foldedContents[idx].visible = !foldedContents[idx].visible ;
}

//onLoads.push(initFoldedContent) ;