var contenuBloc = 'contenu' ; 
var headingTag = 'H3' ; 
var condition = new Array('P' , 'petiteFonction' ); 
var depart = 'H1'; 
var heightMax = 300 ; 
var articlesInterdits = new Array(42 ,43,44,45 ); 

function makeSommaire() { 
	if( document.getElementById(contenuBloc) ) { 
		var contenu = document.getElementById(contenuBloc);
		//var gauche = document.getElementById('menuGauche');
		var sommaire ='' ; 
		var divs = contenu.getElementsByTagName('DIV');
		sommaire +="<ul>" ; 
	
		var first = true ; 
		for (var i=0; i<divs.length; i++) { 
			//alert(i+ divs[i].className  ) ;
			// juste les marqués
			if (divs[i].className=='hide'){
				if (first==true){
					divs[i].className='showed' ; 
					first=false;
				} else { 
					divs[i].className='hided' ; 
				}
				// identifier notre div 
				divs[i].id='hided'+i;
				var titre = divs[i].getElementsByTagName(headingTag); 
				if( titre[0] ){
					//sommaire += '<li><a onclick="show(\''+divs[i].id+'\');" href="#'+divs[i].id+'" >'+titre[0].innerHTML+'</a></li>';
					sommaire += '<li><a onclick="show(\''+divs[i].id+'\');return false;" href="#" >'+titre[0].innerHTML+'</a></li>';
				}
			}
		}
		sommaire += '</ul>' ; 
		// quand tout est fini 
		sommaireDiv = document.createElement("div");
		sommaireDiv.id='sommaire' ; 
				/*if( document.getElementById('visuel')){ 
					// apres le visuel
					insertAfter(content , sommaireDiv , document.getElementById('visuel')); 
				} else if ( document.getElementById('introContenu') ) { 
					//apres le contenu si pas de visuel
					insertAfter(content , sommaireDiv , document.getElementById('introContenu')); 
				} else {*/
					// juste avant le premier div identifié selon la regle
		
		
				/*} */
		if (sommaireDiv){
			sommaireDiv.innerHTML= sommaire ;
		}	
		
		for (var k=0; k<30; k++) { 
			if( document.getElementById('hided'+k) ){ 
				//alert('hided'+k)  ; 
				//sommaire2 = sommaireDiv.cloneNode(true);
				contenu.insertBefore ( sommaireDiv  ,  document.getElementById('hided'+k)) ;
				//gauche.appendChild (sommaireDiv ) ;
				break;
			} 
		}
		//alert(sommaireDiv.innerHTML);
		//alert(idLien);
		if (idLien!=0)
		{
			show('hided'+idLien);

		}
		
	} 
}

function makePager() { 
	if( document.getElementById(contenuBloc) ) { 
		var contenu = document.getElementById(contenuBloc);
		var gauche = document.getElementById('menuGauche');
		var sommaire ='' ; 
		var divs = contenu.getElementsByTagName('div');
		sommaire +="<p id='pagination'>page " ; 
		var first = true ; 
		var j=1 ; 
		for (var i=0; i<divs.length; i++) { 
			
			// juste les marqués
			if (divs[i].className=='hide'){
				if (first==true){
					divs[i].className='showed' ; 
					first=false;
				} else { 
					divs[i].className='hided' ; 
				}
				// identifier notre div 
				divs[i].id='hided'+i;
				var titre = j; 
				sommaire += '<a onclick="show(\''+divs[i].id+'\');return false;" href="#" >'+titre+'</a>';
				j++;
			}
		}
		sommaire += '</p>' ; 
		// quand tout est fini 
		sommaireDiv = document.createElement("div");
		sommaireDiv.id='sommaire' ; 
				/*if( document.getElementById('visuel')){ 
					// apres le visuel
					insertAfter(content , sommaireDiv , document.getElementById('visuel')); 
				} else if ( document.getElementById('introContenu') ) { 
					//apres le contenu si pas de visuel
					insertAfter(content , sommaireDiv , document.getElementById('introContenu')); 
				} else {*/
					// juste avant le premier div identifié selon la regle
		
		
				/*} */
		if (sommaireDiv){
			sommaireDiv.innerHTML= sommaire ;
		}	
		
		for (var k=0; k<30; k++) { 
			if( document.getElementById('hided'+k) ){ 
				//alert('hided'+k)  ; 
				//sommaire2 = sommaireDiv.cloneNode(true);
				contenu.insertBefore ( sommaireDiv  ,  document.getElementById('hided'+k)) ;
				//gauche.appendChild (sommaireDiv ) ;
				break;
			} 
		}

	} 
}

function show(id) { 
		hideshowed();
		//juste changer la classe
		if (document.getElementById(id)){
			document.getElementById(id).className='showed';
		}
}
function hideshowed() { 
		var divs = document.getElementById(contenuBloc).getElementsByTagName('div');
		for (var i=0; i<divs.length; i++) { 
			if (divs[i].className=='showed'){
				divs[i].className='hided' ; 
			}
		}
}

