// how to generate next units information from the above!?!
// should generate it on the fly based on current unit. Best use of resources?
// Actually - this is not where it needs to be done - it needs to be done on 
// front/navnext pages - the info is only relevant for the module structure diagram

// just deal with current unit
var unitCode = top.currUnit
var nextUnitGroupTop = false
var found = false
var nextUnitGroupIndex = 0
var currentUnitGroupType = "sequence"
var currentUnitGroupNumToSelect = 0
top.refLibShow = false

// let's reset the array
var recommendedNext = new Array()

// let's see what files are optional right from the get go
if (top.newModule) {
	for (i=0; i<top.unitGroupTop.length; i++) {
		if ((top.unitGroupTop[i].type=="selection") && (top.unitGroupTop[i].numToSelect==0)) {
			// all units in this unit group are optional
			var optionalUnitGroupCode = top.unitGroupTop[i].code
			for (j=0; j<top.unitGroupLower[optionalUnitGroupCode].length; j++) {
				if (top.unitGroupLower[optionalUnitGroupCode][j].type=="unit") {
					top.unitsArray[optionalUnitGroupCode].optional = true
				} else {
					//need to drill down further
					setUnitsOptional(optionalUnitGroupCode)
				}
			}
		}
	}
	for (i=0; i<top.unitGroupLowerIndex.length; i++) {
		var tmpUnitGroupCode = top.unitGroupLowerIndex[i]
		if (top.unitGroupLower[tmpUnitGroupCode]) {
			for (n=0; n<top.unitGroupLower[tmpUnitGroupCode].length; n++) {
				if ((top.unitGroupLower[tmpUnitGroupCode][n].type=="selection") && (top.unitGroupLower[tmpUnitGroupCode][n].numToSelect==0)) {
					// all units in this unit group are optional
					var optionalUnitGroupCode = top.unitGroupLower[tmpUnitGroupCode][n].code
					for (j=0; j<top.unitGroupLower[optionalUnitGroupCode].length; j++) {
						if (top.unitGroupLower[optionalUnitGroupCode][j].type=="unit") {
							top.unitsArray[optionalUnitGroupCode].optional = true
						} else {
							//need to drill down further
							setUnitsOptional(optionalUnitGroupCode)
						}
					}
				}
			}
		}	
	}
}

// this function goes down a tree to set all units inside it as optional
// shouldn't get used that much
function setUnitsOptional(optUnitGroupCode) {
	for (k=0; k<top.unitGroupLower[optUnitGroupCode].length; k++) {
		if (top.unitGroupLower[optUnitGroupCode][k].type=="unit") {
			var tempOptUnitCode = top.unitGroupLower[optUnitGroupCode][k].code
			top.unitsArray[tempOptUnitCode].optional = true
		} 	
	}
}

// should we calculate the other optional units (generated as a result of visited units 
// within selections) here or integrated into the finding the unit process? 
// Let's just do it here for now!!



// we've just arrived at this module - no units have been visited, so the first unit must be the recommended one
if (unitCode=="null") {
	recommendedNext[recommendedNext.length]= top.unitsArrayIndex[0]
	found = true
}
//if this is the last unit of the module then there are no more recommendations!
if (unitCode==top.unitGroupTop[top.unitGroupTop.length-1].code) {
	found = true
	nextUnitGroupTop = false
}

