/**
* Enable user interactions on the T&C entries, using some specific CSS class names
* @company Optus
* @author David HAN SZE CHUEN
* @creationDate 17/05/2007
* JS CODE to initialise the T&C:
* 	<script type="text/javascript" src="jquery-tnc_handler.js"></script>
* HTML tnc sample:
* <div class="tnc">
* 	<p class="tnc_head">
* 		Terms &amp; Conditions
* 	</p>
* 	<div class="tnc_body">
* 		text
* 	</div>
* </div>
*/
if (jQuery) { // if jQuery is available
	if (jQuery.fn.tnc === undefined) {
		jQuery.fn.tnc = { // TNC class
			// CONFIG
			// some logical CSS classnames
			tncContainerCss: 'tnc',
			tncHeadCss: 'tnc_head',
			tncBodyCss: 'tnc_body', 
			showBodyMessage: 'more details',
			hideBodyMessage: 'less details',
			showImageUrl: 'images/plus.gif',
			hideImageUrl: 'images/minus.gif',
			showHeadMessage: 'more details',
			hideHeadMessage: 'less details',
			tncHeadCustomCss: {
				paddingLeft: '15px',
				cursor: 'pointer',
			},
			isInitialised: false, // runs the init function only one time
			// register onclick event on the show/hide links of the tnc entries
			init: function() {
				var tncContainers = jQuery('.' + jQuery.fn.tnc.tncContainerCss);
				tncContainers.each(function(){ // for each grouping of tnc containers
					var currentTncContainer = jQuery(this);
					var tncHeadBlocks = jQuery(this).children('.' + jQuery.fn.tnc.tncHeadCss);
					var tncBodyBlocks = jQuery(this).children('.' + jQuery.fn.tnc.tncBodyCss);
					
					tncBodyBlocks.css('display', 'none'); // hide the body blocks at the beginning
					tncHeadBlocks.attr('title', jQuery.fn.tnc.showBodyMessage);
					currentTncContainer.get(0).isBodyVisible = false;
					
					// apply some more custom styling
  			 tncHeadBlocks.css(jQuery.fn.tnc.tncHeadCustomCss).text(jQuery.fn.tnc.showHeadMessage);
					
					//tncHeadBlocks.each(function() { // for each tnc head block in the current tnc container
					tncHeadBlocks.click(function() {
						var currentTncHeadBlock = jQuery(this);
						if (currentTncContainer.get(0).isBodyVisible) {
							tncHeadBlocks.attr('title', jQuery.fn.tnc.showBodyMessage).css('background-image', 'url(' + jQuery.fn.tnc.showImageUrl + ')').text(jQuery.fn.tnc.showHeadMessage);
						} else {
							tncHeadBlocks.attr('title', jQuery.fn.tnc.hideBodyMessage).css('background-image', 'url(' + jQuery.fn.tnc.hideImageUrl + ')').text(jQuery.fn.tnc.hideHeadMessage);
						}
						tncBodyBlocks.slideToggle();
						currentTncContainer.get(0).isBodyVisible = !currentTncContainer.get(0).isBodyVisible;
					});
				});
			}, // end of init: function tncInit(),
			// run the register function only one time
			initOnce: function(){
				if (jQuery.fn.tnc.isInitialised === false) {
					jQuery.fn.tnc.init();
					jQuery.fn.tnc.isInitialised = true;
				}
			} // end of initOnce: function()
		}; // end of class tnc
		// start when ready
		jQuery(document).ready(function() {
			jQuery(this).tnc.initOnce();
		});
	}
}
