agenda = new Object();
var dure_slide = 800;
var bloc_prec = "chataignier";
var bloc_prec2 = "sciage";
var mon_menu_prec = "menu_chataignier";
var mon_menu_prec2 = "menu_sciage";

	agenda.hbs = null;
	agenda.hns = null;
	agenda.hcs = null;
	
agenda.init = function() {
	agenda.header_blanc = $("header_blanc");
	agenda.header_noir = $("header_noir");
	agenda.header_consulter = $("header_consulter");
	agenda.fond_ville_blanc = $("fond_ville_blanc");
	agenda.fond_ville = $("fond_ville");
	agenda.move_agenda = $("display_agenda");
	//console.log(agenda.header_consulter);
		
	// Cache les div
	agenda.testDisplay(agenda.header_blanc);
	agenda.testDisplay(agenda.header_noir);
	agenda.testDisplay(agenda.header_consulter);
	agenda.testDisplay(agenda.fond_ville_blanc);
	agenda.testDisplay(agenda.display_agenda);
	
	agenda.hbs = new Fx.Slide("header_blanc", {
    duration: dure_slide,
    transition: Fx.Transitions.easeIn
	});
	agenda.hns = new Fx.Slide("header_noir", {
    duration: dure_slide,
    transition: Fx.Transitions.easeOut
	});
	agenda.hcs = new Fx.Slide("header_consulter", {
    duration: dure_slide,
    transition: Fx.Transitions.easeOut
	}).show();
	agenda.fv = new Fx.Morph("fond_ville", {
		duration: dure_slide,
		wait:false
	});
	
	
	
	agenda.header_consulter.getParent().setStyle('width', '979px');
	
	agenda.sel = $('sel_ville');
	agenda.get_sel();
}	

agenda.simule_click = function(ville_id) {
	$$('li.agenda_date-'+ville_id).each(
		function(el) {
				agenda.date_click(el, ville_id);
		});
}

agenda.date_click = function(these, ville_id) {
	
	// on determine sa position dans les li
	childs = $('menu_ville').getChildren();
	lapos = 0;
	trouve = false;
	childs.each(
		function(el) {
			if(el.id != these.id && trouve == false) {
				lapos++;
			} else {
				trouve= true;
			}
		}
	);
	//console.log(these.id + " / position: " + lapos);
	
	agenda.repositionne(lapos);

	agenda.getEvent(ville_id);
	
	if(agenda.header_noir.hasClass('close')) {
		agenda.openAgenda();
	}
	
	agenda.get_sel(0);
}

agenda.repositionne = function(nb) {
	mychilds = $('menu_ville').getChildren();
	for(i=0;i<nb;i++) {
		prem = mychilds[i];
		var copy = prem.clone(true, true);
		copy.inject($('menu_ville'));
	}	
	//$('menu_container').set('tween', {duration: 500} );
	//$('menu_container').tween("margin-left", -194 * nb);
	agenda.repositionne_fin(nb);	
}

agenda.repositionne_fin = function(nb) {
	
	$('menu_container').setStyle("margin-left", 0);
//	console.log("reinit1");
	for(i=0;i<nb;i++) {
		prem = $('menu_ville').getFirst();
		prem.destroy();
		//console.log("destroy");
	}
	
	$('menu_container').setStyle("margin-left", 0);
		//console.log("reinit2")
		
}

// Remplace les div en display none par des show hide
agenda.testDisplay = function(el) {
		if($defined(el) && $(el).getStyle('display') == 'none') {
			$(el).setStyle('display', 'block');
			$(el).slide('hide');
			$(el).addClass('close');
		}
}

// ferme les big maquettes pour fixer un bug css sous ie
agenda.closeMaquette = function() {
	if (Browser.Engine.name=='trident') {
		if(typeof slidemaquette != "undefined") {
			if(slidemaquette.opened != "") {
				slidemaquette.fermer(slidemaquette.opened);
			}
		}
	}
}
// ouvre la fenetre d'agenda
agenda.openAgenda = function() {
	agenda.closeMaquette();
	
	//console.log(agenda.hcs);
	agenda.fv.start({'height' : 0 });
	//agenda.hcs.slideOut().chain(function() { agenda.hns.slideIn(); });
	//agenda.hcs.slideOut();
	agenda.hbs.slideOut().chain(function() {
		agenda.hns.slideIn();
		if (Browser.Engine.trident){
		// Internet Explorer
			agenda.fv.start({'height' : 72});
		}else {
		// Autres (Mozilla, Opera, etc.)
			agenda.fv.start({'height' : 71 });
		}	 
	});
	//agenda.hns.slideIn();
	agenda.header_consulter.addClass('close');
	agenda.header_blanc.addClass('close');
	agenda.header_noir.removeClass('close');
}

