// These functions apply the specified (print) style sheet to a page - March 2006 - Dustin Lundebrek

// Set the name of the print style sheet (this script assumes the print style sheet is in the same directory as the default style sheet)
var printStylesheet = 'ct-print.css';
// Set ID of element that links to print version - if you change this, update the style sheet and HTML template
var printCSSLink = 'printCSS';

// Global setting for whether to display Print Version link
// showPrintVersion variable can also be used to toggle this setting in individual documents
var showPrintVersion = false; // set to true to display printCSS link, false to not display

window.onload = applyPrintCSS;

// If the url query string is "?print", set css link source to printStylesheet
function applyPrintCSS() {
	if (document.location.search == '?print') {
		links = document.getElementsByTagName('link');
		for (var i=0; i < links.length; i++) {
			if (RegExp(/.+\.css/).test(links[i].href) == true) {
				cssContext = links[i].href.replace(/(.+\/).+css/, '$1');
				links[i].href = cssContext + printStylesheet;
			}
		}
	}
	else if(showPrintVersion == true) { 
		document.getElementById(printCSSLink).style.display = 'inline'; // Displays printCSS element (which is hidden by default, per the main style sheet)
	}
	document.getElementById(printCSSLink).onclick = function() {
		printCSS();
	}
}


// Appends "?print" to the current URL and opens it
function printCSS() {
	re = /(.+)#.+/; // Regular expression for finding an internal link component
	thisPageURL = document.location + "";
	thisPageURLre = thisPageURL.replace(re, '$1') // Removes internal link component, if any
	thisPageURLrePrint = thisPageURLre + "?print"; // Appends "?print" to url
	document.location = thisPageURLrePrint; // Opens new url
}
