﻿var movingElement = null;
var ShowedSpace = null;
var SpaceToShow = null;
var movingBoxes = new Array();
var movingSpaces = new Array();
var uniqId = 1;
document.onselectstart = ACMSelectText;
document.onmousemove = ACMMoveItem;
document.onmouseup = ACMStopMoveItem;
document.onmousedown = ACMStartMoveItem;

document.body.onload = ACMGetInitialPosition;

function ACMGetInitialPosition()
{
    ACMGetActualBoxes();
    var tab = GetCookie("ACMMenuLeft").split("|");
    var orderedBoxes = new Array();
    var orderedSpaces = new Array();
    for(j=0;j<tab.length-1;j+=2)
    {
        for(k=0;k<movingBoxes.length;k++)
        {
            if (movingBoxes[k].children[0].innerText == tab[j])
            {
                orderedBoxes[orderedBoxes.length] = movingBoxes[k];
                orderedSpaces[orderedSpaces.length] = movingSpaces[k];
                if ( tab[j+1] == "none")
                {
                    ACMCollapseBox(orderedBoxes[orderedBoxes.length-1].children[0].children[0],false);
                }
                else
                {
                    ACMExpandBox(orderedBoxes[orderedBoxes.length-1].children[0].children[0],false);
                }
            }
        }
    }
    for (j=1;j<orderedBoxes.length;j++)
    {
            orderedSpaces[j-1].insertAdjacentElement('afterEnd',orderedBoxes[j]);
            orderedBoxes[j].insertAdjacentElement('afterEnd',orderedSpaces[j]);
    }
}


function ACMSelectText()
{
    if(event.srcElement.tagName != "TEXTAREA" && event.srcElement.tagName != "INPUT" && movingElement!=null )
    {
        return false
    }
}

function ACMStartMoveItem()
{
    if (event.button != 1)    {return false}
    if (movingElement != null) {ACMStopMoveItem;return false}
    movingElement = event.srcElement;
    if (movingElement != null)
    {
        if (movingElement.className == "BoxArrow") {ACMCollapseBox(movingElement)}
        else if (movingElement.className == "BoxArrowActive") {ACMExpandBox(movingElement)}
        var box = movingElement;
        while(box != null && box.id != "myInfoBox")
        {
            box = box.parentElement;
        }
        if (box != null) //Moving InfoBox
        {
            movingElement = document.getElementById("myInfoBox");
            movingElement.Left = getAbsoluteLeft(movingElement);
            movingElement.Top = getAbsoluteTop(movingElement);
            movingElement.offsetX = event.clientX + document.documentElement.scrollLeft -  movingElement.Left;
            movingElement.offsetY = event.clientY + document.documentElement.scrollTop -  movingElement.Top;
            ACMMoveItem();
            return false;
        }
        
        if (movingElement.className != "BoxHeader") {movingElement = null;return false}
        while(movingElement.className != "Box")
        {
            movingElement = movingElement.parentElement;
            if (movingElement == null) { return false}
        }
        movingElement.Left = getAbsoluteLeft(movingElement);
        movingElement.Top = getAbsoluteTop(movingElement);
        movingElement.offsetX = event.clientX + document.documentElement.scrollLeft -  movingElement.Left;
        movingElement.offsetY = event.clientY + document.documentElement.scrollTop -  movingElement.Top;
        movingElement.className = "BoxMoving";
        ACMGetActualBoxes();
        ACMShowSpaceFromBox();
        ACMMoveItem();
        ACMinitIds();
    }
}
function ACMGetActualBoxes()
{
    movingBoxes = new Array();
    movingSpaces = new Array();
    var n=0;
    var container = document.getElementById("BoxContainer")
    {
        for (k=1;k<container.children.length-1;k+=2)
        {
            movingBoxes[movingBoxes.length] = container.children[k];
            movingBoxes[movingBoxes.length-1].Place = n;
            movingSpaces[movingSpaces.length] = container.children[k+1];
            movingSpaces[movingSpaces.length-1].Place = n;
            n++;
        }
        movingSpaces[movingSpaces.length] = container.children[0];
    }
}
function ACMinitIds()
{
    for (j=0;j<movingSpaces.length;j++)
    {
        movingSpaces[j].id = "Space_"+j;
    }
    for (j=0;j<movingBoxes.length;j++)
    {
        movingBoxes[j].id = "Box_"+j;
    }
}

function ACMStopMoveItem()
{
    if (event.button != 1)    {return false}
    if (movingElement != null)
    {
        if (movingElement.id == "myInfoBox") { movingElement = null;return false }
        movingElement.className = "Box";
        ACMSetSpaceHeight(ShowedSpace, 5);
        ShowedSpace = null;
        SpaceToShow = null;
        movingElement = null;
        for (j=0;j<movingSpaces.length;j++)
        {
            ACMSetSpaceHeight(movingSpaces[j], 5);
        }
        SaveCookie("ACMMenuLeft")
    }
}

