var Common = {
	fermerEdito: function (){
		var myVerticalSlide = new Fx.Slide('top');
		myVerticalSlide.slideOut();
	},
	
	// moteur
	
	rechargerMoteur: function(){
		$('formulaire_details').set('send', {
			url: 'index.php?action=ajax_moteur',
			method: 'get',
			onSuccess : function (response) {
				if ( response!="") {
					//console.log(response);
					Common.hideLoading('');
					$('cadre_moteur').innerHTML = response;
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {					
					Common.showLoading('');
			}
		});
		$('formulaire_details').send();
	},
	rechargerMoteurVol: function(){
		$('formulaire_details').set('send', {
			url: 'index.php?action=ajax_moteur_vol',
			method: 'get',
			onSuccess : function (response) {
			if ( response!="") {
				//console.log(response);
				Common.hideLoading('');
				$('cadre_moteur').innerHTML = response;
			}					
		},
		onFailure: function () {
			//alert('erreur dans l\'application');
		},
		onRequest : function () {					
			Common.showLoading('');
		}
		});
		$('formulaire_details').send();
	},
	chargerMoteur: function(type){
		$('formulaire_cache').set('send', {
			url: 'index.php?action=ajax_moteur',
			method: 'get',
			onSuccess : function (response) {
				if ( response!="") {
					//console.log(response);
					//Common.hideLoading('');
					$('cadre_moteur').innerHTML = response;
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {
				var loadMoteur = new Element('img', {
					'id'    : 'loadMoteur',
					'src'	: 'templates/ladepeche/images/attente.gif',
					'styles': {
				      'width'	:	'200px',
				      'height'	:	'200px'
					}
				});
				loadMoteur.inject($('cadre_moteur')) ;
				//Common.showLoading('');
			}
		});
		$('formulaire_cache').send();
	},
	chargerMoteurVol: function(type){
		$('formulaire_cache').set('send', {
			url: 'index.php?action=ajax_moteur_vol',
			method: 'get',
			onSuccess : function (response) {
			if ( response!="") {
				//console.log(response);
				$('cadre_moteur').innerHTML = response;
				Autocompleter_vol();
				Vol.calendrier();
			}					
		},
		onFailure: function () {
			alert('erreur dans l\'application');
		},
		onRequest : function () {					
			//Common.showLoadingMoteur('');
		}
		});
		$('formulaire_cache').send();
	},
	chargerMoteurForfait: function(type){
		$('formulaire_cache').set('send', {
			url: 'index.php?action=ajax_moteur_forfait',
			method: 'get',
			evalScripts:true,
			onSuccess : function (response) {
				if ( response!="") {
					//alert('test');
					//console.log(response);
					$('cadre_moteur').innerHTML = response;
					Autocompleter_vol();
					Forfait.calendrier();
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {
				var loadMoteur = new Element('img', {
						'id'    : 'loadMoteur',
						'src'	: 'templates/ladepeche/images/attente.gif',
						'styles': {
					      'width'	:	'200px',
					      'height'	:	'200px'
						}
					});
				loadMoteur.inject($('cadre_moteur')) ;
				//Common.showLoadingMoteur('');
			}
			});
		$('formulaire_cache').send();
	},
	rechercher: function(domaine){
		Common.showLoading();
		if(typeof(domaine)=='undefined'){
			domaine = '';
		}
		$('formulaire_details').action = domaine+"index.php";
		$('mode_page').innerHTML = '<input type="hidden" name="page" value="resultat" />';
		$('formulaire_details').method="get";
		$('formulaire_details').submit();
	},
	rechercherVol: function(){
		var frm_id='';
		if (document.getElementById('moteur_left')) {
			frm_id='moteur_left';
		} else {
			frm_id='moteur_vol';
		}
		$(frm_id).set('send', {
			url: 'index.php?action=interrogation_catalogue_vol',
			method: 'get',
			onSuccess : function (response) {
				if ( response!="") {
					//console.log(response);
					//alert(response);
					eval(response);
					Common.hideLoading();
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {					
					//Common.showLoadingMoteur('');
				Common.showLoading();
			}
		});
		$(frm_id).send();
	},
	rechercherVolPush: function(filtre,domaine){
		var myRequest = new Request({
			method: 'get', 
			url: domaine+'index.php?action=interrogation_catalogue_vol',
			onSuccess : function (response) {
				if ( response!="") {
					//console.log(response);
					//alert(response);
					eval(response);
					Common.hideLoading();
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {					
					//Common.showLoadingMoteur('');
				Common.showLoading();
			}
		});
		myRequest.send(filtre);
	},
	rechercherForfait: function(){
		$('moteur_forfait').set('send', {
			url: 'index.php?action=interrogation_catalogue_forfait',
			method: 'get',
			onSuccess : function (response) {
				Common.hideLoading();
				if (response != "") {
					//console.log(response);
					eval(response);
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {		
				Common.showLoading();
			}
		});
		$('moteur_forfait').send();
	},
	rechercherAffinerForfait: function(){
		$('moteur_forfait_avancee').set('send', {
			url: 'index.php?action=interrogation_catalogue_forfait',
			method: 'get',
			onSuccess : function (response) {
			Common.hideLoading();
			if (response != "") {
				//console.log(response);
				eval(response);
			}					
		},
		onFailure: function () {
			//alert('erreur dans l\'application');
		},
		onRequest : function () {		
			Common.showLoading();
		}
		});
		$('moteur_forfait_avancee').send();
	},
	rechercherForfaitPush: function(filtre,domaine){
		var myRequest = new Request({
			method: 'get', 
			url: domaine+'index.php?action=interrogation_catalogue_forfait',
			onSuccess : function (response) {
				if ( response!="") {
					//console.log(response);
					//alert(response);
					eval(response);
					Common.hideLoading();
				}					
			},
			onFailure: function () {
				//alert('erreur dans l\'application');
			},
			onRequest : function () {					
					//Common.showLoadingMoteur('');
				Common.showLoading();
			}
		});
		myRequest.send(filtre);
	},
	redirect: function(get){
		alert(get);
		window.location.href('index.php?'+get);
	},
	changeCritere: function(obj,nom){
		var critere_changed = "";
		critere_changed = obj.name.replace('filtre[','');
		critere_changed = critere_changed.replace(']','');
		
		var critere_value = $('filtre_old_'+nom).value;
		
		$('last_critere_name').value = critere_changed;
		$('last_critere_value').value = critere_value;
		$('num_page_moteur').value = "";
		$('tri_moteur').value = "";
	},
	getIEVersionNumber: function() {
	    var ua = navigator.userAgent;
	    var MSIEOffset = ua.indexOf("MSIE ");
	    
	    if (MSIEOffset == -1) {
	        return 0;
	    } else {
	        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	    }
	},
	centerPopup: function(element,parent) {
		var height=document.getElementById(element).offsetHeight;//hauteur de l'élément à positionner
		var width=document.getElementById(element).offsetWidth;//largeur de l'élément à positionner
		if(parent == "body"){
			myParent=document.body;
		}else{
			myParent=document.getElementById(element).parentNode;
		}
		var pHeight=myParent.offsetHeight;//Hauteur de l'élément parent
		var pWidth=myParent.offsetWidth;//Largeur de l'élément parent
		var sTop=myParent.scrollTop;//Hauteur de défilement de l'élément parent
		var sLeft=myParent.scrollLeft;//Longueur de défilement de l'élément parent
		var posY=(pHeight/2)-(height/2)+sTop;//Calcul de la position en Y
		var posX=(pWidth/2)-(width/2)+sLeft;//Calcul de la position en X
		document.getElementById(element).style.top=posY+"px";
		document.getElementById(element).style.left=posX+"px";
	},	
	
	// page r�sultat
	
	checkTri: function(tri){
		if($('tri_moteur')){
			$('tri_moteur').value=tri;
			$('num_page_moteur').value="1";
			Common.rechercher('');
		}
	},
	checkPage: function(num_page){
		if($('num_page_moteur')){
			$('num_page_moteur').value = num_page;
			Common.rechercher('');
		}
	},
	getSejour: function(id_sejour){
		var newUrl = self.location.href;
		newUrl = newUrl.replace("page=resultat","page=sejour");
		document.location.href = newUrl + "&id_sejour="+id_sejour;
	},
	imprimerDevis: function(){
		//alert("imprimer");
		window.print();
	},
	getDispo: function(){
		$('formulaire_resa').set('send', {
			url: 'index.php?action=interrogation_quotation',
			method: 'get',
			evalScripts : true,
			onSuccess : function (response) {
				if ( response!="") {
					if(response==""){
					} else {
						eval(response);
						Common.hideLoading();
					}
				}					
			},
			onFailure: function () {
				alert('erreur dans l\'application');
			},
			onRequest : function () {					
				Common.showLoading();
			}
		});
		$('formulaire_resa').send();
	},
	processReservation: function(){
		$('page').value= 'process_reservation';
		$('formulaire_resa').action = "index.php";
		$('formulaire_resa').method="get";
		$('formulaire_resa').submit();
	},
	hideLoading:function(style_page) {
		ieVersion = Common.getIEVersionNumber();
		if (ieVersion==6)
		{
			$$(".chp_recherche").each(function(el) {           
		        el.style.display = "";
		        //alert("fgd");
			});
			$$("#formReserver1").each(function(el) {           
		        el.style.display = "";
		        //alert("fgd");
			});
			$$('select').each(function(el) {           
		        el.style.display = "";
			});
		}
		if ($("attente")) {
			$("attente").dispose();
			if(style_page == "alone"){
				$("loadingHome").hide();
				$("loadingHome").dispose();
			}else{
				$("loading").hide();
				$("loading").dispose();
			}
		}
	},
	getIEVersionNumber:function() {
	    var ua = navigator.userAgent;
	    var MSIEOffset = ua.indexOf("MSIE ");
	    
	    if (MSIEOffset == -1) {
	        return 0;
	    } else {
	        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
	    }
	},
	showLoading:function(style_page) {
		
		if(!$('loading')) {
			ieVersion = Common.getIEVersionNumber();
			if (ieVersion==6)
			{
				$$(".chp_recherche").each(function(el) {           
			        el.style.display = "none";
			        //alert("fgd");
				});
				$$("#formReserver1").each(function(el) {           
			        el.style.display = "none";
			        //alert("fgd");
				});
				$$('select').each(function(el) {           
			        el.style.display = "none";
				});
			}
			Attente = new Element('div', {
				'id'    : 'attente',
				'styles': {
			      'display'           : '',
			      'position'          : 'absolute',
			      'opacity'           : 1,
			      'z-index'           : 10000,
			      'background-color'  : "#fff",
			      'height'            : '200px',
			      'width'             : '200px'
				}
			});
			
			var obj;
			if ($('searchForm')) {
				obj = $('searchForm');
			} else {
				obj = $('searchFormHome');
			}
			
			
			Attente.set('html', '<img src="templates/ladepeche/images/attente.gif" style="width:200px;height:200px;" />');
			if(style_page == "alone"){
				OverlayHome = new Element('div', {
					'id'    : 'loadingHome',
					'styles': {
				      'display'           : 'none',
				      'position'          : 'absolute',
				      'top'               : '0',
				      'left'              : '0',
				      'opacity'           : .75,
				      'z-index'           : 10000,
				      'background-color'  : "#fff",
				      'height'            : obj.getHeight() + 'px',
				      'width'             : obj.getWidth() + 'px'
					}
				});
				OverlayHome.injectInside(obj);
				Attente.injectInside($('loadingHome'));
				$("loadingHome").show();
			}else{
				Overlay = new Element('div', {
					'id'    : 'loading',
					'styles': {
				      'display'           : 'none',
				      'position'          : 'absolute',
				      'top'               : '0',
				      'left'              : '0',
				      'opacity'           : .75,
				      'z-index'           : 15000,
				      'background-color'  : "#fff",
				      'height'            : window.getScrollHeight() + 'px',
				      'width'             : window.getScrollWidth() + 'px'
					}
				});
				Overlay.injectInside(document.body);
				Attente.injectInside($('loading'));
				Element.implement({  
					//implement show
					show: function() {
						this.setStyle('display','');
					},  
					//implement hide
					hide: function() {  
						this.setStyle('display','none');  
					} 
				});
				$("loading").show();
			}
			Common.centrerLoading();
		}
	},
	centrerLoading:function() {
       if($('attente')) {
			var tailleLoading, talleWindow, nouvellePosition;
	        $('attente').setStyle('position', 'fixed');
	        if(Common.getIEVersionNumber() == 6) {
	        	$('attente').setStyle('position', 'absolute');
	        	//$(id_element).setStyle('left', documentElement.scrollTop+body.scrollTop);
	        }
	        // les x
	        tailleLoading = $('attente').getSize().y;
	        talleWindow = window.getSize().y;
	        nouvellePosition = (talleWindow - tailleLoading) / 2;
	        $('attente').setStyle('top', nouvellePosition+'px');
	        // les y
	        tailleLoading = $('attente').getSize().x;
	        talleWindow = window.getSize().x;
	        nouvellePosition = (talleWindow - tailleLoading) / 2;
	        $('attente').setStyle('left', nouvellePosition+'px');
       }
	},
	centrer:function(id_element) {
       if($(id_element)) {
			var tailleLoading, talleWindow, nouvellePosition;
	        $(id_element).setStyle('position', 'fixed');
	        if(Common.getIEVersionNumber() == 6) {
	        	$(id_element).setStyle('position', 'absolute');
	        	//$(id_element).setStyle('left', documentElement.scrollTop+body.scrollTop);
	        }
	        // les x
	        tailleLoading = $(id_element).getSize().y;
	        talleWindow = window.getSize().y;
	        nouvellePosition = (talleWindow - tailleLoading) / 2;
	        $(id_element).setStyle('top', parseInt(nouvellePosition)+'px');
	        // les y
	        tailleLoading = $(id_element).getSize().x;
	        talleWindow = window.getSize().x;
	        nouvellePosition = (talleWindow - tailleLoading) / 2;
	        $(id_element).setStyle('left', parseInt(nouvellePosition)+'px');
       }
	},
	inscrireNewsletter: function(){
		Sexy.confirm("Voulez-vous que l'adresse "+$('champNewsletter').value+" re&ccedil;oive la newsletter ?",{ 
			onComplete: 
			    function(result) {
			    	if(result){
						//On peut ajouter en db
						$('formInscription').set('send', {
						url: 'index.php?action=inscription_newsletter',
						method: 'post',
						onSuccess : function (response) {
							Common.hideLoading();
							var Sexy = new SexyAlertBox();
							if (response=="ok") {
								Sexy.error("Email correctement ajout&eacute;");
							} else {
								Sexy.error(response);
							}							
						},
						onFailure: function () {
							Common.hideLoading();
						},
						onRequest : function () {					
							Common.showLoading();
						}
						});
						$('formInscription').send();
			    	}
				}
			});
	},
	controleInscription: function(){
		var myCheck = new FormCheck('formInscription', {
			submitByAjax: true,	
			ajaxEvalScripts: false,			
			display : {
				showErrors : 1,
	            indicateErrors : 1,
	            indicateErrorsInit : 0,
	            checkValueIfEmpty : 0,
	            scrollToFirst : true
	        },
	        alerts : {
	            required : 'Ce champs est obligatoire. Merci de remplir une valeur',
	            email : 'Merci de rentrer une adresse valide'
	        },
	        onAjaxRequest : function(res) {

			},
	        onAjaxSuccess : function(res) {
	        	Common.inscrireNewsletter();
	        }
	    });
	},
	showDiaporama: function(id_sejour) {
		var myRequest = new Request({
			method: 'post', 
			url: 'index.php?action=diaporama',
			data: 'id_sejour='+id_sejour,
			onSuccess : function (response) {
				Common.hideLoading();
				Fond = new Element('div', {
					'id'	: 'fond',
					'styles': {
					  'display'           : 'block',
				      'position'          : 'absolute',
				      'top'               : '0',
				      'left'              : '0',
				      'opacity'           : .75,
				      'z-index'           : 9000,
				      'background-color'  : "#fff",
				      'height'            : window.getScrollHeight() + 'px',
				      'width'             : window.getScrollWidth() + 'px'
					}				
				});
				Fond.injectInside(document.body);
				Diaporama = new Element('div', {
					'id'    : 'diaporama',
					'styles': {
				      'display'           : '',
				      'position'          : 'relative',
				      'opacity'           : 1,
				      'z-index'           : 10000,
				      'background-color'  : "#fff"
					}
				});
				Diaporama.set('html', response);
				Diaporama.injectInside(document.body);
				var images = $$('.slideShowData');
			    var data = new Array();
				for(var i=0; i<images.length; i++) {
					data[i] = images[i].getAttribute('id');
				}
				var myShow = new Slideshow.KenBurns('show', data, { captions: false, controller: true, delay: 5000, duration: 2000, height: '', hu: '' });
			    Common.centrer('diaporama');
			    $('fermerDiapo').addEvent('click', function(){
			    	$('diaporama').destroy();
			    	$('fond').destroy();
			    	if(Common.getIEVersionNumber() == 6) {
				    	$$('select').each(function(el) {           
					        el.style.display = "";
						});
					}
			    });
			    if(Common.getIEVersionNumber() == 6) {
			    	$$('select').each(function(el) {           
				        el.style.display = "none";
					});
				}
			},
			onFailure: function () {
				alert('erreur dans l\'application');
			},
			onRequest : function () {
				Common.showLoading();
			}
		});
		myRequest.send();
	},
	bloqueCtrlV : function (evt) {	
		if(evt.ctrlKey) {
			//Touche v ou V enfoncée
			if((evt.keyCode == 86) || (evt.keyCode == 118)) { 
				evt.returnValue = false;
			}
		}	
	}
}

window.addEvent('domready', function() {
	if($('edito_close')){
		$('edito_close').addEvent('click', Common.fermerEdito);	
	}
	if($('warning_msg')){
		msg=$('warning_msg').innerHTML;
		var Sexy = new SexyAlertBox();
		Sexy.error(msg);
	}

	Element.implement({  
		//implement show
		show: function() {
			this.setStyle('display','');
		},  
		//implement hide
		hide: function() {  
			this.setStyle('display','none');  
		} 
	});
	window.addEvent('resize', Common.centrerLoading);
});