// The page prints or previews itself
// by Lost Boys - www.lostboys.nl

PrintSelf = function() {
	var url = document.location.href;
	if(url.match("print_self")) {
		var root = document.getElementById("canvas");
		if(url.match("no_images")) addClass(root, "print-noimages");
		// focus on itself and print
		window.self.focus();
		// give other scripts some time to do their thing, like setting classes, before printing
		window.setTimeout(function() { window.self.print() }, 800);
	}
}

PreviewSelf = function(obj) {
	var url = document.location.href;
	if(url.match("preview_self")) {
		var root = document.getElementById("canvas");
		var dutch = /nl/i.test(getLanguage());
		var printTitle = dutch? "Printvoorbeeld: " : "Print preview: ";

		if(url.match("no_images")) addClass(root, "print-noimages");
		if(url.match("print_partly")) addClass(root, "print-partly");
		document.title = printTitle + document.title;
		// switch to print stylesheet				
		var els = document.getElementsByTagName("link");
		for(var i = 0; i < els.length; i++) {
			if(els[i].rel.indexOf("stylesheet") > -1 && els[i].media.indexOf("print") > -1) var pcss = els[i];	
		}
		for(var i = 0; i < els.length; i++) {
			if(els[i].rel.indexOf("stylesheet") > -1 && els[i].media.indexOf("screen") > -1) {
				els[i].href = pcss.href;
			}
		}
		// create button
		var button = document.createElement("a");
		button.id = "printbutton";
		button.href = "#";
		button.title = dutch? "Print deze pagina" : "Print this page";
		button.onclick = function() {
			document.title = document.title.replace(printTitle, "");
			this.parentNode.removeChild(this);
			window.print();
			window.close();
		}
		root.insertBefore(button, root.firstChild);		
		var buttonImg = document.createElement("img");
		buttonImg.alt = "Print";
		buttonImg.src = "/ns2007/static/images/buttons/printbutton"+(dutch? '' : '_en')+".gif";
		button.appendChild(buttonImg);
		// scroll to top (for when there's a hash in the URL)
		window.scrollTo(0, 0);
	}
}

/* generate urls from anchors, hidden for screen, but used for print */

GenerateAnchors = function() {
	//JMu: moet er wel een element content bestaan... om te kunnen werken
	if (document.getElementById("content")) {
		var els = document.getElementById("content").getElementsByTagName("a");
		for(var i = 0; i < els.length; i++) {
			if(/^#$/.test(els[i].getAttribute('href'))) continue;
			var url = els[i].href;
			url = url.split("#")[0];
			if(url.length == 0 || url == document.location.href) continue; // skip local "#" links.
	//		url = url.split("?")[0];
	//		if(!document.location.href.match(url)) {
			url = url.replace("http://", "");
	//		var r = new RegExp(".+/$");
	//		url = url.replace(r, "");
			if (els[i].className.indexOf("article-skip") == -1) {
				var s = document.createElement("span");
				s.className = "for-print";
				s.innerHTML = " (" + url + ")";
				try {
					els[i].parentNode.insertBefore(s, els[i].nextSibling);	
				} catch (e) {
					els[i].parentNode.appendChild(s);	
				}
			}
		}
	}
}



EventListener.addEvent(window, "load", function() {
	GenerateAnchors();
	PrintSelf();
	PreviewSelf();
} );