/** *************************************
 * 
 * Starlabs Interface Script   
 *
 * @author:	Patrick Kanne
 * @email:	mephisto@quaint.info
 *
 * @filename: 		func.interface.inc.js	
 * @version:		20040216 - 005
 * @description:	main interface scripting for Starlabs website.
 * 
 ************************************* **/
 
/*  ------  enviroment  ------  */ 
//most of these SHOULD be located in the parent documnent as to ensure 
//page/site/interface independance

/*  ------  main eventhandlers;  ------  */

window.onresize = setFrameHeight;

/*  ------  variables  ------  */

//assign some vars
 var currTop;			//current top : chapter name placeholder
 var currSub;			//current sub : page name placeholder
 var currIdx;			//current subnav image indexNo. : see hack description in history
 var topLoad = false;	//topnav loaded check
 var subLoad = false;	//current subnav loaded check
 

//set some too
 var mpx 		= 'img/ppx/mpx.gif';	//empty pixel gif placeholder
 var albumpx 	= 'img/album/';	//chapter related images
 var hudpx 		= 'img/base/';		//hud related images
 var imgsindx	= 17;	//this sets the first subnav-image-index minus one. 
 						//see history for image-hack details
						//this one might be a gemeric which is checked at first load time?
 
//make some arrays
 var swapAr = new Array();		//placeholder for swappable images
 var navAr = new Array();		//navigation content and activated status

/*  ------  navarray   ------  */
 //NOTE: 	second array thingy is height of spacer image in subnav!!!
 //			nothing to do with the active status of that chapter
 navAr['top'] = new Array(		new topPageObj('news',100),
								new topPageObj('briefing', 95),
								new topPageObj('media', 94),
								new topPageObj('forum', 93),
								new topPageObj('about', 90)
								);
 //NOTE: 	from here second array thingy the active status of that chapter!!!
 navAr['news'] = new Array(		new subPageObj('current',1),
								new subPageObj('archive',1)
								);

 navAr['briefing'] = new Array(	new subPageObj('story', 1),			
								new subPageObj('characters',1),
								new subPageObj('setting',1)
								);
	
 navAr['media'] = new Array(	new subPageObj('movies', 0),			
								new subPageObj('screenshots', 1),
								new subPageObj('desktops', 0)
								);
	
 navAr['forum'] = new Array(	new subPageObj('listing', 1),
 								new subPageObj('dispatch', 1),			
								new subPageObj('discussion', 1),
								new subPageObj('feedback', 1),
								new subPageObj('offtopic', 1)
								);
								
 navAr['about'] = new Array(	new subPageObj('the_game', 1),			
								new subPageObj('team_bios', 1),
								new subPageObj('the_site', 1),
								new subPageObj('faq', 1)
								);
/*  ------  /navarray   ------ */ 

/*  ------  forum array  ------  */
var forumAr = new Array();
forumAr['4503'] = 'dispatch';
forumAr['4504'] = 'discussion';
forumAr['4501'] = 'feedback';
forumAr['4502'] = 'offtopic';
forumAr['4584'] = 'team';
/*  ------  /forum array  ------  */


								
/*  ------  object functions  ------  */

/** *************************************
 ** - func.interface -> subPageObj
 * @access:	private
 * @desc:	sub pages object
 * @param:	name, string, subpage name
 			active, is_active state current page (enum; 1/0)
 ************************************** */
function subPageObj(name,active)
{
	this.name = name;
	this.active = active;
}
//object subpage
/** *************************************
 ** - func.interface -> topPageObj
 * @access:	private
 * @desc:	sub pages object
 * @param:	name, string, subpage name
 			spc, int, subnav top-spacer.gif height
 ************************************** */
function topPageObj(name,spc)
{
	this.name = name;
	this.spc = spc;
}
//object topppage

/*  ------  search functoins  ------  */

