/*forssfolio functions*/

/* inits the site */
function init() {
/*	setBrowser();
	addEventListeners();
	goDefault();
	window.defaultStatus='Forssfolio';*/
}

/*adds specific actions to elements*/
function addEventListeners()
{

	/*add back to a class="historyBtn", and array for disabling/enabling-colors*/
	var historyBtnsTmp = document.getElementsByTagName("a");
	historyBtns = new Array();
	for(i = 0;i <historyBtnsTmp.length;i++) {
		if(historyBtnsTmp[i].className != null && historyBtnsTmp[i].className.indexOf('historyBtn') != -1)
		{
			historyBtns[historyBtns.length] = historyBtnsTmp[i];
			addEventListenerForObj(historyBtnsTmp[i],'click',hist,true);
		}
	}

	/*adds back on click for project imgs */
	var imgs = document.getElementsByTagName("img");
	for(i = 0;i <imgs.length;i++) {
		var theEl = imgs[i];
		if (theEl.className != null && theEl.className.search(/imgScreenshot/) != -1)
		{
			addEventListenerForObj(theEl,'click',hist,true);
		}
	}

	/*adds status message for links*/
	var links = document.getElementsByTagName("a");
	for(i = 0;i <links.length;i++) {
		var theEl = links[i];
		addEventListenerForObj(theEl,'mouseover',linkMsg,true);
		addEventListenerForObj(theEl,'mouseout',linkMsg,true);
	}

}

