// JavaScript Document
// JavaScript Document
/*
var img_fond = RELPATH + "/shared/skins/pratis_med/front/images/home/onglet-inactif.jpg";
var img_fond_on = RELPATH + "/shared/skins/pratis_med/front/images/home/onglet-actif.jpg";
var fond_bloc = RELPATH + "/shared/skins/pratis_med/front/images/home/fond_bloc.jpg";
var activeTab = 'VMI';
var tab_width = 110;
var maxHeight = 250;
var maxWidth = 613;
var marginLeft = 0;
*/

function trim(string)
{
	var regExpBeginning = /^\s+/;
	var regExpEnd       = /\s+$/;
    return string.replace(regExpBeginning, "").replace(regExpEnd, "");
}

function showTab(tab)
{

	var onglets = document.getElementsByTagName('div');
	var position = 0;
	for(var i=0; i<onglets.length; i++)
	{
	    if (onglets[i].id == 'header')
		{
		    var headerElement = onglets[i];

		    headerElement.onmouseover = function()
		    {
		        link = "http://" + window.location.hostname + window.location.pathname + "?o=" + trim(this.firstChild.nodeValue);
				//window.location = link;
				showTab(trim(this.firstChild.nodeValue));
		    };
		    /*
			divElement.onmouseover = function ()
			{
			    this.style.cursor = 'pointer';
				this.style.backgroundImage = 'url("fond-inactif-survol.png")';
			};
			*/
			/*
			headerElement.onmouseout = function ()
			{
			    this.style.cursor = 'defaut';
				this.style.backgroundImage = 'url(' + img_fond + ')';
			};
			*/

			//var tableElement = headerElement.parentNode.childNodes;
			var contentElement = headerElement.parentNode.childNodes[1];
			ongletElement = headerElement.parentNode;
			
		    if (tab != trim(headerElement.firstChild.nodeValue))
		    {
		        //tableElement.item(j).parentNode.style.fontSize = '12px';
		        //tableElement.item(j).parentNode.style.fontWeight = 'normal';
		        headerElement.style.fontWeight = 'normal';
		        headerElement.style.backgroundImage = 'url(' + img_fond + ')';
		        headerElement.style.fontSize = '12px';
		        headerElement.style.color = '#b0b0b0';
		        headerElement.style.height = '14px';
		        headerElement.style.top = '-45px';
		        headerElement.style.zIndex = '1';
		        headerElement.style.left = ''+position+'px';
		        headerElement.style.cursor = 'default';
		        headerElement.style.textDecoration = 'none';
		        contentElement.setAttribute("cssText", "z-index: 1;");
		        ongletElement.setAttribute("cssText", "z-index: 1;");

		        if (document.all && !window.opera) // pour internet explorer
		        {
		            headerElement.style.setAttribute("cssText", "cursor: default; text-decoration: none; color: #b0b0b0; background-image: url(" + img_fond + "); min-width: " + tab_width + "px; width: " + tab_width + "px; left: " + position + "px; height: 14px; top: -45px; font-weight: normal; font-size: 12px; z-index: 1; ");
		            ongletElement.style.setAttribute("cssText", "z-index: 1;");
				}
		        contentElement.style.display = 'none';
			}
			else if (tab == trim(headerElement.firstChild.nodeValue))
			{
		        headerElement.style.fontWeight = 'bold';
		        headerElement.style.backgroundImage = 'url(' + img_fond_on + ')';
		        headerElement.style.fontSize = '12px';
		        headerElement.style.color = '#0159e8';
		        headerElement.style.height = '19px';
		        headerElement.style.top = '-49px';
			    headerElement.style.zIndex = '500';
		        headerElement.style.left = ''+position+'px';
		        headerElement.style.cursor = 'default';
		        headerElement.style.textDecoration = 'none';
		        ongletElement.setAttribute("cssText", "z-index: 500;");
		        contentElement.setAttribute("cssText", "z-index: 500;");

		        if (document.all && !window.opera) // pour internet explorer
		        {
		            headerElement.style.setAttribute("cssText", "cursor: default; text-decoration: none; color: #0159e8; background-image: url(" + img_fond_on + "); min-width: " + tab_width + "px; width: " + tab_width + "px; left: " + position + "px; height: 19px; top: -49px; font-weight: bold; font-size: 12px; z-index: 500;");
		            ongletElement.style.setAttribute("cssText", "z-index: 500;");
		            //tableElement.item(j).parentNode.style.setAttribute("cssText", "font-size: 14px; font-weight: normal; z-index: 500;");
				}
		        contentElement.style.display = 'block';

			}


		    position = position + tab_width;
		}
	}
}