function ACMMoveItem()
{
    if (movingElement != null)
    {
        movingElement.Left = event.clientX + document.documentElement.scrollLeft - movingElement.offsetX
        movingElement.Top = event.clientY + document.documentElement.scrollTop - movingElement.offsetY
        movingElement.style.left = movingElement.Left + " px";
        movingElement.style.top = movingElement.Top + " px";
        if (movingElement.id != "myInfoBox") { ACMCheckPosition()}
    }
}

function ACMCheckPosition()
{
    for (j=0;j<movingBoxes.length;j++)
    {
        movingSpaces[j].Top =  getAbsoluteTop(movingSpaces[j]);
        if (movingSpaces[j].myHeight == null) {movingSpaces[j].myHeight = 5;}
        if (movingElement.Top >= getAbsoluteTop(movingBoxes[j]) + (movingBoxes[j].offsetHeight/2) && movingElement.Top < (getAbsoluteTop(movingSpaces[j]) + movingSpaces[j].myHeight) && movingSpaces[j]!=ShowedSpace && (j == movingSpaces.length-1 || movingBoxes[j+1]!=movingElement))
        {
//            ACMSetSpaceHeight(movingSpaces[j],ShowedSpace.myHeight+5);
//            ACMSetSpaceHeight(ShowedSpace,5);
//            ShowedSpace = movingSpaces[j];
            movingSpaces[j].insertAdjacentElement('afterEnd',movingElement);
            movingElement.insertAdjacentElement('afterEnd',ShowedSpace);
            var lastPlace = movingElement.Place;
            ACMGetActualBoxes();
            ACMSetSpaceHeight(SpaceToShow, 5);
            if (movingElement.Place > lastPlace)
            { //going down
                if (lastPlace == 0)
                { SpaceToShow = movingSpaces[movingSpaces.length-1]; }
                else
                { SpaceToShow = movingSpaces[lastPlace-1]; }
               
                ACMSetSpaceHeight(SpaceToShow,movingElement.offsetHeight+10)
                ACMSetSpaceHeight(ShowedSpace,0)
                ACMMoveSpaceToHeight(SpaceToShow.id,5)
                ACMMoveSpaceToHeight(ShowedSpace.id,movingElement.offsetHeight+5)
            }
            else
            { //going up
                SpaceToShow = movingSpaces[lastPlace];
                ACMSetSpaceHeight(SpaceToShow,movingElement.offsetHeight+10)
                ACMSetSpaceHeight(ShowedSpace,0)
                ACMMoveSpaceToHeight(SpaceToShow.id,5)
                ACMMoveSpaceToHeight(ShowedSpace.id,movingElement.offsetHeight+5)
            }
        }
        else if (movingElement.Top < getAbsoluteTop(movingBoxes[0]) + 5 &&  movingBoxes[0] != movingElement)
        {
            movingBoxes[0].insertAdjacentElement('beforeBegin',movingElement);
            movingElement.insertAdjacentElement('afterEnd',ShowedSpace);
            
            var lastPlace = movingElement.Place;
            ACMGetActualBoxes();
            ACMSetSpaceHeight(SpaceToShow, 5);
            SpaceToShow = movingSpaces[lastPlace];
            
            ACMSetSpaceHeight(SpaceToShow,movingElement.offsetHeight+10)
            ACMSetSpaceHeight(ShowedSpace,0)
            ACMMoveSpaceToHeight(SpaceToShow.id,5)
            ACMMoveSpaceToHeight(ShowedSpace.id,movingElement.offsetHeight+5)
        }
    }
}
function getAbsoluteLeft(object)
{
    if (object.style.position == "absolute")
    {
        return object.offsetLeft;
    }
    else
    {
        
        var myLeft = 0;
        do
        {
            myLeft += object.offsetLeft;
            object = object.parentElement;
        //} while(object != null)
        } while(object.id != "BoxContainer")
        return myLeft
    }
}
function getAbsoluteTop(object)
{
    if (object.style.position == "absolute")
    {
        return object.offsetTop - object.scrollTop;
    }
    else
    {
        var myTop = 0;
        do
        {
            myTop += object.offsetTop;
            object = object.offsetParent;
        //} while(object != null)
        } while(object.id != "BoxContainer")
        return myTop
    }
}

function ACMShowSpaceFromBox()
{
    for(j=0;j<movingBoxes.length;j++)
    {
        if (movingElement == movingBoxes[j])
        {
            ShowedSpace = movingSpaces[j];
        }
    }
    ACMSetSpaceHeight(ShowedSpace, movingElement.offsetHeight + 5)
}

