var arr_toc = new Array();
arr_toc[0] = new toc(0);
var debugBox = null;

function initToc()
{
	var tocLists = $('site_nav').getElementsByTagName('ul');
	for(var i=0;i<tocLists.length;i++)
	{
		with(tocLists[i])
		{
			if(/^toc_[0-9]+$/.test(id) && childNodes.length>0)
			{
				var navPath = getInnerText(previousSibling) + '|';
				for(var j=0;j<childNodes.length;j++)
				{
					var atag = childNodes[j].firstChild;
					setTocNavPathHref(atag,navPath+getInnerText(atag));
				}
			}
		}
	}

	toc_toggle('HISTORY',0,1);
}

function debug(val,clear)
{
	if(!debugBox || debugBox.visible)
	{
		var initDebugBox = function()
		{
			$('debug').innerHTML='<div><a onclick="debug(null,true);">clear</a> | <a onclick="debugBox.hide(true);">close</a></div>';
			debugBox.show();
		}
		if (!debugBox || clear)
		{
			debugBox = new DABox('debug');
			initDebugBox();
		}		
		if (val)
		{
			$('debug').innerHTML+='<hr>'+val;
			debugBox.movetomouse(0,20);
		}
	}
}

function mhMenu(tag,menu)
{
	tag.style.backgroundColor='#003399';
	tag.style.color='#ffffff';
	var div = $('mh_'+menu);
	if(div)
	{
		var menuBox = div.dabox;
	}
	else
	{
		var menuBox = new DABox('mh_'+menu,10);
		div = menuBox.div;
		div.style.backgroundColor='#003399';
		
		var setMenuContent = function(url,content)
		{
			menuBox.setcontent(content);
			var liTags = menuBox.div.getElementsByTagName('li');
			for (var i=0;i<liTags.length;i++)
			{
				liTags[i].onclick=closeMenu;
			}
		}
		
		serverScript('/AJAX/'+menu+'_menu.asp',setMenuContent);
	}
	
	if(!menuBox.visible)
	{
		var pos = getPosition(tag);
		pos.top+=tag.offsetHeight;
		menuBox.position(pos.left,pos.top);
		menuBox.fadein();
	}

	var menuMouseOut = function(e)
	{
		var event = fixEvent(e);
		var el = event.relatedTarget || event.toElement;
		while(el)
			if(el==menuBox.div || el==tag)
				break;
			else
				el=el.parentNode;
				
		if(!el)
		{
			closeMenu();
		}
	}
	
	var closeMenu = function()
	{
		tag.style.backgroundColor='#ffffff';
		tag.style.color='#003399';
		menuBox.hide();
	}
	
	menuBox.div.onmouseout=menuMouseOut;
	tag.onmouseout=menuMouseOut;
}

function guarantee()
{
	var div = $('guarantee_box');
	if(div)
	{
		var mb = div.dabox;
	}
	else
	{
		var mb = new DABox('guarantee_box',4);
		mb.div.className += ' popup';
		mb.div.onclick=function(e)
		{
			mb.fadeout();
		};
		
		serverScript('/guarantee.asp?ajax=1',function(url,content)
		{
			mb.setcontent(content);
			mb.appendCloseButton();
			
		});
	}

	if(!mb.visible)
	{
		mb.position(mouseX,mouseY);
		mb.fadein(500);
	}
}

function contact()
{
	PopUpWindow('https://'+window.location.hostname+'/contact.asp','contact','720','540','1','200');
}

function requestCatalog()
{
	PopUpWindow('https://'+window.location.hostname+'/requestcatalog.asp','contact','720','580','1','200');
}

function freeSample(product)
{
	PopUpWindow('https://'+window.location.hostname+'/'+product+'/SAMPLE-'+product,'contact','720','500','1','200');
}

