/**
 * @fileoverview Global functions
 */
/**
 * Hack to reduce background image flickering in IE 6
 */
/*@cc_on
	@if (@_jscript_version == 5.6)
		try {
			document.execCommand("BackgroundImageCache", false, true);
		} catch(err) {}
	@end
@*/

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

/**
 * Display an alert dialog when inactive links are clicked.
 */
NetR.linkInfo = function() {
	var sInfoText = 'Denna länk är 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
 */
NetR.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
	};
}();

/**
 * @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
	};
}();

/**
 * Creates a link that triggers the browser's window.print function.
 */
NetR.addPrintLink = function () {
	var options = {
		targetEl: 'content-primary', // Id of the element the link is appended to
		linkText: 'Skriv ut sidan',
		linkId: 'print-link'
	};
	/**
	* 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
$(document).ready(function() {
	NetR.linkInfo.init();
	NetR.addARIA.init();
	NetR.JSTarget.init({
		val: 'new-window'
	});
	// NetR.addPrintLink.init({
	// 	linkText: 'Skriv ut sidan'
	// });

	// Rounded corners!
	$('.m:not(.m-banners)').addCorners();
	
	
		// Hide contact form
		var contactForm = $('#contact-publisher');
		var contactLink = $('.publisher:first');
		if (contactForm.size() && contactLink.size()) {
			if (window.location.hash == '#contact-publisher') {
				contactForm.show();
				contactForm.isHidden = false;
			} else {
				contactForm.hide();
				contactForm.isHidden = true;
			}
			contactLink.click(function(e) {
				e.preventDefault();
				if (contactForm.isHidden) {
					contactForm.animate({
						height: 'show',
						opacity: 'show'
					}, 'slow', function() {
						var targetOffset = contactForm.offset().top;
						$('html,body').animate({
							scrollTop: targetOffset
						}, 1000, function() {
							window.location.hash = '#contact-publisher';
						});
						contactForm.isHidden = false;
					});
				} else {
					contactForm.animate({
						height: "hide",
						opacity: "hide"
					}, 'slow', function() {
						contactForm.isHidden = true;
					});
				}
			});
		}
	
	
	
	
	NetR.FormFixer = (function () {
	return {
		// Pass in any form elements as jQuery objects or selector strings
		init: function () {
			if (arguments.length) {
				$.each(arguments, function () {
					$(this).each(function () {
						SolnaStad.FormFixer.fixButtons(this);
					});
				});
			}
		},
		// Replace submit-inputs with spans and buttons for better styling
		fixButtons: function (form) {
			$('input[type=submit]', form).each(function () {
				HLF.fixButtons(this);
			});
		},
		fixButton: function (element) {
			var oldButton = $(element),
			    newButton = $('<button type="submit">' + oldButton.val() + '</button>');
			// Copy attributes from old button to new button
			$.each(['id', 'class', 'name', 'value'], function () {
				var attr = oldButton.attr(this.toString());
				if (attr) {
					newButton.attr(this.toString(), attr);
				}
			});
			// Insert new button
			oldButton.after(newButton);
			newButton.wrap('<span class="button"><span><span><span><span></span></span></span></span></span>');
			// Add clearfix to submit-area
			oldButton.closest('.submit-area').addClass('cf');
			// Hide old button. If it is removed from the DOM, pressing enter in the form won't work in IE
			oldButton.hide();
		},
		fixLink: function (element) {
			var oldLink = $(element),
			    newLink = $('<a>' + oldLink.html() + '</a>');
			// Copy attributes from old button to new button
			$.each(['id', 'class', 'href', 'rel'], function () {
				var attr = oldLink.attr(this.toString());
				if (attr) {
					newLink.attr(this.toString(), attr);
				}
			});
			// Insert new button
			oldLink.after(newLink);
			newLink.wrap('<span class="button"><span><span><span><span></span></span></span></span></span>');
			oldLink.remove();
		}
	};
})();
	
	
	$(function () {
	$('#main input[type=submit]').each(function () {
		NetR.FormFixer.fixButton(this);
	});
	
	$('.btn a').each(function () {
		NetR.FormFixer.fixLink(this);
	});
	
	$('.red-btn a').each(function () {
		NetR.FormFixer.fixLink(this);
	});

	NetR.addPrintLink.init({
		targetEl: 'article-info'
	});

});
});
