/* MENU SLIDERS */
/****************/

//google.load("mootools", "1.2.2") ;

// Find all link elements and add an onfocus attribte and value
function hideFocusBorders(){
	var theahrefs = document.getElementsByTagName("a");
	if (!theahrefs){return;}
	for(var x=0;x!=theahrefs.length;x++) {
		theahrefs[x].onfocus = function stopLinkFocus(){this.hideFocus=true;};
	}
}

function MenuFX() {
	hideFocusBorders() ;
	menuSliders = new Array() ;
	menuToggler = $('interrupteur') ;
	currentPage = window.location.href ;

	menuItems =	[	{"name": "manifeste", "duration": 1000, "onglet": -119, "status": false},
					{"name": "marketingoperationnel", "duration": 1250, "onglet": -117, "status": false},
					{"name": "communicationevenementielle", "duration": 1100, "onglet": -117, "status": false},
					{"name": "lacrea", "duration": 1200, "onglet": -114, "status": false},
					{"name": "lesassocies", "duration": 1050, "onglet": -112, "status": false},
					{"name": "lequipe", "duration": 1300, "onglet": -127, "status": false},
					{"name": "outils", "duration": 1150, "onglet": -130, "status": false}
				] ;

	menuMarker = new Element('div', { 'id': 'menumarker' }) ;
	menuMarker.setStyles({
		'visibility': 'visible',
		'opacity': 0
	}) ;
	fadeMarker = new Fx.Morph(menuMarker, {duration:200, link:'cancel'}) ;
	toggler = GetCookie('toggler') ;

	var addSlide = function(item, index) {
		// Set Effect
		menuSliders[index] = new Fx.Morph($('navtop_'+item.name).getElement('a'), {link:'cancel'}) ;
		// Set Map Activator
		if ($('plan_'+item.name) != null) {
			$('plan_'+item.name).addEvents({
				'mouseenter': function(e) {
					e.stop() ;
					this.setStyle('cursor', 'pointer') ;
					menuSliders[index].options.transition = Fx.Transitions.Bounce.easeOut ;
					menuSliders[index].options.duration = item.duration-200 ;
					if (menuItems[index].status == false) {
						menuItems[index].status = true ;
						menuSliders[index].cancel().start({'margin-top': 0}) ;
					}
				},
				'mouseleave': function(e) {
					e.stop() ;
					this.setStyle('cursor', 'pointer') ;
					if (menuToggler.className == 'off') {
						menuSliders[index].options.transition = Fx.Transitions.linear ;
						menuSliders[index].options.duration = item.duration/2 ;
						menuItems[index].status = false ;
						menuSliders[index].cancel().start({'margin-top': item.onglet}) ;
					}
				}
			}) ;
		}

		// Set Menu Activator
		$('navtop_'+item.name).getElement('a').addEvents({
			'mouseenter': function(e) {
				e.stop() ;
				if ($('plan_'+item.name) != null) {
					$('plan_'+item.name).className = 'on' ;
				}
				menuSliders[index].options.transition = Fx.Transitions.Bounce.easeOut ;
				menuSliders[index].options.duration = item.duration-200 ;
				if (menuItems[index].status == false) {
					menuItems[index].status = true ;
					menuSliders[index].cancel().start({'margin-top': 0}) ;
				}
			},
			'mouseleave': function(e) {
				e.stop() ;
				if ($('plan_'+item.name) != null) {
					$('plan_'+item.name).className = '' ;
				}
				menuSliders[index].options.transition = Fx.Transitions.linear ;
				menuSliders[index].options.duration = item.duration/2 ;
				menuItems[index].status = false ;
				menuSliders[index].cancel().start({'margin-top': item.onglet}) ;
			}	
		}) ;

		// Set menu Items visibles
		$('navtop_'+item.name).setStyles({
			 visibility: 'visible'
		});
							
		if (item.href+window.location.search == currentPage) {
			$('navtop_'+item.name).getElement('a').setStyle('margin-top', 0) ;
			$('navtop_'+item.name).getElement('a').removeEvents('mouseenter').removeEvents('mouseleave') ;
			if ($('plan_'+item.name) != null) {
				$('plan_'+item.name).removeEvents('mouseenter').removeEvents('mouseleave') ;
			}
			SetCookie(item.name, 'done') ;
			menuItems[index].status = true ;
			if (toggler != null) {
				menuMarker.setStyle('opacity', 1) ;
				menuMarker.inject($('navtop_'+item.name)) ;
			}
		}
	}

	// Définit l'apparition des menus (Slide Down).
	menuItems.each(function(item, index) {
		menuItems[index]['href'] = $('navtop_'+item.name).getElement('a').getAttribute("href") ;
		$('navtop_'+item.name).getElement('a').setStyle('margin-top', item.onglet) ;
		addSlide(item, index);
		var done = GetCookie(item.name) ;
		if (done != null && item.href+window.location.search != currentPage) {
			var tt = $('navtop_'+item.name).getElement('a').className = 'done' ;
		}
	});

	// Set Toggler
	menuToggler.addEvent('click', function(e) {
		e = new Event(e) ;
		if (menuToggler.className == 'off') {
			menuToggler.className = 'on' ;
			SetCookie('toggler', 'on') ;
			menuItems.each(function(item, index) {
				menuSliders[index].options.transition = Fx.Transitions.Bounce.easeOut ;
				menuSliders[index].options.duration = item.duration ;
				menuSliders[index].cancel().start({'margin-top': 0}) ;
				$('navtop_'+item.name).getElement('a').removeEvents('mouseenter').removeEvents('mouseleave') ;
				if ($('plan_'+item.name) != null) {
					$('plan_'+item.name).removeEvents('mouseenter').removeEvents('mouseleave') ;
				}
				if (item.href+window.location.search == currentPage) {
					menuMarker.inject($('navtop_'+item.name)) ;
					fadeMarker.cancel().start({'opacity' : 1});
				}
			}) ;
		}
		else {			
			menuToggler.className = 'off' ;
			DeleteCookie('toggler') ;
			menuItems.each(function(item, index) {
				menuSliders[index].cancel() ;
				if (item.href+window.location.search == currentPage) {
					fadeMarker.cancel().start({'opacity': 0}) ;
				}
				if (item.href+window.location.search != currentPage) {
					addSlide(item, index) ;
					menuSliders[index].options.transition = Fx.Transitions.linear ;
					menuSliders[index].options.duration = item.duration/2 ;
					menuSliders[index].start({'margin-top': item.onglet}).chain(function() {
						DeleteCookie(item.name) ;
						$('navtop_'+item.name).getElement('a').className = null ;
					});
				}
			}) ;
		} ;
		e.stop() ;
	}) ;

	if (toggler != null) {
			menuToggler.className = 'on' ;
			menuItems.each(function(item, index) {
				$('navtop_'+item.name).getElement('a').removeEvents('mouseenter').removeEvents('mouseleave') ;
				$('navtop_'+item.name).getElement('a').setStyle('margin-top', 0) ;
			}) ;
	}
}

window.addEvent('domready', function() {
	MenuFX();
});
//google.setOnLoadCallback(OnLoad) ;