//ferme la fenetre d'agenda
agenda.closeAgenda = function() {	

	agenda.closeMaquette();
	agenda.fv.start({'height' : 0 });
	if (Browser.Engine.trident){
	// Internet Explorer
		agenda.hns.slideOut().chain(function() { agenda.fv.start({'height' : 110 }).chain(function() { agenda.hbs.slideIn(); }); });
	}else {
	// Autres (Mozilla, Opera, etc.)
		agenda.hns.slideOut().chain(function() { agenda.fv.start({'height' : 107 }).chain(function() { agenda.hbs.slideIn(); }); });
	}
	agenda.header_noir.addClass('close');
	agenda.header_consulter.removeClass('close');
	agenda.header_blanc.removeClass('close');
	
}

//affiche la bulle de la ville
agenda.showBul = function(id) {
	if($("bul_" + id)) {
		var bul = $("bul_" + id);
		bul.setStyle('display', 'block');
	}
}

//cache la bulle de la ville
agenda.hideBul = function(id) {
	if($("bul_" + id)) {
		var bul = $("bul_" + id);
		bul.setStyle('display', 'none');
	}
}

//affiche le gif de loading ajax
agenda.waitloading = function(el) {
	$(el).set('html', "<div class='ajax_loader_noir'><img src='/img/ajax-loader_noir.gif'></div>");
}

//affiche un evenement via ajax
agenda.show_request = function(response) {
	document.getElementById('agenda').innerHTML = response;
}

//Requete pour afficher un nouvel evenement
agenda.getEvent = function(id) {
	if($('agenda')) {
		var ag = $('agenda');
		var req = new Request({
			method: 'get',
			url: "/ajax/getagenda.php",
			data: { 'vil' : id },
			onRequest: function() {agenda.waitloading(ag);},
			onComplete: function(response) { agenda.show_request(response); }
		}).send();
	}
}

//Place le fond rouge de la ville selon le over
agenda.get_sel = function(these) {
	var dest_left;
	$('menu_ville').getElements('li').each(function(el) {
		
		$(el).addEvent('mouseover', function() {
			dest_left = $(el).getCoordinates().left - $(el).getParent().getCoordinates().left;
			agenda.sel.morph({left : dest_left});
		});
		
		el.addEvent('mouseout', function() {
			agenda.sel.morph({left : 0});
		});
		
		$(el).addEvent('click', function() {
			 //console.log(dest_left);
			$('bloc_ville').setStyle('display', 'block');
			$('bloc_ville').setStyle('left', dest_left+'px');
			$('bloc_ville').addEvent('mouseout', function() {
				$('bloc_ville').setStyle('display', 'none');
			});
			agenda.sel.morph({left : 0});
			// on simule qu'on se barre de la div bloc_ville et donc on rammene le carre rouge sur nous	
			agenda.bloc_rouge_revient.delay(1000);
		});
	});
}

agenda.bloc_rouge_revient = function() {
	$('bloc_ville').fireEvent('mouseout');		
}

/* Fonctions pour la page statique essence et transformation */
function menu_switch(these){		
				lid = these.id;
				lid = lid.replace(/menu_/,'');
				$(bloc_prec).setStyle("display", "none");
				$(lid).setStyle("display", "block");
				bloc_prec = lid;
      }
			
			
function item_switch(these){
	if(mon_menu_prec != "") {
		$(mon_menu_prec).removeClass("essence_item_hover");
	}
	$(these).addClass("essence_item_hover");
	mon_menu_prec = these;
}


function menu_switch2(these){		
				lid = these.id;
				lid = lid.replace(/menu_/,'');
				$(bloc_prec2).setStyle("display", "none");
				$(lid).setStyle("display", "block");
				bloc_prec2 = lid;
      }
			
function item_switch2(these){
	if(mon_menu_prec2 != "") {
		$(mon_menu_prec2).removeClass("essence_item_hover");
	}
	$(these).addClass("essence_item_hover");
	mon_menu_prec2 = these;
}

window.addEvent('domready',function() { 
		agenda.init();
	}
);



