﻿/**
* @fileoverview Set certain links to be opened in new windows
* @requires jQuery
*/
/* Create NetR namespace */
if (typeof NetR === "undefined") {
    var NetR = {};
}

/**
* Opens certain links in new windows.
* Adds a target attribute and a warning message to affected links.
*/
NetR.JSTarget = function() {
    // Default configuration
    var config = {
        strAtt: 'class', // The attribute to be examined
        strVal: 'new-window', // The value that tells the script that the link should open in a new window
        strWarning: ' (nytt fönster)' // The text that is appended to the link to warn the user that a new window will open
    };
    /**
    * @param {object} props Optional object containing configuration properties in the format
    * {property1:value, property2:value}
    */
    function init(props) {
        if (!document.getElementById || !document.createTextNode) {
            return;
        } // Check for DOM support
        // If any properties were supplied, apply them to the config Object.
        for (var key in props) {
            if (config.hasOwnProperty(key)) {
                config[key] = props[key];
            }
        }
        var oWarning;
        var oLink;
        var arrLinks = document.getElementsByTagName('a');
        var oRegExp = new RegExp("(^|\\s)" + config.strVal + "(\\s|$)");
        for (var i = 0, len = arrLinks.length; i < len; i++) {
            oLink = arrLinks[i];
            // If the attribute is class, check for className
            if ((config.strAtt === 'class') && (oRegExp.test(oLink.className)) || (oRegExp.test(oLink.getAttribute(config.strAtt)))) {
                if (config.strWarning.length > 0) {
                    oWarning = document.createElement("em");
                    oWarning.appendChild(document.createTextNode(config.strWarning));
                    oLink.appendChild(oWarning);
                }
                oLink.target = '_blank';
            }
        }
    }
    return {
        init: init
    };
} ();


// Init on document ready
jQuery(document).ready(function() {
    /* Initiate adding target attribute */
    NetR.JSTarget.init({strWarning: ''});
});