/**
* For reusing the functions in this script, your table has to 
* follow a certain class assignment pattern.
*
* Example:
*
* <table class="uwrTable" id="uniqueID_1">
* 	<col width="...">
* 	<thead>...</thead>
*	<tbody>	
*		<tr id="uniqueID_2" class="expandable">
*			<td class="uwrTb"><img id="uniqueID_2-img" src="images/knoten_geschlossen.gif"/>&nbsp;...</td>
*		</tr>
*		<tr id="uniqueID_2-details" class="details" style="display: none;">
*			<td class="uwrTb">...</td>
*		</tr>
*		<tr id="uniqueID_3" class="expandable">
*			<td class="uwrTb"><img id="uniqueID_3-img" src="images/knoten_geschlossen.gif"/>&nbsp;...</td>
*		</tr>
*		<tr id="uniqueID_3-details" class="details" style="display: none;">
*			<td class="uwrTb">...</td>
*		</tr>
* 	</tbody>
* </table> 
*/
function initFolding() {
	$('.expandable').live('click', function() {
		var id = '#' + $(this).attr('id');
		$(id + '-details').toggleClass("hiddenDetails");	
		if($(id + '-details').css('display')=='block'){
			$(id + '-details').css('display','none');
		} else {
			$(id + '-details').css('display','');
		}
			
		if($(id + '-img').is('.collapsed')){
			$(id + '-img').attr('src','images/knoten_offen.gif');
			$(id + '-img').toggleClass('collapsed');
			$(id + '-img').toggleClass('expanded');
		} else {
			$(id + '-img').attr('src','images/knoten_geschlossen.gif');
			$(id + '-img').toggleClass('expanded');
			$(id + '-img').toggleClass('collapsed');
		}
		//refreshExpandCollapseLink();
	});
}

/* For direct use within onClick */
function fold(id) {
	id = '#' + id;
	$(id + '-details').toggleClass("hiddenDetails");	
	if($(id + '-details').css('display')=='block'){
		$(id + '-details').css('display','none');
	} else {
		$(id + '-details').css('display','');
	}

	if($(id + '-img').is('.collapsed')){
		$(id + '-img').attr('src','images/knoten_offen.gif');
		$(id + '-img').toggleClass('collapsed');
		$(id + '-img').toggleClass('expanded');
	} else {
		$(id + '-img').attr('src','images/knoten_geschlossen.gif');
		$(id + '-img').toggleClass('expanded');
		$(id + '-img').toggleClass('collapsed');
	}
}

function refreshExpandCollapseLink() {
	var allExpanded = true;
	$('.expandable').each(function() {
		if (!$(this).next().is(':visible')) {
			allExpanded = false;
		}
	});
	
	if(allExpanded) {
		showCollapseAllLink();
	} else { 
		showExpandAllLink();
	}
}

function showExpandAllLink() {
	$('#collapseAll-link').hide();
	$('#expandAll-link').show();
}

function showCollapseAllLink() {
	$('#expandAll-link').hide();
	$('#collapseAll-link').show();
}

function expandAll() {
	$('.expandable').each(function() {
		if (!$(this).next().is(':visible')) {
			$(this).trigger('click');
		}
	});
	//showCollapseAllLink();
}

function collapseAll() {
	$('.expandable').each(function() {
		if ($(this).next().is(':visible')) {
			$(this).trigger('click');
		}
	});
	//showExpandAllLink();
}

