//$(document).ready(
$(function() { 
	if (!use_ex()) {
		$("a[@href$='/printversion']").attr("href","javascript:Print.swithToPrintLayout();");
		} else	{if (has_pvs()) Print.swithToPrintLayout();}
	}
);

function use_ex() {
	return (typeof(use_printversionex) != 'undefined') && (use_printversionex !=0);
}

function has_pvs() {
	return (location.href).search(/\/printversion$/i) != -1;
}

function createStyle(item) {
	var index, path;
	var item_path = item.innerHTML;
	for (i in document.styleSheets) {
		var style = document.styleSheets[i];
		if (style.imports && (style.imports.length>0) ) {
			path = style.imports[0].href;
			if (item_path.indexOf(path) != -1) {
				index = i;break;
			}
		}
	}
	
/*
	var elem = document.createElement('<STYLE type="text/css" media="screen">');
//	elem = document.createElement("<STYLE type='text/css' media='screen'>");
	//elem.type = "text/css";
	//elem.innerHTML = item.innerHTML;
	document.documentElement.firstChild.appendChild(elem);
	//elem.addRule('DIV B', "color:blue");
	alert('elem='+ elem.outerHTML+'style: '+elem.sheet);
*/	
	//return elem;
	if (index) {
		//var elem = document.createStyleSheet(path, index);
		var sheet = document.createStyleSheet('', index); //path
		sheet.media = "screen";
		sheet.addImport(path);
		var elem = sheet.owningElement;
		elem.type = item.type;
		Print.added.push([elem]);
	}
	
//	var elem = document.createElement('<STYLE type="text/css" media="screen">');
//	elem.addImport('/sites/all/themes/myTest/print.css');
	//elem.type = item.type;
	//elem.media = "screen";
//	alert('elem='+ elem.outerHTML +' item='+item.outerHTML+' type='+item.type);
}

var Print = {
	added: [],
	addPrintButton: function(){
		var div=$('<div id="pvs-buttons"> <button id="pvs-print">&nbsp;</button> <button id="pvs-close">&nbsp;</button> </div>').prependTo('body');
		$('#pvs-print', div).click(function() {window.print();});
		$('#pvs-close', div).click(function() {Print.swithToNormalLayout();});
	},
	removePrintButton: function(){
		$('#pvs-buttons').remove();
		$('#pvs-buttons').remove();
	},
	swithToPrintLayout: function(){
		//window.switchLayout = false;
		
		if(!use_ex()) {
			$("style,link[@rel='stylesheet']").filter("[@media='print']").each(
				function() {
					var arg = this.outerHTML;
					if (arg) {
						var div = document.createElement("div");
						div.innerHTML = "div<div>" + arg + "</div>";
						var elem = div.lastChild.childNodes[0];
						try {
							elem.media = "screen";	
						}catch(e){ elem = createStyle(this); } // not good IE 6.0.3790
					} else {
						var elem = this.cloneNode(true);
						elem.media = "screen";
					}
					if (elem) {
						this.parentNode.insertBefore(elem, this);
						Print.added.push([elem]);
					}
				}
			);
		}
		this.addPrintButton();
	},
	swithToNormalLayout: function(){
		//window.switchLayout = true;
		this.removePrintButton();
		if (!use_ex()) {
			for (i in this.added) {$(this.added[i]).remove();}
			this.added = [];
			// begin for IE
			if ($.browser.msie) {
				$("style,link[@rel='stylesheet']").not("[@media='print']").each(
					function() {this.disabled = true; this.disabled = false;}
				);
			}
			// end for IE
		} else if (has_pvs()) {
			var path = location.href;
			location.href = path.replace(/\/printversion$/i,'');
		}
	}
};

