/*****************************************************
* ypSlideOutMenu
* 3/04/2001
*
* a nice little script to create exclusive, slide-out
* menus for ns4, ns6, mozilla, opera, ie4, ie5 on 
* mac and win32. I've got no linux or unix to test on but 
* it should(?) work... 
*
* Revised:
* - 08/29/2002 : added .hideAll()
* - 2004.12.23 : -added code to allow menu to display over the
*                 top of select areas in IE.  
*                -changes are in global area, constructor, showmenu and hide methods
*                -there is a debug method at EOF and the debug variable below should 
*                 be set to false for production
*                                            smb1@cornell.edu - 2004.12.23
*
* --youngpup--
*****************************************************/

// required for the IE menuing over select areas fix, smb1@cornell.edu - 2005.01.04
DEBUG = false;
OPAGUE = 'opaque';

ypSlideOutMenu.Registry = []
ypSlideOutMenu.aniLen = 250
ypSlideOutMenu.hideDelay = 250
ypSlideOutMenu.minCPUResolution = 10


// constructor
function ypSlideOutMenu(id, dir, left, top, width, height)
{
	this.ie = document.all ? 1 : 0
	this.ns4 = document.layers ? 1 : 0
	this.dom = document.getElementById ? 1 : 0
	
	// Correct flags for Opera which likes to mimic other browsers. -mas18, 2005.06.07
	if ( navigator.userAgent.indexOf("Opera") != -1 ) {
		this.ie = 0;
		this.ns4 = 0;
		this.dom = 1;
	}
	
	if (this.ie || this.ns4 || this.dom) {

        /**
         These four attributes are required for IE IFRAME fix.
         smb1@cornell.edu - 2004.12.23
        */
        if( this.ie ) { 
          try{
			/* Per http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q261188 set src=dummy.html */
            this.opaqueiframedef = '<IFRAME src="dummy.html" id="' + OPAGUE + id + '" style="DISPLAY: none; POSITION: absolute;" frameBorder="0" scrolling="no"/>';
            this.opaqueiframewidth = ( id.indexOf( 'tab' ) > -1 ? 127 : 118 );
            //this.opaqueiframeheightmultiplier = ( id.indexOf( 'tab' ) > -1 ? 32 : 27 );
            //this.opaqueiframeheightlenadjust = ( id.indexOf( 'tab' ) > -1 ? 4 : 3 );
		  }catch( e ) { showJSError( e ); }  
		}// if  
        // end IE IFRAME fix

		this.id = id
		this.dir = dir
		this.orientation = dir == "left" || dir == "right" ? "h" : "v"
		this.dirType = dir == "right" || dir == "down" ? "-" : "+"
		this.dim = this.orientation == "h" ? width : height
		this.hideTimer = false
		this.aniTimer = false
		this.open = false
		this.over = false
		this.startTime = 0
		this.gRef = "ypSlideOutMenu_"+id
		eval(this.gRef+"=this")
		ypSlideOutMenu.Registry[id] = this
		var d = document
		var strCSS = '<style type="text/css">';
		strCSS += '#' + this.id + 'Container { visibility:hidden; '
		strCSS += 'left:' + left + 'px; '
		strCSS += 'top:' + top + 'px; '
		strCSS += 'overflow:hidden; z-index:1000; }'
		strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; '
		strCSS += 'width:' + width + 'px; '
		strCSS += 'height:' + height + 'px; '
		strCSS += 'clip:rect(0 ' + width + ' ' + height + ' 0); '
		strCSS += '}'
		strCSS += '</style>'
		d.write(strCSS)
		this.load()

	}
}

