/**
 * @author micromongo
 */
var TextBox = Class.create();
TextBox.prototype = {
	initialize: function(htmlclass, pattern, reverse){
		obj = this;
		random = Math.random() * 100;
		this.random = random;
		template = new Template(pattern);
		$$('.'+htmlclass).each(function(e) {
			control = e.next();
			control.hide();
			control.id = "c_" + random;
			e.id = "p_" + random;
			e.hide;
			e.childElements().each(function(e,i) {
				id = 'p_' + i + '_' + random;
				e.id = id;
				e.hide();
				
				vars = { htmlclass: '', htmlid: "c_" + i + '_' + random , title: i + 1};
				tmp = template.evaluate(vars);
				if(reverse) {
					control.insert({
						top: tmp
					});
				}
				else {
					control.insert(tmp, {
						position: 'after'
					});
				}
				
				control.childElements().each(function(x) {
					x.observe('click', function(event){
						obj.display(Event.element(event).innerHTML -1);
					});	
				});	
				if(control.childElements().length > 1) {
					control.show();
				}
			});
			control.insert('<div class="clear"></div>', {
				position: 'after'
			});
			
		});
		
		this.current = 0;
		this.display(this.current);
		return false;
	},
	
	display: function(id) {
		oldtext = $('p_' + this.current + "_"+ this.random);
		oldtext.hide();
		oldcontrol = $('c_' + this.current + "_"+ this.random);
		oldcontrol.removeClassName("on");
		
		newtext = $('p_' + id + "_"+ this.random);
		newtext.show();
		newtext = $('c_' + id + "_"+ this.random);
		newtext.addClassName("on");
		this.current = id;
	}
	
}