var FormComments = new Class({
	
	Implements: [Options, Events],
	
	options: {
		invalidClass		: "invalid",
		authorDefaultValue	: "autor",
		commentDefaultValue	: "Comentario",
		messageErrorText	: "El comentario es requerido",
		authorErrorText		: "El autor es requerido",
		onSend:$empty
	},
	
	ajaxProperties: {
		author		: "APARTMENT_COMMENT_AUTHOR",
		message		: "APARTMENT_COMMENT_MESSAGE"
	},
	
	initialize: function()
	{
		this.setUp();
	},
	
	setUp: function()
	{
		this.bus = new SearchBus();
		this.wrapper		= new Element("div", {"class":"formComments"});
		this.whoField		= new Element("input", {type:"text", value:this.options.authorDefaultValue}).inject(this.wrapper);
		this.whoField.addEvent("focus", function(){
			if(this.whoField.get("value") == this.options.authorDefaultValue)
			{
				this.whoField.set("value", "");
			}
		}.bind(this))
		
		this.whoField.addEvent("blur", function(){
			if(this.whoField.get("value") == "")
			{
				this.whoField.set("value", this.options.authorDefaultValue);
			}
		}.bind(this))
		
		this.commentField	= new Element("textarea", {value:this.options.commentDefaultValue}).inject(this.wrapper);
		this.commentField.resizable();
		
		this.commentField.addEvent("focus", function(){
			if(this.commentField.get("value") == this.options.commentDefaultValue)
			{
				this.commentField.set("value", "");
			}
		}.bind(this))
		
		this.commentField.addEvent("blur", function(){
			if(this.commentField.get("value") == "")
			{
				this.commentField.set("value", this.options.commentDefaultValue);
			}
		}.bind(this))
		
		this.commentField.addEvent("keyup", this.validateMessage.bind(this));
		this.whoField.addEvent("keyup", this.validateAuthor.bind(this));
		
		this.authorError = new Element("div", {"class":"error", text:this.options.authorErrorText});
		this.messageError = new Element("div", {"class":"error", text:this.options.messageErrorText});
		
		
		this.sendButton		= new Element("a", {href:"#", text:"Enviar", "class":"sendButton"}).inject(this.wrapper);
		this.sendButton.addEvent("click", function(event){event.stop()});
		this.sendButton.addEvent("click", this.send.bind(this));
		
		this.closeButton		= new Element("a", {href:"#", text:"Cerrar", "class":"closeForm"}).inject(this.wrapper);
		this.closeButton.addEvent("click", function(event){event.stop()});
		this.closeButton.addEvent("click", this.dispose.bind(this));
	},
	
	inject: function(container, where)
	{
		this.wrapper.inject(container, where);
	},
	
	dispose: function()
	{
		this.authorError.dispose();
		this.messageError.dispose();
		this.wrapper = this.wrapper.dispose();
	},
	
	validateAuthor: function()
	{
		this.authorError.dispose();
		this.whoField.removeClass(this.options.invalidClass);
		if(this.whoField.get("value") == "" || this.whoField.get("value") == this.options.authorDefaultValue)
		{
			this.authorError.inject(this.whoField, "after");
			this.whoField.addClass(this.options.invalidClass);
			this.valid = false;
		}
	},
	
	validateMessage: function()
	{
		this.messageError.dispose();
		this.commentField.removeClass(this.options.invalidClass);
		if(this.commentField.get("value") == "" || this.commentField.get("value") == this.options.commentDefaultValue)
		{
			this.messageError.inject(this.commentField, "after");
			this.commentField.addClass(this.options.invalidClass);
			this.valid = false;
		}
	},
	
	validate: function()
	{
		this.valid = true;
		
		this.validateAuthor();
		this.validateMessage();
		
		return this.valid;
	},
	
	getValue: function()
	{
		var data 	 = {};
		data[this.ajaxProperties.author] 	= this.whoField.get("value");
		data[this.ajaxProperties.message] 	= this.commentField.get("value");
		return data;
	},
	
	resetValues: function()
	{
		this.whoField.set("value", this.options.authorDefaultValue);
		this.commentField.set("value", this.options.commentDefaultValue);
	},
	
	send: function()
	{
		if(!this.validate())
		{
			return false;
		}
		
		this.fireEvent("onSend", [this.getValue()]);
	}
	
})