NMT_FrmMagic = {
	 imgIndicator: '/media/icons/indicator.gif'
	,imgError: '/media/icons/delete2.gif'
	,fromPage: ''
	
	,init: function()
	{
		if ('string' == typeof(NMT_FrmMagic.imgIndicator))
		{
	    NMT_FrmMagic.imgIndicator = new Element('img', {src: NMT_FrmMagic.imgIndicator, style: 'vertical-align: bottom; padding-left: 10px;'});
	    NMT_FrmMagic.imgError = new Element('img', {src: NMT_FrmMagic.imgError, style: 'vertical-align: bottom; padding-left: 10px;'});
		}
	}
};

window.addEvent('domready', function()
{
	NMT_FrmMagic.init();
	
	$$('.frm_require').addEvents({
		change: function()
		{
			if (this.errorImg)
			{
				if (this.errorImg.getParent())
				{
					this.errorImg.remove();
				}
			}
		}
		,focus: function()
		{
			if (this.hasClass('frm_error'))
			{
				this.value = '';
				this.removeClass('frm_error');	
			}
		}
	});
	
	$$('form').each(function(el)
	{
		if (el.preppedMagic) return; el.preppedMagic = true;
		els = el.getElements('.frm_require');
		if (0 == els.length)
		{
			return;
		}
	
		el.action = '/plugin/de.newmedia-tech/ajax/frmMagic.php';
		el.btnSubmit = el.getElement('input[type=submit]');
		if (!el.btnSubmit) el.btnSubmit = el.getElement('input[type=image]');
		el.errorImgs = [];
		
		el.Container1 = new Element('div', {style: 'overflow: hidden;'});
		el.Container2 = new Element('div', {style: 'display: none;'});
		
		el.Fx = new Fx.Styles(el.Container1);
		
		el.Container1.injectBefore(el);
		el.Container2.injectBefore(el);
		
		el.Container1.adopt(el);
		
		var comeFrom = new Element('input', {
			 type: 'hidden'
			,value: NMT_FrmMagic.fromPage
			,name: 'from'
		});
		comeFrom.injectBefore(el.btnSubmit);
		
		el.parseRespone = function(resp)
		{
			var resp = Json.evaluate(resp);
			if (resp.debug) console.log(resp.debug);
			
			this.Indicator.remove();
	
			this.btnSubmit.value = this.btnSubmit.oldVlaue;
			this.btnSubmit.disabled = false;
			
			switch (resp.status)
			{
				case 'error' :
					resp.errors.each(function(el)
					{
						el = $(el);
						el.addClass('frm_error');
            if (el.value == '')
            {
              el.value = 'Bitte ausfüllen';
            }
						
//						var img = NMT_FrmMagic.imgError.clone();
//						img.injectAfter(el);
//						this.errorImgs.push(img);
//						el.errorImg = img;
						
					}.bind(this));
          
          if (resp.contents && resp.contents.lenght > 0)
          {
            resp.contents.each(function(cnt)
            {
              if ((el = $(cnt.id)))
              {
                el.setHTML(cnt.content);
              }
            });
          }
					break;
				case 'ok' :
				
					this.Fx.stop().start({
						 height: 0
						,opacity: 0
					}).chain(function()
					{
						this.frm.Container2.adopt(this);
						this.frm.Container1.empty();
						var Msg = new Element('div', {style: 'color: green; text-align: center; padding: 20px;'})
						Msg.setHTML(this.resp.msg);
						this.frm.Container1.adopt(Msg);
						
						
						this.frm.Fx.stop().start({
							 height: this.frm.Container1.scrollHeight
							,opacity: 1
						}).chain(function()
						{
							if (resp.opt_stayopen)
							{
								return;
							}
							
							if (resp.opt_goto)
							{
								window.setTimeout(function(){
									document.location.href = this.to;
								}.bind({to: resp.opt_goto}), 2000);
								return;
							}
							
							window.setTimeout(function()
							{
								this.Fx.stop().start({
									 height: 0
									,opacity: 0
								}).chain(function()
								{
									this.Container1.empty();
									this.Container1.adopt(this);
									this.Fx.stop().start({
										 height: this.Container1.scrollHeight
										,opacity: 1
									});
								}.bind(this));
							}.bind(this.frm), 5000);
						}.bind({frm: this.frm, resp: resp}));
					}.bind({frm: this, resp: resp}));
					break;
			}
		};
		
		el.addEvents({
			hide: function()
			{
				this.Fx.stop().start({
					 height: 0
					,opacity: 0
				});
			}.bind(el)
			
			,show: function()
			{
				this.Fx.stop().start({
					 height: this.Container1.scrollHeight
					,opacity: 1
				});
			}.bind(el)
			
			,submit: function(e)
			{
				e.stop();
				
        this.btnSubmit.oldVlaue = this.btnSubmit.value;
        this.btnSubmit.value = 'Daten werden gesendet';
        this.btnSubmit.disabled = true;
				
				this.Indicator = NMT_FrmMagic.imgIndicator.clone();
				this.Indicator.injectAfter(this.btnSubmit);
				this.submitStarted = new Date();
				
				try
				{
	        this.errorImgs.each(function(el)
	        {
	          if (el.getParent())
	          {
	            el.remove();
	          }
	        });
				}
				catch (e)
				{
					
				}
				
				this.send({
					onComplete: function(resp)
					{
						var shallPass = 1500;
						var timePassed = new Date() - this.submitStarted;
						var timeLeft = shallPass - timePassed;
						if (timeLeft > 0)
						{
							window.setTimeout(function()
							{
								this.frm.parseRespone(this.resp);
							}.bind({resp: resp, frm: this}), timeLeft);
						}
						else
						{
							this.parseRespone(resp)
						}
					}.bind(this)
				});
			}.bindWithEvent(el)
		});
	});
});