/*
 * JavaScript Components Beta
 * http://dewcms.com/
 *
 * Copyright 2010, dewenir
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.dewenir.es/license
 *
 * Date: Sat Feb 13 22:33:48 2010 -0500
 */
var componenteRegistro = {
		cssRegistrate: '.registrate',
		cssIgnoreAjax: 'ignoreAjax',
		idRespuesta: '#panel-mensaje-sistema',
		cssRespuestaValidacion: '.respuesta-servidor',
		cssFormularioDeRegistro: 'formularioDeRegistro',
		isFormlarioCargado: false,
		init: function(){
			this.registrate = $('#header ' + this.cssRegistrate);
			if(this.registrate.size() <=0 ) return;
			this.config();
			this.crearContenedorFormulario();
			this.enganchar();
		},
		config: function(){
			return;
		},
		loadingTemplate: function(){
			var html = '';
			html += "<p class=\"loadingGaleria\">waiting flickr gallery, please wait...</p>\n";
			return html;
		},
		comportamientoClick: function(enlace){
			this.formularioRegistro.is(':visible')? this.formularioRegistro.hide() : this.formularioRegistro.show();
			if(!this.isFormlarioCargado) this.peticion(enlace); 
		},
		crearContenedorFormulario: function(){
			this.registrate.parent().append('<div class=\"' + this.cssFormularioDeRegistro + '\"><p class=\"loading\">Formulario de registro...</p></div>');
			this.formularioRegistro = $('.' + this.cssFormularioDeRegistro);
			this.formularioRegistro.hide();
			return;
		},
		enganchar: function(){
			var that = this;
			$(this.registrate).find('a').each(function(){
				var enlace = $(this);
				if(enlace.hasClass(that.cssIgnoreAjax)) return;
				enlace.bind('click', function(evt){
					that.registrate.hide();
					that.comportamientoClick(evt);
					return false;
				});
			});
			return;		
		},
		contenedorGaleria: function(){
			var html = '';
			html += '<div class=\"galeria-flickr _flickr-inicio"\><\/div>';
			return html;
		},
		engancharSubmit: function(){
			var that = this;
			this.formulario = this.formularioRegistro.find('form');
			this.submit = this.formulario.find('input[type=submit]');
			this.submit.bind('click', function(evt){
				that.ejecutar();
				return false;
			});
			return;
		},
		engancharCancel: function(){
			var that = this;
			this.cancel = this.formulario.find('p a');
			this.cancel.bind('click', function(evt){
				that.formularioRegistro.hide();
				that.registrate.show();
				return false;
			});
			return;			
		},
		peticion: function(enlace){		
			var that = this;
			var action = $(enlace.target).attr('href');
			$.ajax({
				type: "POST",
			   	url: action,
			   	dataType: "html",
			   	success: function(respuesta){
					that.isFormlarioCargado = true;
					var respuesta = $(respuesta);
					var formulario = respuesta.find('#panel-formularios');
					that.formularioRegistro.html(formulario);
					that.engancharSubmit();
					that.engancharCancel();
					return;
			   	},
			   	error: function (XMLHttpRequest, textStatus, errorThrown) {
			   		//$("#error").html(XMLHttpRequest.responseText);
				   alert('lo sentimos. Ha habido un error en el servidor ' + textStatus);
			   	}
			});
		},	
		ejecutar: function(event){		
			var that = this;	
			var action = this.formulario.attr('action');
			var inputs = this.formulario.serialize();
			$.ajax({
				type: "POST",
			   	url: action,
			   	data: inputs,
			   	dataType: "html",
			   	success: function(respuesta){
					var $respuesta = $(respuesta);
					var idServidor = $respuesta.find('input#respuesta').val();
					var html = $respuesta.find(that.idRespuesta).html();
					if(idServidor != 4){
						that.formularioRegistro.html(html);
						that.isFormlarioCargado = false;
						setTimeout(function(){
							that.formularioRegistro.slideUp(200, function(){
								that.formularioRegistro.html('');
								that.registrate.show();								
							});
						}, 4000);						
					}else{
						var capaMensajes = that.formularioRegistro.find(that.cssRespuestaValidacion);
						capaMensajes.remove();
						that.formularioRegistro.prepend(html); 
					}
					return;
			   	},
			   	error: function (XMLHttpRequest, textStatus, errorThrown) {
			   		//$("#error").html(XMLHttpRequest.responseText);
				   alert('lo sentimos. Ha habido un error en el servidor ' + textStatus);
			   	}
			});					
		}		
	};	
