/**
 * @fileoverview Global functions
 */

/* Create GBG namespace */
if(typeof GBG == "undefined"){ var GBG = {}; }

/**
 * Display an alert dialog when inactive links are clicked.
 */
GBG.linkInfo = function() {
	var sInfoText = 'Denna l\u00C3\u00A4nk \u00C3\u00A4r inte aktiv i prototypen.';
	function init() {
		var links = document.getElementsByTagName('a');
		var re = /inactive|^netrp-/;
		var oLink;
		for (var i=0, l=links.length; i<l; i++) {
			oLink = links[i];
			/* The second parameter is needed for IE to return the actual value of the href attribute */
			if (re.test(oLink.getAttribute('href',2))) {
				oLink.onclick = function() {
					alert(sInfoText);
					return false;
				};
			}
		}
	}
	return {
		init:init
	};
}();

/**
 * @requires jQuery
 * Add ARIA Landmark Roles
 */
GBG.addARIA = function() {
	function init() {
		$('#header').attr({role: 'banner'});
		$('#content-primary').attr({role: 'main'});
		$('#nav-main').attr({role: 'navigation'});
		$('#nav-sub').attr({role: 'navigation'});
		$('#content-secondary').attr({role: 'complementary'});
		$('#search').attr({role: 'search'});
		$('#footer').attr({role: 'contentinfo'});
	}
	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
 */
GBG.InputPopulate = function() {
	var options = {
		sInputClass: 'populate', // Class name for input elements to autopopulate
		sHiddenClass: 'structural', // Class name that gets assigned to hidden label elements
		sHideLabelClass: 'hidelabel', // If the input has this className, its label is hidden
		sOriginalTextClass: 'original' // Class used to style the placeholder text
	};
	/**
	* 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];
			}
		}
		// Find all input[type=text] elements with the given className
		$('input[type=text].' + options.sInputClass).each(function () {
			// Hide the input's label
			if ($(this).hasClass(options.sHideLabelClass)) {
				$('label[for="' + this.id + '"]').addClass(options.sHiddenClass);
			}
			// Get the placeholder text
			if (this.title != '') {
				this.phText = this.title;
			} else {
				this.phText = $('label[for="' + this.id + '"]').text();
			}
			// If value is empty and phText is not, assign phText to value
			if ((this.value == '') && (this.phText != '')) {
				this.value = this.phText;
				$(this).addClass(options.sOriginalTextClass);
			}
			// Add event handlers for focus and blur
			$(this).bind('focus', function() {
				// If value and title are equal on focus, clear value
				if (this.value == this.phText) {
					this.value = '';
					$(this).removeClass(options.sOriginalTextClass);
					this.select(); // Make input caret visible in IE
				}
			});
			$(this).bind('blur', function() {
				// If the field is empty on blur, assign title to value
				if (!this.value.length) {
					this.value = this.phText;
					$(this).addClass(options.sOriginalTextClass);
				}
			});
		});
	}
	return {
		init: init
	};
}();

// Init on document ready
$(document).ready(function() {
	GBG.linkInfo.init();
	GBG.addARIA.init();
	GBG.InputPopulate.init();
});
