/*************************************************************************
	
Name:	array.js
Desc:	array creation & handling functions

*************************************************************************/


//creates an array using given value as the value for each item
// array(v, v, v)
function createValueArray(len, value) {
	var arr = new Array();
	for (var i=0; i<len; i++) {
		arr.push(value);
	}
	return arr;
}


//create an array with values (start ... end)
//ie: func(3,6) = array(3,4,5,6)
function createNumberedArray(start, end) {
	var inc = (end > start) ? 1 : -1;
	var arr = new Array();
	for (var i = start; i != end; i+=inc) {
		arr.push(i);
	}
	arr.push(i);
	
	return arr;
}

//return index of found item in array
function arrayIndex(value, arr) {
	for (var i in arr) {
		if (arr[i] == value) {
			return i;
		}
	}
	return -1;
}

//returns true if value is in array
function in_array(value, arr) {
	return (arrayIndex(value, arr) >= 0);
}

//return an array containing keys of passed array
function array_keys(obj) {
	var arr = new Array();
	for (var i in obj) {
		arr.push(i);
	}
	return arr;
}

//merges arrays and returns result
function array_merge(arr1, arr2) {
	var arr = new Array();	//make new array so we dont overwrite the source
	for (var i=0; i<arr1.length; i++) {arr.push(arr1[i]);}
	for (var i=0; i<arr2.length; i++) {arr.push(arr2[i]);}
	return arr;
}




//merges object and returns result
//obj2 overrides obj1
function object_merge(obj1, obj2) {
	var obj = new Object();	//make new object so we dont overwrite the source
	for (var key in obj1) {obj[key] = obj1[key];}
	for (var key in obj2) {obj[key] = obj2[key];}
	return obj;
}





//sets a nested array value
function arraySetNest(arr, path, value, depth) {
	if (depth == null) {depth = 0;}
	var count = path.length;
	var name = path[depth];
	
	if (arr[name] != null) {
		if (depth == (count-1)) {		//if depth correct then change item
			arr[name] = value;
		} else {
			if (is_array(arr[name])) {//if array then continue traversing
				arr[name] = arraySetNest(arr[name], path, value, depth+1);
			} else {					//otherwise continue traversing (will create new path)
				arr[name] = arraySetNest(new Array(), path, value, depth+1);
			}
		}
	} else {							//item NOT found so create one
		if (depth == (count-1)) {		//if at correct depth then set item
			arr[name] = value;		//otherwise continue traversion (will create new path)
		} else {
			var subArr = new Array();
			arr[name] = arraySetNest(subArr, path, value, depth+1);
		}
	}
	return arr;	//return updated array
}



//gets a nested array value (or NULL)
function arrayGetNest(arr, path, depth) {
	//return path.join(',');
	if (depth == null) {depth = 0;}
	var count = path.length;
	var name = path[depth];
	var value = null;
	
	if (arr[name] != null) {
		var val = arr[name];
		if (depth == (count-1)) {
			return val;
		} else {
			//if array then continue traversing
			if (is_array(val) || is_object(val)) {
				value = arrayGetNest(val, path, depth+1);
			}
		}
	}
	//return null on fail
	return value;
}