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

/* The following section of contants are moved from pathway.vm */
// These are the divs will be hidden when print preview. Must assign to _div_styles.
// In addition, the print preview script should also look for div's that have 
// classname 'no_print'.
var _div_styles = {
  'header': '',
  'footer': '',
  'navigation': '',
  '_cookie_support_div': '',
  '_session_timeout_div': '',
  'pathway' : '',
  'subcontentL' : '',
  'toplinks': '',
  'content_curve': '',
  'divider_bar_narrow': ''
};
var _no_print_div_classname = 'no_print';
// This is the main content div to be printed. Must assign to _content_div.
// If the div with 'content' id can not be found in the layout; the print preview
// script will look for div with classname 'print_content'.
var _content_div = 'content';
var _content_div_classname = 'print_content';


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;
	}
	var additional_divs = document.getElementsByClassName(_no_print_div_classname);
	for (var i in additional_divs) {
		var div = additional_divs[i];
		if (div && div.id) 
			div_styles_array[div.id] = 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);
	// If the div with that id == _content_div can not be found, keep on search the div with classname == 'print_content'
	if (!main_content)
		main_content = document.getElementsByClassName(_content_div_classname)[0];
		
    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;
     }
   }
}
*/