function toc_toggle(levelid,subgroup,nowindowshade)
{
	if(levelid=='HISTORY')
		var tag=$('toc_history');
	else
		var tag=$('toc_'+subgroup);
	if(tag)
	{
		var temp = tag.innerHTML.trim();
		if (temp == '')
		{
			var f = function(key,content)
			{
				if(!nowindowshade)
					windowshade(tag,content);
				else
					tag.innerHTML=content;

				// Construct NavPath string
				var atags = tag.getElementsByTagName('a');
				if(atags.length)
				{
					var el = atags[0];
					while(el)
						if(el.className=='toc_container')
							break;
						else
							el=el.parentNode;
					
					if(el)
					{
						navPath = getInnerText(el.firstChild) + '|';
					}

					for (var i=0;i<atags.length;i++)
					{
						setTocNavPathHref(atags[i],navPath+getInnerText(atags[i]));
					}
				}
			}
			serverScript('/JS_ACTIONS/toc_subgroup.asp?levelid='+levelid+'&subgroup='+subgroup,f);
		}
		else
			windowshade(tag);
	}
	return false;
}

function windowshade(tag,content)
{
	var m = 500; //milliseconds
	if(content)	
	{
		tag.style.position='absolute';
		tag.style.visibility='hidden';
		tag.style.height='auto';
		tag.innerHTML=content;
		var n=tag.offsetHeight;
		var c=1;
		tag.style.height='1px';
		tag.style.visibility='visible';
		tag.style.position='static';
	}
	else
	{
		var n=1;
		var c=tag.offsetHeight;
	}

	var x = 1;
	var z = 1;
	var t1 = 0;
	var t2 = 0;
	var d = Math.abs(c-n); //delta

	if(n>c)
	{
		var tst = function(c,n){return c<=n;}
		var i = 1;
	}
	else
	{
		var tst = function(c,n){return c>=n;}
		var i = -1;
	}
	
	while(tst(c,n))
	{
		t1 = Math.round(m / d * x / Math.pow(10,z));
		if (t1>t2)
			setTimeout("$('" + tag.id + "').style.height='"+c.toString()+"px'",t1);
		x++;
		c=c+i;
		z=z-1/d;
		t2=t1;
	}
	if(!content)
	{
		setTimeout("$('" + tag.id + "').innerHTML=''",t1+50);
	}
	setTimeout("$('" + tag.id + "').style.height='auto'",t1+50);
}


/* class toc */

function toc(depth)
{
	this.depth = depth;
	this.linkTag = null;
	this.visible=false;
	this.dabox = null;
}

toc.prototype.mouseover = function(linkTag,delay)
{
	if(!this.dabox)
	{
		this.dabox = new DABox('toc_dabox'+this.depth,10+this.depth);
		arr_toc[this.depth+1] = new toc(this.depth+1);
	}
	
	this.visible=true;
	if (this.linkTag==linkTag)
		this.dabox.show();
	else
	{
		this.dabox.hide();
		this.linkTag = linkTag;
		
		setTimeout("arr_toc["+this.depth+"].show('"+this.linkTag.href+"');",1000*delay);
	}
	for(var i=this.depth;i>=0;i--)
	{
		arr_toc[i].linkTag.style.color='#900';
		arr_toc[i].linkTag.style.backgroundImage='url("/IMG/design/toc_arrow.png")';
		arr_toc[i].linkTag.style.backgroundPosition='0 -45px';
	}
}

toc.prototype.mouseout = function()
{
	this.visible=false;
	setTimeout('arr_toc['+this.depth+'].hide();',500);
	if(this.linkTag)
	{
		this.linkTag.style.color='#000';
		this.linkTag.style.backgroundImage='';
		this.linkTag.style.backgroundPosition='0 0';
	}
}

