//var path = '/basedure/dev.roulezmalin.com';
var path = '/basedure/www.roulezmalin.fr';
var oto1, oto2, timer, myFx;

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

	SqueezeBox.initialize();

	if($('divMoteurPInterne')) timer = setInterval("slideMenu()", 500);

	if($('formEnteteLogin')) {
		$('formEnteteLogin').addEvent('submit', function(e) {

			e.stop();

			var rAjax = new Request.HTML({
				url: path+'/include/covoitureur/covoitureur_identification-utilisateur-action.php?xhr=1',
				evalScripts: false
			}).post($('formEnteteLogin'));

			rAjax.addEvent('onComplete', function(responseTree, responseElements, responseHTML, responseJavaScript){

				if(!$('pformEnteteLogin-0')) {
					var inf = new Element('p', {'id': 'pformEnteteLogin-0', 'class': 'retourErreur'});
					inf.inject($('formEnteteLogin'), 'top');
				}

				$('pformEnteteLogin-0').set('html', responseHTML);
				if(responseJavaScript) eval(responseJavaScript);

			});
		});
	}

	// Validation du formlauire
	if($('btTrouver')) {
		$('form_choix_ville').addEvent('submit', function(e) {

			e.stop();

			ecoov.trouver();
		});
	}


	// Texte Aide utilisation Carte standard
	if($('utilisationCarte')) {
		SqueezeBox.assign($('utilisationCarte'), {
				size: {x: 350, y: 150},
				url	: path+'/include/carto/carto_aide-utilisation.php?xhr=1&texte=1',
				title: 'Aide cartographique',
				menuTitle: false
		});
	}

	// Texte Aide utilisation Carte sans reglette echelle
	if($('utilisationCarte2')) {
		SqueezeBox.assign($('utilisationCarte2'), {
				size: {x: 350, y: 150},
				url	: path+'/include/carto/carto_aide-utilisation.php?xhr=1&texte=2',
				title: 'Aide cartographique',
				menuTitle: false
		});
	}
	
	// Texte Aide utilisation Carte standard V2
	if($('utilisationCarte3')) {
		SqueezeBox.assign($('utilisationCarte3'), {
				size: {x: 350, y: 150},
				url	: path+'/include/carto/carto_aide-utilisation.php?xhr=1&texte=3',
				title: 'Aide cartographique',
				menuTitle: false
		});
	}

	// AUTOCOMPLETION
	if($('depart_ville')){

		var departement_depart = DEPARTEMENT;
		var departement_destination = DEPARTEMENT;

		// Si select departement classique
		if($('choixDepartementDepart')) {
			$('choixDepartementDepart').addEvent('change', function(e) {
				departement_depart = $('choixDepartementDepart').value;
				oto1.execute(path+'/include/recherche/recherche_ville-autocompletion.php?dep='+departement_depart);
			});

			$('choixDepartementDestination').addEvent('change', function(e) {
				departement_destination = $('choixDepartementDestination').value;
				oto2.execute(path+'/include/recherche/recherche_ville-autocompletion.php?dep='+departement_destination);
			});
		}

		// Gestion de l'autocompletion sur la Ville de depart
		oto1 = new Autocompleter.Request.HTML('depart_ville', {
			postVar		: 'ville',
			minLength	: 3,
			relative	: false,
			postData	: {
				'html': '1'
			},
			overflow : true,
			width: 'auto',
			indicatorClass: 'autocompleter-loading',
			injectChoice: function(choice) {
				var text  = choice.getLast();
				var value = text.innerHTML;
				choice.inputValue = value;
				text.set('html', this.markQueryValue(value));
				this.addChoiceEvents(choice);
			}
		});
				
		
		oto1.execute(path+'/include/recherche/recherche_ville-autocompletion.php?dep='+departement_depart);
			


		// Si départ défini
		if($('depart_ville') && $('depart_ville') != 'Ville') {

			// Lieu
			if($('depart_lieu')) {
				new Autocompleter.Request.JSON('depart_lieu', path+'/include/recherche/recherche_lieu-autocompletion.php', {
			        postVar		: 'lieu',
					minLength	: 3
			    });
			}

		}

		
		// Gestion de l'autocompletion sur la Ville de destination
		if($('destination_ville')){	
		oto2 = new Autocompleter.Request.HTML('destination_ville', {
			postVar		: 'ville',
			minLength	: 3,
			relative	: false,
			postData	: {
				'html'  : '1'
			},
			overflow : false,
			width: 'auto',
			indicatorClass: 'autocompleter-loading',
			injectChoice: function(choice) {
				var text  = choice.getLast();
				var value = text.innerHTML;
				choice.inputValue = value;
				text.set('html', this.markQueryValue(value));
				this.addChoiceEvents(choice);
        }
		});
				
		oto2.execute(path+'/include/recherche/recherche_ville-autocompletion.php?dep='+departement_destination);
		}
		// Si destination  défini
		if($('destination_ville') && $('destination_ville') != 'Ville') {

			// Lieu
			if($('destination_lieu')) {
				
				new Autocompleter.Request.JSON('destination_lieu', path+'/include/recherche/recherche_lieu-autocompletion.php', {
			        postVar		: 'lieu',
					minLength	: 3
			    });
			}

		}
	}


	// ############################################################################
	// Aggrandissement de la carte gmap

	if($('carteSmall')) {
		$('carteSmall').addEvent('click', function(event) {

			var divcarte_fx = new Fx.Morph('divMoteurCartePInterne', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});
			var carte_fx = new Fx.Morph('carte', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});
			var fond_fx = new Fx.Morph('divFondBlanc', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});

			divcarte_fx.start({'height': 575, 'width': 980});
			carte_fx.start({'height': 500, 'width': 960 });
			fond_fx.start({'height': 570, 'width': 980 });

			$('divMoteurCartePInterne').setStyle('background', 'url(/media/site/image/fond_divMoteurCartePInterneSans_corne.png) no-repeat scroll 0pt 23px');
			carte_fx.addEvent('onComplete', function(){map.checkResize()});

		});

		$('carteLight').addEvent('click', function(event) {

			var divcarte_fx = new Fx.Morph('divMoteurCartePInterne', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});
			var carte_fx = new Fx.Morph('carte', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});
			var fond_fx = new Fx.Morph('divFondBlanc', {duration: 'short', transition: Fx.Transitions.Sine.easeOut, opacity: 80});

			divcarte_fx.start({'height': 323, 'width': 250});
			carte_fx.start({'height': 260, 'width': 240});
			fond_fx.start({'height': 30, 'width': 250 });

			$('divMoteurCartePInterne').setStyle('background', 'transparent url(/media/site/image/fond-carteInterne.png) no-repeat scroll 0pt 23px');
			carte_fx.addEvent('onComplete', function(){map.checkResize()});
		});
	}


	// Fin gmap
	// ############################################################################
	if(polyline1_1) map.addOverlay(polyline1_1);
});

