// Sliding Menu Class
// Requires Browser.js - browser diagnostics
// Requires WDocument.js - Document and Element wrappers

function Menu(label, obj)
{
    this.label      = label;
    this.obj        = obj;
    this.id         = Menu._LIST.length;
    this.loopTimer  = null;
    this.isIn       = true;
    this.isOut      = false;
    if( Browser.IsNS4 ) {
	 this.maxTop = 41;
	 this.minTop = -34;
    } else {
	 this.maxTop = 45;
	 this.minTop = -30;
    }
    this.isLastStep = false;
    this.stepTime   = 25;
    this.stepSize   = 6;

    this.saneMax    = this.maxTop+2*this.stepSize;
    this.saneMin    = this.minTop-2*this.stepSize;
    Menu._LIST[this.id]=this;

    return(this);
}

Menu._LIST    = new Array();
Menu._StepIn  = function(i) { Menu._LIST[i].stepIn()  }
Menu._StepOut = function(i) { Menu._LIST[i].stepOut() }
Menu.OnStart = null;
Menu.OnStop  = null;

Menu.RetractAll = function() {
    var i;

    for( i=0; i<Menu._LIST.length; i++ ) {
	if( !Menu._LIST[i].isIn ) {
	    Menu._LIST[i].retract();
	}
    }
}

Menu.AllHome = function() {
    var i;
    
    for( i=0; i<Menu._LIST.length; i++ ) {
	Menu._LIST[i].home();
    }
}

Menu.prototype.home = function() {
     this.obj.moveTo3D(this.left, this.minTop, 1);
}

Menu.prototype.deploy = function() {
    if( Menu.OnStart != null ) {
	eval(Menu.OnStart);
    }

    // For sanity-checking added for Opera 5
    this.saneMax    = this.maxTop+2*this.stepSize;
    this.saneMin    = this.minTop-2*this.stepSize;

    this.isIn = false;
    this.isLastStep = false;
    clearInterval(this.loopTimer);
    this.loopTimer=setInterval("Menu._StepOut("+this.id+")", this.stepTime);
}

Menu.prototype.retract = function() {
    if( Menu.OnStart != null ) {
	eval(Menu.OnStart);
    }

    // For sanity-checking added for Opera 5
    this.saneMax    = this.maxTop+2*this.stepSize;
    this.saneMin    = this.minTop-2*this.stepSize;

    clearInterval(this.loopTimer);
    this.isOut = false;
    this.isLastStep = false;
    this.loopTimer=setInterval("Menu._StepIn("+this.id+")", this.stepTime);
}

Menu.prototype.toggle = function() {
    if( this.isOut ) {
	return this.retract();
    }
    return this.deploy();
}

Menu.prototype.haltOut = function()
{
    if( Menu.OnStop != null ) {
	eval(Menu.OnStop);
    }
    clearInterval(this.loopTimer);
    this.loopTimer=null;
    this.obj.moveTo3D(this.left, this.maxTop, 3);
    this.isOut = true;
    window.status=this.label+' menu out';
}

Menu.prototype.haltIn = function()
{
    if( Menu.OnStop != null ) {
	eval(Menu.OnStop);
    }
    clearInterval(this.loopTimer);
    this.loopTimer=null;
    this.obj.moveTo3D(this.left, this.minTop, 1);
    this.isIn=true;
    window.status=this.label+' menu in';
}


Menu.prototype.stepOut = function() 
{
    var curTop = this.obj.top();

    // Sanity-check added for Opera 5
    if( curTop < this.saneMin || curTop > this.saneMax ) {
	this.haltOut();
	return;
    }

    if( curTop >= this.maxTop ) {
        if( this.isLastStep ) {
	    this.haltOut()
        } else {
            this.isLastStep = true;
        }
    } else {
	this.obj.moveTo3D(this.left, curTop+this.stepSize, 1);
    }
}

Menu.prototype.stepIn = function() {
    var curTop=this.obj.top();

    // Sanity-check added for Opera 5
    if( curTop < this.saneMin || curTop > this.saneMax ) {
	this.haltIn();
	return;
    }

    if( curTop <= this.minTop ) {
        if( this.isLastStep ) {
	    this.haltIn();
        } else {
            this.isLastStep = true;
        }
    } else {
	 this.obj.moveTo3D(this.left, curTop-this.stepSize, 1);
    }

}

