/**
	Auto Zoom Extension for Google Chrome by Szsz.pl

	Copyright (c) 2009,2010 Szsz.pl All rights reserved
	Use of this source code is governed by a BSD-style license
	that can be found in the LICENSE file.
*/

var zoomFixName = "Gmail Fix";
var zoomFix = (function() {

	var zoom, ctl, frame;
	var node_list, i;

	var setZoom = function () {

		/* init */
		if(!ctl) {
			if(!frame) frame = document.getElementById("canvas_frame");
			if(!frame || !frame.contentDocument || !frame.contentDocument.head) {
				setTimeout(setZoom, 20);
				return;
			}

			ctl = frame.contentDocument.createElement("style");
			frame.contentDocument.head.insertBefore(ctl);
			frame.contentDocument.body.addEventListener("DOMSubtreeModified", function(e) {
				if (zoom && e.srcElement.nodeName == "IFRAME")
					e.srcElement.contentDocument.body.style.zoom = zoom + "%";
			}, false);
		}

		if(zoom) {
			ctl.innerText = [
					"div[class=\"ii gt\"]",		/* body (text)	*/
					"textarea[name=body]",		/* reply (text)	*/
				].join(", ") + " {font-size:" + zoom + "%}\n" +
				[
					"table.F",			/* mail list	*/
					"table[class=\"cf gJ\"]",	/* from		*/
					"div.if h1.ha",			/* subject	*/
				].join(", ") +	" {zoom:" + zoom + "%}\n" +
					"div[class=\"ii gt\"] > *" +	/* body (html)	*/
					" {zoom:" + zoom + "%; font-size:" + (10000/zoom) + "%}\n"


			/* reply (html) */
			node_list = frame.contentDocument.getElementsByTagName("iframe");
			for(i=0; i<node_list.length; i++) {
				node_list[i].contentDocument.body.style.zoom = zoom + "%";
			}
			node_list = null;

		} else {
			ctl.innerText = "";

			node_list = frame.contentDocument.getElementsByTagName("iframe");
			for(i=0; i<node_list.length; i++) {
				node_list[i].contentDocument.body.style.zoom = null;
			}
			node_list = null;
		}
	}

	return function (z) {
		zoom = z;
		setZoom();
	}
	

}());

var zoomTest = (function() {

	var ctl;

	return function(css) {

		if(!ctl) {
			var content = document.getElementById("canvas_frame").contentDocument;
			content.head.insertBefore( ctl = content.createElement("style") );
		}

		ctl.innerText = css;
		console.warn(ctl, css);
	}

}());
