var TextImage = new Class({
	initialize: function(element, options){
		this.setOptions({
			"path":"/textimage/TextImage.ashx?",
			"font-family":"gothicbold.ttf",
			"font-size":null,
			"font-weight":"bold",
			"font-style":"regular",
			"text-align":null,
			"color":"#000000",
			"background-color":"#FFFFFF",
			"width":0,
			"height":0,
			"autosize":true,
			"antialias":true,
			"transparent":true
		}, options);

		$ES("html").addClass("textImage-enabled");

		this.path = this.options.path;
		delete this.options.path;
		
		this.autoFontSize = true;
		if(this.options["font-size"])
			this.autoFontSize = false;
		
		if($type(element) == "string"){
			window.addEvent("domready",function(){
				$$(element).each(this.replace.bind(this));
			}.bind(this));
		}
	},
	
	replace: function(element){
		if(element.hasClass("textImage-replaced")) return;
				
		this.text = "";
		this.isImage = false;
			
		if(element.getTag() == "img"){
			this.text = element.getAttribute("alt");
			this.isImage = true;
		} else {
			this.text = escape(element.innerHTML);
			this.alt = element.getText();
		}
		
		if(!this.options["text-align"])
			this.options["text-align"] = element.getStyle("text-align");
		
		if(this.autoFontSize)
			this.options["font-size"] = element.getStyle("font-size").toFloat();
		
		if(this.options.autosize){
			this.containerSize = element.getSize();
			
			this.pt = element.getStyle("padding-top").toInt();
			this.pr = element.getStyle("padding-right").toInt();
			this.pb = element.getStyle("padding-bottom").toInt();
			this.pl = element.getStyle("padding-left").toInt();

			this.options.width = (this.containerSize.size.x - this.pl - this.pr);
			this.options.height = (this.containerSize.size.y - this.pt - this.pb);
		}

		var qs = Object.toQueryString(this.options);
		qs += "&text=" + this.text;
		
		if(this.isImage){
			element.src = this.path + qs;
		} else {
			element.setText("");
			new Asset.image(this.path + qs,{"alt":this.alt}).injectInside(element);
		}
		
		element.addClass("textImage-replaced");
	}
});

TextImage.implement(new Options);
