var printPage = function() {
	var body = printPage.iframe.contentDocument.body;
	var htmlText = "";
	printPage.iframe.contentDocument.title = "page";

	for (var i = 0, max = arguments.length; i < max; i++) {
		var arg = arguments[i];
		if (typeof(arg) == "string") {
			htmlText += arg;
		} else {
			htmlText += "<" + arg.nodeName + ">" + arg.innerHTML + "</" + arg.nodeName + ">";
		}
	}


	body.innerHTML = printPage.printHeader + htmlText + printPage.printFooter;

	/*body.innerHTML = "";

	for (var i = 0, max = arguments.length; i < max; i++) {
		var node = arguments[i];
		var iNode = printPage.iframe.contentDocument.importNode(node, true);
		body.appendChild(iNode);
	}*/

	printPage.iframe.contentWindow.focus();
	printPage.iframe.contentWindow.print();
};

printPage.printHeader = "<div style=\"margin-bottom: 20px;margin-left: 15px;\">«ЛароПак СПб»<br/>СПб, ул. Камчатская, д. 1, тел./факс: 405-75-75<br/>www.laropack.ru</div>";
printPage.printFooter = "<div style=\"margin-top: 20px; text-align: center;\">______________________________________________________<br/>© 2008 ООО &quot;ЛароПак&quot;</div>";

printPage.init = function() {
	printPage.iframe = document.createElement("iframe");
	printPage.iframe.style.width = "1px";
	printPage.iframe.style.height = "1px";
	printPage.iframe.style.border = "none";
	document.body.appendChild(printPage.iframe);

	setTimeout(function() {
		var head = printPage.iframe.contentDocument.body.parentNode.childNodes[0];
		/*var st = printPage.iframe.contentDocument.createElement("style");
		st.type = "text/css";
		var cssText = "body, h1, h2, h3, h4, p, div { font-size: 14px; padding: 0; margin: 0; }\n" +
			".sp-title { font-size: 178px; font-weight: bold; }\n" +
			".sp-meta-title { font-weight: bold; }\n" +
			"table { border-collapse: collapse; border: 1px solid #C0C0C0; font-weight: bold; }\n" +
			"td { border: 1px solid #C0C0C0; }\n" +
			"a {text-decoration: none; color: #000000; }\n" +
			"body > br { display: none; }\n";
		if (st.styleSheet) {
			st.styleSheet.cssText = cssText;
		} else {
			st.innerHTML = cssText;
		}
		head.appendChild(st);
*/


		var styleNode = printPage.iframe.contentDocument.createElement("link");
		styleNode.rel = "stylesheet";
		styleNode.href = "/wp-content/themes/mystique/js/print.css";
		head.appendChild(styleNode);
	}, 1000);
};

printPage.removeNoprintTags = function(expression, tagName) {
	//var re = new RegExp("<" + tagName + ".*?_noprint.*?>.*?<\/" + tagName + ">", "igm");
	var re = new RegExp("<[a-z]+[^<>]*?_noprint[^<>]*?>[^\0]*?<\/[a-z]+>", "img");
	return expression.replace(re, "");
};

var printContactsPage = function() {
	printPage(
		jQuery("#_contacts")[0],
		jQuery("#_map")[0]
	);
};

var printCategoryPage = function() {
	var node1 = jQuery(".list_productdisplay")[0];
	var node2 = jQuery(".price-policy")[0];

	var newTable = node1.cloneNode(true);
	var nodeList = jQuery("td[_noprint=]", newTable).remove();
	//console.log(nodeList);

	//var html1 = "<" + node1.tagName + ">" + printPage.removeNoprintTags(node1.innerHTML) + "</" + node1.tagName + ">";
	var html1 = "<" + node1.tagName + ">" + newTable.innerHTML + "</" + node1.tagName + ">";

	var html1 = replaceAll(html1, "<center>", "");
	var html1 = replaceAll(html1, "</center>", "");
	var html1 = replaceAll(html1, "class=\"product-discount\"", "class=\"product-discount\" width=\"100%\" style=\"text-align: right;\"");
	var html1 = replaceAll(html1, "class=\"wpsc_price_td\"", "class=\"wpsc_price_td\" style=\"text-align: center;\"");
	var html1 = replaceAll(html1, "class=\"oldprice\"", "class=\"oldprice\" style=\"text-decoration: line-through;\"");

 	//alert(res3);
	printPage(
		html1,
		node2
	);
};

function replaceAll(str, what, to) {
   return str.split(what).join(to);
}
