// 
// Regles pour le site www.petitegraine.com
//
// v1.0 - 26/01/09
//
// 
var currenteffect = null;
var currentcible = null;
var nextcible = null;


function fademenu() {
	
	new Effect.Opacity('menubg', { to: 0, duration: 0.8 });
	currentcible = null;
	currenteffect = null;
	nextcible = null;
}

var localrules = {
	'TD.navitem' : function(e) {
		e.onmouseover = function(){
			nextcible = e.firstDescendant().rel;
			$A(e.select('img')).each(function(image) {
				image.src = 'themes/images/menu/' + e.firstDescendant().rel + '-on.png';
			});
			//alert($(e).positionedOffset('bandeau'));
			//$('menubg').show();
			new Effect.Opacity('menubg', { to: 1, duration: 0.5 });
			if(nextcible != currentcible) {
				//alert(currenteffect + nextcible + ' vs ' + currentcible);
				var newx = $(e).positionedOffset('bandeau').left + ($(e).getWidth() / 2);
				currenteffect = new Effect.Move('menubg', 
					{ x: newx, y: 3, transition: Effect.Transitions.spring, mode: 'absolute', 
					afterFinish: function(effect) { 
						currenteffect = null;
					}});
			}
			//e.setStyle('background-image: url(themes/images/menu-bg.png)');
			return false;
		}
		e.onmouseout = function(){
			nextcible = null;
			$A(e.select('img')).each(function(image) {
				image.src = 'themes/images/menu/' + e.firstDescendant().rel + '.png';
			});
			new Effect.Opacity('menubg', { to: 1, duration: 0.4, afterFinish: function(effect) { 
				if(nextcible == null) {
					new Effect.Opacity('menubg', { to: 0, duration: 0.4 });
					currentcible = null;
					currenteffect = null;
				}}});
			
		//	$('menubg').hide();
			//alert(currenteffect + nextcible + ' vs ' + currentcible);
			/*if(currenteffect == null) {
				currenteffect = new Effect.Move('menubg', 
				{ x: newx, y: 3, transition: Effect.Transitions.spring, mode: 'absolute', 
				afterFinish: function(effect) { 
					currenteffect = null;
				}});
			}*/
			return false;
		}
		e.onclick = function() {
			$A(e.select('a')).each(function(aa) {
				
				document.location = aa.href;
				return false;
			});
		}
	}
}

Behaviour.register(localrules);
