/*
 * jQuery addons for links
 */

(function ($) {

	$.extend($.fn, {
		/*
		 * Convert obfuscated mailto elements into real links
		 */
		activateEmailLinks: function (options) {
			options = $.extend({}, {
				textSelector: '.email-text:first', // Optional element with text to be used as the visible link text
				addressSelector: '.email-address:first', // Element with obfuscated email adress (entity encoded, decimal or hexadecimal)
				salt: 'INGEN_SPAM_' // Optional prefix to further reduce the risk of spam bots picking up addresses
			}, options || {});

			return this.each(function () {
				var textElem = $(options.textSelector, this);
				var addressElem = $(options.addressSelector, this);
				if (addressElem.length) {
					var addressText = addressElem.text().replace(options.salt, "");
					var textText = (textElem.length ? textElem.text() : addressText).replace(options.salt, "");
					$(this).replaceWith('<a href="mailto:' + addressText + '">' + textText + '</a>');
				}
			});
		},

		/*
		 * When clicked, supplied links should open i a new window
		 */
		triggerNewWindow: function (options) {
			options = $.extend({}, {
				widthRegEx: /(^|\\s)w([0-9]+)(\\s|$)/, // Regular expression for width (matches w400)
				heightRegEx: /(^|\\s)h([0-9]+)(\\s|$)/, // Regular expression for height (matches h400)
				warning: '', // 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
			}, options || {});

			return this.each(function () {
				if (options.image) {
					$(this).addClass(options.imageLinkClass).append($('<img>', {
						src: options.image,
						alt: options.warning,
						className: options.hiddenClass
					}));
				} else {
					
				}


				// 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 {
				// 	if (options.warning != null && options.warning.length > 0) {
				// 		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';
			});
		}
	});
})(jQuery);