var strTree = '<ul id="learn">';
var learn = null;
var xmlDoc = null;
var links = [];
var titles = [];
var nextLink = null;
var prevLink = null;
var first = true;
var vmi = null;

jQuery(document).ready(function() {
    loadXMLDoc("/public/learn/interior/2009/hun/content.xml");
	learn = xmlDoc.childNodes;

	tree(learn);
	linksList(learn);
	titlesList(learn);
	$('learnTree').innerHTML += strTree + '</ul>';
	anchor.init();
});

function titlesList(node)
{
	for(var i = 0; i < node.length; i++)
	{
		if(node.item(i).nodeType==1)
		{
            if(node.item(i).childNodes.length==0)
            {
               titles.push(node.item(i).getAttribute('title'));
  			}
  			else
  			{
                titlesList(node.item(i).childNodes);
  			}
  		}
	}
}

function linksList(node)
{
	for(var i = 0; i < node.length; i++)
	{
		if(node.item(i).nodeType==1)
		{
            if(node.item(i).childNodes.length==0)
            {
               links.push(node.item(i).getAttribute('link'));
  			}
  			else
  			{
                linksList(node.item(i).childNodes);
  			}
  		}
	}
}

function nextprev(needle)
{
	for(var i = 0; i < links.length; i++)
	{
		if(links[i]==needle)
		{
			//set the link parameters
			prevLink = i==0 ? links[links.length-1] : links[i-1];
			nextLink = i==(links.length-1) ? links[0] : links[i+1];

			//update the onmouseover text with the titles
			$('click_div_prev').innerHTML = i==0 ? titles[links.length-1] : titles[i-1];
			$('click_div_next').innerHTML = i==(links.length-1) ? titles[0] : titles[i+1];
		}
	}
}

function removeChildSafe(obj)
{
    while(obj.childNodes.length > 0)
	{
        removeChildSafe(obj.childNodes[obj.childNodes.length-1]);
    }
    obj.parentNode.removeChild(obj);
}

function setLinks()
{
	$('prevLinkStart').innerHTML = '<a class="anchorLink" href="#learnTopAnchor" onclick="javascript:showLearn(\'' + prevLink + '\')" onmouseover="tooltip.show(\'' + jQuery('#click_div_prev').html() + '\')" onmouseout="tooltip.hide()">&lt;&lt; Prevoius Page</a>';
	$('nextLinkStart').innerHTML = '<a class="anchorLink" href="#learnTopAnchor" onclick="javascript:showLearn(\'' + nextLink + '\')" onmouseover="tooltip.show(\'' + jQuery('#click_div_next').html() + '\')" onmouseout="tooltip.hide()">Next Page  &gt;&gt;</a>';
	$('prevLinkEnd').innerHTML = $('prevLinkStart').innerHTML;
	$('nextLinkEnd').innerHTML = $('nextLinkStart').innerHTML;
}

function calcHeight()
{
	var the_height = $('help_content').contentWindow.document.body.scrollHeight;
	jQuery('#help_content').height(the_height + 20);
	anchor.init();
}

function showLearn(src)
{
	jQuery('#linksStart').show();
	jQuery('#linksEnd').show();
	first ? $('learnCont').innerHTML = '<iframe onload="calcHeight()" scrolling="no" id="help_content" src="' + src + '" width="100%" height="1" frameborder="0"></iframe>' : $('help_content').src = src;
	nextprev(src);
	setLinks();
	closer();
	boldLearn(src);
	first = false;
}

function closer()
{
	jQuery('#learn .folder .open').attr('class','closed');
	jQuery('#learn ul').hide();
}

function boldLearn(src)
{
	var learn = jQuery('#learn');
	var classLi = jQuery('#learn .file');

	i = 0;
	while(classLi[i])
	{
		if(classLi[i].childNodes[0].childNodes[0].id==src)
		{
			//if the folder is closed than we open it
            if(classLi[i].parentNode.style.display=='none')
            {
				classLi[i].parentNode.style.display = 'block';
            }
            //then change the class to open
            $('li_' + classLi[i].parentNode.id).childNodes[0].className = 'open';

			classLi[i].childNodes[0].childNodes[0].className = 'bold';
		}
		else
		{
            classLi[i].childNodes[0].childNodes[0].className = 'anchorLink';
		}
		i++;
	}
}

function loadXMLDoc(dname)
{
	jQuery.each(jQuery.browser, function(i, val){
		if(i=="msie" && val==true)
		{
			xmlActiveXObj = new ActiveXObject("Microsoft.XMLDOM");
			xmlActiveXObj.async = false;
	  		xmlActiveXObj.load(dname);
			xmlDoc = xmlActiveXObj.documentElement;
			return true;
		}
		else if(i=="msie" && val==false)
		{
			var xmlhttp = new window.XMLHttpRequest();
			xmlhttp.open("GET", dname, false);
			xmlhttp.send(null);
			xmlDoc = xmlhttp.responseXML.documentElement;
			return true;
		}
	});
}

function tree(node)
{
    for(var i = 0; i < node.length; i++)
	{
		if(node.item(i).nodeType==1)
		{
            if(node.item(i).childNodes.length==0)
            {
                strTree += '<li class="file"><span>';
                strTree += '<a class="anchorLink" href="#learnTopAnchor" id="' + node.item(i).getAttribute('link') + '" onclick="javascript:showLearn(\'';
				strTree += node.item(i).getAttribute('link');
				strTree += '\')">';
				strTree += node.item(i).getAttribute('title');
				strTree += '</a>';
				strTree += '</span></li>';
  			}
  			else
  			{
                strTree += '<li class="folder" id="li_' + node.item(i).getAttribute('link') + '">';
				strTree += '<span class="closed"';
				strTree += ' onclick="javascript:open_close(\'' + node.item(i).getAttribute('link') + '\',this)"';
				strTree += '>';
			 	strTree += node.item(i).getAttribute('title');
				strTree += '</span></li>';
                strTree += '<ul id="' + node.item(i).getAttribute('link') + '" style="display:none;">';
                tree(node.item(i).childNodes);
  				strTree += '</ul>';
  			}
		}
	}
}

function open_close(id, obj)
{
	if(obj.className=='closed')
	    obj.className='open';
	else
	    obj.className='closed';

	jQuery('#' + id).toggle();
}