function slideMenu() {

	var myFx = new Fx.Tween('divMoteurPInterne', {
		duration : 500,
		transition: Fx.Transitions.Sine.easeOut
	});

	var drap = 0;

	var maF = function repos_bloc(val) {
		myFx.start('top', val);
	}

	var hauteur_initiale = 90;
	var hauteur_total = $('footer').getPosition();

	var pos_b = $('divMoteurPInterne').getPosition();
	var pos_scroll = window.getScrollTop();

	// On depasse l'entete on demarre le scroll
	if((pos_scroll-90) < hauteur_initiale) myFx.start('top', 90);

	// On demarre le scroll
	else {

		// Si on est trop pres du pied de page on reste au top qui va bien
		if((($('divMoteurPInterne').getPosition().y+90)+815) >= hauteur_total.y && (pos_scroll+700) > hauteur_total.y) {
			maF(hauteur_total.y-815);
		}

		else if((pos_scroll+700) < hauteur_total.y) {
			maF(pos_scroll - hauteur_initiale);
		}

		else maF(pos_scroll - hauteur_initiale);
	}
}


function voirFiche(id){

	var o = {
				size: {x: 700, y: 500},
				url	: path+'/include/offre/offre_details.php?xhr=1&modecarte=1',
				title: 'Détails du trajet',
				menuTitle: false
	};

	SqueezeBox.fromElement(id, o);

}


