// Core javascript extension functions for sites created by LearningChange.
// Some functions inspired by or taken from Prototype.js library (prototype.conio.net)

window.debug = false;

// Gets all elements with passed ids and returns a single object or array
function $() {
	if(!document.getElementById) return false;
	if(arguments.length < 1 || !document.getElementById(arguments[0])) {
		return false;

	} else if(arguments.length == 1) {
		if(document.getElementById(arguments[0])) {
			return document.getElementById(arguments[0]);
		} else {
			return false;
		}

	} else {
		var items = new Array();

		for (i = 0 ; i < arguments.length ; i++) {
			if(document.getElementById(arguments[i])) {
				items.push(document.getElementById(arguments[i]));
			}
		}

		return items;
	}
}

// Gets all elements with specified class
document.getElementsByClassName = function(className) {
	var children = document.getElementsByTagName('*') || document.all;
	var elements = new Array();

	for (var ctr = 0; ctr < children.length; ctr++) {
		var child = children[ctr];
		var classNames = child.className.split(' ');
		for (var ctr2 = 0; ctr2 < classNames.length; ctr2++) {
			if (classNames[ctr2] == className) {
				elements.push(child);
				break;
			}
		}
	}
	if(elements.length > 0) {
		return elements;
	} else {
		return false;
	}
}

// Prints a formatted string (like PHP printf())
function printf(formattedString) {
	var s = formattedString;
	if(s.match(/%s/g).length != (arguments.length-1)) {
		alert('Warning. Number of placeholders does not match number of arguments');
		return false;
	}
	for (var ctr = 1 ; ctr < arguments.length ; ctr++) {
		if(s.indexOf('%s') == -1) break;

		s = s.replace(/%s/, arguments[ctr]);
	}

	document.write(s);
	return true;

}
document.printf = printf;

// Returns a formatted string (like PHP sprintf())
function sprintf(formattedString) {
	var s = formattedString;
	if(s.match(/%[^a-zA-Z]*[a-zA-Z]/g).length != (arguments.length-1)) {
		alert('Warning. Number of placeholders does not match number of arguments');
		return false;
	}
	for (var ctr = 1 ; ctr < arguments.length ; ctr++) {
		if(s.indexOf('%s') == -1) break;

		s = s.replace(/%s/, arguments[ctr]);
	}

	return s;
}

// Merges multiple arrays together (like PHP array_merge())
Array.prototype.merge = function() {
	if(arguments.length < 1) return false;

	for (var ctr = 0 ; ctr < arguments.length ; ctr++) {
		if(typeof arguments[ctr] == 'object' && arguments.length > 0) {
			for (y = 0 ; y < arguments[ctr].length ; y++) {
				this.push(arguments[ctr][y]);

			}
		} else if(typeof arguments[ctr] == 'string') {
			this.push(arguments[ctr]);
		}
	}
	return true;
}

// Checks for the existence of a value in an array
Array.prototype.in_array = function(to_check) {
	for (var ctr = 0 ; ctr < this.length ; ctr++) {
		if(this[ctr] == to_check) {
			return true;
			break;
		}
	}
	return false;
}

// Retrieves value from array if it exists
Array.prototype.get_value = function(to_get) {
	for (var ctr = 0 ; ctr < this.length ; ctr++) {
		if(this[ctr] == to_get) {
			return this[ctr];
			break;
		}
	}
	return false;
}

// Returns first element of array
Array.prototype.first = function() {
	return this[0];
}

// Returns last element of array
Array.prototype.last = function() {
	return this[this.length -1];
}

function $CT(text) {
	if(text && typeof text == 'string') {
		return document.createTextNode(text);
	} else {
		if(window.debug) {
			alert('First argument must be of type String');
		}
		return false;
	}
}

function $CE(element, text, props) {
	var newElement = document.createElement(element);
	if(text && typeof text == 'string') {
		newElement.appendChild($CT(text));

	} else if(text && typeof text != 'string') {
		if(window.debug) {
			alert('Second argument must be of type String');
		}
		return false;
	}

	if(props && typeof props == 'object') {
		for (properties in props) {
			newElement[properties] = props[properties];
		}
	} else if(props && typeof props != 'object') {
		if (window.debug) {
			alert('Third argument must be of type Object and not be an indexed Array');
		}
		return false;
	}

	return newElement;
}
