/*

Author: Johnc
Proj No: 50181
Proj Name: Flash Construct 2.0
Usage:	Contains generic helper functions.

Date: 10/02/06

*/
// Diagnostic function that alerts all properties of the object its passed.
// Functions and Object's are not fully listed, but instead have " = A Function"
// of " = A Object" added.
function _alertAll()
{
	//alert()

	var names = "Object properties:\n"

	var functionsArray = new Array();
	var objectsArray = new Array();
	var variablesArray = new Array();

	for (var propCount in this)
	{
		//alert(typeof propCount)
		//if ((propCount != "setTheState") && (propCount != "alertAll") && (propCount != "refreshState") && (propCount != "makeTestMeTrackArray") && (propCount != "makeAssesTrackArray"))
		evalProp = eval("this." + propCount)
		propType = typeof evalProp
		//alert(propType)
		switch (propType)
		{
			case "function":
				functionsArray[functionsArray.length] =  propCount + " = A Function\n"
			break;
			case "object":
				objectsArray[objectsArray.length] =  propCount + " = A Object\n"
			break;
			default:
				variablesArray[variablesArray.length] =  propCount + " = " + evalProp + "\n"
			break;
		}
	}


	names += "\nFUNCTIONS\n\n"
	var count = 0;
	functionsArray.sort()
	while (count < functionsArray.length)
	{
		names += functionsArray[count]
		count++
	}

	names += "\nOBJECTS\n\n"
	var count = 0;
	objectsArray.sort()
	while (count < objectsArray.length)
	{
		names += objectsArray[count]
		count++
	}

	names += "\nVARIABLES\n\n"
	var count = 0;
	variablesArray.sort()
	while (count < variablesArray.length)
	{
		names += variablesArray[count]
		count++
	}


	alert(names)
}

// Used for general randomisation goodness. E.g. assessment navigation, question option order etc.
function _randomSort(a, b)
{
	return Math.random() - 0.5;
}

// Alerts what the code has created from the course structure, potentially useful for debugging.
function _alertStructure()
{
	//alert("alertStructure()")
	// If this isn't the course object, find the course object then call its version of this method.
	if (this.parentObj)
	{
		// Recurse up the chain until the parent object is false, then
		// you know you are at the course object.
		var currObj = this;
		while (currObj.parentObj)
		{
			currObj = this.parentObj();
		}
		currObj.alertStructure()
	}
	var alertText = "Structure\n=========\n\n";
	alertText += this.returnStructureInfo()
	alert(alertText)
}


// Used by the above function, producing the stucture info as a string. Could also be piped to text
// output if needed.
function _returnStructureInfo()
{
	var returnText = "";
	var count = 0;
	while (count < this.objDepth)
	{
		returnText += "  "
		count++
	}
	if (this.childObjsAssociative)
	{
		returnText += "+ " + this.fullIDString + " - " + this.objTitle + " (d:" + this.objDepth + ", t: " + this.objType + ", s: " + this.trackingState + ")\n";
		//alert("Found kids for " + this.objTitle)
		for (currObj in this.childObjsAssociative)
		{
			returnText += this.childObjsAssociative[currObj].returnStructureInfo()
		}
	}
	else
	{
		returnText += "- " + this.fullIDString + " - " + this.objTitle + " (d:" + this.objDepth + ", t: " + this.objType + ", s: " + this.trackingState + ")\n";
	}

	return returnText;
}


// Removes the element at the specified index and returns the pruned array.
function _removeArrayElement(whatArray, whatIndex)
{
	this.debug("removeArrayElement(whatArray: " + whatArray.join(",") + ", whatIndex: " + whatIndex + ")", 1, this, true)
	var returnArray = new Array()
	for (var currIndex in whatArray)
	{
		if (currIndex != whatIndex)
			returnArray[returnArray.length] = whatArray[currIndex]
	}
	return returnArray;
}

// Joins one array to another regardless of their type (indexed or associative)
function _joinArrays(array1, array2)
{
	var returnArray = new Array()

	for (var currIndex in array1)
		returnArray[returnArray.length] = array1[currIndex]

	for (var currIndex in array2)
		returnArray[returnArray.length] = array2[currIndex]

	return returnArray;
}

// Produces a random number within the given range.
function _getRandom(minNum, maxNum)
{
	return minNum + Math.round((Math.random() * maxNum))
}

function _debug(msg, priority, whatObj, showCallee)
{
	if (DEBUG)
	{	
		DEBUG.lert(msg, priority, whatObj, showCallee);
	}
}

function _unCamelCase(whatText)
{
	var lowerCaseString = whatText.toLowerCase()
	var expandedWordsArray = new Array()
	var expandedWordEnd = 0;
	var count = 0;
	// Extract the words into an array.
	while (count < whatText.length)
	{
		//this.debug("whatText.charAt(count): " + whatText.charAt(count) + ", lowerCaseString.charAt(count): " + lowerCaseString.charAt(count),3)
		if (whatText.charAt(count) != lowerCaseString.charAt(count))
		{
			//this.debug("SLICING: " + whatText.slice(expandedWordEnd, count),3)
			expandedWordsArray[expandedWordsArray.length] = whatText.slice(expandedWordEnd, count)
			expandedWordEnd = count;
		}
		count++;
	}
	// Add any remaining text as the last word never finds a difference to slice.
	expandedWordsArray[expandedWordsArray.length] = whatText.slice(expandedWordEnd, count)

	//this.debug("expandedWordsArray: ",3,expandedWordsArray)
	count = 0;
	while (count < expandedWordsArray.length)
	{
		if (count == 0)
			expandedWordsArray[count] = expandedWordsArray[count].charAt(0).toUpperCase() + expandedWordsArray[count].slice(1,expandedWordsArray[count].length)
		else
			expandedWordsArray[count] = expandedWordsArray[count].charAt(0).toLowerCase() + expandedWordsArray[count].slice(1,expandedWordsArray[count].length)
		count++
	}

	var deCameledString = expandedWordsArray.join(" ");
	return deCameledString;
}