ypSlideOutMenu.prototype.load = function()
{
	var d = document
	var lyrId1 = this.id + "Container"
	var lyrId2 = this.id + "Content"
	var obj1 = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	var temp
	if (!obj1 || !obj2) window.setTimeout(this.gRef + ".load()", 100)
	else {
		this.container = obj1
		this.menu = obj2
		this.style = this.ns4 ? this.menu : this.menu.style
		this.homePos = eval("0" + this.dirType + this.dim)
		this.outPos = 0
		this.accelConst = (this.outPos - this.homePos) / ypSlideOutMenu.aniLen / ypSlideOutMenu.aniLen 
		// set event handlers.
		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("ypSlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("ypSlideOutMenu.hideMenu('" + this.id + "')")
		//set initial state
		this.endSlide()
	}
}
ypSlideOutMenu.showMenu = function(id)
	{
		var reg = ypSlideOutMenu.Registry
		var obj = ypSlideOutMenu.Registry[id]
		if (obj.container) {
		obj.over = true
		for (menu in reg) if (id != menu) ypSlideOutMenu.hide(menu)
		if (obj.hideTimer) { 
			reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer)
		}
		if (!obj.open && !obj.aniTimer) reg[id].startSlide(true)
		
        //  call the IE IFRAME fix, smb1@cornell.edu - 2004.12.23
        if( obj.ie){

          try{ 

            var divobj = document.getElementById( obj.id + 'Container' );

            if( divobj != null ){

              var iobj = document.getElementById( OPAGUE + obj.id );
              if( iobj == null ) // define in document
                divobj.insertAdjacentHTML( "beforeBegin" , obj.opaqueiframedef );
	
              	var ifobj = document.getElementById( obj.id + 'Content' );
              	var numberOfRows = ifobj.childNodes(0).getElementsByTagName( 'TABLE' )[0].rows.length;

               	iobj = document.getElementById( OPAGUE + obj.id );
 
               	// you want to adjust these each time to account for clicks in different areas
               	iobj.style.top = obj.container.style.top ;
               	iobj.style.left = obj.container.style.left ;
			   
			   	// Code to determine the icon sizes which will result in knowing how large the iFrame needs to be.
				//if(obj.id.indexOf( 'tab' ) > -1 ) { // mas18, 2005.02.16 
			   		try{												
						// maxHeight will be used later to set the table cell heights.
						maxHeight = 1;
						
						// Gather all of the tables and then search each one for images.
						aTables = ifobj.getElementsByTagName( 'TABLE' );
						for(t=0; t < aTables.length; t++) {
							// Find all images within this table and check their sizes.
							aImages = ifobj.childNodes(t).getElementsByTagName( 'IMG' );
							for(p=0; p < aImages.length; p++) {
								h = ifobj.childNodes(t).getElementsByTagName( 'IMG' )[p].height;
								if( h > maxHeight ) {
									maxHeight=h;	
								}//if
							}//for								
						}//for
						
					}catch ( e ) { showJSError( e); }
			   	//}//if // mas18, 2005.02.16 

				//if(obj.id.indexOf( 'tab' ) > -1 )                                  										// mas18, 2005.02.16 
				//	numberOfRows++; // Add 1 to numberOfRows for [Close Menu] line.  										// mas18, 2005.02.16 
					
				cellSpacing     = 1; // Cell spacing between rows.                  										// mas18, 2005.02.17
				topSpacer       = 2; // Pixels above first row. 															// mas18, 2005.02.17
				bottomSpacer    = 2; // Pixels below last row.           													// mas18, 2005.02.17
				tabBottomSpacer = 20; // Pixels needed below last tab row.													// mas18, 2005.02.17
				cellSize		= 30;																						// mas18, 2005.02.17
				tabCellSize		= 31; // Actually varies from 29 to 31.														// mas18, 2005.02.17

               	// If this is a tab then the menu will have an added row for 'Close Menu' and different spacing.			// mas18, 2005.02.16 
               	if( obj.id.indexOf( 'tab' ) > -1 &&  divobj.childNodes(0).getElementsByTagName( 'INPUT' )[0] == null ) {									// mas18, 2005.02.17
					//alert('1');
                	iobj.style.width = obj.opaqueiframewidth - 3; 																							// mas18, 2005.02.17
					iobj.style.height = ( ( numberOfRows * tabCellSize ) + ( ( numberOfRows - 1 ) * cellSpacing ) + ( topSpacer + tabBottomSpacer ) - 1 ); 	// mas18, 2005.02.17
				} else if( obj.id.indexOf( 'tab' ) > -1 ) { 																								// mas18, 2005.02.17
					//alert('2');
                	iobj.style.width = obj.opaqueiframewidth; 
					iobj.style.height = ( ( numberOfRows * tabCellSize ) + ( ( numberOfRows - 1 ) * cellSpacing ) + ( topSpacer + tabBottomSpacer ) -2 );	// mas18, 2005.02.17
				} else if( obj.id.indexOf( 'column' ) > -1) {
					//alert('3');
					iobj.style.width = obj.opaqueiframewidth; 																								// mas18, 2005.02.16 
					iobj.style.height = ( ( numberOfRows * cellSize ) + ( ( numberOfRows - 1 ) * cellSpacing ) + ( topSpacer + bottomSpacer ) + 3 );    	// mas18, 2005.02.17			
                } else{
					//alert('4');
                	iobj.style.width = obj.opaqueiframewidth; 																								// mas18, 2005.02.16 
					iobj.style.height = ( ( numberOfRows * cellSize ) + ( ( numberOfRows - 1 ) * cellSpacing ) + ( topSpacer + bottomSpacer ) - 2 ); 		// mas18, 2005.02.17
			   	}// else
			   
               	iobj.style.display='block';
           }// divobj != null
         }catch( e ) { showJSError( e ); }  
       }// ie 
  }// obj.container
}//ypSlideOutMenu.showMenu

