﻿function Menu(head,child,dir,speed,init_state,ext_on,ext_off)
{
	this.head = document.getElementById(head);
	this.body = document.getElementById(child);
	this.direction = dir;
	this.speed = speed;
	this.ext_on = ext_on;
	this.ext_off = ext_off;
	this.init_state = init_state;
	this.a = 10;
	this._interval = false;
	this._last_state = false;
	this._size = false;
	this._temp = false;
	this._js = false;
	this._div = false;
	this._parent = false;
	this._parent_control = false;
	var self = this;
	var temp = new Array(null,null);
	this.click = function(e)
	{
		if (self._parent_control)
		{
			self._parent._control(self);
			return false;
		}
		else
		{
			Interval.clear(self._interval);
			if (self._last_state == false)
			{
				self._on();
				return false;
			}
			else
			{
				self._off();
				return false;
			}
		}
	}
	this.init = function()
	{
		this.head.onclick = this.click;
		this.head.onkeypress = function(e)
		{
			e||(e=window.event);
			if (!(e.keyCode ==32 || e.keyCode == 0))return;
			self.click();
		}
		for(var i=0;i<this.body.childNodes.length;i++)
		{
			if (this.body.childNodes[i].nodeType==1)
			{
				this._div=this.body.childNodes[i];
				break;
			}
		}
		if (parseInt(this.body.style.height))
		{
			this._size = parseInt(this.body.style.height);
		}
		else
		{
			this._size = this._div.offsetHeight;
		}
		switch (this.init_state)
		{
			case true:
				if (this.body.style.display == 'none')
				{
					this._on();
				}
				else
				{
					this._last_state = true;
				}
				break;
			default:
				if (this.body.style.display !='none')
				{
					this._last_state = true;
					this._off();
				}
				break;
		}
	}
	this._on = function()
	{
		if (self._last_state == false)
		{
			self._last_state = true;
			self.body.style.display="";
			temp[1] = self.a?2*parseInt(Math.sqrt(self.a*self._size))+1:self._size/5;
			if (isNaN(parseInt(self.body.style.height)))self.body.style.height="0px";
			if (self.ext_on)
			{
				self.ext_on(self.head,self.body)
			}
			self._interval = Interval.set(self._action_on,speed);
		}
	}
	this._off = function()
	{
		if (self._last_state == true)
		{
			self._last_state = false;
				temp[0]=self.a?2*parseInt(Math.sqrt(self.a*self._size))+1:self._size/5;;
			if(isNaN(parseInt(self.body.style.height)))self.body.style.height = self._size+'px';
			if (self.ext_off)
			{
				self.ext_off(self.head,self.body)
			}
			self._interval = Interval.set(self._action_off,this.speed);
		}
	}
	this._action_on = function()
	{
		if (parseInt(self.body.style.height)+temp[1]>self._size)
		{
			self.body.style.height = self._size+'px';
			Interval.clear(self._interval);
		}
		else
		{
		self.body.style.height = parseInt(self.body.style.height)+temp[1]+'px';
		temp[1] +=self.a;
		}
	}
	this._action_off = function()
	{
		if(parseInt(self.body.style.height)-temp[0]<0)
		{
			Interval.clear(self._interval);
			self.body.style.display = "none";
		}
		else
		{
			self.body.style.height = parseInt(self.body.style.height)-temp[0]+'px';
			temp[0]-=self.a;
		}
	}
}
function navbar(dir,a,speed,ext_on,ext_off)
{
	this.open_only_one = true;
	this.dir = dir;
	this.a =a;
	this.speed =speed;
	this.ext_on = ext_on;
	this.ext_off = ext_off;
	this.menu_item = new Array();
	this._openning;
	this.open_all = function()
	{
	};
	this.add = function (head,body)
	{
		var temp = new Menu(head,body,this.dir,this.speed,this.ext_on,this.ext_off);
		this.menu_item.push(temp);
	};
	this.init = function ()
	{
			for(var i = 0;i<this.menu_item.length;i++)
			{
				this.menu_item.init();
			}
		
	};
	this._control = function(child)
	{
		var self =child;
		Interval.clear(self._interval);
		if (self._last_state == false)
		{
			if (typeof(self._parent._openning) == 'object')
				{
					self._parent._openning._off();
					self._parent._openning = self;
				}
			self._on();
			return false;
		}
		else
		{
			return false;
		}
	}
}
Interval=
{
	length:20,
	_action : new Array(length),
	_stack : new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19),
	_interval : Array(length),
	_timeout: new Array(length),
	set:function(action_function,speed,time_out)
	{
		time_out = time_out?time_out:35000;
 		var p = Interval._stack.pop();
		if(p)
		{
			Interval._action[p] = action_function;
			Interval._interval[p]=setInterval('if(Interval._action['+p+'])Interval._action['+p+']();',speed);
			Interval._timeout[p] = setTimeout('Interval.clear('+p+')',time_out);
			return p;
		}
	},
	clear:function(p)
	{
		if (Interval._action[p])
		{
			clearInterval(Interval._interval[p]);
			clearTimeout(Interval._timeout[p]);
			Interval._action[p] = "";
			Interval._stack.push(p);
		}
	}
}
var show = false;
var hide = true;
function my_on(head,body)
{
	var tag_a;
	for(var i=0;i<head.childNodes.length;i++)
	{
		if (head.childNodes[i].nodeName=="A")
		{
			tag_a=head.childNodes[i];
			break;
		}
	}
	tag_a.className="on";
}
function my_off(head,body)
{
	var tag_a;
	for(var i=0;i<head.childNodes.length;i++)
	{
		if (head.childNodes[i].nodeName=="A")
		{
			tag_a=head.childNodes[i];
			break;
		}
	}
	tag_a.className="off";
}
window.onload=function()
{
	m1 =new Menu("menu1",'menu1_child','dtu','100',show,my_on,my_off);
	m1.init();
}
