function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
                    

// OPTION 1 - opens in same window (works!)
function print_preview() {
	setActiveStyleSheet('Print Preview');
	add_preview_message();
}


// Opens the code in a new window and sets substring
/*newwindow = window.open(''+self.location+'?print=true','mywin', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0'); newwindow.focus();*/



/* If statement that doesnt work! 
if(window.location.search('?print=true') >0){ 
	alert(window.location.pathname);
	// Set active CSS
	setActiveStyleSheet('Print Preview');
	// Create preview message
	add_preview_message();
} else {	
}*/





function add_preview_message(){

var main_content = document.getElementById('wrapper');
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');
		preview_header.appendChild(preview_header_text);
		
		// Create paragraph
		var preview_para = document.createElement('p');
		var preview_para_text = document.createTextNode('');
		
		var cancel_function_link = document.createElement('a');
			cancel_function_link.onclick = function(){ cancel_print_preview(); return false; };
			cancel_function_link.setAttribute('href', '#');
			cancel_function_link.setAttribute('class', 'buttonLarge');	
		var cancel_function_link_text = document.createTextNode('Exit preview');
		
		cancel_function_link.appendChild(cancel_function_link_text);
		preview_para.appendChild(preview_para_text); //
		preview_para.appendChild(cancel_function_link);
		
		// Create paragraph 2
		var preview_para2 = document.createElement('p');
		var preview_para_text2 = document.createTextNode('');
		
		var cancel_function_link2 = document.createElement('a');
			cancel_function_link2.onclick = function(){ window.print(); };
			cancel_function_link2.setAttribute('href', '#');
			cancel_function_link2.setAttribute('class', 'buttonLarge');	
		var cancel_function_link_text2 = document.createTextNode('Print page');
		
		cancel_function_link2.appendChild(cancel_function_link_text2);
		preview_para2.appendChild(preview_para_text2); //
		preview_para2.appendChild(cancel_function_link2);
		
		// Put it all toegether
		preview_message.appendChild(preview_header); 
		preview_message.appendChild(preview_para);
		preview_message.appendChild(preview_para2);
		main_body.insertBefore(preview_message, main_content);

	}
}

function cancel_print_preview() {
	// Destroy the preview message
	var print_preview = document.getElementById('preview-message');
	var main_body = print_preview.parentNode;
	main_body.removeChild(print_preview);
	
	// Switch back stylesheet
	setActiveStyleSheet('default');
}

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")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}
