/*
* 3rd Party
*/

/*
---
description: Overlay

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1: '*'

provides:
  - Overlay
...
*/

var Overlay = new Class({
	
	Implements: [Options, Events],
	
	options: {
		id: 'overlay',
		color: '#000',
		duration: 500,
		opacity: 0.6,
		zIndex: 5000
	},
	
	initialize: function(container, options){
		this.setOptions(options);
		this.container = document.id(container);
		if (Browser.Engine.trident && Browser.Engine.version <= 6) this.ie6 = true;
		
		this.bound = {
			'window': {
				resize: this.resize.bind(this),
				scroll: this.scroll.bind(this)
			},			
			overlayClick: this.overlayClick.bind(this),
			tweenStart: this.tweenStart.bind(this),
			tweenComplete: this.tweenComplete.bind(this)	  
		};
		
		this.build().attach();
	},
	
	build: function(){
	  this.overlay = new Element('div', {
			id: this.options.id,
			opacity: 0,
			styles: {
				position: (this.ie6) ? 'absolute' : 'fixed',
				background: this.options.color,
				left: 0,
				top: 0,
				'z-index': this.options.zIndex
			}
		}).inject(this.container);
		this.tween = new Fx.Tween(this.overlay, { 
			duration: this.options.duration,
			link: 'cancel',
			property: 'opacity'
		});
	 return this;
	}.protect(),
	
	attach: function(){
		window.addEvents(this.bound.window);
		this.overlay.addEvent('click', this.bound.overlayClick);
		this.tween.addEvents({
			onStart: this.bound.tweenStart,
			onComplete: this.bound.tweenComplete
		});
	 return this;
	},
	
	detach: function(){
		var args = Array.prototype.slice.call(arguments);
		args.each(function(item){
			if(item == 'window') window.removeEvents(this.bound.window);
			if(item == 'overlay') this.overlay.removeEvent('click', this.bound.overlayClick);
		}, this);
		return this;
	},
	
	overlayClick: function(){
		this.fireEvent('click');
		return this;
	},
	
	tweenStart: function(){
		this.overlay.setStyles({
			width: '100%',
			height: this.container.getScrollSize().y
		});
	 return this;
	},
	
	tweenComplete: function(){
		this.fireEvent(this.overlay.get('opacity') == this.options.opacity ? 'show' : 'hide');
		return this;
	},
	
	open: function(){
		this.fireEvent('open');
		this.tween.start(this.options.opacity);
		return this;
	},
	
	close: function(){
		this.fireEvent('close');
		this.tween.start(0);
		return this;
	},
	
	resize: function(){
		this.fireEvent('resize');
		this.overlay.setStyle('height', this.container.getScrollSize().y);
		return this;
	},
	
	scroll: function(){
		this.fireEvent('scroll');
		if (this.ie6) this.overlay.setStyle('left', window.getScroll().x);
		return this;
	}
});

/*
 * Clarity - Copyright 2010
 * ckaiser@clarity.com.ar - info@ckaiser.com.ar
*/

var closeForm = function() {
    var form = $('form-container');
    form.get('tween').start('top', -530).chain(function() { form.setStyle('display', 'none'); });

    $('form-respuesta').setStyle('visibility', 'hidden').set('html', '');
}

var openForm = function() {
    var form = $('form-container');
	
    form.setStyle('display', 'block');

    form.set('tween', {duration: 600});    
    form.tween('top', 0);
}

var overlay = false;

window.addEvent('domready', function () {
    overlay = new Overlay($(document.body), {
     duration: 500,
     opacity: 0.7,
     onClick: function() {
       closeForm();
       this.close();
     },
     onShow: function() {
       new Fx.Scroll(document.body).toTop().chain(function () {
         openForm();
       });
     }
    });

  $('contacto').addEvent('click', function(e) {
    overlay.open();

    e.stop();
    return false;
  });

  $('cancel').addEvent('click', function(e) {
    closeForm();
    overlay.close();
    e.stop();
    return false;
  });

  $('form').addEvent('submit', function(e) {
		e.stop();

		var log = $('form-respuesta').setStyles({'visibility': 'visible'}).set('html', 'Enviando..').addClass('ajax-loading');

		this.set('send', {
			onComplete: function(response) {
				new Fx.Scroll(document.body).toTop();

					  log.removeClass('ajax-loading');
					  log.set('html', response);
				
				$('form-container').tween('top', -460);
				
				(function() {
				  overlay.close();
				  $('submit').set('disabled', false).set('text', 'Enviar');
				  closeForm(); 
				}).delay(4000);
			}
		});
    
		$('submit').set('disabled', true).set('text', 'Enviado');
		
		var error = false;
		
		if (!$('Email').get('value')
		 && !$('Telefono').get('value'))
		{
		  error = 'Debe especificar algun modo de contacto,<br>Telefono o E-Mail.';
		}

		if (!error && !(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i).test($('Email').get('value')) )
		{
		  error = 'El E-Mail especificado es inválido.';
		}
		
		if (error) {
		  log.set('html', error);
		  
		  //F52C44
		  var borderColor = log.getStyle('border-color');
		  log.get('tween').start('border-color', '#ff0000').chain(function() { log.tween('border-color', borderColor) });

		  $('submit').set('disabled', false).set('text', 'Enviar');
		  return false;
		}

		this.send();
	});

});

