/**
 * Ajout des évènements d'onglets
 */
function initFieldsetOnglets( elements )
{
	if( elements.select('a.active').length==0 )
	{
		elements.select('a').first().addClassName('active');
	}

	var regexp = /^(.*)#(.*)$/;
	var match = regexp.exec(elements.select('a.active').first().href);
	$(match[2].split("/").last()).show();
	elements.select('a[class!=active]').each(function(a)
	{
		var regexp = /^(.*)#(.*)$/;
		var match = regexp.exec(a.href);
		$(match[2].split("/").last()).hide();
	});

	
	elements.select('a').each(function(e)
	{
		e.observe('click',fieldsetOngletsClickEvent);
	});
}

/**
 * Gestion des onglets
 */
function fieldsetOngletsClickEvent( event )
{
	var href = fieldsetOngletsClick( Event.element( event ) );
	
	/* Permet de fixer un problème avec IE6 lié au drilldown * /
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @* /
	if(IE6)
	{
		Event.stop( event );
	}
	*/
	fixAnchorTitleBug();
	Event.stop( event );
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/
	if( href!=false && !IE6 )
	{
		document.location = '#'+href+"/";
	}
}

function fieldsetOngletsClick( element )
{
	if( !$(element) || !element.hasAttribute("href") || element.hasClassName('disabled') ) return false;
	var currentHref = element.getAttribute("href");
	var regexp = /^(.*)#(.*)$/;
	
	var match = regexp.exec(currentHref);
	var currentId = Array();
	match[2].split("/").each(function(e)
	{
		currentId.push(e);
		fieldsetOngletsShow( currentId )
	});
	fieldsetOngletsClickCallBack( currentId );
	return match[2];
}

function fieldsetOngletsShow( array )
{
	var activeLink = $$('a[href=#'+array.join('/')+']').first();
	if( activeLink!=undefined )
	{
		activeLink.addClassName('active');
		otherLinks = activeLink.up('ul').select('li>a').without(activeLink);
		otherLinks.each(function(e)
		{
			e.removeClassName('active');
			if( e.getAttribute('href')!=null )
			{
				var regexp = /^(.*)#(.*)$/;
				var match = regexp.exec(e.getAttribute('href'));
				hid = match[2].split('/').last();
			}
			
			if( $(hid) )
			{
				$(hid).hide();
			}
		});
	}
	
	var activeFrame = $(array.last());
	if( activeFrame )
		activeFrame.show();
}

// Fix an obscure bug in which IE appends named anchors in the URL (i.e., #location)
// to the page title once for every Flash object in the page.
// See : http://bugs.adobe.com/jira/browse/FP-240
document.originalTitle = document.title;
function fixAnchorTitleBug() {
	if(!Prototype.Browser.IE) return;
	try{
	/*
		var location = document.location.href;
		var regexp = /^(.*)#(.*)$/;
		var result = regexp.exec(location);
		if( result!=null && typeof result[2]!='undefined' && result[2]!=null )
			var titleAnchor = ' #' + result[2];
		else
			var titleAnchor = '';
		document.title = document.originalTitle + titleAnchor;
	if(console!=undefined) console.log(titleAnchor,document.title,location);
	*/
		document.title = document.originalTitle;
	}catch(e){}
}
//window.attachEvent('onload', fixAnchorTitleBug);


