
SlideOutMenu.Registry = []
SlideOutMenu.aniLen = 700 //vreme za integliane na menuto
SlideOutMenu.hideDelay = 1000 //intervala za pokazvane na menuto
SlideOutMenu.minCPUResolution = 1 //stupkata za izvajdane na menuto
var d = new Date()

// constructor

function SlideOutMenu(id, direction, left, top, width, height)
{

	var fixWidth = 777		//the width of the fixed middle part of the window where the content is placed
	var menuTabLeft = 4; 	//the left point from where the menu starts
	var correction = 0;
	
	//document.clientWidth - the width of the window
	
	var  pos = (document.body.clientWidth - fixWidth)/2 + menuTabLeft;  //pos is the window width minus the fixed width of the content part, then devided by 2 (for both left and right resizable sides) and plus the start point 
			
	if (pos<(menuTabLeft + correction))  {pos = menuTabLeft + correction;} // if pos is less than the menu start point then it is equal to the start point 

	left = left + pos;
	
//alert("SlideOutMenu"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())


	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.direction	   = direction
		this.orientation   = direction == "left" || direction == "right" ? "h" : "v"
		this.directionType = direction == "right" || direction == "down" ? "-" : "+"
		this.dimention	   = this.orientation == "h" ? width : height
		this.hideTimer	   = false
		this.aniTimer	   = false
		this.open		   = false
		this.over		   = false
		this.startTime	   = 0

		// global reference to this object
		this.globalRef = "SlideOutMenu_"+id
		eval(this.globalRef+"=this")

		// add this menu object to an internal list of all menus
		SlideOutMenu.Registry[id] = this

		str=""
		str+=('<style type="text/css">')
		str+=('#' + this.id + 'Container { visibility: hidden; ')
		str+=('left:' + left + 'px; ')
		str+=('top:' + top + 'px; ')
		//str+=('}')
		str+=('overflow:hidden; }')
		str+=('#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; ')
		str+=('width:' + width + 'px; ')
		str+=('height:' + height + 'px; ')
		str+=('z-index:3;')
		str+=('clip:rect(0 ' + width + ' ' + height + ' 0); ')
		str+=('}')
		str+=('</style>')
		
		document.write(str)
		
		this.load()
	}
}

SlideOutMenu.prototype.load = function()
{

//alert("load"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())


	var zag1 = this.id + "Container"
	var zag2 = this.id + "Content"
	var obj1 = this.dom ? document.getElementById(zag1) : this.ie ? document.all[zag1] : document.layers[zag1]
	if (obj1) var obj2 = this.ns4 ? obj1.layers[zag2] : this.ie ? document.all[zag2] : document.getElementById(zag2)
	var temp

	if (!obj1 || !obj2) window.setTimeout(this.globalRef + ".load()", 100)
	else
	{
		this.container	= obj1
		this.menu		= obj2
		this.style		= this.ns4 ? this.menu : this.menu.style
		this.homePos	= eval("0" + this.directionType + this.dimention)
		this.outPos		= 0
		this.accelConst	= (this.outPos - this.homePos) / SlideOutMenu.aniLen / SlideOutMenu.aniLen 

		// set event handlers.
		if (this.ns4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("SlideOutMenu.showMenu('" + this.id + "')")
		this.menu.onmouseout = new Function("SlideOutMenu.hideMenu('" + this.id + "')")

		//set initial state
		this.endSlide()
	}
}

SlideOutMenu.showMenu = function(id)
{

//alert("showMenu"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	var registry = SlideOutMenu.Registry
	var obj = SlideOutMenu.Registry[id]
	
	if (obj.container)
	{
		obj.over = true

		// close other menus.
		//for (menu in registry) if (id != menu) SlideOutMenu.hide(menu)
		// da se maha komentara ot gornia red ako ima poveche ot 1 sub menu !!!!

		// if this menu is scheduled to close, cancel it.
		if (obj.hideTimer) 
		{ 
			registry[id].hideTimer = window.clearTimeout(registry[id].hideTimer) 
		}

		// if this menu is closed, open it.
		if (!obj.open && !obj.aniTimer) 
			registry[id].startSlide(true)
	}
}

SlideOutMenu.hideMenu = function(id)
{

//alert("hideMenu"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())



	// schedules the menu to close after <hideDelay> ms, which
	// gives the user time to cancel the action if they accidentally moused out
	var obj = SlideOutMenu.Registry[id]
	
	if (obj.container)
	{
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("SlideOutMenu.hide('" + id + "')", SlideOutMenu.hideDelay);
	}
}

SlideOutMenu.hide = function(id)
{

//alert("hide"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	var obj = SlideOutMenu.Registry[id]
	obj.over = false

	if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
	
	// flag that this scheduled event has occured.
	obj.hideTimer = 0

	// if this menu is open, close it.
	if (obj.open && !obj.aniTimer) obj.startSlide(false)
}

SlideOutMenu.prototype.startSlide = function(open) 
{

//alert("startSlide"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	this.open = open
	if (open) this.setVisibility(true)
	this.startTime = (new Date()).getTime()	
	this.aniTimer = window.setInterval(this.globalRef + ".slide()", SlideOutMenu.minCPUResolution)
}

SlideOutMenu.prototype.slide = function() 
{

//alert("slide"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	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 && this.directionType == "-")		
			d = -d
		else
			if (this.open && this.Type == "+")
				d = -d
			else
				if (!this.open && this.directionType == "-")
					d = -this.dimention + d
				else							
					d = this.dimention + d

		this.moveTo(d)
	}
}

SlideOutMenu.prototype.endSlide = function()
{

//alert("endSlide"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	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)
	}
}

SlideOutMenu.prototype.setVisibility = function(bShow)
{

//alert("setVisibility"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	var s = this.ns4 ? this.container : this.container.style
	s.visibility = bShow ? "visible" : "hidden"
}

SlideOutMenu.prototype.moveTo = function(p)
{

//alert("moveTo"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	this.style[this.orientation == "h" ? "left" : "top"] = this.ns4 ? p : p + "px"
}

SlideOutMenu.prototype.getPos = function(c) 
{

//alert("getPos"+d.getHours()+"."+d.getMinutes()+"."+d.getSeconds())

	return parseInt(this.style[c])
}

