﻿
$(function () {
    //Check to see if browser supports onbeforeprint (IE6, IE7 and IE8)
    if (window.onbeforeprint !== undefined) {
        //Since the browser is IE, add event to append link text before print
        window.onbeforeprint = ShowLinks;

        //Remove the link text since the document has gone to the printer
        window.onafterprint = HideLinks;
    }
    else {
        //The browser is not IE so attach a print style to append the link's text when printed
        //$('head').append('<style type="text/css" media="print">.print_link:after { content: " (" attr(href) ")"; }</style>');
        //Append the link to the current text
        $(".content-wrapper a:not('.left-navi a')").each(function () {
            var attr = $(this).attr('href');
            if (typeof attr !== 'undefined' && attr !== false && attr !== "") { //Exclude links that have no href attribute.
                $(this).not('[href^="#"]').addClass('printedAnchor'); //Add a special class to those links that we want to print their href atteribute when printing
            }
        });
        $('head').append('<style type="text/css" media="print">.content-wrapper a.printedAnchor:after { content: " (" attr(href) ")"; }</style>');
    }
});

function ShowLinks() {
    //$(".print_link").each(function() {
    $(".content-wrapper a:not('.left-navi a')").each(function () {
        if ($(this).find("img").length > 0) {
            $(this).hide();
        }
        else {
            //Store the link's original text in the jQuery data store               
            $(this).data("linkText", $(this).text());

            //Append the link to the current text
            var attr = $(this).attr('href');
            if (typeof attr !== 'undefined' && attr !== false) { //Exclude links that have no href attribute.
                $(this).not('[href^="#"]').append(" (" + $(this).attr("href") + ")"); // Exclude also links that have a href value starting with "#".
            }
            //$(this).append(" (" + $(this).attr("href") + ")");
        }
    });
}

function HideLinks() {
    //$(".print_link").each(function() {
    $(".content-wrapper a").each(function () {
        //Restore the links text to the original value by pulling it out of the jQuery data store
        if ($(this).find("img").length > 0) {
            $(this).show();
        }
        else {
            $(this).text($(this).data("linkText"));
        }
    });
}         