/** *************************************
 ** - func.interface -> _findMyParent
 * @access:	private
 * @desc:	finds parent.name for this(name) object
 * @param: 	name; subpage name (string)
 * @ret: 	string, (name) parent name
 ************************************** */
function _findMyParent(name)
{
	arln = navAr['top'].length;
	for(var i = 0;i< arln ; ++i ) 
	{	
		cName = navAr['top'][i].name
		sArLn = navAr[cName].length;
		for(var e = 0;e< sArLn ; ++e ) 
		{
			if(navAr[cName][e].name == name)
			{
				ret = cName;
				return ret;
			}
		}
	}
	return false;
}
//function _findMyParent

/** *************************************
 ** - func.interface -> _findMe
 * @access:	private
 * @desc:	returns either the first active page in subpage array
 			or the submitted images indexnumber.
 * @param: 	pname; parent/toppage name (string)
	 		-extra possible args
				name, current image's name
				switch, extra catch, in case I come up with more uses for this funcrtion.
 * @returns:array, (name, imageIndex) first active subpage
			-or-
			int, document.images indexnumber of this image
 ************************************** */
function _findMe(pname)
{
	arln = navAr[pname].length;
	//HACK! apparently the browsers won't cooperate 
	//with dynamic images so a hack:
	//first subnav image registers as 18, count from there
	//NEW!! use (global) imgsindex set in script header for more flexabillity.
	var cN = imgsindx +1;
	for(var i = 0;i< arln ; ++i )  //>
	{
		//if we submitted a name and the marker "index"
		//we're looking for the indexnumber of this object
		if(arguments[1] == navAr[pname][i].name && arguments[2]=='index')
		{
			
			return cN;
		}
		
		//if we submitted only the parentsname then we're looking for the first active
		else if(navAr[pname][i].active == 1 && !arguments[2])
		{
			ret = new Array(navAr[pname][i].name,cN);
			return ret;
		}
		cN++;
	}
}
//function _findMe

/** *************************************
 ** - func.interface -> _findSpcSize
 * @access:	private
 * @desc:	finds first active page in subpage array
 * @param: 	name; parent/toppage name (string)
 * @returns: int, subnav spacer height
 ************************************** */
function _findSpcSize(name)
{
	arln = navAr['top'].length;
	//HACK! apparently the browsers won't cooperate 
	//with dynamic images so a hack:
	//first subnav image registers as 18, count from there
	for(var i = 0;i< arln ; ++i ) 
	{
		if(navAr['top'][i].name == name)
		{
			ret = navAr['top'][i].spc;
			return ret;
		}
	}
}
//function	 _findSpcSize


/** *************************************
 ** - func.interface -> _findMyIndex
 * @access:	private
 * @desc:	[subnav images-hack]
 			this determines what is the doc.images-indexnumber 
			of this chapter
 * @param: nme, string, image name.
 ************************************** */
function _findMyIndex(nme)
{
	/* 
	How this works;
	this reads the subpages object array an compares 'nme' with 
	the pagenames, it then adds the pages position in that particular
	toppage to the global 'imgsindx', which should contain the number 
	of images preceding the first subnav image in the DOM-tree
	*/
	//find parent
	var cParent = _findMyParent(nme);
	//find index
	var cIndex = _findMe(cParent,nme,'index');

	return cIndex;
}
//function




				
/*  ------  interface  functions  ------  */

/** *************************************
 ** - func.interface -> loadHud
 * @access:	public (onload)
 * @desc:	preloads topnavigation images, 
 			sets topLoad = true
			continues with opening top album
 * @param:  none
 ************************************** */
