/**
 * Contact and subscribe form class
 */
ContactForm = new Class({

	Implements: Options,	
		
	Binds: [
		'formListener',
		'successListener',
		'failureListener'
	],

	className: 'ContactForm',

	options: {
		moduleServiceURL: '/controller/contact.controller.php',
		request: 'request-form'
	},

	/**
	 * @constructor
	 */
	initialize:function(element, options) {
		
		this.setOptions(options);
		
		this.requests = [];
		this.busy = new Busy();

		this.form = element;
		this.initializeForm();				

	},
	
	initializeForm: function(){
		this.form.validator = new Form.Validator.Inline(this.form, 
        	{
				stopOnFailure: false,
		      	useTitles: true,
		      	errorPrefix: "",
		
		      	onElementPass: function(element) {
		         	//console.log(element);
					element.getParent().addClass("validated");
		      	},
		      	onElementFail: function(element){
		      		element.getParent().removeClass("validated");
		      	}
		      	
        	}
        ); 
        
        this.form.getElements('a').each(function(button){
			//console.log(button);
			button.addEvent('click',this.formListener);
		},this);

	
	},
	
	// this function submits a type that is the class of the element pressed, and sends all of the form data off with the request
	formListener: function(event) {
		
		event.preventDefault();

    	if(this.form.validator.validate()){
    		// parse form
    		
    		formFields = {};
    		this.form.getElements('input,select').each(function(input){
	    		formFields[input.get('name')] = input.get('value');
	    		//console.log(input);	        	
	    	});
	    	this.form.getElements('textarea').each(function(input){
	    		formFields[input.get('name')] = input.get('value');    	
	    	});
	    	request = new Request.JSON({
	        	url: this.options.moduleServiceURL,
	        	onSuccess: this.successListener,
	        	onFailure: this.failureListener,
	        	data: JSON.encode({
	        		'data': formFields,
	        		'type': event.target.get('class')
	           	})
	        }, this);
    		this.busy.lookBusy();
    		request.send();
    	} 
		
	},
	
	successListener:function(response) {
		
		switch(response.type) {

			case ContactForm.responseTypes.REPLACE:
				
				this.busy.relax();
				var markup = Elements.from(response.data.html, true)[0];
				
	    		fx = new Fx.Reveal(this.form);
	    		fx.dissolve().chain(function() {
	    			markup.inject(this.form,'before');
	    			markup.hide();
	    			markup.reveal();
	    			
	    			this.form.destroy();
	    			this.form = markup;
					
					this.initializeForm();  
			    
		        }.bind(this));
	
				
			break;
			
			case ContactForm.responseTypes.NOOP:
				if(response.msg.html){
					markup = Elements.from(response.msg.html, true)[0];
					markup.inject(this.form,'top');
					markup.hide();
					this.message = markup;
				}else{
					this.message.set('text', response.msg.data);
				}
				this.message.reveal();
				//console.log(response.msg.data);
				this.busy.relax();
				//this.requests.sendListener.dealloc();
				//this.requests.sendListener = null;
	
			break;
			
			case ContactForm.responseTypes.REDIRECT:
				window.location = response.data.url;
			break;
			
			
			
			
		}
		
		
		


		
		
		
	},
	
	failureListener:function() {

		this.busy.relax();
		alert('There was an error sending the form. Please try again.');

	}
});

$extend(ContactForm, {

	requestTypes: {
		FORM:'request-form'
	},
	responseTypes: {
		REPLACE:'replace',
		NOOP:'noop',
		REDIRECT:'redirect'
	},

	CSSClass: 'form'
});