// where is this unit?
// use the fact that the unit name is contained within the code of
// the unitGroupTop that it is in. However - this is not foolproof 
// (in the case where one unit name is a substring of another) so 
// we need then immediately to go on to identify the unit within the 
// heirarchy or move on to the next unitGroupTop
if (!found) {
	for (j=0; j<top.unitGroupTop.length; j++) {
		if (!found) {
			var unitGroupCode = top.unitGroupTop[j].code
			var stringPos = unitGroupCode.indexOf(unitCode)
			nextUnitGroupIndex = j+1
			
			//take this out after testing...
			nextUnitGroupTop = true
			
			if(stringPos > -1) {
				// the string has been found so let's dig deeper
				top.unitGroupTop[j].visited=true
				// is the unitGroupTop the unit itself?
				if (unitGroupCode == unitCode) {
					found = true
					// this means that the next units are going to be in the next 
					// unitGroupTop
					nextUnitGroupTop = true
				} else if (top.unitGroupTop[j].type=="selection") {
					//document.write('got to selection')
					currentUnitGroupType="selection"
					currentUnitGroupNumToSelect=top.unitGroupTop[j].numToSelect
					var tally = 0
					for (z=0; z<top.unitGroupLower[unitGroupCode].length; z++) {
						if (top.unitGroupLower[unitGroupCode][z].visited) {
							tally++
						}
					}
					if (tally<currentUnitGroupNumToSelect) {
						for (z=0; z<top.unitGroupLower[unitGroupCode].length; z++) {
							var tempUnitGroupCode = top.unitGroupLower[unitGroupCode][z].code
							if (top.unitGroupLower[unitGroupCode][z].type=="unit") {
								recommendedNext[recommendedNext.length] = tempUnitGroupCode
							} else{
								//drillDown(tempUnitGroupCode,top.unitGroupLower[unitGroupCode][z].type)
							}
						}
					} else {
						for (z=0; z<top.unitGroupLower[unitGroupCode].length; z++) {
							var tempUnitGroupCode = top.unitGroupLower[unitGroupCode][z].code
							if (top.unitGroupLower[unitGroupCode][z].type=="unit") {
								top.unitGroupLower[unitGroupCode][z].optional=true
								top.unitsArray[tempUnitGroupCode].optional=true
							} else{
								setUnitsOptional(tempUnitGroupCode)
							}
						}		
					}
				} else if (top.unitGroupTop[j].type=="sequence") {
					// ideally this wouldn't exist as it would mean there is a sequence
					// in a sequence
					
				}
				
				// we now need to look at how this top unit group is made up and make some
				// decisions about it
				while (!found) {
					// now got to go down the heirarchy until we find the relevant unit
					// but at the same time we need to get some info about this top unit group
					for (k=0; k< top.unitGroupLower[unitGroupCode].length; k++) {
						var thisGroupCode = top.unitGroupLower[unitGroupCode][k].code
						stringPos = thisGroupCode.indexOf(unitCode)
						if (stringPos > -1) {
						// found it - is it unit or unitGroup?
							top.unitGroupLower[unitGroupCode][k].visited=true
							if (thisGroupCode == unitCode) {
								found = true
								// where to go from here?
								
							} else {
								// not unit so we need to go down a level
								currentUnitGroupType = top.unitGroupLower[unitGroupCode][k].type
								currentUnitGroupNumToSelect = top.unitGroupLower[unitGroupCode][k].numToSelect
								unitGroupCode = thisGroupCode
							}
							if (found) {
								if (currentUnitGroupType=="selection") {
									var tally = 0
									for (n=0; n<top.unitGroupLower[unitGroupCode].length; n++) {
										if (top.unitGroupLower[unitGroupCode][n].visited) {
											tally++
										}																																						
									}
									if (tally<currentUnitGroupNumToSelect) {			
										for (n=0; n<top.unitGroupLower[unitGroupCode].length; n++) {
											if (n!=k) {
												if (top.unitGroupLower[unitGroupCode][n].type=="unit") {
													recommendedNext[recommendedNext.length] = top.unitGroupLower[unitGroupCode][n].code
												} else {
													drillDown(top.unitGroupLower[unitGroupCode][n].code,top.unitGroupLower[unitGroupCode][n].type)
												}
											}
										}
									} else {
										// other units here are now optional
										for (n=0; n<top.unitGroupLower[unitGroupCode].length; n++) {
											if (top.unitGroupLower[unitGroupCode][n].type=="unit") {
												top.unitGroupLower[unitGroupCode][n].optional=true
												//document.write('got here')
												var tempUnitGroupCode = top.unitGroupLower[unitGroupCode][n].code
												top.unitsArray[tempUnitGroupCode].optional=true
											} else {
												setUnitsOptional(top.unitGroupLower[unitGroupCode][n].code)
											}
										}
									}
								}
								if (currentUnitGroupType=="sequence") {
									if (k<top.unitGroupLower[unitGroupCode].length-1) {
										nextUnitGroupTop=false
										if (top.unitGroupLower[unitGroupCode][k+1].type=="unit") {
											recommendedNext[recommendedNext.length] = top.unitGroupLower[unitGroupCode][k+1].code
										} else {
											drillDown(top.unitGroupLower[unitGroupCode][n].code,top.unitGroupLower[unitGroupCode][n].type)
										}
									}
								} 
							}
						}
					}
				}
			}
		}
	}
}
	