function LoadHud()
{
	//alert('loadHud start');
	var arln = navAr['top'].length;
	var subnme = 0;
	for(var i = 0;i< arln ; ++i ) //>
	{
		//[i][0] = name
		subnme = navAr['top'][i].name;
		//preload over/out/on
		swapAr[subnme] = new Array('off','ovr','on');
		swapAr[subnme]['off'] = new Image();
		swapAr[subnme]['off'].src =  albumpx + "topnav/" + subnme + "_off.gif";
		swapAr[subnme]['ovr'] = new Image();
		swapAr[subnme]['ovr'].src =  albumpx + "topnav/" +  subnme + "_ovr.gif";
		swapAr[subnme]['on'] = new Image();
		swapAr[subnme]['on'].src =  albumpx + "topnav/" +  subnme + "_on.gif";
	}
	topLoad = true;
	//set iframeheight
	setFrameHeight();
	
	
	//set first page
	//setTopPage(navAr['top'][0].name);
} 
//function 

/** *************************************
 ** - func.interface -> setPage
 * @access:	private
 * @desc:	set page characteristics
 			continues with opening first album-page
 * @param:	nme; chapter/albumname
 ************************************** */
function setTopPage(nme)
{
	//alert('setPage for: \n'+nme);
	//swap navimg to on
	swapNav(nme,'on');
	//register album 
	doReg('top',nme);
	//change interface
	doHud('top',nme);
	//create subnav content
	makeSubNav(nme);
	//only change page if no nullpointer
/*  ------  PAGE CHANGE  ------  */
	if(arguments[1]!='null')
	{

		//open first ACTIVE albumpage
		//find first active
		var tName = _findMe(nme);
		//HACK! 18 == first image-index, read history
		//setTimeout("loadSubPage('"+tName[0]+"',"+tName[1]+")",2);
		loadSubPage(tName[0],tName[1]);
	}
	//alert('stop setPage');
	doAnim();
	return;
}
//function

/** *************************************
 ** - func.interface -> swapTopPage
 * @access:	public
 * @desc:	unsets page characteristics, 
 			continues with opening next page
 * @param:	nme; chapter/albumname
 ************************************** */
function swapTopPage(nme)
{
	//first unset current page
		//close albumpage
/*  ------  PAGE CHANGE  ------  */
//		loadSubPage('null');
		//close subnav
		makeSubNav('unset');
		//change interface
		doHud('top','unset');
		//set tempvar for imageswap
		var temp = currTop;	
		//unregister album 
		doReg('top',0);
		//swap navimg to off
		swapNav(temp,'off');
	//open new page
		if(nme != 'null') setTopPage(nme);
	//	return;
}
//function swapTopPage

/** *************************************
 ** - func.interface -> setSubPage
 * @access:	public
 * @desc:	registers loaded subpage into interface/hud
 * @param:	nme; pagename
 			n; swap-image index-no, HACK!, read history
 ************************************** */
 
function setSubPage(nme)
{
	//alert('setSubPage for: \n-'+nme+'-');
	//find image indexnumber, see imagehack description
	var c_n = _findMyIndex(nme);
	//catch null-page exception
	if(nme != 'null' || nme)
	{
		var cParent = _findMyParent(nme);
		if(cParent != currTop)
		{
			//unset current top, use nullpointer to prevent 
			//script from loading next page
			//then set new toppage, again, use nullpointer to 
			//prevent script from loading active page
			if(!topLoad)
			{
				//first check wether topnav is loaded, if not; wait and try aagain)
				//this'd be a perfect place to add the frameset check..
				//setSubPage(nme,n)
				setTimeout("setSubPage('"+nme+"',"+c_n+")",15);
				return;
			}
			swapTopPage('null');
			setTopPage(cParent,'null');
		}
		//swap navimg to on
		swapNav(nme,'on',c_n);
		//register page
		doReg('sub',nme,c_n);
		//change interface
		doHud('sub',nme);
	}
	//reload animation
	doAnim();
	return;
}
//function setSubPage


/** *************************************
 ** - func.interface -> unSetSubPage
 * @access:	public
 * @desc:	unregisters loaded subpage 
 * @param:	nme; pagename
 			n; swap-image index-no, HACK!, read history
 ************************************** */