/* Met les div.hide automatiquement à partir des h2 ( un div contiendra un seul h2 )   */ 
function divsfromsemantic() { 
	//alert(contenuBloc);
	var texte = document.getElementById(contenuBloc) ;
	//texte.style.display='none';
	//alert(texte);
	if (texte){
		var titres = texte.getElementsByTagName(headingTag);
			
		for (var k=0; k<titres.length; k++) { 
			hideDiv = document.createElement("div");
			hideDiv.className='hide' ;
			// on insert notre div avant le titre
			texte.insertBefore ( hideDiv  , titres[k]) ;
			// on se positionne sur l'element apres le titre
			next = titres[k].nextSibling;
			var titre = texte.removeChild(titres[k]);
			// on met notre titre dans notre div
			hideDiv.appendChild(titre);
			//alert(condition[1] );
			/*alert( hideDiv.innerHTML ) ; 
			alert( next.nodeType); */
			// tant que ce n'est pas un titre ( demandé ) 
			//alert(next.nodeType);
			//alert(next.nodeType);
			while (next!=null)
			{
				//alert( 'in'); 
				next = texte.removeChild(next);
				
				
				hideDiv.appendChild(next);
				//alert(hideDiv.innerHTML);
				if(hideDiv.nextSibling){
					next=hideDiv.nextSibling;
					if (  next.tagName==headingTag || ( next.tagName == condition[0] && next.id==condition[1] )  )
					{
						//alert('je marrete'+ ' element : ' +next.tagName + next.id);

						break;
					}
				}else next=null;
				
				
				
				
				
			}

			
		}
		//texte.style.display='block';
		makeSommaire();
	}
}

function getElementsBySelector(selector)
{
	var tokens = selector.split(' ');
	var currentContext = new Array(document);
	for(var i=0;i<tokens.length;i++)
	{
		token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');
		if(token.indexOf('#') > -1)
		{
			var bits = token.split('#');
			var tagName = bits[0];
			var id = bits[1];
			var element = document.getElementById(id);
			if(tagName && element.nodeName.toLowerCase() != tagName)
				return new Array();
			currentContext = new Array(element);
			continue;
		}

		if(token.indexOf('.') > -1)
		{
			var bits = token.split('.');
			var tagName = bits[0];
			var className = bits[1];
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
				if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b')))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
	    }

		if(token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/))
		{
			var tagName = RegExp.$1;
			var attrName = RegExp.$2;
			var attrOperator = RegExp.$3;
			var attrValue = RegExp.$4;
			if(!tagName)
				tagName = '*';

			var found = new Array;
			var foundCount = 0;
			for(var h=0;h<currentContext.length;h++)
			{
				var elements;
	        	if(tagName == '*')
					elements = currentContext[h].all ? currentContext[h].all : currentContext[h].getElementsByTagName('*');
				else
					elements = currentContext[h].getElementsByTagName(tagName);

				for(var j=0;j<elements.length;j++)
					found[foundCount++] = elements[j];
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			var checkFunction;
			switch(attrOperator)
			{
				case '=':
					checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
					break;
				case '~':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
					break;
				case '|':
					checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
					break;
				case '^':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
					break;
				case '$':
					checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
					break;
				case '*':
					checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
					break;
				default :
					checkFunction = function(e) { return e.getAttribute(attrName); };
			}

			currentContext = new Array;
			var currentContextIndex = 0;
			for(var k=0;k<found.length;k++)
			{
				if(checkFunction(found[k]))
					currentContext[currentContextIndex++] = found[k];
			}

			continue;
		}

		tagName = token;
		var found = new Array;
		var foundCount = 0;
		for(var h=0;h<currentContext.length;h++)
		{
			var elements = currentContext[h].getElementsByTagName(tagName);
			for(var j=0;j<elements.length; j++)
				found[foundCount++] = elements[j];
		}

		currentContext = found;
	}

	return currentContext;
} 

/* Met les div.hide automatiquement par rapport à une hauteur definie    */ 
function divsFromHeight() { 
	var texte = document.getElementById(contenuBloc) ;
	next=getElementsBySelector(depart)[0].nextSibling;
	while (next!=null){
		hideDiv = document.createElement("div");
		hideDiv.className='hide' ;
		// on insert notre div avant le premier element
		texte.insertBefore ( hideDiv  , next) ;
		// on se positionne sur l'element d'apres
		
		

		/*alert( hideDiv.innerHTML ) ; 
		alert( next.nodeType); */
		// tant que ce n'est pas un titre ( demandé ) 
		while (hideDiv.offsetHeight<heightMax && next!=null){ 
			
			//alert(hideDiv.offsetHeight );
			next = texte.removeChild(next);
			// on met notre element dans notre div
			hideDiv.appendChild(next);
			next=hideDiv.nextSibling;
			
			if ( next.tagName == condition[0] && next.id==condition[1]  )
			{
				break;
			}
			
		}
		if ( next.tagName == condition[0] && next.id==condition[1]  )
			{
				break;
			}

		
	}
}

function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}



/*addEvent(window, 'load', makePager);
addEvent(window, 'load', makePager);
addEvent(window, 'load', divsFromHeight );

addEvent(window, 'load', waitAndMakeSommaire);

function waitAndMakeSommaire() { 
window.setTimeout('makeSommaire()',1);
}
*/
function sommairise(){ 
	var chaine = 'article(0';
	// on ne sommairise pas tt le temps
	for (i=0 ;i<articlesInterdits.length ;i++ ){
		chaine +='|'+articlesInterdits[i];
	}
	chaine+=')';
	reg = new RegExp(chaine );

	if (document.body.className.search(reg)== -1 ){
		divsfromsemantic();
		//addEvent(window, 'load', makeSommaire);
	}
}
var Lurl = window.location;
//alert( url); 
Lurl = Lurl + '&test';
//alert( url); 
//alert(window.location); 
var idLien = 0;
var regSommaire = new RegExp("(.*)voirparagraphe(\\d+)&test") ; 
//alert(Lurl.search(regSommaire));
if (Lurl.search(regSommaire)>=0){
	var idLien =  Lurl.replace(regSommaire,'$2');
	idLien= idLien-1;
}


addEvent(window, 'load', sommairise );
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, true);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}
