/**
* Implements the 'show more...' and 'show less...' links on the 'Narrow your search' criteria.
*
*/

var MAXSHOW=5;

/**
 * getText(node): Find all Text nodes at or beneath the node 'node'.
 * Concatenate their content and return it as a string.
 */
function getText(node) {

	// Recusive function finds all text nodes and appends their text to an array
	function getStrings(node, strings) {
		if(node.nodeType == 3 /* Node.TEXT_NODE */) {
			strings.push(node.data);
		} else if (node.nodeType == 1 /* Node.ELEMENT_NODE */) {
			// Note iteration with firstChild/nextSibling
			for(var m=node.firstChild; m != null; m=m.nextSibling) {
				getStrings(m, strings);
			}
		}
	}

	var strings = [];
	getStrings(node, strings);
	return strings.join("");
}

/**
 * trim(): Emulates the java trim() function.
 */
String.prototype.trim = function() {

	// skip leading and trailing whitespace
	// and return everything in between
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
};

function hideFilters() {

	var filterList=getElements("searchDefList","dl","") ;
	var filterListDT=getElements("","dt",filterList[0]) ;

	//  next bit messy, no way of directly counting dd elements after a dt element

	for(var dt in filterListDT) {
		var count=0 ;
		var nxtSibling=filterListDT[dt].nextSibling;
		while(nxtSibling !=null && (!nxtSibling.tagName || nxtSibling.tagName.toLowerCase() !="dt") ) {
			if(nxtSibling.tagName && nxtSibling.tagName.toLowerCase()=="dd") {
				count++ ;
				if(count > MAXSHOW) {
					nxtSibling.style.display="none" ;
				}
			}
			nxtSibling= nxtSibling.nextSibling;
		}
		if(count > MAXSHOW) {
			// add 'more' text here
                        		var moreElement=document.createElement("dd") ;
		             	addClass(moreElement,"more") ;    
			var sectionTitle = getText(filterListDT[dt]).trim();
	        		moreElement.innerHTML='<a href="#" title="Expand to show more ' + sectionTitle + 's" class="expand">show more...</a>';
                        		moreElement.onclick=new Function("showMore(this,+"+(count-5)+",'" + sectionTitle + "'); return false;");
                        		filterList[0].insertBefore(moreElement, nxtSibling) ;
		}
	}

}

function showMore(filter, numhidden, sectionTitle) {
	var prvSibling=filter.previousSibling;
    	var count=numhidden ;
    	while(count >0 && prvSibling !=null && (!prvSibling.tagName || prvSibling.tagName.toLowerCase() !="dt") ) {
            		if(prvSibling.tagName && prvSibling.tagName.toLowerCase()=="dd") {
               			count-- ;
                    		prvSibling.style.display="block" ;
            		}
            		prvSibling= prvSibling.previousSibling;
    	}
    	filter.innerHTML='<a href="#" title="Contract to show less ' + sectionTitle + 's" class="contract">show less...</a>';
    	filter.onclick=new Function("showLess(this,+"+(numhidden)+",'" + sectionTitle + "'); return false;");
}

function showLess(filter, numhidden, sectionTitle) {
	var prvSibling=filter.previousSibling;
    	var count=numhidden ;
    	while(count >0 && prvSibling !=null && (!prvSibling.tagName || prvSibling.tagName.toLowerCase() !="dt") ) {
            		if(prvSibling.tagName && prvSibling.tagName.toLowerCase()=="dd") {
                    		count-- ;
                    		prvSibling.style.display="none" ;
            		}
            		prvSibling= prvSibling.previousSibling;
    	}
    	filter.innerHTML='<a href="#" title="Expand to show more ' + sectionTitle + 's" class="expand">show more...</a>';
    	filter.onclick=new Function("showMore(this,+"+(numhidden)+",'" + sectionTitle + "'); return false;");
}

addLoadEvent(hideFilters);