function unSetSubPage(nme)
{
	//catch null-page exception
	if(nme != 'null')
	{
		//set tempvars for imageswap
		cid= currIdx;
		csub = currSub;
		//unregister page
		doReg('sub',0,0);
		//swap navimg to off
		//check hack for info on currIdx
		swapNav(csub,'off',cid);
		//unset hud
		doHud('sub','');
	}
	//reload animation
	doAnim();
	return;
}
//function

/** *************************************
 ** - func.interface -> swapSubPage
 * @access:	public
 * @desc:	unsets subpage page content
 			continues with opening new p[age
 * @param:	nme; pagename
 			n; swap-image index-no, HACK!, read history
 ************************************** */
function swapSubPage(nme,n)
{
/*  ------  DEPRICATED!!!  ------  */

		//unset iframe page
/*  ------  PAGE CHANGE  ------  */		
		//loadSubPage('null',0);
		//now open new page
		loadSubPage(nme,n);
		return;
}
//function

/** *************************************
 ** - func.interface -> loadSubPage
 * @access:	private
 * @desc:	swaps actual iframe content
 * @param: 	lvl, level: either TOP or SUB
 			nme, page name
 ************************************** */
function loadSubPage(nme)
{
//	alert('loadSubPage for: \n'+nme);
	var sPage = (nme == 'null')? 'sub.html':'sub.php?page='+nme;
	frames['contentFrame'].location.replace(sPage);
	//hack to take away focus (and lines in IE) from clicked object
	window.focus();
	return;
}
//function loadSubPage

/** *************************************
 ** - func.interface -> doReg
 * @access:	private
 * @desc:	registers current page placeholders
 * @param: 	lvl, level: either TOP or SUB
 			nme, name; page name to register or NULL
			arguments[2]-hack!
 ************************************** */
function doReg(lvl,nme)
{
	//alert('doReg for: \n'+nme);
	if(lvl=='top')
	{
	//we at teh t0p.. reset sub
		currTop = nme;
		currSub = 0;
		currIdx = 0;
		return true;
	}
	else if(lvl=='sub')
	{
	//we only sub, set sub
		currSub = nme;
		currIdx = arguments[2];
		return true;
	}
	else
	{
	//something went terribly wrong..
		return false;
	}
}
//function
 
/** *************************************
 ** - func.interface -> doNav
 * @access:	public
 * @desc:	swaps imagecontent for [lvl]nav object]
 * @param: 	lvl, level: either TOP or SUB
 			nme, page/image name
			mod, modifyer, either on, off, ovr (over),
 ************************************** */
function swapNav(name,mod)
{
	//alert('swapNav for: \n'+ name + '-' + mod); 
	//HACK! this in place to persuade browsers 
	//in swapping dynamic images, last argument 
	//represents imagenumber (document.images[N])
	//see history
	nme = (arguments.length > 2)? arguments[2]:name;
	//if we're at the current chapter, do nothing
	if(name == currTop ||name == currSub)
	{
		return;
	}
	//if content = loaded
	//swap image
	else if(swapAr[name]) 
	{
		if(document.images[nme]) document.images[nme].src = eval('swapAr["'+name+'"]["'+mod+'"].src');	

//		document.images[nme].src = eval('swapAr["'+name+'"]["'+mod+'"].src');
		return;
	}
	return;
}
//function

/** *************************************
 ** - func.interface -> doHud
 * @access:	private
 * @desc:	changes interface objects according to chapter content.
 * @param: 	lvl, level: either TOP or SUB
 			nme, page name
 ************************************** */
function doHud(lvl,nme)
{

	if(lvl=='top')
	{
		//get headerimage/navarea image
		//check for 'null' modifyer
			var hedrimg = (nme == 'unset')? mpx : albumpx + "hdrimg_" + nme + ".jpg"
			var navarb = (nme == 'unset')? mpx : albumpx + "navar_" + nme + ".jpg"
		//change header image
			document.images['header_image'].src = hedrimg;
		//change navarea back
			document.images['subnavarbk'].src = navarb;
		//set page title
			document.title = nme+ " @ HL:Starlabs";
	}
	else if(lvl=='sub')
	{
		//change stuff
		nme = nme.replace('_',' ');
		//do stuff
		document.title =  currTop + " - " + nme +" @ HL:Starlabs";
		document.getElementById('content_header').innerHTML = currTop + " : " + nme;
		
	}
	return;
}
//function 

