var collapseAnchor = function()
{	
	if(document.getElementById("submenu"))
	{
		var zone = document.getElementById("contentbar");
		var text = zone.innerHTML;
		
		// Temporary remove lead title (h2)
		var i = text.toLowerCase().indexOf("</h2>");
		var lead = text.substring(0, i+5);
		text = text.substr(i+5)+"\f";
	
		// Add <div> and <img>
		var reg = new RegExp("(<h3>)(([^p]|p)*?</h3>)(([^p]|p)*?)((<a *?id=)|(<a href=\"#\")|\f)", "gi");	
		text = text.replace(reg,"$1<img src=\"./icones/minus.png\" alt=\"\" class=\"gauche\" />$2<div>$4</div>$6");
		
		// Add id
		reg = new RegExp("(<a.*?id=[\"]{0,1})(.*?)([\"]{0,1}[ >]{1}([^p]|p)*?)(<h3)(([^p]|p)*?<img)(([^p]|p)*?<div)", "gi");
		text = text.replace(reg, "$1$2$3$5 id=\"$2_button\" $6 id=\"$2_img\" $8 id=\"$2_div\" ");
		
		// Restore lead title (h2)
		text = lead + text.substring(0, text.length-1);
		zone.innerHTML = text;
		
		// collapse all anchor sections except the one selected
		this.collapse = function()
		{ 
			var n = document.location.href.indexOf("#");
			if(n > 0)
			{
				var anchor = document.location.href.substr(n+1);
				var div = document.getElementById(anchor+"_div");
				
				if(anchor=="")
				{
					this.expendAll();
				}
				// if selected anchor section dont exist nothing happen
				else if(div)
				{
					var arr = zone.getElementsByTagName("div");
					for(var i=0; i<arr.length; i++)
					{
							arr[i].style.display = "none";
					}
					
					arr = zone.getElementsByTagName("h3");
					for(var i=0; i<arr.length; i++)
					{
							arr[i].getElementsByTagName("img")[0].src = "./icones/plus.png";
					}
					
					document.getElementById(anchor+"_img").src = "./icones/minus.png";
					div.style.display = "block";
					
					// Scroll to the anchor
					var o = document.getElementById(anchor+"_button");
					var divtop = 0;
					do
					{
						divtop += o.offsetTop;
					}
					while((o = o.offsetParent));
					
					window.scrollTo(0, divtop-10);
				}
			}
			// if no anchor section is selected then all sections are expended
			else
			{
				this.expendAll();
			}
		}
		
		this.expendAll = function()
		{
			var arr = zone.getElementsByTagName("div");
			for(var i=0; i<arr.length; i++)
			{
					arr[i].style.display = "none";
			}
			
			arr = zone.getElementsByTagName("h3");
			for(var i=0; i<arr.length; i++)
			{
					arr[i].getElementsByTagName("img")[0].src = "./icones/plus.png";
			}
		}
		
		// Expend/Collapse switch
		this.switchcollapse = function(e)
		{
			var anchor = "";
			if(this.id)
				anchor = this.id.substring(0, this.id.indexOf("_"));
			else
				anchor = e.srcElement.id.substring(0, e.srcElement.id.indexOf("_"));
			var div = document.getElementById(anchor+"_div");

			
			if(div.style.display == "block")
			{
				expendAll();
			}
			else
			{
				expendAll();
				document.getElementById(anchor+"_img").src = "./icones/minus.png";
				div.style.display = "block";

				// Scroll to the anchor
				var o = document.getElementById(anchor+"_button");
				var divtop = 0;
				do
				{
					divtop += o.offsetTop;
				}
				while((o = o.offsetParent));
				
				window.scrollTo(0, divtop-10);
			}
		}
		
		// Add h3 onClick eventhandler
		var arr = zone.getElementsByTagName("h3");
		for(var i=0; i<arr.length; i++)
		{
			try
			{
				arr[i].style.cursor = "pointer";
			}
			catch(e){}
			
			if(arr[i].addEventListener)
				arr[i].addEventListener("click", this.switchcollapse, false);
			else if(arr[i].attachEvent)
				arr[i].attachEvent("onclick", this.switchcollapse);
		}
		
		// Periodicaly check the change of the selected anchor
		// And event should be better but i didnt get anything work (watch)
		this.url = document.location.href;
		this.checkUrl = function()
		{
			if(document.location.href != this.url)
			{
				this.collapse();
				this.url = document.location.href;
			}
			setTimeout("checkUrl()",250);
		}
		checkUrl();
		
		// Initiate
		this.collapse();
	}
}

if(window.attachEvent)
	window.attachEvent("onload", collapseAnchor);
else
	window.addEventListener("load", collapseAnchor, false);
	
