// =================================================================================================
// print.js - For live print previews
//
// Author: Mark Stickley, Evolving Media ©2007
// =================================================================================================

Event.observe(window, 'load', initialise_print, false);

function initialise_print(){
	//find that print button and show it to the world!
	//this method over creating the button with JS lets us decide which pages get print buttons
	button = document.getElementsByClassName('printpage');

	if(button.length){
		button[0].style.display = 'block';
		buttonlink = $$('.printpage a');
		buttonlink[0].setAttribute('onclick', 'showPrintPreview(); return false;');
		buttonlink[0].onclick = showPrintPreview;
	}
}

function showPrintPreview(){
    // Switch the stylesheet
    setActiveStyleSheet('Print Preview');
    
    //encase sIFR replaced flash titles in divs so we can hide them in safari
    encaseFlashTitles();

    // Create preview message
    add_preview_message();

    // Print the page
    window.print();
}

function cancelPrintPreview(){
    // Switch the stylesheet
	setActiveStyleSheet('default');
//	setActiveStyleSheet('text.css'); //both are never in the html at once and so if one fails the other will work.

    // Remove preview message
    remove_preview_message();
    
}


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;
     }
   }
}

function add_preview_message(){
	var preview_message = document.createElement('div');
	preview_message.id = 'preview-message';
	
	var preview_text = document.createElement('p');
	preview_text.innerHTML = 'This is a preview of what you will see when you print this document. This message will not be printed.';
	preview_message.appendChild(preview_text);
	
	var preview_close = document.createElement('a');
	preview_close.onclick = function(){ cancelPrintPreview(); return false; };
	preview_close.setAttribute('href', '#');
	preview_close.innerHTML = 'Close print preview';
	preview_message.appendChild(preview_close);
	
	header = document.getElementsByClassName('header');	
	header[0].parentNode.insertBefore(preview_message, header[0]);
}

function remove_preview_message(){
	Element.remove('preview-message');
}

function encaseFlashTitles(){
	if(navigator.userAgent.indexOf('Apple') != -1){
		alert('Its safari');
		flashobjects = $$('.sIFR-flash');
		
		for(i=0;i<flashobjects.length;i++){
			var parent = flashobjects[i].parentNode;
			var container = document.createElement('div');
			container.setAttribute('class', 'sIFR-flash-container');
	//		container.appendChild(flashobjects[i]);
	//		parent.replaceChild(container, flashobjects[i]);
			var clone = flashobjects[i].cloneNode(true);
			container.appendChild(clone);
			parent.insertBefore(container, flashobjects[i]);
			parent.removeChild(flashobjects[i]);
		}
	}
}
