var ElmtMenu, Menu, dds, dls, dlsPositions, temporisation_in; 
var dlsPositions = new Array();
var colors=new Array("#fcfcfc", "#f7f7f7", "#f1f2f2", "#ebeded", "#e5e8e8", "#dfe2e3", "#d9ddde", "#d3d7d9", "#cdd2d4", "#c7cccf", "#c1c7ca", "#bbc1c5", "#b5bcc0", "#afb6bb", "#a9b1b6")

function addLoadEvent(func)
{
   var oldonload = window.onload;
   if(typeof window.onload != 'function')
   {
      window.onload = func;
   }
   else
   {
      window.onload = function()
      {
         oldonload();
         func();
      }
   }
}

addLoadEvent(function()
{
    dynMenu("menu",500,1500);
    hide();
});

dynMenu=function(idMenu, delai_in, delai_out)
{
    Menu=document.getElementById(idMenu); 
    dds = Menu.getElementsByTagName("dd"); 
    dls = Menu.getElementsByTagName("dl"); 
    temporisation_in=false; 
    var timeout=0; 
    if (dds)
        for (var i=0; i<dls.length; i++)
        {
            dlsPositions[i]=Math.floor(dls[i].offsetLeft + dls[i].offsetWidth/2);
            smenu=dls[i].getElementsByTagName("dd")[0]; 
            smenu.style.visibility = "hidden";
            smenu.style.display="block";
            var posLeft;
            if(Math.floor(dlsPositions[i]+smenu.offsetWidth/2)>660)
                posLeft = 660-smenu.offsetWidth;
            else if(Math.floor(dlsPositions[i]-smenu.offsetWidth/2)<0)
                posLeft = 0;
            else
                posLeft = Math.floor(dlsPositions[i]-smenu.offsetWidth/2);    
			posLeft = Math.max(0, posLeft);    //maa:02/07/09
            smenu.style.marginLeft=posLeft + "px";
            smenu.style.display="none";
            smenu.style.visibility = "visible";
            dls[i].onmouseover=function()
            { 
                ElmtMenu=this.getElementsByTagName("a")[0];
                clearTimeout(timeout); 
                smenu=this.getElementsByTagName("dd")[0]; 
                ElmtMenu.style.backgroundPosition='0 -20px'; 
                ElmtMenu.style.backgroundRepeat='no-repeat';
                if(smenu.style.display=="block")
                    return; 
                if (temporisation_in)
                    timeout=setTimeout("show()",delai_in);
                else
                    timeout=setTimeout("show()",0);
            }
        }
    
    Menu.onmouseout=function()
    {
        clearTimeout(timeout);
        if(ElmtMenu)
        {
            ElmtMenu.style.backgroundPosition='0 0'; 
            ElmtMenu.style.backgroundRepeat='no-repeat';
        }
        timeout=setTimeout('hide()',delai_out);
    }
    
    dls[0].onmouseout=function()
    { 
        if (temporisation_in)
            timeout=setTimeout("hide()",delai_in);
    }
}
    
function hide()
{ 
    for (var j=0;j<dds.length;j++)
        dds[j].style.display="none";
    temporisation_in=false;
}

function show()
{
    hide();
    smenu.style.display="block";
    degrade(0); 
    temporisation_in=true;
}

function degrade(i)
{

    var aSmenu=smenu.getElementsByTagName("a");
    var liSmenu=smenu.getElementsByTagName("li");
    for (var j=0;j<aSmenu.length;j++)
    {    
        aSmenu[j].style.color=colors[i];
        liSmenu[j].style.color=colors[i];
    }
    i++; 
    if(i<16)
        setTimeout("degrade(" + i + ")",30);
    else
    {
        var aSmenu=smenu.getElementsByTagName("a");
        var liSmenu=smenu.getElementsByTagName("li");
        for (var j=0;j<aSmenu.length;j++)
        {    
            aSmenu[j].removeAttribute("style");
            liSmenu[j].removeAttribute("style");
        }
    }    
}

function ouvre_popup(nUrl,w,h)
{
	var winl, wint;
	if (navigator.userAgent.indexOf("Opera") != -1)
	{
		winl = (window.innerWidth) ? (window.innerWidth-w)/2 : 0;
		wint = (window.innerHeight) ? (window.innerHeight-h)/2 : 0;
	}
	else
	{
		winl = (screen.width) ? (screen.width-w)/2 : 0;
		wint = (screen.height) ? (screen.height-h)/2 : 0;
	}
	var options = ((h && w) ? 'width='+ w +',height='+ h +',top='+wint+',left='+winl+',' : '') + 'toolbar=0, location=0, directories=0, status=0, scrollbars=' + (w == 270 ? 0 :1) +', resizable=no, copyhistory=0, menuBar=0';	
	if (window.neo && !neo.closed)
		window.neo.close();
	neo=window.open(nUrl,"",options);
}



var popupContent, popup;
var bodyElmt = document.documentElement;

var ie6 = navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 7")==-1 && navigator.userAgent.indexOf("Opera")==-1;
var netscape = navigator.appName.substring(0,8) == "Netscape";

var repos = true;
var scroll = false;
var mousedown = false;

var pos_x, pos_y;

function addContent(arg1, arg2)
{
    if(arg1.indexOf(".jpg")>arg1.length-5 || arg1.indexOf(".gif")>arg1.length-5)
        popupContent.style.background = "url('"+arg1+"') no-repeat top left";
    else    
        CallServer(arg1+","+arg2);
}

