var MenuTab = function( target ) {
	this.target = target;
	this.enabled = ( this.target.src.lastIndexOf( '_off.' ) != -1 );

  this.onTabOver = function( e ) {
		if ( this.enabled ) {
			var oRegExp = new RegExp( '_off\.' );
			if ( oRegExp.test( this.target.getAttribute( 'src' ) ) )
				this.target.setAttribute( 'src', this.target.getAttribute( 'src' ).replace( oRegExp, '_hover.' ) );
		}
  }
  this.onTabOut = function( e ) {
		if ( this.enabled ) {
			var oRegExp = new RegExp( '_hover\.' );
			if ( oRegExp.test( this.target.getAttribute( 'src' ) ) )
				this.target.setAttribute( 'src', this.target.getAttribute( 'src' ).replace( oRegExp, '_off.' ) );
		}
  }

	this.target.onmouseover = Delegate.create( this, 'onTabOver' );
	this.target.onmouseout = Delegate.create( this, 'onTabOut' );
}

function applyMenuBehaviour() {
	var menu = document.getElementById( 'page_top_navigation' );
	if ( isSet( menu ) ) {
		var LIs = menu.getElementsByTagName( 'li' );
		for( var i=0; i<LIs.length; i++ ) {
			var images = LIs[ i ].getElementsByTagName( 'img' );
			for( var j=0; j<images.length; j++ ) {
				new MenuTab( images[ j ] );
			}
		}
	}
}

var Animation = (function(){
	var _animColl = [];
	var _anim = {
active: true,
		addStep: function(obj)
		{
			_animColl.push(obj);
		}
	};
	
	var lsColl = null;
	var detColl = null;
	
	function displayStep()
	{
		var currentLink = Event.element(arguments[0]);
		var regUri = /\?etape=(\d)/;
		
		if(!!currentLink)
		{
			var resElem = null;
			
			var resUri = currentLink.href.match(regUri);
			if(resUri.length >= 2)
			{
				if(resUri[1] < _animColl.length)
				{
					resElem = _animColl[resUri[1]];
				}
			}
			//alert(resElem)
			if(resElem != null)
			{
				var currentItem = Event.findElement(arguments[0],'li');
				if(!!currentItem)
				{
					lsColl.each(function(elem)
					{
						//elem.observe('click',displayStep);
						if(elem == currentItem)
						{
							elem.addClassName('active');
						}
						else
						{
							elem.removeClassName('active');
						}
					});
					
					detColl.each(function(elem)
					{
						//alert(elem.id+' :: visual-entry-'+resUri[1]+' // '+detColl.length)
						if(elem.id == 'visual-entry-'+resUri[1])
						{
							elem.addClassName('active');
						}
						else
						{
							elem.removeClassName('active');
						}
					});
					
					$('visual-picture').src = resElem.image;
				}
			}
		}
		Event.stop(arguments[0]);
	}
	
	function init()
	{
		var znColl = Element.select(document,'ul.visual-nav a');
		lsColl = Element.select(document,'ul.visual-nav li');
		detColl = Element.select(document,'ul.visual-detail li');
	
		znColl.each(function(elem)
		{
			elem.observe('click',displayStep);
		});
	}
	
	if(_anim.active)
	{
	addListener( window, 'load', init );
	}
	
	return _anim;
})();

addListener( window, 'load', applyMenuBehaviour );