/******************************************************************************************************************************************/
/* kilncare accordion - 14/03/2008
/******************************************************************************************************************************************/

var accordion;

function classAccordion(handle)
{
	/**************************************************************************************************************************************/
	/* ANIMATION SETTINGS (ONLY EDIT THIS BIT PLEASE!!!)
	/**************************************************************************************************************************************/
	this.anmStep	 	= 8;			// number of pixels moved in each animation frame 									(original - 8)
	this.anmFade	 	= 0.1;			// fade in/out increment (0.1 is 10%, can be as small as 0.01)						(original - 0.1)
	this.anmWait	 	= 120;			// initial slider animation delay after mouse begins to move 						(original - 250)
	this.anmTick	 	= 20;			// animation frame interval in milliseconds for slider movement 					(original - 20)
	this.anmTick_Sub	= 30;			// animation frame interval in milliseconds for sub-menu expanding/collapsing 		(original - 30)
	this.anmTick_Fade	= 30;			// animation frame interval in milliseconds for fading								(original - 30)
	/**************************************************************************************************************************************/
	
	this.handle		 	= handle;		// handle of object instance
	
	this.anmTimeout  	= 0;			// animation timeout for slider
	this.anmTimeout2 	= 0;			// animation timeout for expand/collapse
	this.anmTimeout3 	= 0;			// animation timeout for fading
	
	this.curY		 	= 54;			// Y co-ordinate of current slider position
	this.selY		 	= 54;			// Y co-ordinate of selected menu item
	this.lastY		 	= 0;			// Y co-ordinate of last menu item the mouse was over
	
	this.items		 	= new Array();	// array of menu items
	this.index		 	= -1;			// index of currently selected menu item (-1 denotes nothing selected)
	this.subitemOpen 	= false
	this.active 		= false;
	
	// get document objects
	this.container 	 	= document.getElementById('accordionContainer');	// container
	this.hover 		 	= document.getElementById('accordionSelect');		// slider
	this.main 		 	= document.getElementById('accordionBody');			// main body
	
	// set slider as opaque
	if(window.ActiveXObject)
	{
		this.hover.style.filter = "alpha(opacity=0)";
		this.hover.style.zoom = "1"; 
	}
	else
	{
		this.hover.style.opacity = "0.0";	
	}
	
	// extract objects within the main body
	var items2 	= this.main.getElementsByTagName("DIV");
	
	var defaultClick = -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 == "accordionItem") 
		{ 
			if(items2[i].getAttribute("default") == "true")
			{
				defaultClick = 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()");		
			items2[i].onclick 		= new Function("evt", this.handle+".mouseclick("+this.items.length+")");	
			
			// add menu item to array
			this.items[this.items.length] = items2[i]; 
		} 
	}
	
	// assign class functions
	this.mouseclick  = classAccordion_mouseclick;
	this.mouseover   = classAccordion_mouseover;
	this.mouseout    = classAccordion_mouseout;
	this.animate     = classAccordion_animate;
	this.animateSub  = classAccordion_animateSub;
	this.animateFade = classAccordion_animateFade;
	this.slider 	 = classAccordion_slider;
	this.clear		 = classAccordion_clear;
	
	if(defaultClick != -1)
	{
		this.mouseclick(defaultClick);
	}
}

// mouse over a menu item
function classAccordion_mouseover(index)
{
	this.lastY = this.items[index].offsetTop + this.main.offsetTop;				// calculate Y position of menu item
	this.clear();																// clear any current slider animations
	if(!this.subitemOpen && !this.active)
	{
		this.selY = this.lastY;	
		this.slider(this.selY);
	}
	else
	{
		this.anmTimeout = setTimeout(this.handle+".animate()", this.anmWait);		// initiate new slider animation
	}
	this.anmTimeout3 = setTimeout(this.handle+".animateFade(true)", this.anmWait);	// initiate fadein animation 
	this.active = true;
}

// mouse leave a menu item
function classAccordion_mouseout()
{
	if(!this.subitemOpen)
	{
		this.selY = this.lastY;	
	}
	this.clear();																// clear any current slider animations
	this.anmTimeout = setTimeout(this.handle+".animateFade()", this.anmWait);	// initiate new slider animation
}

// mouse clicks a menu item
function classAccordion_mouseclick(index)
{
	this.clear(true);															// clear any current sub-menu animations
	if(window.ActiveXObject)
	{
		this.hover.style.filter = "alpha(opacity=100)";
		this.hover.style.zoom = "1"; 
	}
	else
	{
		this.hover.style.opacity = "1.0";										// set slider as opaque
	}
	
	if(index == this.index)
	{
		this.anmTimeout2 = setTimeout(this.handle+".animateSub("+this.index+",1)", this.anmWait);
		this.index = -1;
	}
	else
	{
		this.anmTimeout2 = setTimeout(this.handle+".animateSub("+index+",0)", this.anmWait);								// initiate new sub-menu open animation
		if(this.index >= 0) { this.anmTimeout2 = setTimeout(this.handle+".animateSub("+this.index+",1)", this.anmWait); }	// initiate new sub-menu close animation
		this.index = index;																									// set currently selected menu item index
	}
	this.selY = this.items[index].offsetTop + this.main.offsetTop;				// calculate Y position of selected menu item
	this.slider(this.selY);														// set slider position
}

