/*
Argonauts
oBu
navparams_processor.js
generates arrays from the data in navigation_params.js
created 06-03-2003
last edited 20-06-2003 oBu

added option for full path links
*/

// part two: logic

var error="";		// contains error string
var depth=0;		// contains navigation depth. must be >0!
var primElements=0;	// contains number of primary navigation elements
var mpt=new Array();	// menupoint
var mnpt=new Array();	// metanavigationpoint
var fpt=new Array();	// footernavigationpoint


//*********************************************************************************
// add full path prefix here, if necessary, otherwise use empty assignment
var fullpath="";
// var fullpath="http://www.dazwhite.co.uk/"
//*********************************************************************************

mncounter=0;
fncounter=0;


var dA=data.split(",");
// strip whitespace:
for (i=0; i<dA.length; i++) {
	// check first and last character if space:
	if (dA[i].indexOf(" ")==0){
		dA[i]=dA[i].slice(1);
	}
	if (dA[i].indexOf(" ")==dA[i].length-1) {
		start=0;
		end=dA[i].length-1;
		dA[i]=dA[i].slice(start, end);
	}
}

// the last element is an empty string. ignore.
checkValue=(dA.length-1)/6;
if (checkValue==parseInt(checkValue)) {
} else {
	error+="checksum failure: corrupt file 'navigation_params.js'\n";
}
// check all entrys, determine number of primary elements, determine nav depth
for (i=0; i<dA.length-1; i++) {
	// check for empty elements
	if (dA[i]=="" || dA[i]==" ") {
		error+="element "+i+" is empty\n";
	}
	// check max navigation depth, and legality of depth indicators:
	if ((i==0) || (i/6 == parseInt(i/6))) {
		el=dA[i];
		if (el==parseInt(el)) {
			el=parseInt(el);
			if (i>5) {
				lastEl=parseInt(dA[i-5]);
			}
			if (el==1 && dA[i+5]=="yes") {
				primElements++;
			}
			if (el==(depth+1)) {
				depth=el;
			} else if (el>depth+2 || (i>5 && el>lastEl+1)) {
				error+="illegal depth indicator in element "+(i/6)+"\n";
			}
		} else {
			error+="depth indicator in element "+(i/6)+" is not a number\n";
		}
	}
	// check for urls
	if ((i==1) || ((i-1)/6 == parseInt((i-1)/6))) {
		value=dA[i].split(".");
		if (value.length<2) {
			// not a url, check if function call:
			value=dA[i].split(":");
			if (value.length<2) {
				error+="not a URL in element "+(i-1)/6+"\n";
			} else {
				if (value[0]!="javascript") {
					alert (value[0]);
					error+="not a URL in element "+(i-1)/6+"\n";
				}
			}
		}
	}
	// ckeck for is-in-navigation - hint
	if ((i==5) || ((i-5)/6 == parseInt((i-5)/6))) {
		if (dA[i]!="yes" && dA[i]!="no" && dA[i]!="meta" && dA[i]!="footer") {
			error+="invalid 'is - in - navigation' - entry in element "+(i-5)/6+"\n";
		}
	}
} 
if (depth==0) {
	error+="navigation depth=0. no navigation generatet!\n";
}
if (error!="") {
	alert (error);
} else {
	// proceed.
	nL=new Array;
	for (i=1; i<=depth; i++) {
		nL[i]=new Array();
	}
	pec=0;
	for (i=0; i<dA.length-1; i+=6) {
		if (dA[i]==0 && dA[i+5]=="meta") {
			mncounter++;
			mnpt[mncounter]=new Array();
			mnpt[mncounter]['lnk']=fullpath+dA[i+1];
			mnpt[mncounter]['nme']=dA[i+2];
			mnpt[mncounter]['img']=dA[i+3];
			mnpt[mncounter]['tgt']=dA[i+4];
			mnpt[mncounter]['iin']=dA[i+5];
		} else if (dA[i]==0 && dA[i+5]=="footer") {
			fncounter++;
			fpt[fncounter]=new Array();
			fpt[fncounter]['lnk']=fullpath+dA[i+1];
			fpt[fncounter]['nme']=dA[i+2];
			fpt[fncounter]['img']=dA[i+3];
			fpt[fncounter]['tgt']=dA[i+4];
			fpt[fncounter]['iin']=dA[i+5];
		}
		if (dA[i]==1 && dA[i+5]=="yes") {
			pec++;
			for (j=2; j<=depth; j++) {
				nL[j]=0;
			}
			mpt[pec]=new Array();
			mpt[pec]['lnk']=fullpath+dA[i+1];
			mpt[pec]['nme']=dA[i+2];
			mpt[pec]['img']=dA[i+3];
			mpt[pec]['tgt']=dA[i+4];
			mpt[pec]['iin']=dA[i+5];
		}
		if (dA[i]==2){
			nL[3]=0;
			for (j=(3); j<=depth; j++) {
				nL[j]=0;
			}
			nL[2]++;
			mpt[pec][nL[2]]=new Array();
			mpt[pec][nL[2]]['lnk']=fullpath+dA[i+1];
			mpt[pec][nL[2]]['nme']=dA[i+2];
			mpt[pec][nL[2]]['img']=dA[i+3];
			mpt[pec][nL[2]]['tgt']=dA[i+4];
			mpt[pec][nL[2]]['iin']=dA[i+5];
		}
		if (dA[i]==3){
			for (j=(4); j<=depth; j++) {
				nL[j]=0;
			}
			nL[3]++;
			mpt[pec][nL[2]][nL[3]]=new Array();
			mpt[pec][nL[2]][nL[3]]['lnk']=fullpath+dA[i+1];
			mpt[pec][nL[2]][nL[3]]['nme']=dA[i+2];
			mpt[pec][nL[2]][nL[3]]['img']=dA[i+3];
			mpt[pec][nL[2]][nL[3]]['tgt']=dA[i+4];
			mpt[pec][nL[2]][nL[3]]['iin']=dA[i+5];
		}
		if (dA[i]==4){
			for (j=(5); j<=depth; j++) {
				nL[j]=0;
			}
			nL[4]++;
			mpt[pec][nL[2]][nL[3]][nL[4]]=new Array();
			mpt[pec][nL[2]][nL[3]][nL[4]]['lnk']=fullpath+dA[i+1];
			mpt[pec][nL[2]][nL[3]][nL[4]]['nme']=dA[i+2];
			mpt[pec][nL[2]][nL[3]][nL[4]]['img']=dA[i+3];
			mpt[pec][nL[2]][nL[3]][nL[4]]['tgt']=dA[i+4];
			mpt[pec][nL[2]][nL[3]][nL[4]]['iin']=dA[i+5];
		}
		if (dA[i]==5){
			for (j=(dA[i]+1); j<=depth; j++) {
				nL[j]=0;
			}
			nL[5]++;
			mpt[primElementCounter][nL[2]][nL[3]][nL[4]][nL[5]]=new Array();
			mpt[pec][nL[2]][nL[3]][nL[4]][nL[5]]['lnk']=fullpath+dA[i+1];
			mpt[pec][nL[2]][nL[3]][nL[4]][nL[5]]['nme']=dA[i+2];
			mpt[pec][nL[2]][nL[3]][nL[4]][nL[5]]['img']=dA[i+3];
			mpt[pec][nL[2]][nL[3]][nL[4]][nL[5]]['tgt']=dA[i+4];
			mpt[pec][nL[2]][nL[3]][nL[4]][nL[5]]['iin']=dA[i+5];
		}
	}
}
// checksum:
if (primElements!=pec) {
	error+="checksum error: check primary navigation elements!\n";
}
