/*******************************************************************************************
 * hideLabel
 * Written by Craig Francis
 * If there are any labels on the page with a 'jsAutoHide' class, then the script will
 * hide them, and enter their label text into the relevant fields (a design thing).
 *******************************************************************************************/

	var hideLabel = new function() {

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

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

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

			this.init = function() {

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

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

				//--------------------------------------------------
				// Get the labels text from the form - remember,
				// ECMA script (JS) is for functionality, not content.

					var labels = document.getElementsByTagName('label');
					for (var k=(labels.length - 1); k >= 0; k--) {
						if (cssjs('check', labels[k], 'jsAutoHide')) {
							hideLabel.setup(labels[k]);
						}
					}

			}

		//--------------------------------------------------
		// Function to setup the labels

			this.setup = function(labelRef) {

				//--------------------------------------------------
				// Get the text for the label

					if (labelRef.textContent) {
						var labelText = labelRef.textContent; // Good DOM 3 browsers
					} else {
						var labelText = labelRef.innerText; // Old browsers
					}

				//--------------------------------------------------
				// Strip off any non-characters

					labelText = labelText.replace(/:[^a-z]*/g, '');

				//--------------------------------------------------
				// Get the input field this label is for

					var labelFor = document.getElementById(labelRef.htmlFor);
					if (labelFor && (labelFor.type == 'text' || labelFor.type == 'password')) {

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

							console.log('hideLabel.js: Found input (' + labelFor.id + ')');

						//--------------------------------------------------
						// Give the textfield the hideLabel property, so
						// in future it knows what the text value should be

							labelFor.jsHideLabelText = labelText;

						//--------------------------------------------------
						// Attach the relevant event handlers

							labelFor.onfocus = function() {
								hideLabel.toggleLabel(this, true);
							};

							labelFor.onblur = function() {
								hideLabel.toggleLabel(this, false);
							};

						//--------------------------------------------------
						// If the field does not already have a value, then
						// give it a default (the label)

							hideLabel.toggleLabel(labelFor, false);

						//--------------------------------------------------
						// Helper class, e.g. if a new width can be set

							cssjs('add', labelFor, 'jsHiddenLabelEnabled');

						//--------------------------------------------------
						// Help Opera, which does not tell us if its running
						// in XML mode, and rejects the styles on load.

							labelRef.style.position = 'absolute';
							labelRef.style.left = '-5000px';

					} else {

						//--------------------------------------------------
						// Fail

							hideLabel.restoreLabels(labelRef);

					}

			}

		//--------------------------------------------------
		// Toggle function

			this.toggleLabel = function(input, focus) {

				if (focus) {

					if (input.type == 'password') {

						// No need to process, as IE6 work around is different

					} else {

						if (input.value == input.jsHideLabelText) {
							input.value = '';
							cssjs('remove', input, 'jsHiddenLabelShown');
						}

					}

				} else {

					if (input.value == '') {

						if (input.type == 'password') { // As IE6 cannot change the input type

							if (input.jsHideLabelInput) {

								input.style.display = 'none';
								input.jsHideLabelInput.style.display = 'inline';

							} else {

								var newPass = createElement('input');
								newPass.type = 'text';
								newPass.id = input.id + 'Label';
								newPass.name = input.name + 'Label';
								newPass.className = input.className;
								newPass.value = input.jsHideLabelText;
								newPass.jsHideLabelText = input.jsHideLabelText;
								newPass.jsHideLabelFor = input;

								newPass.onfocus = function() {
									this.style.display = 'none';
									this.jsHideLabelFor.style.display = 'inline';
									this.jsHideLabelFor.focus();
								}

								cssjs('add', newPass, 'jsHiddenLabelShown');

								input.jsHideLabelInput = newPass;
								input.parentNode.insertBefore(newPass, input);
								input.style.display = 'none';

							}

						} else {

							input.value = input.jsHideLabelText;
							cssjs('add', input, 'jsHiddenLabelShown');

						}

					}

				}

			}

		//--------------------------------------------------
		// If things go horribly wrong, then this function
		// will restore everything back to normal.

			this.restoreLabels = function(labelRef) {

				//--------------------------------------------------
				// If a labelRef has been provided, then do that one,
				// otherwise do all of them.

					if (labelRef === null) {
						var labels = document.getElementsByTagName('label');
						for (var k=(labels.length - 1); k >= 0; k--) {
							if (cssjs('check', labels[k], 'jsAutoHide')) {
								labels[k].style.position = 'static';
							}
						}
					} else {
						labelRef.style.position = 'static';
					}

			}

		//--------------------------------------------------
		// Set JS specific styles ready for page load.

			addCssRule('label.jsAutoHide { position: absolute; left: -5000px; }');

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

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

	}