function getX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
		} while (obj = obj.offsetParent);
	}
	return curleft+marginLeft;
}

function getY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return curtop;
}



var doc = document.getElementsByTagName("tabgroup");

for(var i=0; i<doc.length; i++)
{
	var parent = doc[i].parentNode;
	
	var nElement = document.createElement("div");
	nElement.setAttribute("id", "entourage");
	nElement.setAttribute("name", "entourage");
	
	var elementInnerHTML = doc[i].innerHTML;
	nElement.innerHTML = elementInnerHTML;
	// ajout des noeuds fils
	for(var j=0; j<doc[i].childNodes.length; j++)
	{
		var nNode = doc[i].childNodes[j].cloneNode(true);
		doc[i].removeChild( doc[i].childNodes[j] );
		nElement.appendChild(nNode);
	}
	doc[i].parentNode.replaceChild(nElement, doc[i]);

	//nElement.setAttribute("style", "left: " + getX(parent) + "px; top: " + getY(parent) + "px;");
	if (document.all && !window.opera) // pour ie
	{
	    var st = "left: " + getX(parent) + "px; top: " + getY(parent) + "px;";
		nElement.style.setAttribute("cssText", st);
	}

}

var doc = document.getElementsByTagName("tab");
var tabgroup;
while (doc.length > 0)
{
	nb = 0;

	var nElement = document.createElement("div");
	nElement.setAttribute("class", "onglet");
	nElement.setAttribute("id", "onglet");
	nElement.setAttribute("name", "onglet");

	var nHeader = document.createElement("div");
	nHeader.setAttribute("id", "header");
	nHeader.setAttribute("name", "header");
	nHeader.innerHTML = doc[nb].getAttribute("name");
	nElement.appendChild(nHeader);
	nHeader.setAttribute("style", "left: " + getX(nElement) + "px; top: " + getY(nElement) + "px; min-width: " + tab_width + "px; width: " + tab_width + "px;");
	if (document.all && !window.opera) // pour ie
	{
		nHeader.style.setAttribute("cssText", "left: " + getX(nElement) + "px; top: " + getY(nElement) + "px; min-width: " + tab_width + "px; width: " + tab_width + "px;");
	}

	var nContent = document.createElement("div");
	nContent.setAttribute("id", "content");
	nContent.setAttribute("name", "content");
	nContent.setAttribute("style", "background-image: url(" + fond_bloc + "); min-width: " + maxWidth + "px; width: " + maxWidth + "px; min-height: " + maxHeight + "px; height: " + maxHeight + "px;");
	if (document.all && !window.opera) // pour ie
	{
		nContent.style.setAttribute("cssText", "background-image: url(" + fond_bloc + "); min-width: " + maxWidth + "px; width: " + maxWidth + "px; min-height: " + maxHeight + "px; height: " + maxHeight + "px;");
	}

	nContent.innerHTML = doc[nb].getAttribute("Content");
	nElement.appendChild(nContent);

	doc[nb].parentNode.replaceChild(nElement, doc[nb]);
	tabgroup = nElement.parentNode;
}

if (activeTab == null || activeTab == "")
{

	var docs = document.getElementsByName("onglet");
	index = Math.round( Math.random(0) * (docs.length-1) );
	activeTab = trim(docs[index].firstChild.firstChild.nodeValue);
}

showTab(activeTab);

if (document.all && !window.opera)
{
	// on ajoute un onglet invisible sur ie car probleme lorsqu'on scroll le document
	var nElement = document.createElement("div");
	nElement.setAttribute("class", "onglet");
	nElement.setAttribute("id", "onglet");
	nElement.setAttribute("name", "onglet");
	
	tabgroup.appendChild(nElement);

	document.body.onscroll = function()
	{
	    var docs = document.getElementsByName('onglet');
	    i = docs.length - 1;
	    //for(i=0; i<docs.length; i++)
	    {
	    	docs[i].style.setAttribute("cssText", "left: " + getX(docs[i]) + "px; top: " + getY(docs[i]) + "px; min-width: " + tab_width + "px; width: " + tab_width + "px;");
		}

	};
}
