// {{{ function doPrint
// TODO: more gracefully handle docroot + relative path in the event of an absolute path
function doPrint(button)
{
	// prevent duplication of content
	if (window.print_window && !window.print_window.closed)
	{
		window.print_window.close();
	}

	var docroot = window.location.toString().split('/');
	docroot = docroot[0] + '//' + docroot[2];

	// open new window
	var print_window = window.open(docroot + DrupalBasePath + "print","printwindow","location=1,status=1,scrollbars=1,width=650,height=650");
	window.print_window = print_window;

	print_window.document.body.innerHTML = '<CENTER><DIV STYLE="padding-top:355px;font-weight:bold">Loading...</DIV></CENTER>';
}
// }}}
// {{{ function doPrintFormat
function doPrintFormat()
{
	var source_window = window.opener;
	var print_window = window;

	try {
		var docroot = source_window.location.toString().split('/');
	} catch(e) {
		print_window.close();
	}

	docroot = docroot[0] + '//' + docroot[2];

	var head = print_window.document.getElementsByTagName('head')[0];
	// copy CSS used from the current document
	var css = source_window.document.getElementsByTagName('link');
	for (var i=0; i < css.length; i++)
	{
		// skip non-stylesheet links
		if (css[i].getAttribute('rel') != 'stylesheet')
		{
			continue;
		}

		var css_ = print_window.document.createElement('link');
		css_.setAttribute('rel', 'stylesheet');
		css_.setAttribute('type', css[i].getAttribute('type'));
		css_.setAttribute('media', css[i].getAttribute('media'));
		css_.setAttribute('href', docroot + css[i].getAttribute('href'));

		head.appendChild(css_);
	}

	// copy JS used from the current document
	var js = source_window.document.getElementsByTagName('script');
	for (var i=0; i < js.length; i++)
	{
		var js_ = print_window.document.createElement('script');
		js_.setAttribute('type', js[i].getAttribute('type'))

		if (js[i].getAttribute('src'))
		{
			js_.setAttribute('src', docroot + js[i].getAttribute('src'));
		}

		head.appendChild(js_);
	}

	// color banner
	var banner = print_window.document.createElement('div');
	banner.setAttribute('id', 'print-banner');
	banner.className = 'no-print';

	// print preview header
	var header = print_window.document.createElement('div');
	header.setAttribute('id', 'print-header');
	//header.className = 'node';

	var title = print_window.document.createElement('div');
	title.className = 'title';
	title.appendChild(print_window.document.createTextNode("Print Preview"));
	header.appendChild(title);

	// print controls
	var controls = print_window.document.createElement('div');
	controls.setAttribute('id', 'print-controls');

	// print options
	var options = print_window.document.createElement('div');

	// print option - hide images
	var optionsImages = print_window.document.createElement('input');
	optionsImages.setAttribute('id', 'hideimages');
	optionsImages.setAttribute('type', 'checkbox');
	optionsImages.setAttribute('defaultChecked', true);
	optionsImages.checked = true;
	optionsImages.onclick = function() {toggleImages(optionsImages)};
	options.appendChild(optionsImages);

	var optionsImagesLabel = print_window.document.createElement('label');
	optionsImagesLabel.setAttribute('for', 'hideimages');
	optionsImagesLabel.appendChild(print_window.document.createTextNode("Hide Images"));
	options.appendChild(optionsImagesLabel);

	controls.appendChild(options);

	// print button
	var buttonPrint = print_window.document.createElement('input');
	buttonPrint.setAttribute('type', 'button');
	buttonPrint.setAttribute('value', "Print");
	buttonPrint.className = 'button';
	buttonPrint.onclick = function() {doPrePrint(print_window); return false};
	controls.appendChild(buttonPrint);

	// close button
	var buttonClose = print_window.document.createElement('input');
	buttonClose.setAttribute('type', 'button');
	buttonClose.setAttribute('value', "Close");
	buttonClose.className = 'button';
	buttonClose.onclick = function() {print_window.close(); return false};
	controls.appendChild(buttonClose);

	// write banner
	header.appendChild(controls);
	banner.appendChild(header);
	print_window.document.body.appendChild(banner);

	// create content frame
	var page = print_window.document.createElement('div');
	page.setAttribute('id', 'page');
	page.style.width='550px';

	// copy content
	var content = print_window.document.createElement('div');
	content.setAttribute('id', 'content-inner');
	content.style.paddingTop='10px';
	content.innerHTML = source_window.document.getElementById('content-inner').innerHTML;
	page.appendChild(content);

	print_window.document.body.appendChild(page);

	// hide title links
	print_window.document.getElementById('title_link').style.display = 'none';

	// handle default image state
	toggleImages(optionsImages);
}
// }}}
// {{{ function doPrePrint
function doPrePrint(window)
{
	window.print();
}
// }}}
// {{{ function toggleImages
function toggleImages(button)
{
	var images = document.getElementsByTagName('img');

	if (!images)
	{
		return;
	}

	var display = 'none';
	if (!button.checked)
	{
		display = '';
	}

	for (var i=0; i < images.length; i++)
	{
		images[i].style.display = display;
	}
}
// }}}