/*checks the href for pointers on where to go */
function goDefault() {
	if(document.location.href.search(/\?/) != -1 && document.location.href.search(/#/) == -1) /* ? is escaped because of ie5mac*/
	{
		loc = document.location.href;
		locString = loc.slice(loc.indexOf("?")+1,loc.length);
		eval("go("+locString+")");
	}
	else
	{
		go(0);
	}
}

function goFromSiteMap(areaNr,pageNr,subLevel3,material){
	setDisplay('divSitemap','hide');
	/*if arguments are not sent, set them to 0 */
	if(goFromSiteMap.arguments.length <= 3)
	{
		material = 0;
		if(goFromSiteMap.arguments.length <= 2)
		{
			subLevel3 = 0;
			if (goFromSiteMap.arguments.length <= 1)
				pageNr = 0;
		}
	}
	go(areaNr,pageNr,subLevel3,material);
}

function switchMainTab(obj,area) {
	if(obj.className.search(/active/) == -1)
	{
/*		spans = document.getElementById('mainTabs').getElementsByTagName('span');
		for(i = 0;i <spans.length;i++)
		{
			spans[i].className = '';
		}
		obj.nextSibling.className ='inlineShadow';*/
		go(area);
	}
}

function showAll() {
	/* show all project-divs */
	for(area = 0;area<subNavName.length;area++) {
		for(i = 0;i <subNavs[area].length;i++)
		{
			setDisplay('tabArea'+subNavName[area]);
			setDisplay(subNavName[area]+subNavs[area][i]);
			
		}
	}

		/*show all material-divs */
/*		materialDiv = document.getElementById(subNavName[areaNr]+subNavs[areaNr][pageNr]+'Material');
		for(i = 0; i<materialDiv.childNodes.length;i++){
			if(materialDiv.childNodes[i].className == 'divMaterial')
				setDisplay(materialDiv.childNodes[i].id);
		}*/

	/*hide all subNav-divs and subNavFull for the area and display the active one */
/*
	for(i = 0;i <subNavs[areaNr].length;i++) {
		setDisplay(subNavName[areaNr]+subNavs[areaNr][i],'hide');
		if(document.getElementById(subNavName[areaNr]+subNavs[areaNr][i]+'Full'))
			setDisplay(subNavName[areaNr]+subNavs[areaNr][i]+'Full','hide');
	}
	*/
}

/*main tab switcher*/
function switchTab(areaNr) {
	setDisplay('tabAreaAbout','hide');
	setDisplay('tabAreaFeaturedProjects','hide');
	setDisplay('tabAreaCV','hide');
	setDisplay('tabAreaMisc','hide');
	setDisplay('tabArea'+subNavName[areaNr]);
	
	/* set the className of nav-tabs-list in order to hilight the active tab */
	document.getElementById('nav-tabs-list').className = 'nav-'+subNavName[areaNr];

/*	alert('tab-'+subNavName[areaNr]);
	alert(document.getElementById('nav-tabs-list').className);
	document.getElementById('tabAbout').className = 'mainTab';
	document.getElementById('tabFeaturedProjects').className = 'mainTab';
	document.getElementById('tabCV').className = 'mainTab';
	document.getElementById('tabMisc').className = 'mainTab';
*/
/*	spans = document.getElementById('mainTabs').getElementsByTagName('span');
	tabs = document.getElementById('nav-tabs').getElementsByTagName('li');
	for(i = 0;i <spans.length;i++)
	{
		spans[i].className = '';
		tabs[i].className = 'mainTab';
	}
	document.getElementById('tab'+subNavName[areaNr]).className = 'mainTab active';
	document.getElementById('tab'+subNavName[areaNr]).nextSibling.className ='inlineShadow';*/
}

About = new Array('Past','Present','Future');
FeaturedProjects = new Array('Indyfund','City5','Guide5','Closetothemachine','Kloss','Nerdlife');
CV = new Array('History','Awards','Skills','Referees');
Misc = new Array('Publications','Other','Links');
subNavs = new Array(About,FeaturedProjects,CV,Misc);
subNavName = new Array('About','FeaturedProjects','CV','Misc');
/* main area 0-N, subarea 0-N, long description bool, material 1-N */
subNavPos = new Array(0,0,0,0);

browseHistory = new Array();
currentHistoryPos = -4;
/*reset the main area var*/
mainArea = 0;

function addBrowseHistory(first,second,third,fourth,dir) {
	/*check if back or fwd or new page*/
	if(dir && dir == -1)
	{
		currentHistoryPos = currentHistoryPos - 4;
		toggleEnabled('fwdBtn',1);
		if(currentHistoryPos == 0)
			toggleEnabled('backBtn',0);
	}
	else if (dir && dir == 1)
	{
		currentHistoryPos = currentHistoryPos + 4;
		toggleEnabled('backBtn',1);
		if(currentHistoryPos+4 == browseHistory.length)
			toggleEnabled('fwdBtn',0);
	}
	else
	{
		/*check if forward-history needs to be replaced*/
		if((currentHistoryPos+4)<browseHistory.length) {
			browseHistory = browseHistory.slice(0,currentHistoryPos+4);
		}
		browseHistory[browseHistory.length] = first;
		browseHistory[browseHistory.length] = second;
		browseHistory[browseHistory.length] = third;
		browseHistory[browseHistory.length] = fourth;
		currentHistoryPos = currentHistoryPos + 4;
		/*at init the value of currentHistoryPos is 0, so back button should be disabled*/
		if(currentHistoryPos != 0)
			toggleEnabled('backBtn',1);
		else
			toggleEnabled('backBtn',0);
		toggleEnabled('fwdBtn',0);
	}
}

function toggleEnabled(obj,state) {
	if (obj == 'fwdBtn')
	{
		if(state == 1)
			document.getElementById(obj).className = 'btnEnabled';
		else
			document.getElementById(obj).className = 'btnDisabled';
	}
	else
	{
		if(state == 1)
			stateClass = 'historyBtn btnEnabled';
		else
			stateClass = 'HistoryBtn btnDisabled';	
	
		for(i = 0;i <historyBtns.length;i++) {
			historyBtns[i].className = stateClass;
		}

	}
}

/*changed name of history function to hist because of KHTML problem */
function hist(dir) {
	/*if in beginning of history, dont do anything*/
	if (!(dir == -1 || dir == 1))
		dir = -1;
	/*check when to do nothing on click*/
	if (!((currentHistoryPos == 0 && dir == -1) || (currentHistoryPos == browseHistory.length-4 && dir ==1)))
		go(browseHistory[currentHistoryPos+dir*4],browseHistory[currentHistoryPos+dir*4+1],browseHistory[currentHistoryPos+dir*4+2],browseHistory[currentHistoryPos+dir*4+3],dir);
	/*if dir is back then send that to subNavSwitch*/
}

function go(areaNr,pageNr,subLevel3,material) {
	/*if arguments are not sent, set them to 0 */
	if(go.arguments.length <= 3)
	{
		material = 0;
		if(go.arguments.length <= 2)
		{
			subLevel3 = 0;
			if (go.arguments.length <= 1)
				pageNr = 0;
		}
	}
	/*set variable to main area*/
	mainArea = areaNr;
	/*switch to the active area*/
	switchTab(areaNr);
	/*check if at last subNave-page*/
	if (pageNr > subNavs[areaNr].length-1)
		pageNr = 0;
	else if (pageNr<0)
		pageNr = subNavs[areaNr].length-1;
	subNavPos[areaNr] = pageNr;

	if(areaNr == 1) {
		/* hide all project-divs and display the current one */
		for(i = 0;i <subNavs[areaNr].length;i++) {
			setDisplay('div'+subNavName[areaNr]+subNavs[areaNr][i],'hide');
		}
		setDisplay('div'+subNavName[areaNr]+subNavs[areaNr][pageNr]);

		/*hide all material-divs */
		materialDiv = document.getElementById(subNavName[areaNr]+subNavs[areaNr][pageNr]+'Material');
		for(i = 0; i<materialDiv.childNodes.length;i++){
			if(materialDiv.childNodes[i].className == 'divMaterial')
				setDisplay(materialDiv.childNodes[i].id,'hide');
		}

	}
	/*hide all subNav-divs and subNavFull for the area and display the active one */
	for(i = 0;i <subNavs[areaNr].length;i++) {
		setDisplay(subNavName[areaNr]+subNavs[areaNr][i],'hide');
		if(document.getElementById(subNavName[areaNr]+subNavs[areaNr][i]+'Full'))
			setDisplay(subNavName[areaNr]+subNavs[areaNr][i]+'Full','hide');
	}
	/* if area is FeaturedProjects then show full project description,
	   else if material is set show the active material-div */
	if(subLevel3 == 1)
		setDisplay(subNavName[areaNr]+subNavs[areaNr][pageNr]+'Full');
	else if (material > 0)
		setDisplay(subNavName[areaNr]+subNavs[areaNr][pageNr]+'Material'+material);
	/* if Full desc not on and material not on then show subNav */
	else
		setDisplay(subNavName[areaNr]+subNavs[areaNr][pageNr]);
	/*set subNavigation link colors*/
	theLinks = document.getElementById(subNavName[areaNr]+'Nav');
	for(i = 0;i <theLinks.childNodes.length;i++) {
		if (theLinks.childNodes[i].className == "noLink")
			theLinks.childNodes[i].className = "";
		if (theLinks.childNodes[i].id)
		{
			if (theLinks.childNodes[i].id.substring(subNavName[areaNr].length,subNavName[areaNr].length+1) == pageNr)
				theLinks.childNodes[i].className = "noLink";
		}
	}
	/*due to css-bug in ie4+win */
	if(is_win && is_ie4up) {
		document.getElementById('footInner').style.position = 'static';
		document.getElementById('footInner').style.position = 'relative';
	}

	/*add to browse history*/
	addBrowseHistory(areaNr,pageNr,subLevel3,material,go.arguments[4]);
}
