/**
 * Navig.js		=> Gestion des menus et vignette
**/


// Gestion de la gallerie
	var imageActuelle = 1;
	var maxImage;


	function DesactiveVignette() {
		$$('li.vignette').each(function(vign) {
			vign.removeClass('actif');
		});   
	}


	function SwitchImage(versOu) {                  
	
		// recupere le nombre max d'images
		maxImage = $('listing').getElements('li').length;  
		
		// defini prochaine image à afficher
		switch (versOu) {
			case 'precedent': 
				if (imageActuelle>1) {
					imageActuelle--;
				} else {
					imageActuelle = maxImage;
				}                                     	
				break;         
				                         
			case 'suivant': 
				if (imageActuelle<maxImage) {
					imageActuelle++;        
				} else {
					imageActuelle=1;
				}                        
				break;         
		}                             

		// change l'image 
		DesactiveVignette();
		var vignette = $('vignette'+imageActuelle);
		var urlImage = vignette.getElement('span.url_image').get('html')
		$('diaporama').getElement('img').set('src', urlImage);
		vignette.addClass('actif'); 		
	}


// envoi de la commande au traitement => envoi ami
var sauvegardeMail = function (email) {

		var req = new Request({
		url: 'http://www.urban-fury.net/assets/ajax/sauvegardeMail.php',
		method:'get',
		data:{	
			action:'envoi',
			email:email
		},
		onSuccess: function(retourAjax) { 
			if (retourAjax=='ok') {
				alert('E-mail enregistré');//$('confirm').reveal();
			}
			else {
				alert(retourAjax);//$('confirm').reveal();
			};
		},
		onFailure: function(retourAjax){
			alert(retourAjax);
		
		}		
	});
	req.send();
};



// VÃ©rification de la validitÃ© des champs et envoi au traitement
var verifieMail = function() {
		
	if (VerifieSyntaxe()) {   
		if (erreur>0) {
			alert("Veuillez inserer une adresse e-Mail");  
		} else {   
			var email = 				$('newsletterEmail').get('value');  			

			// envoi de la requete                    
			sauvegardeMail(email);
		}
	};
}

// init des champs avant vÃ©rification erreurs
var initChamps = function () {
	erreur = 0;
	message = "";                                               
	//$$('input').setStyle('border-color', 'transparent');		
	//$$('input').setStyle('background-color', 'transparent');       
}


var VerifieSyntaxe = function() {
	initChamps();
	// Zone coordonnÃ©es facturation
	if ($('newsletterEmail').get('value').length<2) { 		erreur++;    	};

	// a tester email valideâ€¦
	var place = $('newsletterEmail').get('value').indexOf("@",1);
	var point = $('newsletterEmail').get('value').indexOf(".",place+1);
	if ( (place > -1) && ($('newsletterEmail').get('value').length >2) && (point > 1) ) { 
	} else {
	
		erreur++;
	};        
	   

	return true;
}



window.addEvent('domready', function(){

	//  Initialisation gallerie
	if ($('diaporama')!==null) {
		
		var urlImage = $('imgGallerie1').get('src');
		$('imageAffichee').src = urlImage; 

		// clic sur les vignettes
		$$('li.vignette').each(function(vignette) {
			vignette.addEvent('click', function(e) {
 				DesactiveVignette();
 				var urlImage = vignette.getElement('span.url_image').get('html')
				$('diaporama').getElement('img').set('src', urlImage);
				vignette.addClass('actif');
				window.location.hash ='#pager'; 
			}) /** fin fonction - e **/
		});          
	}
	                          
	
                           
	if ($('pager')!==null) {
		// clic sur les fleches precedent   
		$('pager').getElement('a.precedent').addEvent('click', function(e) { 
			e.stop(); 
			
			SwitchImage('precedent');
			window.location.hash ='#pager'; 
  		}) ;
		// clic sur les fleches suivant
		$('pager').getElement('a.suivant').addEvent('click', function(e) {
			e.stop();
			SwitchImage('suivant');   
			window.location.hash ='#pager';  
		}) ;
		// clic sur la gallerie
		$('diaporama').getElement('img').addEvent('click', function(e) {
			SwitchImage('suivant');   
			window.location.hash ='#pager'; 
		}) ;
	};
	

	if ($('blocProductions')!==null) {
		var blocProductions = $('blocProductions').getStyle('height').toInt();
		blocProductions = blocProductions + 10;
		$('blocProductions').setStyle('height', blocProductions +"px");   
		
		var blocMixs = $('blocMixs').getStyle('height').toInt();
		blocMixs = blocMixs + 10;
		$('blocMixs').setStyle('height', blocMixs +"px"); 
	}
	if ($('newsLetter')!==null) {

		// Validation du formulaire
		$('newsletterEnvoi').addEvent('click', function(e) {	   
			e.stop();
	  		verifieMail();

		}); 
	}
	


  // First list, using CSS styles in the JavaScript
  var list = $$('#listeDesMixs LI');

  list.set('morph', {
    duration: 200
  });

  list.addEvents({

    mouseenter: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#161616',
        'color': '#fff',
        'height': 60,
		'padding-left': 10
      });
    },

    mouseleave: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#101010',
        'color': '#000',
        'height': 30,
		'padding-left': 0
      });
    }

  });

  // First list, using CSS styles in the JavaScript
  var list = $$('#listeDesProductions LI');

  list.set('morph', {
    duration: 200
  });

  list.addEvents({

    mouseenter: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#161616',
        'color': '#fff',
        'height': 60,
		'padding-left': 0
      });
    },

    mouseleave: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#101010',
        'color': '#fff',
        'height': 30,
		'padding-left': 0
      });
    }

  });

  // First list, using CSS styles in the JavaScript
  var list = $$('#listeSocial li');

  list.set('morph', {
    duration: 200
  });

  list.addEvents({

    mouseenter: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#161616',
        'color': '#fff',
        'height': 256,

      });
    },

    mouseleave: function(){
      // this refers to the element in an event
      this.morph({
        'background-color': '#161616',
        'color': '#000',
        'height': 60,

      });
    }

  });

});
