
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FILE LOCATION: /templates_2008/scripts/format_printPage.js
DESCRIPTION: Printer version script
DATE of LAST EDIT: March 2010
CHANGE LOG: 
- March 2011: Use jquery to pick up content; pick up "local" css
- March 2010: Changed full-path + "2008_templates" to just "/templates_2008"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

//Create global references
var printWindow = null;
var blIsOpen = false;
var opener;
var objPrint;


function getHeader() {
	var header;
	//BC logo plus print buttons
	header = "<div id='header'>";
	header += "<a href='http://www.gov.bc.ca/'><img id='logo' src='/templates_2008/images/logos/bcgov.gif' width='163' height='58' alt='British Columbia - The Best Place on Earth'></a>";
	header += getPrintButtons() ;
	header += "</div>";
	return header;
}


function getMainCol() {
	var maincontent;
	maincontent = "<div id='content'>";
	maincontent += $("#content").html();
	maincontent += "</div>";
	return(maincontent);
}


function getMainCSS() {
	/* Pick up "local" css.
	
		Find LINK elements where:
		- rel="stylesheet"
		- href does not start with "/templates_2008/"
		- media contains "all" or "print"
		
		Find STYLE elements
		
	*/
	var contentCss = "";
	
	$("HEAD>LINK:[rel='stylesheet']").not("[href^='/templates_2008/']").filter("[media=''],[media*='all'],[media*='print']").each(function(index) {
		contentCss += "<link rel='stylesheet' media='all' href='" + $(this).attr("href") + "' />";
	});
	
	$("HEAD>STYLE").filter("[media=''],[media*='all'],[media*='print']").each(function(index) {
		contentCss += "<style type='text/css'>" + $(this).html() + "</style>";
	});
		
	return(contentCss);
}

function getPrintButtons() {
	var strFooter;
		strFooter = "<div class='print-buttons'>" +
			"<a href='javascript:print(); window.self.close();'>" + 
			"<img src='/templates_2008/images/buttons/printClose.gif' id='printClose' width='108' height='28' alt='Print and close'></a>" +
			"<a href='javascript:window.self.close();'>" + 
			"<img src='/templates_2008/images/buttons/printCancel.gif' id='printCancel' width='108' height='28' alt='Cancel'></a>" +
			"</div>";
	return strFooter;
}

function doPrint(objWindow) {
	if (blPrintWindowOpen()) {
		printWindow.focus();//make the preview window bubble to surface
	} else {
		
		objPrint = new objPrintObject();
		blIsOpen = true;
		opener = objWindow;
		printWindow = window.open("about:blank", "PrintVersion","left=100,screenX=200,top=50,screeny=100,resizable=yes,toolbar=yes,menubar=yes,width=800,height=600,modal=yes,scrollbars=yes,status=yes");
		printWindow.document.open();
	
		if($.browser.msie){
			printWindow.document.createElement("css3-container");
		}
	
		printWindow.document.write("<html><head>");
		printWindow.document.write(objPrint.mainCSS);
		printWindow.document.write("<style type='text/css' media='all'>@import '/templates_2008/css/custom.css';</style>");
		printWindow.document.write("<style type='text/css' media='all'>@import '/templates_2008/css/print.css';</style>");
		printWindow.document.write("</head>");
		printWindow.document.write("<body>");
		printWindow.document.write("<div id='wrapper'><a name='top'></a>");
		printWindow.document.write(objPrint.header);
		printWindow.document.write("<div id='content'>");
		printWindow.document.write(objPrint.mainCol);
		printWindow.document.write("</div>");
		printWindow.document.write("<div id='footer'>" + objPrint.printButtons + "</div>");
		printWindow.document.write("</div></body></html>");
		printWindow.document.close();
		
	}
}


function objPrintObject() {
	this.mainCSS = getMainCSS();
	this.header = getHeader();
	this.mainCol = getMainCol();
	this.printButtons = getPrintButtons();
	this.ParentWindow = window;
}


function blPrintWindowOpen() {
	if ((printWindow != null) && (blIsOpen == true)) {
		return (! printWindow.closed);
	} else {
		blIsOpen = false;
		return false;
	}
}


function closeWindow() {
	if((printWindow != null) && (blIsOpen == true)) {
		printWindow.close();
		printWindow=null;
		blIsOpen = false;
		return true;
	}
	return false;
}

