/*******************************************************************************************
 * widgetRadio
 * Written by Craig Francis
 * Add a radio disclosure, which will show/hide another panel on the page
 *******************************************************************************************/

//--------------------------------------------------
// Example:
//
//	if (typeof otherDisclose != "undefined" && typeof otherDisclose.addDisclosure != "undefined") {
//		otherDisclose.addDisclosure("fldExample", "exampleHolder");
//		otherDisclose.addDisclosure("' . html($fldExample->getFieldIdByKey('other')) . '", "exampleHolder");
//	}
//
//--------------------------------------------------

	var widgetRadio = new function () {

		//--------------------------------------------------
		// Do not allow older browsers to run this script

			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Add a configuration - possibly done before page
		// load, so don't run straight away

			this.disclosures = new Array();
			this.loadDone = false;

			this.addDisclosure = function(fieldId, otherHolderId) {

				//--------------------------------------------------
				// Debug

					console.log('widgetRadio.js: Configuration for field "' + fieldId + '", to disclose "' + otherHolderId + '"');

				//--------------------------------------------------
				// Hide, before load

					addCssRule('#' + otherHolderId + ' { position: absolute; left: -5000px; }');

				//--------------------------------------------------
				// Process

					if (this.loadDone) {

						widgetRadio.setupDisclosure(fieldId, otherHolderId);

					} else {

						widgetRadio.disclosures[widgetRadio.disclosures.length] = {
							fieldId: fieldId,
							otherHolderId: otherHolderId
						}

					}

			}

		//--------------------------------------------------
		// Initialisation function used for setup

			this.init = function () {

				//--------------------------------------------------
				// Debug

					console.log('widgetRadio.js: Initialisation');

				//--------------------------------------------------
				// Setup each disclosure

					var d;

					this.loadDone = true;

					for (var k=(widgetRadio.disclosures.length - 1); k >= 0; k--) {
						d = widgetRadio.disclosures[k];
						widgetRadio.setupDisclosure(d.fieldId, d.otherHolderId);
					}

			}

		//--------------------------------------------------
		// Setup

			this.setupDisclosure = function(fieldId, otherHolderId) {

				//--------------------------------------------------
				// Debug

					console.log('widgetRadio.js: Setup for field "' + fieldId + '", to disclose "' + otherHolderId + '"');

				//--------------------------------------------------
				// References

					var fieldRef = document.getElementById(fieldId);
					var otherHolderRef = document.getElementById(otherHolderId);

					if (!fieldRef || !otherHolderRef) {

						if (!fieldRef) {
							console.log('widgetRadio.js: Could not find field "' + fieldId + '"');
						}

						if (otherHolderRef) {
							otherHolderRef.style.position = 'static';
							otherHolderRef.style.left = 'auto';
						} else {
							console.log('widgetRadio.js: Could not find holder "' + otherHolderId + '"');
						}

						return;

					}

				//--------------------------------------------------
				// Reference to the 'other' field

					fieldRef.otherHolderShower = true;
					fieldRef.otherHolderRef = otherHolderRef;

				//--------------------------------------------------
				// Apply the event handler, and trigger once, for
				// the 'default' state

					if (fieldRef.nodeName.toLowerCase() == 'input') { // Radio

						var parent = getParent(fieldRef, 'form');
						if (parent === null) {
							parent = document;
						}

						var radios = parent.getElementsByTagName('input');
						for (var k = (radios.length - 1); k >= 0; k--) {

							if (radios[k].type.toLowerCase() == 'radio' && radios[k].name == fieldRef.name) {

								radios[k].otherHolderRef = otherHolderRef;

								//radios[k].onchange = function() {
								//	widgetRadio.trigger(this);
								//}

								radios[k].onclick = function() {
									widgetRadio.trigger(this); // Safari: does not trigger the onchange event
									//this.blur(); // IE6: cannot see the onchange event when it changes
								}

							}
						}

					} else { // Select

						fieldRef.onchange = function() {
							widgetRadio.trigger(this);
						}

					}

					widgetRadio.trigger(fieldRef);

			}

		//--------------------------------------------------
		// Function to tiger the show/hide of the other field

			this.trigger = function(fieldRef) {

				//--------------------------------------------------
				// Debug

					console.log('widgetRadio.js: Trigger for field "' + fieldRef.id + '"');

				//--------------------------------------------------
				// Is the last item selected?

					if (fieldRef.nodeName.toLowerCase() == 'input') { // Radio
						var lastItemSelected = (fieldRef.otherHolderShower && fieldRef.checked);
					} else {
						var lastItemSelected = (fieldRef.selectedIndex == (fieldRef.options.length - 1));
					}

				//--------------------------------------------------
				// Show or hide the 'other' field

					if (lastItemSelected) {
						fieldRef.otherHolderRef.style.position = 'static';
						fieldRef.otherHolderRef.style.left = 'auto';
					} else {
						fieldRef.otherHolderRef.style.position = 'absolute';
						fieldRef.otherHolderRef.style.left = '-5000px';
					}

					fieldRef.style.position = 'relative'; // Safari: preserves initial value of 'position'

			}

		//--------------------------------------------------
		// When the page has loaded, run the init function

			addLoadEvent (function() {
				widgetRadio.init();
			});

	}