function ACMSetSpaceHeight(space, height)
{
    if(space!=null)
    {
        space.style.height = height + " px";
        space.myHeight = Math.round(height) ;
        clearTimeout(space.Timer);
    }
}
function ACMMoveSpaceToHeight(spaceId, height)
{
    space = document.getElementById(spaceId);
    if(space.Step == null ||space.Origin == null)
    {
        space.Step = -1;
        space.Origin = space.myHeight;
    }
    space.Step++;
    if (space.myHeight > height)
    { //Collapse space
    
        //nextHeight = Math.max(space.Origin - Math.floor(((Math.atan(Math.PI*(space.Step-4)/2)+(Math.PI/2))/Math.PI)*Math.abs(space.Origin - height)),height);
        nextHeight = Math.max(space.myHeight-20,height);
        ACMSetSpaceHeight(space, nextHeight);
        space.Timer = setTimeout("ACMMoveSpaceToHeight('"+ spaceId + "',"+height+");",30)
    }
    else if( space.myHeight < height)
    { //Expand space
    
        //nextHeight = Math.min(space.Origin + Math.ceil(((Math.atan(Math.PI*(space.Step-4)/2)+(Math.PI/2))/Math.PI)*Math.abs(space.Origin - height)),height);
        nextHeight = Math.min(space.myHeight+20,height);
        ACMSetSpaceHeight(space, nextHeight);
        space.Timer = setTimeout("ACMMoveSpaceToHeight('"+ spaceId + "',"+height+");",30)
    }
    else
    { //Finish
        space.Step = null;
        space.Origin = null;
    }
}

function ACMCollapseBox(arrow,save)
{
    if(save==null){save=true}
    arrow.className = "BoxArrowActive";
    var obj = arrow.parentElement.parentElement.children[1];
    obj.OriginalHeight = obj.offsetHeight;
    //obj.id = "inner_"+ (++uniqId);
    //ACMMoveInnerToHeight(obj.id,0);
    obj.style.display = "none";
    if(save==true){ SaveCookie("ACMMenuLeft")}
}
function ACMExpandBox(arrow,save)
{
    if(save==null){save=true}
    arrow.className = "BoxArrow";
    var obj = arrow.parentElement.parentElement.children[1];
    //obj.id = "inner_"+ (++uniqId);
    //ACMMoveInnerToHeight(obj.id,obj.OriginalHeight);
    
    //obj.style.height = obj.OriginalHeight+"px";
    obj.style.display = "block";
    if(save==true){ SaveCookie("ACMMenuLeft")}
}
function ACMMoveInnerToHeight(innerId, height)
{
    inner = document.getElementById(innerId);
    if (inner.offsetHeight > height)
    { //Collapse inner
        nextHeight = Math.max(inner.offsetHeight-20,height);
        inner.style.height = nextHeight+"px";
    setTimeout("ACMMoveInnerToHeight('"+innerId+"',"+height+")",30)
    }
    else if( inner.offsetHeight < height)
    { //Expand inner
        nextHeight = Math.min(inner.offsetHeight+20,height);
        inner.style.height = nextHeight+"px";
    setTimeout("ACMMoveInnerToHeight('"+innerId+"',"+height+")",30)
    }
}

function SaveCookie(nom)
{
    var strcook="";
    for (j=0;j<movingBoxes.length;j++)
    {
        strcook+=movingBoxes[j].children[0].innerText+"|";
        strcook+=movingBoxes[j].children[1].style.display+"|";
    }

    var today = new Date();
    var date = new Date();
    date.setTime(today.getTime()+1000*60*60*24*365)
    document.cookie=nom+"="+escape(strcook)+"; path=/; expires="+date.toGMTString() ;
}

function GetCookie(nom)
{
    var i,j ;
    var cookie = document.cookie ;
    i = cookie.indexOf(nom) ;
    if(i==-1) return "" ;
    j = cookie.indexOf(";",i) ;
    if(j==-1) j=cookie.length ;
    return unescape(cookie.substring(i+nom.length+1,j)); 
}

function SendNavLeftTableQuoteAjax(url)
{ 
         var httpRequest = false;
       if (window.XMLHttpRequest) { // Mozilla, Safari,...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // Voir la note ci-dessous à propos de cette ligne
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (e) {}
            }
        }

    httpRequest.onreadystatechange  = function()
    { 
         if(httpRequest.readyState  == 4)
         {
              if(httpRequest.status  == 200) eval(httpRequest.responseText);
         }
    }
    var dt = new Date();
    httpRequest.open( "GET", url+"?mini=true&callajax=true&dt="+dt.getTime(),  true); 
    httpRequest.send(null); 
    
    setTimeout("SendNavLeftTableQuoteAjax('" + url + "')",20000);
}
