// JavaScript Document

//
// support for collapsible extension
//

function collapse(n)
{
	var bdy = document.getElementById("collbdy-"+n);
	var img = document.getElementById("collimg-"+n);
	if (bdy.style.display=="none")
	{
		bdy.style.display = "block";
		img.src = "/wiki/skins/huka/close.gif";
	}
	else
	{
		bdy.style.display = "none";
		img.src = "/wiki/skins/huka/open.gif";
	}
}

// tooltip handling
var ie6=document.documentElement?1:0 ;	
var ie4=document.all?1:0 ;
var nn6=(document.getElementById && !document.all)?1:0 ; 
var timer; 
var objy=0;
var objx=0 ;

var newdiv = document.createElement("DIV"); var first=true ;
if (nn6) newdiv.setAttribute("style","top:100px;left:10px;position :fixed")
else newdiv.style.position="absolute" ;

var tt_header="<div id='tooltip' class='tool_div' onMouseOver='clearTimeout(timer)' onMouseOut='hide_tt()'>" +
"<table cellpadding=0 cellspacing=0 class='tool_header' width=360><tbody class='tool_text'><tr><td>";

var tt_footer="</td></tr><tr><td  class='lastr'></td></tr></table></div>";

function show_tt(evt, content) 
{
	clearTimeout(timer) ;
	var message=tt_header + content + tt_footer ;
	if (first) 
	{
		newdiv=document.body.insertBefore(newdiv, document.body.firstChild);
		first=false ; 
	}
	newdiv.innerHTML = message ;
	
	// check position of event
	if (ie6)
	{
		objy=evt.clientY + document.documentElement.scrollTop ; 
		objx=evt.clientX + document.documentElement.scrollLeft ;
	}
	else if (ie4) 
	{
		objy=evt.clientY + document.body.scrollTop ; 
		objx=evt.clientX + document.body.scrollLeft ;
	}
	
	if(!nn6) {
	 newdiv.style.top=objy+ 10 ;
	 if (objx>360 ) objx=objx-360 ;
	 else if (objx>170) objx=objx-180  ;
         newdiv.style.left=objx ; }
	
	document.getElementById('tooltip').style.visibility="visible" ;
}

function hide_tt() 
{
	timer=setTimeout ("do_hide_tt()", 500);
}

function do_hide_tt() 
{
	document.getElementById('tooltip').style.visibility="hidden";
}





function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}


//
// support TocHandler
//

/*
 * Keep the TOC static on scroll
 * (used on IE, firefox uses CSS 'fixed' )
 */
var offsetleft = 10;	
var offsettop  = 235;
var ie=document.all?1:0;
var saveTag = null;

//
function handleScroll()
{
	var the_toc = document.getElementById("tocnew");
	if (document.documentElement.scrollTop < offsettop)
	{
		the_toc.style.pixelTop = offsettop;
		the_toc.style.height = (getWindowHeight() - (offsettop - document.documentElement.scrollTop));
//window.status="1, "+ the_toc.style.height;
	}
	else 
	{
		the_toc.style.height = getWindowHeight();
//window.status="2, "+ the_toc.style.height;
		if (ie)
		{
			// reposition TOC on IE only
			the_toc.style.pixelTop  = document.documentElement.scrollTop;
			//the_toc.style.pixelLeft = document.documentElement.scrollLeft + offsetleft + findPosX(content);
		}
	}
	//
	// hilight current section
	var sections = document.getElementsByName("anchor");
	for (var i = sections.length-1; i>=0; i--)
	{
		var section = sections[i];
		if (document.documentElement.scrollTop + getWindowHeight()*0.25 > findPosY(section))
		{
			//
			// unhilight prev tag
			if (saveTag)
			{ 
				saveTag.style.backgroundImage = "none";
				saveTag.firstChild.lastChild.style.color ="#0C5FC7" ;
				saveTag.firstChild.firstChild.style.color ="#0C5FC7" ;
				saveTag.style.backgroundColor="transparent";
			} 
			// bug of explorer to display background when split lines
			// find next element to hilight
			var m = ie?1:2; // in firefox, each element has also 'text' sibling, hence has to multiply child number by 2
			var tocs = document.getElementById("toc")/*TABLE*/.firstChild/*TBODY*/.firstChild/*TR*/.firstChild/*TD*/.childNodes[1*m]/*UL*/;
//alert("tag="+tocs.nodeName+", len="+tocs.childNodes.length+", i="+i+", tocs[i*m+1-ie]="+tocs.childNodes[i*m]);
			tocs.childNodes[i*m+1-ie].style.backgroundColor = "#B6D2E7";
			tocs.childNodes[i*m+1-ie].style.backgroundImage = "url('/wiki/skins/huka/toc_li.gif')"
			tocs.childNodes[i*m+1-ie].firstChild.firstChild.style.color ="#033858" ;
			tocs.childNodes[i*m+1-ie].firstChild.lastChild.style.color ="#033858" ;
			
//			tocs.childNodes[i*m+1-ie].scrollIntoView();
			//
			// save current tag
			saveTag = tocs.childNodes[i*m+1-ie];
			break; // loop no more
		}
	} 
}

function handleResize()
{
	var the_toc = document.getElementById("tocnew");
	if (document.documentElement.scrollTop < offsettop)
		the_toc.style.height = getWindowHeight() - (offsettop - document.documentElement.scrollTop);
	else
		the_toc.style.height = getWindowHeight() - offsettop;

//window.status = getWindowHeight()+", ["+the_toc.style.height+"]";
}

//
// install handlers
// only for pages with floating TOC
if (document.getElementById("tocnew"))
{
	window.onresize = handleResize;
	handleResize();
	
	window.onscroll = handleScroll;
	handleScroll();
}


/*
 * Prepare to mark current section
 */
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
//
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// function to close scroller
function toggleToc(obj) 
{
	document.getElementById("tocnew").style.visibility='hidden';
	document.getElementById("bodyContent").style.backgroundImage='none';
}


