var sendToFriends = new Class({
	Implements: Options,
	options:  {
		id:    'sendToFriends',
		check: '',
		ref:   ''
	},
	//Function run when class is created
	initialize : function(options){
		this.setOptions(options);
		this.container = document.id( document.body );
		// Crea Overlay
		this.createOverlay();
		// Crea Form invio dati
		this.createForm();
	},
	
	/**
	 Crea Overlay */
	createOverlay : function(){
		// Istanza
		var olay = new Overlay(document.body , { 
			duration: 300,
			onClick: function() {
				$('overlay').dispose();
				$(this.options.id).dispose();
			}.bind( this )
		});
		// Apre Overlay
		olay.open();
	},
	
	/**
	 Crea Form */
	createForm : function(){
		//Contenitore Form
		this.formId = new Element('div',{
			'id': this.options.id,
			'styles': {
									'position': 'absolute',
									'left': 0,
									'top': 0,
									'z-index': 5001
								}
		}).inject( document.body );

		// HTML Form
		var HTML =  '<div class="friendsContainer">\
									<h1 class="friendsH1">Segnala a un amico</h1><a id="friendsChiudi" href="#">Chiudi</a>\
									<form action="/assets/sendtofriends.php" id="formSendToFriends" method="post"> \
										<label for="nome_tuo">Tuo nome</label>\
									  <input class="text" type="text" name="nome_tuo" id="nome_tuo" value="" />\
										<label for="email_tua">Tua email</label>\
									  <input class="text" type="text" name="email_tua" id="email_tua" value="" />\
									  <label for="nome_amico">Nome amico</label>\
									  <input class="text" type="text" name="nome_amico" id="nome_amico" value="" />\
										<label for="email_amico">Mail Amico</label>\
									  <input class="text" type="text" name="email_amico" id="email_amico" value="" />\
									  <input type="hidden" name="ref" id="ref" value="' + this.options.ref + '" />\
									  <input type="hidden" name="check" id="check" value="' + this.options.check + '" />\
									  <br />\
									  <p id="friends_feed"></p>\
									  <p class="submit">\
									  <button id="invita_amico" type="submit">Invia</button>\
									  </p>\
									 </form>\
								</div>';
		
		// Inserisce HTML
		this.formId.set("html", HTML);
		
		// Comportamenti
		$('friendsChiudi').addEvent('click', function(event) {
			event.stop();
			this.removeForm();
		}.bind(this));
		
		// Submit Form
		$('formSendToFriends').addEvent('submit', function(e) {
			e.stop();
			var log = $('friends_feed').empty().set("html", "invio in corso...");
			// Data
			var n1 = $('nome_tuo').get('value').trim();
			var n2 = $('nome_amico').get('value').trim();
			var e1 = $('email_tua').get('value').trim();
			var e2 = $('email_amico').get('value').trim();

			// Invia form dopo verifica
			if( n1 != "" && n2 != "" && e1 != "" && e2 != ""){
				// Response
				this.set('send', {onComplete: function(response) {
					if( response == true || response == "true" ){
						log.set('html', "<span class=\"green\">Segnalazione inviata correttamente</span>");
						// Reset Values
						$('nome_tuo').set("value","");
						$('nome_amico').set("value","");
						$('email_tua').set("value","");
						$('email_amico').set("value","");
					}else{
						log.set('html', "<span class=\"red\">Ops, si è verificato un errore. Riprova.</span>");
					}
				}});
				//Send the form.
				this.send();
			}else{
				// Messaggio di mancata compilazione campi
				var log = $('friends_feed').empty().set("html", "<span class=\"red\">Compila tutti i campi.</span>");
			}
		});
	},
	
	/**
	 Elimina Overlay e Form */
	removeForm : function(){
		$('overlay').dispose();
		$(this.options.id).dispose();
	}
});
