/* Create NetR namespace */
if (typeof NetR === "undefined") {
	var NetR = {};
}

/**
 * Copy the value of a textarea's title attribute to its value attribute.
 * Clear the textarea field on focus if its value is the same as its title.
 * Repopulate the textarea on blur if it is empty.
 * Hide the textarea field's associated label if it has one.
 * @requires jQuery
 */
var NetRElementPopulate = function() {
	var sElementClass = 'populate'; // Class name for textarea elements to autopopulate
	var sHiddenClass = 'structural'; // Class name that gets assigned to hidden label elements
	var sHideLabelClass = 'hidelabel'; // If the textarea has this className, its label is hidden
	function hideLabel(sId) {
		var arrLabels = document.getElementsByTagName('label');
		var iLabels = arrLabels.length;
		var oLabel;
		for (var i=0; i<iLabels; i++) {
			oLabel = arrLabels[i];
			if (oLabel.htmlFor == sId) {
				oLabel.className = oLabel.className + ' ' + sHiddenClass;
			}
		}
	}
	// Main function
	return {
		init:function() {
			// Find all textarea elements with the given className
			var arrElements = $('.' + sElementClass);
			var iElements = arrElements.length;
			var oElement;
			for (var i=0; i<iElements; i++) {
				oElement = arrElements[i];
				// Make sure it's a textarea
				if ((oElement.type != 'text') && (oElement.type != 'textarea')) { continue; }
				// Hide the textarea's label
				if ($(oElement).hasClass(sHideLabelClass)) { hideLabel(oElement.id); }
				// If value is empty and title is not, assign title to value
				if ((oElement.value == '') && (oElement.title != '')) { oElement.value = oElement.title; }
				// Add event handlers for focus and blur
				$(oElement).bind('focus', function() {
					// If value and title are equal on focus, clear value
					if (this.value == this.title) {
						this.value = '';
						this.select(); // Make Element caret visible in IE
					}
				});
				$(oElement).bind('blur', function() {
					// If the field is empty on blur, assign title to value
					if (!this.value.length) { this.value = this.title; }
				});
			}
		}
	};
}();

// Init on document ready
$(document).ready(function() {
	$("#m-slideshow .lightbox").lightBox({
		txtImage: "Bild",
		txtOf: "av"
	});
	NetRElementPopulate.init(); // Populate textarea or input elements
});