﻿/**** lpdj4/menu.js ****/

function Menu() {
    var itemOn = null;
    var itemsList = new Array();
    
    function menuItem(mainMenu, bouton, menu, cache) {
        this.main = mainMenu;
        this.menuPosition = 140;
        this.activeDown = false;
        this.activeUp = false;
        this.timeOutUp = null;

        this.cache = cache;
        
        this.menu = menu;
        this.menu.isOn=false;
        this.menu.cache = this.cache;
        if (this.menu) {
            this.menu.bouton = bouton;
            this.menu.obj = this;
            this.menu.onmouseover = function() { this.isOn = true; this.obj.mouseOver() }
            this.menu.onmouseout = function() { this.isOn=false; var tmp = setTimeout(  function() { document.getElementById("menuServices").obj.mouseOut(); },10 ); }
            this.menu.repositionne = function() {
                var styleTop = (this.bouton.offsetTop-2).toString() + "px";
                this.cache.style.top = styleTop;
            }
        }
        this.bouton = bouton;
        this.bouton.obj = this;
        this.bouton.isOn = false;
        this.bouton.active = false;
        this.bouton.menu = this.menu;
        this.bouton.onmouseover = function() { this.isOn = true; this.obj.mouseOver(); }
        this.bouton.onmouseout = function() { this.isOn=false; var tmp = setTimeout(  function() { document.getElementById("menuServices").obj.mouseOut(); },10 ); }
        this.bouton.setOn = function() { this.style.color =  "#008000";  }
        this.bouton.setOff = function() { this.style.color =  "#555555"; }
        this.bouton.setSelected = function() {  }
    
        this.mouseOver = function() {
            if (!document.onmousemove ) {
                if(this.main.itemOn != this) {   
                    this.main.itemOn = this;
                    clearTimeout(this.timeOutUp);
                    this.setOn();
                }
            }
        }

        this.mouseOut = function() {
            if(!this.bouton.isOn && !this.menu.isOn) {
                var temps = 500;
                this.main.itemOn = null;
                this.bouton.setOff();
                if(this.menuPosition >= 3 )  temps = 50; 
                this.timeOutUp = setTimeout( function() { document.getElementById("menuServices").obj.pullUp(); },temps); 
            }
        }
        
        this.setOn = function() {
            this.bouton.setOn();

            if(this.menu) {
                this.menu.repositionne();
                this.menu.style.visibility = "visible";
                if(this.activeUp) { clearTimeout(this.timeOutUp); this.activeUp = false; }
                if (!this.activeDown) this.pullDown();
            }
        }

        this.pullDown = function() { 
            this.activeDown = true; 
            this.bouton.active = true;
            
            var temps = 10;
            if (this.menuPosition > 70) { this.menuPosition -= 10;   }
            else if (this.menuPosition > 40){ this.menuPosition -= 5;   }
            else if (this.menuPosition > 20){ this.menuPosition -= 3;   }
            else if (this.menuPosition > 10){ this.menuPosition -= 2;   }
            else if (this.menuPosition > 6){ this.menuPosition -= 1; temps = 20; }
            else if (this.menuPosition > 3){ this.menuPosition -= 1; temps = 50; }
            else { this.menuPosition -= 1; temps = 100; }

            if(this.menuPosition >= 3 && (this.bouton.isOn || this.menu.isOn) ) {
                this.menu.style.left = this.menuPosition.toString() + "px";
                
                setTimeout( function() { document.getElementById("menuServices").obj.pullDown(); },temps);
            }
            else {
                this.activeDown = false;
                this.bouton.active = false;
                if (diaporama) if (diaporama.active) diaporama.runBackground();
            }
        }

        this.pullUp = function() { 
            this.activeUp = true;
            this.bouton.active = true;
            
            this.menuPosition += 7;
            
            if(this.menuPosition <= 140 ) {
                this.menu.style.left = this.menuPosition.toString() + "px";
                this.timeOutUp = setTimeout( function() { document.getElementById("menuServices").obj.pullUp(); },10);
            } else {
                this.menu.style.visibility = "hidden";
                this.activeUp = false;
                this.bouton.active = false;
                if (diaporama) if (diaporama.active) diaporama.runBackground();
        }
        }
    }
    
    this.addMenu = function(bouton, menu, cache) {
       var bidon = new menuItem(this, bouton, menu, cache);
    }

    
}