/******************************************************************************************************************************************/
/* kilncare menu - 14/03/2008
/******************************************************************************************************************************************/

var menu;

function classMenu(handle)
{
	/**************************************************************************************************************************************/
	/* ANIMATION SETTINGS (ONLY EDIT THIS BIT PLEASE!!!)
	/**************************************************************************************************************************************/
	this.anmStep	 	= 6;			// number of pixels moved in each animation frame 									(original - 8)
	this.anmWait	 	= 1000;			// millisecond delay before closing dropdown once mouse moved away					(original - 1000)
	this.anmTick	 	= 15;			// animation frame interval in milliseconds for slider movement 					(original - 20)
	/**************************************************************************************************************************************/
	
	this.handle		 	= handle;		// handle of object instance
	
	this.anmTimeout  	= 0;			// animation timeout for slider
	
	this.items		 	= new Array();	// array of menu items
	this.index		 	= -1;			// index of currently selected menu item (-1 denotes nothing selected)
	this.popupIndex		= -1;			
	
	// get document objects
	this.container 	 	= document.getElementById('menuContainer');		// container
	this.popup 	 		= document.getElementById('menuPopup');			// popup
	this.content 	 	= document.getElementById('menuPopup_Content');	// popup content
	this.footer 	 	= document.getElementById('menuPopup_Footer');	// popup footer
	
	this.popup.onmouseover 	= new Function("evt", this.handle+".mouseover(-1)");		
	this.popup.onmouseout 	= new Function("evt", this.handle+".mouseout()");		
	
	// extract objects within the main body
	var items2 	= this.container.getElementsByTagName("DIV");
	
	this.defaultItem = -1;
	
	// loop through objects and identify menu items
	for(var i=0; i<items2.length; i++) 
	{ 
		// check if menu item by classname
		if(items2[i].className == "menuItem") 
		{ 
			if(items2[i].getAttribute("default") == "true")
			{
				this.defaultItem = this.items.length;	
			}
			
			// assign mouse event handlers
			items2[i].onmouseover 	= new Function("evt", this.handle+".mouseover("+this.items.length+")");		
			items2[i].onmouseout 	= new Function("evt", this.handle+".mouseout()");		
			
			// add menu item to array
			this.items[this.items.length] = items2[i]; 
		} 
	}
	
	// assign class functions
	this.mouseclick  = classMenu_mouseclick;
	this.mouseover   = classMenu_mouseover;
	this.mouseout    = classMenu_mouseout;
	this.animate     = classMenu_animate;
	this.clear		 = classMenu_clear;
	this.resetClass	 = classMenu_resetClass;
	
	if(this.defaultItem != -1)
	{
		this.items[this.defaultItem].className = 'menuItem_sel';
	}
}

function classMenu_resetClass(index)
{
	if(index != this.defaultItem)
	{
		this.items[index].className = 'menuItem';
	}
}

// mouse over a menu item
function classMenu_mouseover(index)
{
	this.clear();
	
	if(index > - 1)
	{
		if(this.index > -1)
		{
			this.resetClass(this.index);
			this.popup.style.display = 'none';
		}
		
		var subitems = this.items[index].getElementsByTagName("DIV");		
		
		var left = this.items[index].offsetLeft;
		var top = this.items[index].offsetTop;
		var element = this.items[index];
		while(element.offsetParent != null) 
		{   
			var parent 	= element.offsetParent;
			left 	   += parent.offsetLeft;
			top 	   += parent.offsetTop;
			element 	= parent;
		}		
				
		this.items[index].className = 'menuItem_sel';
		
		if(subitems[0].innerHTML != "")
		{
			this.popupIndex = index;
			
			this.popup.style.left = this.items[index].offsetLeft;
			if(window.ActiveXObject)
			{
				this.popup.style.top = top + 15;
			}
			else
			{
				this.popup.style.top = top + 18;
			}
			this.popup.style.display = "block";
			
			this.footer.style.width = "0px";
			
			var inneritem = this.content.getElementsByTagName("DIV");	
			inneritem[0].innerHTML = subitems[0].innerHTML;
			
			this.footer.style.width = this.content.offsetWidth +"px";
			this.footer.style.display = 'block';
			
			this.anmTimeout = setTimeout(this.handle+".animate()", this.anmTick);
		}
		else
		{
			this.anmTimeout = setTimeout(this.handle+".animate(true, true)", this.anmTick);
		}
		this.index = index;
	}
}

// mouse leave a menu item
function classMenu_mouseout()
{
	this.clear();																	// clear any current animations
	if(this.popup.style.display == 'block')
	{
		this.anmTimeout = setTimeout(this.handle+".animate(true)", this.anmWait);	// initiate new animation
	}
	else
	{
		this.resetClass(this.index);		
		this.animate(true);															// force animation							
	}
}

// mouse clicks a menu item
function classMenu_mouseclick(index) { }

// animate slider
function classMenu_animate(closing, skip)
{	
	if(typeof closing == "undefined") 	{ closing 	= false; 	}
	if(typeof skip == "undefined") 		{ skip 		= false;	}	
	
	var curHeight = (this.content.style.height) ? removePx(this.content.style.height) : 0;
	var newHeight = (closing) ? curHeight - this.anmStep : curHeight + this.anmStep;
	
	var inneritem = this.content.getElementsByTagName("DIV");	
	
	this.content.style.height = newHeight + "px";
	
	if(closing)
	{
		if(newHeight < this.anmStep)			
		{
			this.popup.style.display = 'none';
			if(this.popupIndex == this.index)
			{
				this.popupIndex = -1;
				if(!skip) { this.resetClass(this.index); }
			}
		}
		else
		{
			setTimeout(this.handle+".animate("+closing+", "+skip+")", this.anmTick);		
		}
	}
	else
	{	
		if(newHeight > (inneritem[0].offsetHeight - this.anmStep))				
		{
			this.content.style.height = inneritem[0].offsetHeight + this.footer.offsetHeight;				
		}
		else
		{
			setTimeout(this.handle+".animate("+closing+")", this.anmTick);		
		}
	}
}

// clear current animation sequences
function classMenu_clear()
{
	if(this.anmTimeout) { clearTimeout(this.anmTimeout); }					
}

// initiate accordion object
function initMenu()
{
	menu = new classMenu("menu");												// initiate new menu object
}