// CD Menu
//****************************************************************************//

function menu( x, y, height, bfapp ) {
  this.bfapp = bfapp;
  this.menuItems = new Array();
  this.x = x;
  this.y = y;
  this.height = height;
  this.numlinks = 0;
  this.selected = -1;
  this.cancelfadein = false;
  
  this.add = function( imgurl, url, title, instant ) {
    if( typeof( instant ) == undefined ) instant = false;
    document.write(
      '<img id="case' + this.numlinks +
      '" OnClick="javascript:menu.caseClicked(' + this.numlinks +
      ')" OnMouseOver="javascript:menu.mouseOver(' + this.numlinks +
      ')" OnMouseOut="javascript:menu.mouseOut(' + this.numlinks +
      ')" alt="' + title +
      '" title="' + title +
      '" class="case" style="top:150px;" src="' + imgurl +
      '"/>'
    );
    this.menuItems[this.numlinks] = getObjectRef( "case" + this.numlinks );
    this.menuItems[this.numlinks].linkedPage = url;
    this.numlinks++;
    this.redistribute( instant );
  }
  
  this.redistribute = function( instant ) {
    if( typeof( instant ) == undefined ) instant = false;
    var addedspace = 2;
    var pos = this.y;
    var wnumlinks = this.numlinks;
    if( this.selected >= 0 ) wnumlinks += addedspace - 1;
    var space = this.height / wnumlinks;
    
    var motion = false;
    
    for( i=0; i<this.numlinks; i++ ) {
      var dst = pos;
      var src = parseInt(this.menuItems[i].style.top);
      if( instant ) {
        this.menuItems[i].style.top = dst + "px";
      }else{
        this.menuItems[i].style.top = (src+(dst-src)/(3)) + "px";
      }
      this.menuItems[i].style.left = this.x + "px";

      if( i == this.selected ) {
        pos += space*addedspace;
      }else{
        pos += space;
      }

      if( Math.abs( dst - src ) > 5 ) motion = true;
      
    }
    
    if( motion ) setTimeout("menu.redistribute()", 40);
  }
  
  this.deselect = function() {
    this.selected = -1;
    this.redistribute();
    this.bfapp.loadPage('http://stevenlovegrove.f2s.com/home/');
    return false;
  }
  
  this.caseClicked = function( num ) {
    this.selected = num;
    this.redistribute();
    this.bfapp.loadPage( this.menuItems[num].linkedPage );
  }
  
  this.mouseOver = function( num ) {
    this.cancelfadein = true;
    
    for( i=0; i<this.numlinks; i++ ) {
      if( i == num ) {
        setOpacity( this.menuItems[i], 100 );
      }else{
        setOpacity( this.menuItems[i], 40 );
      }
    }
  }
  
  this.mouseOut = function( num ) {
    this.cancelfadein = false;
    this.fadein();
  }
  
  this.fadein = function() {
    if( this.cancelfadein ) return;
    var changed = false;
    for( i=0; i<this.numlinks; i++ ) {
      var src = getOpacity( this.menuItems[i] );
      if( Math.abs( 100-src ) > 1 ) changed = true;
      setOpacity( this.menuItems[i], src+(100-src)/5 );
    }
    
    if( changed ) setTimeout("menu.fadein()", 100);
  }
  
}
