/* For use with the print preview using stylesheet */
/* Please refer to print.css, print_preview.css and PrintPreviewUsingStyle.vm */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function cache_div_styles(div_styles_array)
{
	for (var div in div_styles_array) {
		if ($(div))
			div_styles_array[div] = $(div).style.display;
	}
}

function change_div_styles (div_styles_array, toStyle)
{
	for (var div in div_styles_array) {
		if ($(div))
  	       $(div).style.display = toStyle;
	}
}
function reset_div_styles (cached_div_styles)
{
	for (var div in cached_div_styles) {
		if ($(div))
			$(div).style.display = cached_div_styles[div];
	}
}

function print_preview(div_styles_array) {
	// Cache the stylesheet right before the print preview
    cache_div_styles(div_styles_array);
	// hide all the navigation divs.
	change_div_styles(div_styles_array, 'none');
	// Create preview message
	add_preview_message();
	window.print();
}

function add_preview_message(){
    //var main_content = document.getElementById('content');
	var main_content = $(_content_div);
    var main_body = main_content.parentNode;

	if (document.getElementById){
		
		var preview_message = document.createElement('div');
		preview_message.id = 'preview-message';

		// Create Heading
		var preview_header = document.createElement('h3');
		var preview_header_text = document.createTextNode('This is a print preview of the page.');
		preview_header.appendChild(preview_header_text);
		
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function(){ cancel_print_preview(_div_styles); return false; };
			cancel_function_link.setAttribute('href', '#');	
		var cancel_function_link_text = document.createTextNode(' Return to the original page.');
		cancel_function_link.appendChild(cancel_function_link_text);
		
		preview_header.appendChild(cancel_function_link);
		
		// Put it all toegether
		preview_message.appendChild(preview_header); 
		main_body.insertBefore(preview_message, main_content);
	}
}

function cancel_print_preview(div_styles_array) {
	// Destroy the preview message
	var print_preview = document.getElementById('preview-message');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);
	reset_div_styles(div_styles_array);
}

/*
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1) {
        //&& a.getAttribute("title")) {
	dump("::setActiveStyleSheet title " + a.getAttribute("title") + "\n");
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
*/