function ouvre(w, h, arg1, arg2, s)
{
    if(s!=null)
        scroll = s;
    if(popup)
        document.body.removeChild(popup);
    popupContent=document.createElement("div");
    addContent(arg1,arg2);
    popupContent.setAttribute("id","popupContent");
    popupContent.style.height=h+"px";
    popup = document.createElement("div");
    popup.setAttribute("id","popup");
    popup.style.marginTop=(-h/2)+"px";
    popup.style.marginLeft=(-w/2)+"px";
    popup.style.width=w+"px";
    popup.innerHTML = "<span onclick=\"popup.style.visibility=\'hidden\'\">x</span>";
    popup.appendChild(popupContent);
    document.body.appendChild(popup);
    if(!scroll || ie6)
    {
        popup.style.position = 'absolute';
        Reposition(popup.offsetTop + popup.offsetHeight/2);
    }
    else
        popup.style.position = 'fixed';
    pos_x = popup.offsetLeft;
    pos_y = popup.offsetTop;
    popup.onmousedown = boutonPresse;
    document.onmouseup = boutonRelache;
    popup.onmouseover = function(){popup.style.cursor='move';};
}

function ReceiveServerData(arg)
{
    //http://www.quirksmode.org/bugreports/archives/2006/01/IE_wont_allow_documentcreateElementstyle.html
    if(navigator.appName == "Microsoft Internet Explorer" && false) //trop lent : on charge tous les CSS à l'avance.
    {
        alert("1");
        regstyle=new RegExp("(.*)(<style.*?>)(.*?)(</style>)(.*)", "g");
        
        arg = arg.split("\r\n").join("");
        rawsheet = arg.replace(regstyle,"$3");
        html = arg.replace(regstyle,"$5");
        popupContent.innerHTML = html;
        
        newcss = document.createElement("style");
        newcss.type="text/css";
        newcss.media="all";
        cssrules = rawsheet.split("}");
        newcss = document.styleSheets[0];
        alert("2");
        for(i=cssrules.length-2;i>=0;i--) 
        {   
            newrule = cssrules[i].split("{");
            properties = newrule[0].split(",");
            for (var j=0;j<properties.length;j++)
                newcss.addRule(properties[j],newrule[1]);
        }
        alert("3");
        document.getElementsByTagName("head")[0].appendChild(newcss);
    }
    else
        popupContent.innerHTML = arg;
}

function ImagesToBackground()
{
    images=document.getElementsByTagName("img");
    imagesToRemove = new Array();
    backgroundToAdd = new Array();
    for(var i=0;i<images.length;i++) 
    { 
        imageNode = images[i];
        if(IsDescendantOf(imageNode, popup)) 
        {
            var background = document.createElement("div");
            var src;
            for(var j=0;j<imageNode.attributes.length;j++) 
            {
                attName = imageNode.attributes[j].name;
                attValue = imageNode.attributes[j].value;
                if(attName=="src")
                    src = attValue;
                else if(attName!="alt")
                    background.setAttribute(attName, attValue);
                // pour IE (http://www.nanoum.net/blog/9_setAttribute.html)    
                if (attName == "class")
                    background.setAttribute("className",attValue);
            }
            background.style.cssText += "background:url(\""+ src+ "\") no-repeat 0 0;"+"width:"+imageNode.offsetWidth+"px;height:"+imageNode.offsetHeight+"px;";
            imagesToRemove.push(imageNode);
            backgroundToAdd.push(background);
        }
    }
    for(var i=0;i<imagesToRemove.length;i++) 
        imagesToRemove[i].parentNode.replaceChild(backgroundToAdd[i], imagesToRemove[i]);
}

function IsDescendantOf(tag1, tag2)
{
    while(tag1.tagName.toLowerCase()!= "body")
    {
        if(tag1==tag2)
            return true;
        tag1 = tag1.parentNode;
    }
    return false;
}

function Reposition(h) 
{
    popup.style.top = (h + bodyElmt.scrollTop) +"px";
    pos_y = popup.offsetTop;
    if(repos && scroll)
        setTimeout("Reposition("+h+")",0);
}

function boutonPresse()
{
    //seulement Internet Explorer comprends document.readyState (celà permet d'être sûr que toutes les images sont chargées, on peut alors les mettre en background car leur propriété offsetwidth et offsetheight sont définitives)
    if(navigator.appName == "Microsoft Internet Explorer")
        while(!document.readyState)
            {}
    ImagesToBackground();

    if(x<popup.offsetLeft+parseInt(popup.style.width)-20)
    {
        orig_x = x - pos_x;
        orig_y = y - pos_y;
        mousedown = true;
        repos = false;
    }
}

function boutonRelache()
{
    mousedown = false;
    repos = true;
    if(ie6 && scroll)
        Reposition(parseInt(popup.style.top)- bodyElmt.scrollTop);
}

function deplacementSouris(e)
{
    x = (netscape) ? e.pageX : event.x + bodyElmt.scrollLeft;
    y = (netscape) ? e.pageY : event.y + bodyElmt.scrollTop;
    if (mousedown && document.getElementById)
    {
        pos_x = x - orig_x;
        pos_y = y - orig_y;
        popup.style.left = (pos_x - parseInt(popup.style.marginLeft))+"px";
        popup.style.top = (pos_y - parseInt(popup.style.marginTop))+"px";
    }
}

if (netscape)
    document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = deplacementSouris;

