/**
 * @fileoverview Global js functions used on most pages.
*/
/* Create NetR namespace */
if(typeof NetR == "undefined"){ var NetR = {}; }

/**
 * @requires jQuery
 * Finds all links with the supplied combination of attribute and value
 * and sets their target attribute to '_blank' to open a new window.
 * An image can be used instead of plain text.
 */
NetR.JSTarget = function () {
	var options = {
		att: 'class', // The attribute to look for
		val: 'new-window', // The value that triggers a new window
		widthPrefix: 'w', // Prefixes for width and height (e.g. w400 h400)
		heightPrefix: 'h',
		warning: 'Nytt fönster', // Text that is appended to the link
		image: null, // The URL for an image that is used instead of plain text
		imageLinkClass: 'nw-image', // Class added to links that contain images
		hiddenClass: 'structural' // Class added to the image
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		var oWarning, oImage;
		var reAtt = new RegExp("(^|\\s)" + options.val + "(\\s|$)");
		var reWidth = new RegExp("(^|\\s)" + options.widthPrefix + "([0-9]+)(\\s|$)");
		var reHeight = new RegExp("(^|\\s)" + options.heightPrefix + "([0-9]+)(\\s|$)");
		$('a').each(function () {
			var sAttVal;
			if (options.att == 'class') {
				sAttVal = this.className;
			} else {
				sAttVal = this.getAttribute(options.att);
			}
			if (reAtt.test(sAttVal)) {
				if (options.image) {
					oImage = document.createElement('img');
					oImage.src = options.image;
					oImage.setAttribute('alt', options.warning);
					oImage.className = options.hiddenClass;
					this.appendChild(oImage);
					$(this).addClass(options.imageLinkClass);
					this.setAttribute('title', options.warning);
				} else {
					oWarning = document.createElement("em");
					oWarning.appendChild(document.createTextNode(' (' + options.warning + ')'));
					this.appendChild(oWarning);
				}
				// If width and height values exist, open a sized window
				if (reWidth.test(sAttVal) && reHeight.test(sAttVal)) {
					$(this).click(function () {
						var sOptions = 'menubar=yes,toolbar=no,location=yes,resizable=yes,scrollbars=yes,status=yes,width=' + reWidth.exec(sAttVal)[2] + ',height=' + reHeight.exec(sAttVal)[2];
						window.open(this.href, '_blank', sOptions);
						return false;
					});
				}
				this.target = '_blank';
			}
		});
		oWarning = null;
		oImage = null;
	}
	return {
		init: init
	};
}();

/**
 * Copy the value of an input field's title attribute to its value attribute.
 * Clear the input field on focus if its value is the same as its title.
 * Repopulate the input field on blur if it is empty.
 * Hide the input field's associated label if it has one.
 * @requires jQuery
 */
var NetRInputPopulate = function() {
	var sInputClass = 'populate'; // Class name for input elements to autopopulate
	var sHiddenClass = 'structural'; // Class name that gets assigned to hidden label elements
	var sHideLabelClass = 'hidelabel'; // If the input 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 input elements with the given className
			var arrInputs = jQuery('input.' + sInputClass);
			var iInputs = arrInputs.length;
			var oInput;
			for (var i=0; i<iInputs; i++) {
				oInput = arrInputs[i];
				// Make sure it's a text input
				if (oInput.type != 'text') { continue; }
				// Hide the input's label
				if (jQuery(oInput).hasClass(sHideLabelClass)) { hideLabel(oInput.id); }
				// If value is empty and title is not, assign title to value
				if ((oInput.value == '') && (oInput.title != '')) { oInput.value = oInput.title; }
				// Add event handlers for focus and blur
				jQuery(oInput).bind('focus', function() {
					// If value and title are equal on focus, clear value
					if (this.value == this.title) {
						this.value = '';
						this.select(); // Make input caret visible in IE
					}
				});
				jQuery(oInput).bind('blur', function() {
					// If the field is empty on blur, assign title to value
					if (!this.value.length) { this.value = this.title; }
				});
			}
		}
	};
}();

/**
 * Creates a link that triggers the browser's window.print function.
 */
NetR.addPrintLink = function () {
	var options = {
		targetEl: 'content-primary', // Default id of the element the link is appended to
		linkText: 'Skriv ut sidan', // Default link text
		linkId: 'print-link' // Default link id
	};
	/**
	* Initialization
	*/
	function init(opts) {
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		var oTarget = document.getElementById(options.targetEl);
		if (!oTarget) {return;}
		if (!window.print) {return;}
		var oLink = document.createElement('a');
		oLink.id = options.linkId;
		oLink.href = '#';
		oLink.appendChild(document.createTextNode(options.linkText));
		oLink.onclick = function() {
			window.print();
			return false;
		}
		oTarget.appendChild(oLink);
	}
	return {
		init: init
	};
}();

// Init on document ready
jQuery(document).ready(function() {
	NetR.JSTarget.init({
		val: 'new-window'
	});
	NetR.addPrintLink.init({
		targetEl: 'page-tools',
		linkText: _('print_page') // Uses strings in netr.lang.js
	});
	NetRInputPopulate.init(); // Populate input fields
	jQuery("tbody tr:odd").addClass("odd");
});
