/*****************************************************
 * Sliding menu based on youngpup's ypSlideOutMenu
 * See http://youngpup.net
 *****************************************************/

SlideOutMenu.activeMenu = null
SlideOutMenu.nextMenu = null

SlideOutMenu.aniLen = 250
SlideOutMenu.minCPUResolution = 10

function SlideOutMenu(id, width, height, open, independent) {
  this.ie  = document.all ? 1 : 0
  this.ns4 = document.layers ? 1 : 0
  this.dom = document.getElementById ? 1 : 0

  if (this.ie || this.ns4 || this.dom) {
    this.id          = id
    this.height      = height
	this.width       = width
    this.open        = open
    this.over        = false
    this.independent = independent
    this.startTime   = 0
    this.aniTimer    = false

    // global reference to this object
    this.gRef = "SlideOutMenu_"+id
    eval(this.gRef+"=this")
  
    var d = document
    var lyrId1 = this.id + "Container"
    var lyrId2 = this.id + "Content"
    this.container = this.dom ? d.getElementById(lyrId1) : this.ie ? d.all[lyrId1] : d.layers[lyrId1]
    if (this.container) this.menu = this.ns4 ? obj1.layers[lyrId2] : this.ie ? d.all[lyrId2] : d.getElementById(lyrId2)
	
    this.style    = this.ns4 ? this.menu : this.menu.style
    this.homePos  = -this.height
    this.outPos   = 0
    this.accelConst = (this.outPos - this.homePos) / SlideOutMenu.aniLen / SlideOutMenu.aniLen 

	this.moveTo(this.open ? this.outPos : this.homePos)
	
    var strCSS = '<style type="text/css">';
    strCSS += '#' + this.id + 'Container { visibility:' + (open ? "visible" : "hidden") + '; '
    strCSS += 'overflow:hidden; z-index:100; }'
    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)
  }
}
  
SlideOutMenu.prototype.toggleMenu = function() {
    if (this.open) this.hideMenu()
    else this.showMenu()
}

SlideOutMenu.prototype.showMenu = function() {
  if (!this.open) {	
  	this.onactivate();
	if (this.aniTimer) window.clearTimeout(this.aniTimer)
	if (!this.independent) {
		var m = SlideOutMenu.activeMenu
    	if (m && m != this) {
    		SlideOutMenu.nextMenu = this
    		m.hideMenu()
    		return;
		}
		else {
			SlideOutMenu.nextMenu = null
			SlideOutMenu.activeMenu = this
		}
    }
    this.startSlide(true)
  }
}

SlideOutMenu.prototype.hideMenu = function() {
  if (this.open) {
	if (this.aniTimer) window.clearTimeout(this.aniTimer)
	if (!this.independent) SlideOutMenu.activeMenu = null
	this.ondeactivate();
    this.startSlide(false)
  }
}

SlideOutMenu.prototype.startSlide = function(open) {
  this.open = open
  if (open) this.setVisibility(true)
  this.startTime = (new Date()).getTime() 
  this.aniTimer = window.setInterval(this.gRef + ".slide()", SlideOutMenu.minCPUResolution)
}

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

SlideOutMenu.prototype.endSlide = function() {
  this.aniTimer = window.clearTimeout(this.aniTimer)
  this.moveTo(this.open ? this.outPos : this.homePos)
  this[this.open ? "onactive" : "oninactive"]()
  if (!this.open) {
  	this.setVisibility(false)
  	if (SlideOutMenu.nextMenu) SlideOutMenu.nextMenu.showMenu()
  }
}

SlideOutMenu.prototype.setVisibility = function(bShow) { 
  var s = this.ns4 ? this.container : this.container.style
  s.visibility = bShow ? "visible" : "hidden"
}
SlideOutMenu.prototype.moveTo = function(p) { 
  this.style.top = this.ns4 ? p : p + "px"
}
SlideOutMenu.prototype.getPos = function(c) {
  return parseInt(this.style[c])
}

// events
SlideOutMenu.prototype.onactivate = function() { }
SlideOutMenu.prototype.ondeactivate = function() { }
SlideOutMenu.prototype.onactive = function() { }
SlideOutMenu.prototype.oninactive = function() { }

