// JavaScript Document

jQuery(document).ready(function($){
	
	// hide any js-hide elements 
	// (eg: submit buttons that need to be displayed for non-javascript enabled browsers)
	$('.js-hide').hide();
	
	// add close buttons to closeable elements
	$('.closeable').prepend('<div class="closeButton">Close</div>');
	$('.closeButton').bind('click',function(e){
		// hide and remove
		$(this).parent().hide('fast',function(){$(this).remove();});
	});
	
	// set up showHide divs and buttons
	var sh = $('.showHide');
	// create buttons before each showHide DIV with the appropriate label
	sh.each(function(){								   
		try{var showText = $(this).attr('title').substr(0,$(this).attr('title').indexOf('|'));}catch(e){var showText = 'Show';}
		try{var hideText = $(this).attr('title').substr($(this).attr('title').indexOf('|')+1);}catch(e){var hideText = 'Hide';}
		$(this).before('<span class="showHideButton"><span class="showText">'+showText+'</span><span class="hideText">'+hideText+'</span></span>');
	});
	sh.hide();
	
	// define the click function for all showHideButtons	
	$('.showHideButton').bind('click',function(e){
		// toggle label	
		var st = $(this).find('.showText');
		var ht = $(this).find('.hideText');
		var content = $(this).next('.showHide');	
		
		//alert(content);
		
		// if content is hidden then we must be trying to show it
		if(content.css('display')!='block' && content.css('display')!='inline'){
			// show 'Hide' text
			ht.show();
			// hide 'Show' text
			st.hide();
			// show content
			content.show('fast');
		}else{
			// hide 'Hide' text
			ht.hide();
			// show 'Show' text
			st.show();
			// hide content
			content.hide('fast');
		}
		
		
	});
	
	// ---------------------------------------------------------------------------------------
	
	// set up showHideRow divs and buttons
	var shr = $('.showHideRow');
	shr.hide();
	// create buttons before each showHideRow DIV with the appropriate label
	shr.each(function(){								   
		try{var showText = $(this).attr('title').substr(0,$(this).attr('title').indexOf('|'));}catch(e){var showText = 'Show';}
		try{var hideText = $(this).attr('title').substr($(this).attr('title').indexOf('|')+1);}catch(e){var hideText = 'Hide';}
		var rh = $(this).prev(".rowHeader");
		rh.append('<span class="showHideRowButton"><span class="showText">'+showText+'</span><span class="hideText" style="display:none;">'+hideText+'</span></span>');
	});
	
	// define the click function for all showHideRowButtons	
	$('.showHideRowButton').bind('click',function(e){
		// toggle label
		var st = $(this).find('.showText');
		var ht = $(this).find('.hideText');
		var content = $(this).parent().next('.showHideRow');
		
		
		// if content is hidden then we must be trying to show it
		if(content.css('display')!='block' && content.css('display')!='inline'){
			// show 'Hide' text
			ht.show();
			// hide 'Show' text
			st.hide();
			// show content
			content.show('fast');
		}else{
			// hide 'Hide' text
			ht.hide();
			// show 'Show' text
			st.show();
			// hide content
			content.hide('fast');
		}
		
	});
	
	$('.hideText').hide();
});