// in some circumstances we will want to go on to the next top unit group to find some next units
// this may iterate if the next top unit group is optional
var thisUnitGroupCode = top.unitGroupTop[nextUnitGroupIndex].code
while (nextUnitGroupTop) {
	// now what are the first available units in this top unit group?
		if (top.unitGroupTop[nextUnitGroupIndex].type == "unit") {
			recommendedNext[recommendedNext.length]= thisUnitGroupCode
		} else if (top.unitGroupTop[nextUnitGroupIndex].type == "selection") {
			for (i=0; i<top.unitGroupLower[thisUnitGroupCode].length; i++) {
				if (top.unitGroupLower[thisUnitGroupCode][i].type == "unit") {					
					recommendedNext[recommendedNext.length]= top.unitGroupLower[thisUnitGroupCode][i].code
				} else {
					// need to go down the hierarchy
					drillDown(top.unitGroupLower[thisUnitGroupCode][i].code,top.unitGroupLower[thisUnitGroupCode][i].type)
				}		
			}
		} else if (top.unitGroupTop[nextUnitGroupIndex].type == "sequence") {
			if (top.unitGroupLower[thisUnitGroupCode][0].type == "unit") {
				recommendedNext[recommendedNext.length]= top.unitGroupLower[thisUnitGroupCode][0].code
			} else {
				// need to go down the hierarchy
				drillDown(top.unitGroupLower[thisUnitGroupCode][0].code,top.unitGroupLower[thisUnitGroupCode][0].type)
			}
		}
		
		// is it possible to also skip over to the next group?
		if ((top.unitGroupTop[nextUnitGroupIndex].type=="selection") && (top.unitGroupTop[nextUnitGroupIndex].numToSelect==0)) {
			nextUnitGroupTop = true
			nextUnitGroupIndex++
			thisUnitGroupCode = top.unitGroupTop[nextUnitGroupIndex].code
			
		} else {
			nextUnitGroupTop = false
		}
}

// need a function to handle further drilling down into hierarchy
function drillDown(thisGroupCode,thisGroupType) {
	// i suspect this first "if" is never needed...
	if (thisGroupType=="unit") {
		recommendedNext[recommendedNext.length]=top.unitGroupLower[thisGroupCode].code
	} else if (thisGroupType=="selection") {
		//document.write('got here2')
		for (m=0; m<top.unitGroupLower[thisGroupCode].length; m++) {
			if (top.unitGroupLower[thisGroupCode][m].type=="unit") {
				recommendedNext[recommendedNext.length]=top.unitGroupLower[thisGroupCode][m].code
			} else {
				drillDown(top.unitGroupLower[thisGroupCode][m].code,top.unitGroupLower[thisGroupCode][m].type)
			}
		}
	} else if (thisGroupType=="sequence") {
		//document.write('got here3')
		if (top.unitGroupLower[thisGroupCode][0].type=="unit") {
			recommendedNext[recommendedNext.length]=top.unitGroupLower[thisGroupCode][0].code
		} else {
			drillDown(top.unitGroupLower[thisGroupCode][0].code,top.unitGroupLower[thisGroupCode][0].type)
		}
	}
}

// we worked out the optional units above - now lets color them!!
for (i=0; i<top.unitsArrayIndex.length; i++) {
	var tmpUnitCode = top.unitsArrayIndex[i]
	if (top.unitsArray[tmpUnitCode].optional) {
		setBGColor(tmpUnitCode,"#FF6600")
	}
}

// color the visited units

for (i=0; i<top.unitsArrayIndex.length; i++) {
	var unitCode = new String()
	unitCode = top.unitsArrayIndex[i]
	//document.write(top.unitsArray[unitCode].visited)
	//document.write(unitCode)
	if (top.unitsArray[unitCode].visited) {
		//document.write(" visited <br>")
		setBGColor(top.unitsArrayIndex[i], "#999999")
	}
}

// color the unit last visited
if (top.currUnit!="null") {
	setBGColor(top.currUnit, "#66FF99")
}

// now we have worked out what the next recommended units are - lets indicate this on the diagram
for (i=0; i<recommendedNext.length; i++) {
	var localUnitCode = recommendedNext[i]
	if ((!top.unitsArray[localUnitCode].visited) &&(!top.unitsArray[localUnitCode].optional)){
		setBGColor(recommendedNext[i],"#FFFFFF")
		setBorderWidth(recommendedNext[i],"5px")
	}	
}

document.write('<div class="key">recommended = white | last visited = green | visited = grey | optional = orange</div>')