var TextRender = {
	
	uid: 0,
	lib: "",
	settings: new Object(),
	
	library: function(path) {
		this.lib = path;
	},
	
	define: function(tag, style, propset) {
		tag = tag.toUpperCase();
		if (!this.settings[tag]) {
			this.settings[tag] = new Object();
		}
		this.settings[tag][style] = propset;
	},
	
	build: function() {
		for(var tag in this.settings) {
			var rels = document.getElementsByTagName(tag);
			for (var i = 0; i < rels.length; i++) {
				var clName = BrowserCheck.isIE() ? "className" : "class";
				var style = String(rels[i].getAttribute(clName));
				if (style == "null") { //fix IE8
					var style = String(rels[i].getAttribute("class"));
				}
				var propset = this.settings[tag][style];
				if (propset)
					this._transform(rels[i], propset);
			}
			this.settings[tag] = null;
		}
		this.reset();
	},
	
	resize: function(id, h) {
		document.getElementById(id).style.height = parseInt(h) + "px";
	},
	
	resizeInline: function(id, w, h) {
		document.getElementById(id).style.width = parseInt(w) + "px";
		document.getElementById(id).style.height = parseInt(h) + "px";
	},
	
	reset: function() {
		this.settings = new Object();
	},
	
	// private
	_removeChilds: function(obj) {
		var kids = obj.childNodes;
		for (j = 0; j < kids.length; j++) {
		    obj.removeChild(kids[j]);
		}
	},
	
	_nextUid: function() {
		return this.uid++;
	},
	
	_transform: function(obj, propset) {
		var html = obj.innerHTML;
		var link = obj.getAttribute("href");
		var onclick = obj.getAttribute("onclick");
		this._removeChilds(obj);
		if (obj.nodeName.toLowerCase() == "a") {
			var span = document.createElement("SPAN");
			obj.parentNode.insertBefore(span, obj);
			//obj.parentNode.removeChild(obj);
			obj.style.display = "none";
			obj = span;
		}
		var div = document.createElement("DIV");
	//	var uid = "textrender-" + this._nextUid();
		var uid = "textrender" + this._nextUid();
		div.setAttribute("id", uid);
		div.style.display = "none";
		obj.appendChild(div);
		this._append(uid, propset, html, link, onclick);
		div.style.display = "block";
	},
	
	_append: function(uid, propset, html, link, onclick) {
		var font = this.lib + propset.font;
		var bg = "#ffffff";
		var transparent = true;
		if (propset.background) {
			bg = propset.background;
			transparent = false;
		}
	//	var so = new SWFObject(font, "tfobj-" + uid, (!propset.inline ? String(propset.width) : "100%"), "100%", "8", bg);
		var so = new SWFObject(font, "tfobj" + uid, (!propset.inline ? String(propset.width) : "100%"), "100%", "8", bg);
		so.addParam("menu", "false");
		so.addParam("scale", "noScale");
		if (transparent) {
			so.addParam("wmode", "transparent");
		}
		so.addVariable("uid", uid);
		so.addVariable("text", this._convert_special_chars(html));
		so.addVariable("width", propset.width);
		so.addVariable("size", propset.size);
		so.addVariable("color", propset.color);
		so.addVariable("align", propset.align);
		// custom
		propset.background ? so.addVariable("background", propset.background) : null;
		propset.colorOver ? so.addVariable("colorOver", propset.colorOver) : null;
		propset.inline ? so.addVariable("inline", propset.inline) : null;
		propset.linespace ? so.addVariable("linespace", propset.linespace) : null;
		propset.lock ? so.addVariable("lock", propset.lock) : null;
		propset.underline ? so.addVariable("underline", propset.underline) : null;
		propset.rotation ? so.addVariable("rotation", propset.rotation) : null;
		propset.gradient ? so.addVariable("gradient", propset.gradient) : null;
		propset.is_link ? so.addVariable("is_link", propset.is_link) : null;
	 	link ? so.addVariable("link", link) : null;
		onclick ? so.addVariable("onclick", onclick) : null;
		so.write(uid);
	},

	_convert_special_chars: function(html) {
		html = html.replace(/&amp;/g, escape("&#38;") );
		html = html.replace(/"/g,     escape("&#22;") );
		html = html.replace(/\+/g,    escape("&#43;") );
		return html;
	}
	
}

var BrowserCheck = {
	
	isIE: function() {
		return this._test(/MSIE/);
	},
	
	// private
	_test: function(t) {
		var na = navigator.userAgent;
		return t.test(na);
	}
	
}