/**
 * TEXT AND PRINT HOW-TO
 * clicking the print or textmode buttons will toggle to text mode and back.
 * In textmode only elements and their descendants marked with css-class "printableNOT" are hidden.
 * Also all class-specific stylings have been removed, but inline stylings persist or
 *   stylings based on position.
 * 
 * You can modify the default behaviour with special printable tags:
 *  -printableNOT excludes the classed element and all of its descendants from textmode,
 *    regardless whether or not descendants contain any printable tags.
 *  -printableBG spares the background defined without classes, preferably as inline
 *  -printableImg prints the img designated with this class
 */

$(function () {
	
	//bind click-handler to text-mode- & print-buttons
	$("#textModeButton").click(function () {
		switchTextMode(false);
	});
	$("#printButton").click(function () {
		switchTextMode(true);
	});
	//reveal print-icon when page regains focus
	$(document.body).focus(function() {
		
	});
});

//set a global variable to direct the following function
var displayingPageAsText = false;
function switchTextMode(printRequested) {
	
	if(printRequested && ! displayingPageAsText) {
		displayPageAsText();
		printIt();
	}
	else if(printRequested && displayingPageAsText) {
		printIt();
	}
	else if(displayingPageAsText) {
		displayPageAsNormal();
	}
	else if(! displayingPageAsText) {
		displayPageAsText();
	}
}
function displayPageAsText() {
	
	displayingPageAsText = true;

	//get all the content that needs to be printed or shown in text
	var printableContent = $(document.body).find("#content").clone(false);
	var printToolbar = $("#printAndTextBar").clone(true);
	
	if(printableContent.size() == 0) {
		alert("Sivulla ei ole mitään tulostettavaa!");
		displayingPageAsText = false;
	}
	else {
		//Hide everything
		$(document.body).children().each(function() {
			var i = $(this);
			//make it possible to revert changes
			i.attr("cssDisplay",i.css("display"));
			i.css("display","none");
		});
		
		//Iterate every item and filter them
		printableContent.find("*").each(function() {
			var i = $(this);
			
			//remove liferay cms editing toolbars
			if(i.hasClass("icons-container") || i.hasClass("portlet-borderless-bar") || i.hasClass("portlet-topper-toolbar")) {
				i.remove();
			}
			//remove liferay cms icon if it is not in use, check for section first to prevent intensive lookup for all of i`s iterations
			if(i.is("section")) {
				var icon = i.find("header > h1 > span > img");
				if(icon.css("display") == "none") {
					icon.remove();
				}
			}
			//remove liferay cms chat-portlet from the bottom of the viewport
			if(i.hasClass("chat-portlet")) {
				i.remove();
			}
			
			//set items marked with visibility: hidden; to display: none; so they wont be visible when classes are removed
			if(i.css("visibility") == "hidden") {
				i.css("visibility","hidden"); //set visibility value as inline styling so removing classes wont change that
			}
			
			// **** special-tag filters here! **** remember to document in how-to!!
			
			if(i.hasClass("printableNOT")) {
				i.remove();
			}
			if( ! i.hasClass("printableBG")) {
				i.css({
					'background-color' : 'transparent',
					'background-image' : 'none'
				});
			}
			/*if( ! i.hasClass("printableColor")) {
				i.css({
					'color' : '#000000',
					'text-shadow' : 'none'
				});
			}*/
			if( i.is("img") && ! i.hasClass("printableImg") ) {
				i.remove();
			}
			  
			
			// ****   EO special-tag filters  ****
			
			if(i.hasClass("site-breadcrumbs") || i.hasClass("breadcrumbs")) {
				;//do not remove classes
			}
			else {
				i.removeClass();
				i.removeAttr("id");
			}
			
		});

		
		//add the cloned printables to body to make them visible
		var textContainer = document.createElement("DIV");
		textContainer.setAttribute("id", "textModeContainer");
		document.body.appendChild(textContainer);
		$(textContainer).append(printToolbar);
		$(textContainer).append(printableContent);
		
		//Fixes hover image getting stuck
		$(printToolbar).find(".jq_button").mouseout();
	}
}
function displayPageAsNormal() {
	
	displayingPageAsText = false;
	
	$("#textModeContainer").remove();
	
	$(document.body).children().each(function() {
		var i = $(this);
		i.css("display",  i.attr("cssDisplay")  );
	});
	
}
function printIt() {
		
	$("#textModeContainer #printAndTextBar").css("display", "none");
	window.print();
	listenForDocumentFocus();
}
/*
 * Re-displays #printAndTextBar after document has gained focus.
 */
function listenForDocumentFocus() {
	
	if( document.hasFocus() ) {
		$("#textModeContainer #printAndTextBar").css("display", "block");
	}
	else {
		setTimeout("listenForDocumentFocus()", 300);
	}	
}
