/* Print tool
 * - developer can choose to enable it
 */
 
function initPrintTool() {

/* exit if page tools element not found or if tool exists */
	if (!document.getElementById(idList.pageTools)) return false;
	if (document.getElementById(idList.printTool)) return false;
	
	var strQueryString = window.location.search;

	/* Find the script's URI (if there's a query string) */
	var strScriptUri = "";
	if (window.location.href.indexOf("?") == -1) {
		strScriptUri = window.location.href;
	} else {
		strScriptUri = window.location.href.substring(0,window.location.href.indexOf("?"));
	}
	
	var objLink = document.createElement('a');
	objLink.id = idList.printTool;
	if (strQueryString.length == 0 || strQueryString == "?") { 
		objLink.href = strScriptUri + "?" + "print=true";
	} else {
		/* detect if there already is a print parameter in the URL */
		if (strQueryString.match(/print(=([^&]*))?/)) {
			objLink.href = strScriptUri + strQueryString.replace(/print(=([^&]*))?/g,'print=true');
		/* if not, add one */
		} else {
			objLink.href = strScriptUri + strQueryString.replace(/([^\&\=]*)$/,'$1&print=true');
		}
	}
	
	addEvent(objLink,'mouseover',function() { cssjs('add',this,cssClasses.over); } );
	addEvent(objLink,'mouseout',function() { cssjs('remove',this,cssClasses.over); } );
	
	/* insert tool in DOM */
	document.getElementById(idList.pageTools).appendChild(objLink);
}

function initPrintPreview () {

	var objHead = document.getElementsByTagName('head')[0];

	/* process all <style> tags
	 * arrStyleTags is a nodelist and will change when the page is modfied.
	 * Loop backwards to avoid the effects of this. */
	var arrStyleTags = objHead.getElementsByTagName('style');
	for (var i=arrStyleTags.length-1; i > -1; i--) {
	    if (arrStyleTags[i].media == "screen") {
		/* disable the tag */
		objHead.removeChild(arrStyleTags[i]);
	    } else if (arrStyleTags[i].media == "print" || !arrStyleTags[i].media) {
		arrStyleTags[i].media = "screen, print";
	    }
	}

	/* process all <link rel="stylesheet"> tags as well */
	/* Same problem as above, so loop backwards. */
	var arrLinkTags = objHead.getElementsByTagName('link');
	for (var i=arrLinkTags.length-1; i > -1; i--) {
	    if (arrLinkTags[i].rel == "stylesheet" && arrLinkTags[i].media == "screen") {
		/* disable the tag */
		objHead.removeChild(arrLinkTags[i]);
	    } else if (arrLinkTags[i].media == "print" || !arrLinkTags[i].media) {
		arrLinkTags[i].media = "screen, print";
	    }
	}


	/* Add buttons */
	var objOptions = document.createElement('div');
	var objPrintButton = document.createElement('input');
	var objReturnLink = document.createElement('a');
	
	objOptions.id = idList.printOptions;
	objPrintButton.id = idList.printButton;
	objReturnLink.id = idList.cancelPrint;
	
	objPrintButton.type = "button";	
	addEvent(objPrintButton,'click', function () { window.print(); } );
	
	objReturnLink.href = window.location.href.replace(/(\?|\&)print=true/,'');
	
	/* assemble the parts */
	objOptions.appendChild(objPrintButton);
	objOptions.appendChild(objReturnLink);
	
	/* insert as first element in body */
	document.getElementsByTagName('body')[0].insertBefore(objOptions,document.getElementsByTagName('body')[0].firstChild);
	
	/* enable style sheet for print preview options */
	cssjs('add', document.getElementsByTagName('html')[0],cssClasses.printPreview);
	
	/* required for Safari */
	if (/WebKit/i.test(navigator.userAgent)) { // sniff
		var newSS=document.createElement('link'); 
		newSS.rel = 'stylesheet'; 
		newSS.type = 'text/css';
		//newSS.href='data:text/css,'+escape(styles); 
		newSS.href = STYLE_SHEETS_HOME + 'print.css';
		document.getElementsByTagName("head")[0].appendChild(newSS);
	}

}

