/////////////////////////////////
///// GESTIONAIR APPLICATION
/////////////////////////////////


/// Depends on :
///   mootools Core (1.2.4)

// Namespace
var GESTIONAIR = {
	version:"1.0.0_alpha",
	config:{}
};
GESTIONAIR.components={};

////////////////////////////////////////////////////////////////////////////////
///// INIT APPLICATION
////////////////////////////////////////////////////////////////////////////////

GESTIONAIR.initApplication = function() {

/////// CONTACT FORM	
	
	// Init Form Contact
	if($('form_send_mail')){
		new GESTIONAIR.components.ContactForm({
			name:"Contact Form",
			id:"form_send_mail"
		});
	};

	// Init Form Friend
	if($('slide')){
		new GESTIONAIR.components.Slide({
			name:"Slide",
			id:"slide"
		});
	};

};

////// DOMREADY
window.addEvent('domready', GESTIONAIR.initApplication);


////////////////////////////////////////////////////////////////////////////////
///// CLASS COMPONENTS
////////////////////////////////////////////////////////////////////////////////

//---------------------------------------------------
// Class component ContactForm
GESTIONAIR.components.ContactForm = new Class({
    initialize: function(config){
	    this.id = config.id?config.id:"";
	    this.name = config.name?config.name:"";
	    
	    $('form_send_mail').addEvent('submit', this.submit);

	    var _component = this ; // this = Class instance
	    
    }, // initialize

    submit: function(e){
    	
    	// Stop other actions
    	new Event(e).stop();
    	
    	// Check the required input (with class="required")
    	var check_required = null;
    	$$('#form_send_mail input[class*="required"]').each(function(el){
    		if(el.get('value')==''){
    			el.setStyle('border','1px solid #990000');
    			check_required = 1 ;
    		} else {
    			el.setStyle('border','1px solid #97Bf0D');
    		}
    	});
    	
    	// Submit form if required input are submitted
    	if(check_required == null){

    		// Config Ajax request from form (url = action)
    		this.set('send', { 
    			onRequest: function(response) {
    				$('log').removeClass('log_load').addClass('log_error').empty().set('html','<p>Message en cours de vérification.</p>');	
    			},
    			onComplete: function(response) {
    				$('log').removeClass('log_load').set('html',response);
    			},
    			onFailure: function(response) { 
    				$('log').removeClass('log_load').addClass('log_error').empty().set('html','<p>Le serveur ne répond pas.</p>');
    			}	
    		});
    		// Send Ajax
    		this.send();

    	}
    } // submit	
});

//---------------------------------------------------
// Class component Slide
GESTIONAIR.components.Slide = new Class({
    initialize: function(config){
	    this.id = config.id?config.id:"";
	    this.name = config.name?config.name:"";
	    this.slide_index = 1;

	    var slide_index = this.slide_index ;
	    var parentNode = $('slide');	    

	    $('slide-next').addEvent('click', this.slider('next'));
	    $('slide-back').addEvent('click', this.slider('back'));

	    var event = null;

    }, // initialize

    // Slider Method
    slider: function(event) {									

	        //event.stop();

	    	var slide = $('slide');
	    	//var slide_index = 1;

	    	// Count the number of images
	    	function node_counter(parentNode) {
				var countNode = null ;
				var node = parentNode.getFirst('div');
 				while (node!=null) {
					countNode++;
					node = node.getNext('div');
 				}
 				if(countNode==null) { return countNode = 0 ; }
 				if(countNode!=null) { return countNode ; }
			}
	    
	    	var slide_max = node_counter(slide);

	    	if(event=="next") {
	    		var distance = this.slide_index*-620 ;
	    		var fx = new Fx.Morph(slide, {duration: '1000', transition: Fx.Transitions.Sine.easeIn});
	    		fx.start({	 
	    			'margin-left': distance
	    		});
	    		this.slide_index=this.slide_index+1;
	    	}	

	    	if(event=="back")  {
	    		var distance = (this.slide_index-2)*-620 ;
	    		var fx = new Fx.Morph(slide, {duration: '1000', transition: Fx.Transitions.Sine.easeIn});
	    		fx.start({	 
	    			'margin-left': distance
	    		});
	    		this.slide_index = this.slide_index-1 ;
	    	}	

		// Bouton Back
		if(this.slide_index==1) { 
			$('back').innerHTML = ''; 
		}
		if(this.slide_index!=1) { 
			$('back').innerHTML = "<div id=\"slide-back\">&lt;&lt;</div>"; 
		}

		// Bouton Next
		if(this.slide_index==slide_max) { 
			$('next').innerHTML = ""; 
		}
		if(this.slide_index!=slide_max) { 
			$('next').innerHTML = "<div id=\"slide-next\">&gt;&gt;</div>"; 
		}

    } // end of slider method
}); // end class slide
															