/* Function to fix IE's Bug showing active links and remove rectangle around links */
var debug = true;

function init(id, src, text, introtxt) {
    if (document.getElementsByTagName) {
        var anchors = document.getElementsByTagName("a");
        for (var i = 0; i < anchors.length; i++) {
            var myAnchor = anchors[i];
            if (!myAnchor.onclick) {
                myAnchor.onclick = function () {
                    if (document.all) { // only for IE
                        this.blur();
                    }
                    /*
                    if (window.location.hostname != this.hostname) { // assume that this is an external link
                        makeNewWindow(this);
                        return false;
                    }

                    // Temp...
                    else if (debug && this.href.indexOf("404.html") != -1) {
                        return false;
                    }
                    */
                    return true;
                }
            }
        }
    }
    renderPrintLink(id, src, text, introtxt);
}

window.onload = init;

function printPage() {

    var isIE = (navigator.appName == "Microsoft Internet Explorer") ? true: false;
    var platform = navigator.platform;
    var isMac = (platform.match(/mac/i)) ? true : false;
    var isMacIE = isIE && isMac;
    var isSafari = navigator.userAgent.indexOf("Safari") != -1;

    if (window.print && !isMacIE && !isSafari) {
        window.print();
    } else if (document.getElementsByTagName) {
        var a;
        for(var i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
            if (a.getAttribute("rel").indexOf("style") != -1) {
                a.disabled = true;
                if (a.getAttribute("rel").indexOf("alt") != -1 && a.getAttribute("title") == "Druckansicht") {
                    a.disabled = false;
                }
            }
        }
    } else {
        alert("Diese Funktion wird von Ihrem Browser nicht unterstützt.\nBitte benutzen Sie die Druckfunktion Ihres Browsers.");
    }

}

if (!document.getElementById) {
    document.getElementById = function() { return null; }
}

function renderPrintLink(id, src, text, introtxt) {

    var node = document.getElementById(id);

    if (node != null) {
        if (introtxt != null && introtxt != "") {
            introtxt += " ";
            node.appendChild(document.createTextNode(introtxt));
        }

        var img = document.createElement("img");
        img.setAttribute("src", src);
        img.setAttribute("alt", text);
        node.appendChild(img);

        var a = document.createElement("a");
        a.href = "javascript:printPage();";
        a.style.color =  "#2b477f"; // Stupid IE
        a.appendChild(document.createTextNode(text));
        node.appendChild(a);

        var span = document.createElement("span");
        span.className = "white";
        span.appendChild(document.createTextNode(" |"));
        node.appendChild(span);

    }
}

// Global variable for subwindow reference
var newWindow;
// Generate and fill the new window
function makeNewWindow(obj) {
    newWindow = window.open("", "sub", "status,resizable,height=200,width=400");
    // handle Navigator 2, which doesn't have an opener property
    if (!newWindow.opener) {
        newWindow.opener = window;
    }
    // delay writing until window exists in IE/Windows
    setTimeout("writeToWindow('" + obj + "')", 500);
    newWindow.focus();
}

function writeToWindow(obj) {
    // assemble content for new window
    var newContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n";
    newContent += "    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n\n";
    newContent += "<html>\n<head>\n";
    newContent += "<title>Online-Styleguide : Hinweis</title>\n";
    for(var i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel") == "stylesheet") {
            newContent += "<link rel=\"stylesheet\" title=\"Standard\" media=\"screen, projection\" href=\"" + a.getAttribute('href') + "\" type=\"text/css\" />\n";
        }
    }
    newContent += "</head>\n";
    newContent += "<body id=\"hinweis\"><div><p>Die Adresse <a href=\"" + obj + "\" target=\"_blank\" title=\"&Ouml;ffnet neues Fenster\">" + (obj.indexOf('http://www') != -1 ? obj.substring(7) : obj) + "</a> ist nur &uuml;ber einen Internetzugang erreichbar.</p></div>";
    newContent += "</body>\n</html>";
    // write HTML to new window document
    newWindow.document.write(newContent);
    newWindow.document.close(); // close layout stream
}