/** *************************************
 ** - func.interface -> makeSubNav
 * @access:	private
 * @desc:	compiles and prints subnavigation - content
 * @param: 	nme, parent page name
 * @needs:	navAr, 	array containing base navigation elements
 					-format: navar['nme'] => ['subnme'] => ['active(1/0)']
				
 ************************************** */
function makeSubNav(nme)
{
	//alert('makeSubNav for: \n'+nme);
	
	if(nme != 'unset')
	{	
		//initiate main subnav-html string
		//>>>>>height should be determined by chapter
		cheight = _findSpcSize(nme);
		var subnavstr = "<img src=\""+mpx+"\" width=\"133\" height=\""+cheight+"\"><br>";
		//for each element within navAr['nme']
		//create buttonstring
		arln = navAr[nme].length;
		//HACK! apparently the browsers won't cooperate 
		//with dynamic images so a hack:
		//first subnav image registers as 18, count from there
		var N = 18;
		for(var i = 0;i< arln ; ++i ) //>
		{ 
		//add image to string
			//current subnav name
			subnme = navAr[nme][i].name;
			//vspace exception
			cvspace = (subnme == 'feedback' ||subnme == 'offtopic' || subnme == 'desktops'|| subnme == 'discussion')? 0:1;			
			//buttonstring has (if not 0/mpx)
			if(navAr[nme][i].active== 1)
			{
				//HACK!!!! heh, generic placement of vspaces.. umm.. yeah..
				cvspace = (subnme == 'feedback' ||subnme == 'offtopic' || subnme == 'desktops')? 0:1;
				//navlnk = new Array("<a href=\"sub.php?page="+subnme+"\" target=\"contentFrame\" onMouseOver=\"swapNav('"+subnme+"','ovr',"+N+")\" onMouseOut=\"swapNav('"+subnme+"','off',"+N+")\">", "</a><br>");
				navlnk = new Array("<a href=\"javascript:loadSubPage('"+subnme+"');\" onMouseOver=\"swapNav('"+subnme+"','ovr',"+N+")\" onMouseOut=\"swapNav('"+subnme+"','off',"+N+")\">", "</a><br>");
				navimg = "<img src=\"" + albumpx + "subnav/" + nme + "_" + subnme + "_off.gif\" name=\""+ nme +"\" width=\"121\" height=\"24\" alt=\"subnav goto: " + nme + " >> " + subnme + "\" border=\"0\" vspace=\""+cvspace+"\" hspace=\"6\">";
				navstr = navlnk[0] + navimg + navlnk[1];
			}
			else
			{
				navstr = "<img src=\"" + mpx + "\" width=\"121\" height=\"24\" vspace=\""+cvspace+"\" hspace=\"6\">";
			}
			//add to subnavstring, which starts with mpx/N hight
			subnavstr += navstr; 
			//reset temp string
			navstr = 0;
			//HACK! increment N
			N++;
			//preload over/out/on if not done allready
			if(!swapAr[subnme])
			{
				swapAr[subnme] = new Array('off','ovr','on');
				swapAr[subnme]['off'] = new Image();
				swapAr[subnme]['off'].src =  albumpx + "subnav/" + nme + "_" + subnme + "_off.gif";
				swapAr[subnme]['ovr'] = new Image();
				swapAr[subnme]['ovr'].src =  albumpx + "subnav/" + nme + "_" + subnme + "_ovr.gif";
				swapAr[subnme]['on'] = new Image();
				swapAr[subnme]['on'].src =  albumpx + "subnav/" + nme + "_" + subnme + "_on.gif";
			}
		}
		//write to page
		document.getElementById('subnavar').innerHTML = subnavstr;
		//register subnav loaded (?)
		currLoad = true;
	}
	else if(nme == "unset")
	{	
		document.getElementById('subnavar').innerHTML = '&nbsp';
		//unregister subnav loaded (?)
		currLoad = false;
	}
	return true;
}
//function

