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

Object.extend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
	return destination;
}

if (!window.Core) {
	var Core = new Object();
}
Core = {
	version: '0.3',
	author: 'Matt Bower',
	debug: false
}

Core.browser = {
	versionNum: '',
	platform: '',
	engine: '',
	check: function() {
		var ua = navigator.userAgent;
		if(ua.search(/Mac(?:intosh)?/) > -1) {
			this.platform.mac = true;
		} else if(ua.indexOf('Windows') > -1) {
			this.platform.windows = true;
		}

		switch(true) {

		case ua.indexOf('Firefox') > -1:
			this.versionNum = ua.match(/Firefox\/([^ \t]+)/)[1];
			this.firefox = true;
			this.engine = 'gecko';
			break;
		case ua.indexOf('Opera') > -1:
			this.versionNum = ua.match(/Opera\/([^ \t]+)/)[1];
			this.opera = true;
			break;
		case ua.indexOf('Safari') > -1:
			this.versionNum = ua.match(/Safari\/([^ \t]+)/)[1];
			this.safari = true;
			this.engine.webkit = true;
			break;
		case ua.indexOf('Netscape') > -1:
			this.versionNum = ua.match(/Netscape\/([^ \t]+)/)[1];
			this.netscape = true;
			this.engine = 'gecko';
			break;
		case ua.indexOf('Camino') > -1:
			this.versionNum = ua.match(/Camino\/([^ \t]+)/)[1];
			this.camino = true;
			this.engine = 'gecko';
			break;
		case ua.indexOf('OmniWeb') > -1:
			this.versionNum = ua.match(/Omniweb\/([^ \t]+)/)[1];
			this.omniweb = true;
			this.engine.webkit = true;
			break;
		case ua.indexOf('SeaMonkey') > -1:
			this.versionNum = ua.match(/SeaMonkey\/([^ \t]+)/)[1];
			this.seamonkey = true;
			this.engine = 'gecko';
			break;
		case ua.indexOf('MSIE') > -1:
			this.versionNum = ua.match(/MSIE ([\d.]+)/)[1];
			this.msie = true;
			break;
		case ua.indexOf('Mozilla') > -1:
			this.versionNum = ua.match(/Mozilla\/([^ \t]+)/)[1];
			this.mozilla = true;
			this.engine = 'gecko';
			break;
		}
	}

}

if (!window.Browser) {
	var Browser = new Object();
}

Browser = Core.browser;

Core.browser.check();
//alert(Browser.engine);
// alert(Core.browser.versionNum);

window.onDOMload = function(func) {
	alert(typeof func);
	func.apply(document);
	if(Browser.engine == 'gecko' || Browser.opera) {
		document.addEventListener( "DOMContentLoaded", window.onDOMload, false );
	}
}

// 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;
	}
}

var $$ = document.getElementsByClassName;


// 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;
	}
}
*/


function __(element) {
	// Make sure all functions are usable
	if(!document.getElementById || !document.getElementsByTagName || !document.getElementsByClassName) return false;

	if(element.match(/^[a-z]/)) {
		return document.getElementsByTagName(element);
	} else if(element.charAt(0) == '.') {
		return document.getElementsByClassName(element.substr(1));
	} else if(element.charAt(0) == '#') {
		return document.getElementById(element.substr(1));
	} 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;
}

Object.extend(Array.prototype, {
	// Retrieves value from array if it exists
	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
	first: function() {
		return this[0];
	},

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

Object.extend(String.prototype, {
	// strips all whitespace at beginning and end of string
	trim: function() {
		return this.replace(/^\s*(.+?)\s*$/, "$1");
	},

	// strips all whitespace at beginning of string
	ltrim: function() {
		return this.replace(/^\s*(.+?)$/, "$1");
	},

	// strips all whitespace at end of string
	rtrim: function() {
		return this.replace(/^(.+?)\s*$/, "$1");
	},

	// repeats a string
	repeat: function(repeatCount) {
		var newString = '';
		for (var i = 0 ; i < repeatCount ; i++) {
			 newString += this;

		}
		return newString;
	},

	// capitalizes first letter of a string
	firstToUpperCase: function() {
		var firstChar = this.charAt(0).toUpperCase();
		var restOfString = this.substr(1);
		return firstChar + restOfString;
	},

	// capitalizes first letter of every word of a string
	toTitleCase: function() {
		var exceptions = new Array('and', 'but', 'or', 'is', 'a');
		var words = this.split(' ');
		for (var i = 0 ; i < words.length ; i++) {
			if(words[i].length == 0 || words[i]) continue;
			words[i] = words[i].firstToUpperCase();

		}
		return words.join(' ');
	}
});

Object.extend(Object.prototype, {
	// Merges multiple arrays together (like PHP array_merge())

	// patch <db> :: resolving jquery conflicts
	// this is causing conflicts with jquery. trying tto run site with it removed
	/*
	merge: function() {
		if(arguments.length < 1) return false;
	
		for (var i = 0 ; i < arguments.length ; i++) {
			if(arguments[i].length == undefined) {
				for (props in arguments[i]) {
					this[props] = arguments[i][props];
				}
			} else if (arguments[i].length > 0) {
				for (var j = 0 ; j < arguments[i].length ; j++) {
					this.push(arguments[i][j]) ;
				}
			}
			
		}
		return true;
	},
	*/
	prop_exists: function(prop) {
		var found = false;
		for (props in this) {
			if(props == prop) {
				found = true;
				break;
			} else {
				continue;
			}
		}

		return found;
	}/*,
	
	// Checks for the existence of a value in an array
	in_array: function(value) {
		var found = false;
		if(this.length == undefined) {
			for (props in this) {
				if(this[props] == value) {
					found = true;
					break;
				} else {
					continue;
				}
			}
		} else if (arguments[i].length > 0) {
			for (var i = 0 ; i < this.length ; i++) {
				if(this[i] == value) {
						found = true;
						break;
					} else {
						continue;
				}
			}
		}

		return found;
	}
*/	
});