ypSlideOutMenu.hideMenu = function(id)
{
	var obj = ypSlideOutMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("ypSlideOutMenu.hide('" + id + "')", ypSlideOutMenu.hideDelay);

	}
}
ypSlideOutMenu.hideAll = function()
{
	var reg = ypSlideOutMenu.Registry
	for (menu in reg) {
		ypSlideOutMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}
ypSlideOutMenu.hide = function(id)
{
	var obj = ypSlideOutMenu.Registry[id]
	obj.over = false
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	obj.hideTimer = 0
	if (obj.open && !obj.aniTimer) obj.startSlide(false)

    //  call the IE IFRAME fix, smb1@cornell.edu - 2004.12.23
    if( obj.ie ) {
	 try{ 
      var ifobj = document.getElementById( OPAGUE + obj.id );
      if( ifobj != null )
        ifobj.style.display='none';
     }catch( e ) { showJSError( e ); }  
	}// if	
	  
}//ypSlideOutMenu.hide 
ypSlideOutMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]()
	this.open = open
	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime() 
	this.aniTimer = window.setInterval(this.gRef + ".slide()", ypSlideOutMenu.minCPUResolution)
}

ypSlideOutMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime
	if (elapsed > ypSlideOutMenu.aniLen) this.endSlide()
	else {
		var d = Math.round(Math.pow(ypSlideOutMenu.aniLen-elapsed, 2) * this.accelConst)
		if (this.open && this.dirType == "-") d = -d
		else if (this.open && this.dirType == "+") d = -d
		else if (!this.open && this.dirType == "-") d = -this.dim + d
		else d = this.dim + d
		this.moveTo(d)
	}
}

ypSlideOutMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer)
	this.moveTo(this.open ? this.outPos : this.homePos)
	if (!this.open) this.setVisibility(false)
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over)
	}
}
ypSlideOutMenu.prototype.setVisibility = function(bShow) { 
	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}
ypSlideOutMenu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}
ypSlideOutMenu.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}
ypSlideOutMenu.prototype.onactivate = function() { }
ypSlideOutMenu.prototype.ondeactivate = function() { }

/**
 smb1@cornell.edu - 2004.12.23
*/
function showJSError( e ) {

 if( DEBUG )
   alert( '.js error: ' + e.description );
}// showJSError