/*  ------  HACKS!!  ------  */

/** *************************************
 ** - func.interface -> setFrameHeight
 * @access:	public
 * @desc:	Since it's quie impossible to set a height percentage 
 			for iframes when this is *not* related to the complete 
			body we're going to do a little maths here.
			Frameheight should be all that's left of the browsers 
			visible html after substraction of header and footer.
 ************************************** */
function setFrameHeight()
{
	//check on Nestcape instance since that's easyest
	var availableHeight = (navigator.appName == 'Netscape')?	 window.innerHeight:document.body.clientHeight;
	//read the topsetting of the frame container to check header-height
	var currentTop = parseInt(document.getElementById('content_page').style.top);
	//do maths, substract 10px for good measure
	var setHeight = availableHeight - currentTop - 4;
	//set frame
	//alert(setHeight);
	document.getElementById('contentFrame').style.height = setHeight+'px';
	return;
}
//function


/** *************************************
 ** - func.interface -> doAnim
 * @access:	private
 * @desc:	resets hud animation; when clicking 
 			anything within exploder the anim just 
			drops dead, this reloads the image
 * 
 ************************************** */
function doAnim()
{
	document.getElementById('hudAnim').src = 'img/base/hud_anim3.gif';
	window.focus();
	return;
}
//function


/** *************************************
 * Document history

  
 * @date:	20040216
 * @version	0.0.5
 * @04:40 	lots of changes;
 			changed base page array, 
				subpages are now registered as objects
				added object function; subPageObj
			added activated check in subnav buildup (makeSubNav)
				this reads the nwe pagearray[n].active
			added functionality to toppage swap to look for first 
				active page, in sted ofthe first page,
				this also reads the nwe pagearray[n].active
			Started to add "random" returns to see if I can break 
				from the weird load-hangup which seems to occur 
				when switching pages
 			added functionality to top-navarray, toppages are now 
				objects with; name, spc (which is the height (int) of 
				the spacer used in the subnav (hack? what hack??)
			added function to read top-nav-array to get spc, (makeSubNav)
				now adds this height when comp[iling subnav content.
			added HACK!! to (makeSubNav) to change the subnav image-vspace
				to get better image positioning (either 0 or 1) (hey, it works okay >;Þ )



 * @date:	20040115
 * @version	0.0.4
 * @01:30 	added some catches @ doHud 
 			also expanded page header to reflect main 
  
 * @date:	20040102
 * @version	0.0.3
 * @05:00 	added support for registering subpage
 			catched current level at image swap (swapNav)
				hack becomes a bit tedious!!
  
 * @date:	20040101
 * @version	0.0.2
 * @05:30 	first basic page functionality finished.
 			pagechange scripts and nav-generation functional, 
			basic page-swap script inplace
			drastic change in the array-setup (javascript != phpscript apparently :))
 * >HACK!! 	[subnav images-hack]
 			there's a hack in place to make the browser 
			react to the subnav imageswap:it seems that when an 
			image is created with an innerHTML write, it's not 
			accessable by it's [name] property. since the location 
			of the images (dom-wise) remains as with the page 
			buildup they ARE accessable through the doc.images-array 
			(document.images[n]). this I used to access those on 
			the fly generated images untill a better solution presents 
			itself.. (hey, it works okay.. :))
 
 * @date:	2003-12-28
 * @version 0.0.1
 * @08:00 	partial basic framework added based on flowchart (site_navscript 01),
 			doReg, swapNav, doHud and makeSubNav. none tested and by no means complete
			also, created room for most basic varsm started with navarray(not finished). 
 
 ************************************* **/ 

//EOF
 