toc.prototype.show = function(linkHref)
{
	if(this.visible && this.linkTag.href==linkHref)
	{
		var me = this;
		
		var f = function(key,content)
		{
			me.dabox.setcontent(content.replace(/^[a-z]+\r/,''));
			me.dabox.div.style.backgroundImage='';
			me.dabox.div.style.backgroundRepeat='no-repeat';
			me.dabox.div.style.paddingLeft = '0';
			me.dabox.div.style.paddingRight = '0';
			
			var scrollBottom = f_scrollTop()+f_clientHeight();

			if(/<!--movetomouse-->/.test(content))
			{
				var offsetX = 10;
				if(mouseY+me.dabox.getHeight()-15>scrollBottom)
					var offsetY = scrollBottom-me.dabox.getHeight()-mouseY;
				else
					var offsetY = -15;
				me.dabox.movetomouse(offsetX, offsetY);
			}
			else
			{
				var p = getPosition(me.linkTag);
				var posY = Math.round(p.top-me.dabox.getHeight()/3,0);

				if(posY+me.dabox.getHeight()>scrollBottom)
				{
					posY=scrollBottom-me.dabox.getHeight();
					var bgTop = p.top-posY-40;
				}
				else
				{
					var bgTop = (me.dabox.getHeight()/3-40);
				}
				
				if(p.left+me.linkTag.offsetWidth+me.dabox.getWidth()>(f_scrollLeft()+f_clientWidth()) && p.left-me.dabox.getWidth()-30>f_scrollLeft())
				{
					var posX = p.left-me.dabox.getWidth()-30;
					me.dabox.div.style.backgroundImage='url("/IMG/design/callout_arrow_right.png")';
					me.dabox.div.style.backgroundPosition=me.dabox.getWidth()+'px '+bgTop.toString()+'px';
					me.dabox.div.style.paddingRight = '30px';
				}
				else
				{
					var posX = p.left+me.linkTag.offsetWidth
					me.dabox.div.style.backgroundImage='url("/IMG/design/callout_arrow_left.png")';
					me.dabox.div.style.backgroundPosition='0 '+bgTop.toString()+'px';
					me.dabox.div.style.paddingLeft = '30px';
				}
				
				me.dabox.position(posX,posY);
			}
			me.dabox.fadein();

			var atags = me.dabox.div.getElementsByTagName('a');
			for (var i=0;i<atags.length;i++)
			{
				atags[i].onclick = me.setNavPathHref;
			}
		}
		
		var u = new SiteUrl(linkHref);
		
		serverScript('/JS_ACTIONS/toc_mouseover.asp?levelid='+u.levelid+'&pageid='+u.pageid+'&subgroup='+u.subgroup+'&depth='+this.depth,f);
	}
}

toc.prototype.setNavPathHref = function(e)
{
	var event = fixEvent(e);

	var navPath = getInnerText(arr_toc[0].linkTag.parentNode.parentNode.previousSibling) + '|' + getInnerText(arr_toc[0].linkTag);
	for (var i=0;i<=this.depth;i++)
	{
		if(arr_toc[i].linkTag)
			navPath+=('|'+getInnerText(arr_toc[i].linkTag));
	}
	navPath+='|'+(getInnerText(event.target) || 'Image');
	setTocNavPathHref(event.target,navPath);
}

toc.prototype.hide = function()
{
	if (!this.visible && !arr_toc[this.depth+(this.depth<arr_toc.length-1?1:0)].visible)
	{
		this.linkTag=null;
		this.dabox.hide();
		if (this.depth>0)
			if (!arr_toc[this.depth-1].visible)
				arr_toc[this.depth-1].mouseout();
	}
}

function setTocNavPathHref(atag,path)
{
	if(path)
		var navPath = path;
	else
	{
		var navPath = '';
		var el = atag;
		while(el)
			if(el.className=='toc_container')
				break;
			else
				el=el.parentNode;
		
		if(el)
		{
			navPath = getInnerText(el.firstChild) + '|';
		}
		navPath+=getInnerText(atag);
	}
	
	atag.href+=('?navpath='+encodeURIComponent(navPath));
}

function loadSlideBox()
{
	var mb = new DABox('slidebox');
	var slidebox = $('slidebox');
		
	mb.setcontent('<div class=slidebox><a id="foo" onclick="this.parentNode.parentNode.dabox.fadeout();">HIDE</a></div>');
	mb.position(slidebox.parentNode.offsetWidth,0);
	mb.fadein();
	mb.slideto((slidebox.parentNode.offsetWidth-1000)/2+750-slidebox.offsetWidth,150,2000);
}
