var FormSendToFriend = new Class({
	
	Implements: [Options, Events],
	
	options: {
		onSend				:$empty,
		
		invalidClass		: "invalid",
		nameErrorText		: "El nombre es requerido",
		emailInvalidText	: "El email es incorrecto",
		emailRequiredText	: "El email es requerido"
	},
	
	ajaxProperties: {
		mailListProperty: "EMAIL_LIST",
		nameProperty	: "EMAIL_FROM_NAME",
		messageProperty : "EMAIL_MESSAGE"
	},
	
	mailFields: [],
	
	initialize: function()
	{
		this.setUp();
	},
	
	setUp: function()
	{
		this.bus 			= new SearchBus();
		this.formSentFeedback = new Element("div", {text:"Email enviado correctamente"});
		this.wrapper		= new Element("div", {"class":"formSendTofriend"});
		this.namelabel		= new Element("label", {text:"Tu nombre"}).inject(this.wrapper);
		this.name			= new Element("input", {type:"text"}).inject(this.wrapper);
		this.nameError		= new Element("span", {text:this.options.nameErrorText, "class":"error"});
		this.name.addEvent("keyup", this.validateName.bind(this));

		this.messagelabel	= new Element("label", {text:"Tu mensaje"}).inject(this.wrapper);
		this.message		= new Element("textarea").inject(this.wrapper);
		this.message.resizable();
		
		this.mails 			= new Element("div", {"class":"mailsContainer", html:"<strong>Enviar a los siguientes e-mails:</strong>"}).inject(this.wrapper);
		this.emailError		= new Element("span", {text:this.options.emailRequiredText, "class":"error"});
		
		this.buttonMore 	= new Element("a", {"href":"#", "class":"moreMails", text:"+ añadir otro correo electrónico"}).inject(this.wrapper);
		this.buttonMore.addEvent("click", function(event){event.stop()});
		this.buttonMore.addEvent("click", this.createMailField.bind(this));
		
		this.createMailField();
		
		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));
	},
	
	createMailField: function()
	{
		var fieldContainer 		= new Element("div", {"class":"mailFieldContainer"}).inject(this.mails);
		
		
		fieldContainer.error	= new Element("span", {text:this.options.emailInvalidText, "class":"error"});
		fieldContainer.input 	= new Element("input", {type:"text"}).inject(fieldContainer);
		fieldContainer.input.focus();
		
		fieldContainer.input.addEvent("keyup", this.validateMails.bind(this));
		
		var removeButton 		= new Element("a", {href:"#", text:"[x]"}).inject(fieldContainer);
		removeButton.addEvent("click", function(event){event.stop();});
		
		removeButton.addEvent("click", this.removeMailField.bind(this, [fieldContainer]))
		
		this.mailFields.push(fieldContainer);
	},
	
	resetMailField: function()
	{
		this.mailFields.each(function(field){field.destroy()});
		this.mailFields = [];
		this.createMailField();
	},
	
	removeMailField: function(field)
	{
		if(this.mailFields.length == 1)
		{
			return;
		}
		
		field.destroy();
		this.mailFields.erase(field);
	},
	
	inject: function(container, where)
	{
		this.wrapper.inject(container, where);
		this.mailFields[0].input.focus();
	},
	
	dispose: function()
	{
		this.wrapper = this.wrapper.dispose();
	},
	
	getValue: function()
	{
		var data 	 	= {};
		data[this.ajaxProperties.mailListProperty]= [];
		data[this.ajaxProperties.nameProperty]		= this.name.get("value");
		data[this.ajaxProperties.messageProperty]	= this.message.get("value");
		
		this.mailFields.each(function(field){
			data[this.ajaxProperties.mailListProperty].push(field.input.get("value"));
		}.bind(this))
		
		return data;
	},
	
	resetValues: function()
	{
	},
	
	send: function()
	{
		if(!this.validate())
		{
			return false;
		}
		
		this.fireEvent("onSend", [this.getValue()]);
	},
	
	
	/*Validation*/
	
	resetValidation: function()
	{
		this.mailFields.each(function(field){
			field.input.removeClass(this.options.invalidClass);
		}.bind(this))
		this.name.removeClass(this.options.invalidClass);
		this.valid = true;
	},
	
	validateName: function()
	{
		this.nameError.dispose();
		if(this.name.get("value") == "")
		{
			this.valid = false;
			this.nameError.inject(this.name,"after");
			this.name.addClass(this.options.invalidClass);
		}
	},
	
	validateMails: function()
	{
		this.emailError.dispose();
		if(this.mailFields.length == 1 && this.mailFields[0].input.get("value") == "")
		{
			this.valid = false;
			this.emailError.inject(this.mailFields[0],"before");
			this.mailFields[0].input.addClass(this.options.invalidClass);
		}
		else
		{
			var allEmpty = true;
			this.mailFields.each(function(field){
				if(field.input.get("value") != "")
				{
					allEmpty = false;
				}
			}.bind(this))
			
			if(allEmpty)
			{
				this.emailError.inject(this.mailFields[0],"before");
			}
		}
		
		
		this.mailFields.each(this.validateEmailField.bind(this))
	},
	
	validateEmailField: function(field){
		
		field.error.dispose();
		
		if(field.input.get("value") != "" && !this.validateEmail(field.input.get("value")))
		{
			this.valid = false;
			field.error.inject(field);
			field.input.addClass(this.options.invalidClass);
		}
	},
	
	validate: function()
	{
		
		this.resetValidation();
		
		this.validateName();
		this.validateMails();
		
		return this.valid;
	},
	
	validateEmail: function(mail)
	{  
		 var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		 return emailPattern.test(mail);  
	},
	
	showSendFeedback: function()
	{
		this.formSentFeedback.inject(this.wrapper);
	},
	
	hideSendFeedback: function()
	{
		this.formSentFeedback.dispose();
	},
	
	loading: function()
	{
		this.wrapper.addClass("loading");
	},
	
	loaded: function()
	{
		this.wrapper.removeClass("loading");
	}
	
})