
Ext.onReady(function() {
	
	initSliderNews();
	
	initRollOverElementListingHome();
	
	initRollOverElementListing();
	
	initRollOverElementBoxClient();
	
	initRollOverElementNews();
	
	initRollOverExpertises();
	
	initSubMenuItems();
	
	initMenuExpertises();
	
});

/**
 * Sliders des news bas de page
 */
function initSliderNews(){
	if($('container_list_news_footer')){
		 var sliderNews = new noobSlide({
			autoPlay:true,
			interval:3000,
			box: $('container_list_news_footer'),
			nbElementsVisible:1,
			size:280,
			items: $('container_list_news_footer').getChildren('.footer_news_contener'),	
			addButtons: {
				previous: $('btn_prev_news'),
				next: $('btn_next_news')
			}
		});
	}
}

/**
 * Menu expertise col droite
 */
function initMenuExpertises(){
	if($('accordionsExpertises')){
		creatAccordion('domaine_title', 'domaine_expertises', $('indiceAccordionDomaineExpertise').value, '', '')
	}
}

/**
* Gestion des roll over des elements de listing
*/
function initRollOverElementListing () {
	Ext.select('.listing_box_clients').on('mouseenter', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#222222', from: '#efefef'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('.listing_box_clients_title');
		elContent.animate({
				color: {to: '#ffffff', from: '#333333'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
	
	Ext.select('.listing_box_clients').on('mouseleave', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#efefef', from: '#222222'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('.listing_box_clients_title');
		elContent.animate({
				color: {to: '#333333', from: '#ffffff'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
}

/**
* Gestion des roll over box client
*/
function initRollOverElementBoxClient () {
	Ext.select('.box_client').on('mouseenter', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#222222', from: '#efefef'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('.box_client_title');
		elContent.animate({
				color: {to: '#ffffff', from: '#333333'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
	
	Ext.select('.box_client').on('mouseleave', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#efefef', from: '#222222'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('.box_client_title');
		elContent.animate({
				color: {to: '#333333', from: '#ffffff'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
}

/**
* Gestion des roll over news
*/
function initRollOverElementNews () {
	Ext.select('.listing_box_actualites').on('mouseenter', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#222222', from: '#efefef'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('h4');
		elContent.animate({
				color: {to: '#ffffff', from: '#333333'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
	});
	
	Ext.select('.listing_box_actualites').on('mouseleave', function(e, t) {
		Ext.fly(this).animate({
				backgroundColor: {to: '#efefef', from: '#222222'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
		var elContent = Ext.fly(this).child('h4');
		elContent.animate({
				color: {to: '#333333', from: '#ffffff'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
	});
}

/**
* Gestion des roll over des elements de listing
*/
function initRollOverElementListingHome(){
	
	Ext.select('.home_box_clients').on('mouseenter', function(e, t) {
		
		var elContent = Ext.fly(this).child('.box_title_element_article');
		elContent.animate({
				backgroundColor: {to: '#222222', from: '#E2E2E2'},
				color: {to: '#ffffff', from: '#333333'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
	
	Ext.select('.home_box_clients').on('mouseleave', function(e, t) {
		
		var elContent = Ext.fly(this).child('.box_title_element_article');
		elContent.animate({
				backgroundColor: {to: '#E2E2E2', from: '#222222'},
				color: {to: '#333333', from: '#ffffff'}
			},effectSubMenuDuration,null,'easeOut','color'
		);
		
	});
}

/**
* Gestion des roll over de la home
*/
function initRollOverExpertises(){
	
	Ext.select('.home_box_expertise').on('mouseenter', function(e, t) {
		
		var elContent = Ext.fly(this).child('.home_box_expertise_content');
		elContent.animate({top: {to: 83, from: 153}},effectSubMenuDuration);
		
	});
	
	Ext.select('.home_box_expertise').on('mouseleave', function(e, t) {
		
		var elContent = Ext.fly(this).child('.home_box_expertise_content');
		elContent.animate({top: {to: 153, from: 83}}, effectSubMenuDuration);
		
	});
	
}

/**
 * Post le formulaire de contact
 */
function doPostFormContact(type){
	
	var idContainerFormContactError 	= 'container_form_contact_error';
	var idContainerFormContactLoader 	= 'container_form_contact_loader';
	var idContainerFormContactFields 	= 'container_form_contact_fields';
	var idContainerFormContactSuccess 	= 'container_form_contact_success';
	var urlFormContact 					= '/index/contact/'; 
	var idFormContact					= 'f_contact';
	
	if(type == 'short'){
		idContainerFormContactError 	= 'container_form_short_contact_error';
		idContainerFormContactLoader 	= 'container_form_short_contact_loader';
		idContainerFormContactFields 	= 'container_form_short_contact_fields';
		idContainerFormContactSuccess 	= 'container_form_short_contact_success';
		urlFormContact 					= '/index/short-contact/'; 
		idFormContact					= 'f_contact_short';
	}
	
	Ext.fly(idContainerFormContactError).setStyle('display', 'none');
	Ext.fly(idContainerFormContactLoader).setStyle('display', 'block');
	
	Ext.Ajax.request({
		url: urlFormContact,
		form: idFormContact,
		success: function(response, opts) {
			
			Ext.fly(idContainerFormContactLoader).setStyle('display', 'none');
			
			var obj = Ext.decode(response.responseText);
			
			if(obj.contact.form_error){
				
				Ext.fly(idContainerFormContactError).setStyle('display', 'block');
				
				Ext.fly(idContainerFormContactError).animate({ opacity: {to: 1, from: 0} }, effectSubMenuDuration);

				for(var i=0;i<obj.contact.fields.length;i++){
					
					if(obj.contact.fields[i].error){
						var field = Ext.get(obj.contact.fields[i].fieldName);
						field.addClass('field_form_contact_error');
						field.on('change', function(){
							this.removeClass('field_form_contact_error');
							var listFieldError = Ext.select('.field_form_contact_error');
							if(listFieldError.elements.length == 0 && Ext.fly(idContainerFormContactError).getStyle('opacity') == 1 ){
								Ext.fly(idContainerFormContactError).animate({ opacity: {to: 0, from: 1} }, effectSubMenuDuration);
							}
						});
						field.on('keypress', function(){
							this.removeClass('field_form_contact_error');
							var listFieldError = Ext.select('.field_form_contact_error');
							if(listFieldError.elements.length == 0 && Ext.fly(idContainerFormContactError).getStyle('opacity') == 1 ){
								Ext.fly(idContainerFormContactError).animate({ opacity: {to: 0, from: 1} }, effectSubMenuDuration);
							}
						});
					}
				
				}
			
			}else if(obj.contact.form_success){
				
				Ext.fly(idContainerFormContactFields).animate({ opacity: {to: 0, from: 1} }, effectSubMenuDuration);
				Ext.fly(idContainerFormContactFields).setStyle('display', 'none');
				Ext.fly(idContainerFormContactSuccess).setStyle('display', 'block');
				Ext.fly(idContainerFormContactSuccess).animate({ opacity: {to: 1, from: 0} }, effectSubMenuDuration);
				
			}
		},
		failure: function(response, opts) {
			
			Ext.fly(idContainerFormContactLoader).setStyle('display', 'none');
			
			//console.log('server-side failure with status code ' + response.status);
		}
	});
	
}

/**
 * JQUERY
 */
var $j = jQuery.noConflict();

$j(document).ready(function(){
	$j('#column_2').columnize({columns:2, lastNeverTallest:true});
	$j('.column h3').addClass('dontend');
	
	/**
	 * NEWSLETTER
	 */
	var newsletterForm = $j('form[name=newsletter]');
	if (newsletterForm.length == 1) {
		
		var initNameValue = newsletterForm.find('input[name=name]').val();
		newsletterForm.find('input[name=name]').focus(function(){
			if ($j(this).val() == initNameValue) {
				$j(this).val('');
			}
		});
		newsletterForm.find('input[name=name]').blur(function(){
			if ($j(this).val() == '') {
				$j(this).val(initNameValue);
			}
		});
		
		var initEmailValue = newsletterForm.find('input[name=email]').val();
		newsletterForm.find('input[name=email]').focus(function(){
			if ($j(this).val() == initEmailValue) {
				$j(this).val('');
			}
		});
		newsletterForm.find('input[name=email]').blur(function(){
			if ($j(this).val() == '') {
				$j(this).val(initEmailValue);
			}
		});
		
		newsletterForm.find('input[type=submit]').click(function(){
			$j.getJSON('/index/newsletter/', {
				name: 	newsletterForm.find('input[name=name]').val(),
				email: 	newsletterForm.find('input[name=email]').val()
			}, function (json) {
				if (json) {
					$j('#formNewsletter').hide();
					$j('#messageNewsletter').show();
				}
			});
			
			return false;
		});
	}
	
	/**
	 * SLIDER CLIENTS
	 */
	$j('#slider_clients').cycle({
		fx: 'fade',
		 timeout:  4000
	});
});

/**
 * MOOTOOLS
 */
/* Creation d'accordeon attend 4 paramètres 
	toggleClassName		:	 Nom de class des toggle à instancier
	elementClassName	:	 Nom de class des element associés aux toggles
	imgPucePlus		:	Chemin complet de la puce plus à attribuer
	imgPuceMoins		:	Chemin complet de la puce moins à attribuer
*/
function creatAccordion(toggleClassName, elementClassName, numElementToOpen, imgPucePlus, imgPuceMoins){
	objAccordion = new Accordion($$('.'+toggleClassName), $$('.'+elementClassName), {
		alwaysHide: false,
		display: numElementToOpen,
		alwaysHide: true,
		onActive: function(toggle, element){
			//On vérifie que l'état de l'élément pour l'ouvrir ou le fermer
			if(toggle.hasClass('open')){
				toggle.removeClass('open');
				// Changement de la puce ouverture fermeture avec un effet
				if(toggle.getElement('img') && imgPucePlus){
					toggle.getElement('img').src = imgPucePlus;
				}
			}else{
				toggle.addClass('open');
				// Changement de la puce ouverture fermeture avec un effet
				if(toggle.getElement('img') && imgPuceMoins){
					toggle.getElement('img').src = imgPuceMoins;
				}
			}
		},
		onBackground: function(toggle, element){
			toggle.removeClass('open');
			//Changement de la puce ouverture fermeture avec un effet								
			if(toggle.getElement('img') && imgPucePlus){
				toggle.getElement('img').src = imgPucePlus;
			}
		}
	});
	return objAccordion;
}