// animate slider
function classAccordion_animate()
{	
	var diff;
	diff = this.lastY - this.curY;												// calculate difference between current and new Y position
	if(diff > 0)																// if new position "below"
	{
		if(diff < this.anmStep)													// if remaining gap is less than animation step
		{
			this.hover.style.top = this.lastY;									// set slider to final position
			this.curY = this.lastY;												// update current Y position
		}
		else
		{
			this.slider(this.curY + this.anmStep);								// set slider to next animation position
			setTimeout(this.handle+".animate()", this.anmTick);					// initiate next animation frame
		}
	}
	else																		// if new position "above"
	{
		if(diff > (0 - this.anmStep))											// if remaining gap is less than animation step
		{
			this.hover.style.top = this.lastY;									// set slider to final position
			this.curY = this.lastY;												// update current Y position
		}
		else
		{
			this.slider(this.curY - this.anmStep);								// set slider to next animation position
			setTimeout(this.handle+".animate()", this.anmTick);					// initiate next animation frame
		}
	}
}


// animate fade in/out
function classAccordion_animateFade(fadein)
{
	if(typeof fadein == "undefined") { fadein = false; }						// determine if fading in or out
	
	var opacity;
	
	if(fadein)																	// if fading in
	{
		if(!this.subitemOpen || this.lastY != this.selY)						// if submenu not open or destination differs from 
		{
			if(this.hover.style.opacity < 1)									// if not opaque
			{
				if(window.ActiveXObject)
				{
					this.hover.style.filter = "alpha(opacity=100)";
					this.hover.style.zoom = "1";
				}
				this.hover.style.opacity = parseFloat(this.hover.style.opacity) + this.anmFade;			// increment opacity
				
				this.anmTimeout3 = setTimeout(this.handle+".animateFade(true)", this.anmTick_Fade);			// initiate next animation frame
			}
		}
	}
	else																		// if fading out
	{
		if(!this.subitemOpen || this.lastY != this.selY)						// if current position not selected position
		{
			if(this.hover.style.opacity > 0)									// if not transparent
			{
				this.hover.style.opacity = parseFloat(this.hover.style.opacity) - this.anmFade;		// increment opacity
				this.anmTimeout3 = setTimeout(this.handle+".animateFade()", this.anmTick_Fade);		// initiate next animation frame
			}
			else
			{
				this.hover.style.opacity = "0.0";								// ensure it's completely transparent
				this.lastY = this.selY;											// set last Y position as selected position
				this.slider(this.lastY);										// set slider position
				this.active = false;
			}
		}
	}
}

// animate sub-menu
function classAccordion_animateSub(index, closing)
{	
	if(window.ActiveXObject)
	{
		this.hover.style.filter = "alpha(opacity=0)";
		this.hover.style.zoom = "1"; 
	}
	else
	{
		this.hover.style.opacity = "0.0";										// set slider as opaque
	}
	var subitems = this.items[index].getElementsByTagName("DIV");				// get submenu object of menu item
	subitems[0].style.display = 'block';										// set display to determine height
	var inneritem = subitems[0].getElementsByTagName("DIV");					// get container object of submenu
	subitems[0].style.overflow = "hidden";										// prevent overflow from showing during animation
	
	var curHeight = (subitems[0].style.height) ? removePx(subitems[0].style.height) : 0;			// calculate actual height of submenu
	var newHeight = (closing) ? curHeight - this.anmStep : curHeight + this.anmStep;				// calculate new height of submenu
	
	subitems[0].style.height = newHeight + "px";								// set height of submenu
	this.items[index].style.height = newHeight + 40;							// set height of menu item
	
	if(closing)																	// if closing animation
	{
		if(newHeight <= this.anmStep)											// if remaining gap is less than animation step
		{
			subitems[0].style.height = 0;										// set submenu height to zero
			subitems[0].style.display = 'none';									// set display to nothing
			this.items[index].className = 'accordionItem';						// reset menu item class
			this.subitemOpen = false;
		}
		else
		{
			setTimeout(this.handle+".animateSub("+index+","+closing+")", this.anmTick_Sub);			// initiate next animation frame
		}
	}
	else																		// if opening animation
	{
		this.items[index].className = 'accordionItem_Sel';						// set menu item class
		if(newHeight >= (inneritem[0].offsetHeight - this.anmStep))				// if remaining gap is less than animation step
		{
			subitems[0].style.height = inneritem[0].offsetHeight;				// set submenu height to content height
			this.subitemOpen = true;
		}
		else
		{
			setTimeout(this.handle+".animateSub("+index+","+closing+")", this.anmTick_Sub);			// initiate next animation frame
		}
	}
	if(this.items[this.index])
	{
		this.selY = this.items[this.index].offsetTop + this.main.offsetTop;		// calculate slider position
	}
	else
	{
		this.selY = this.main.offsetTop;										// calculate slider position
	}
	this.lastY = this.selY;														// set last Y position
	this.clear();																// clear any slider animations
}

// set position of slider
function classAccordion_slider(value)
{
	this.hover.style.top = value;												// set slider Y position
	this.curY = value;															// set current Y position
}

// clear current animation sequences
function classAccordion_clear(submenu)
{
	if(typeof submenu == "undefined") { submenu = false; }
	if(submenu)	
	{
		if(this.anmTimeout2) { clearTimeout(this.anmTimeout); }					// clear submenu animation
		this.anmTimeout2 = false;
	}
	else
	{
		if(this.anmTimeout) { clearTimeout(this.anmTimeout); }					// clear slider animation
		this.anmTimeout = false;
	}
}

// initiate accordion object
function initAccordion()
{
	accordion = new classAccordion("accordion");								// initiate new accordion object
}

function removePx(str)
{
	str = doReplace(str, 'px', '');
	return parseInt(str);
}
function doReplace(str, from, to)
{
	str = str.replace(from, to);
	return str;
}