
/*

	# ----------------------------------------------------------------------------- #
	#																#
	# global.js														#
	#																#
	# MySpinalCord.com													#
	# Copyright 2011 Thommy Morgan										#
	# All Rights Reserved												#
	#																#
	# ----------------------------------------------------------------------------- #


		
*/
	 
	
	/*
 	# ----------------------------------------------------------------------------- #
 	# Preprocessor variables											 	#
	# ----------------------------------------------------------------------------- #*/


	var TRUE 		= YES 	= SUCCESS		= EXISTS		= true;
	var FALSE 	= NO 	= FAIL 		= ERROR		= false;
	
	
	/*
 	# ----------------------------------------------------------------------------- #
 	# Preprocessor functions											 	#
	# ----------------------------------------------------------------------------- #*/
	
	
	window.enumerate = function(arr, asBitwise) {
		for(var i in arr) {
			i = parseInt(i);
			if(i == 0)		eval(arr[i] + " = 0;");
			else {
				if(!asBitwise)	eval(arr[i] + " = " + i.toString() + ";");
				else			eval(arr[i] + " = 1 << " + (i-1).toString() + ";");
			}
		}
	}
	
	
	window.clear_params = function(string) {
		return string.indexOf("?") > 0 ? string.substring(0, string.indexOf("?")) : string;
	}
	
	
	window.wait_for = function(c, f, t) {
		
		this.condition 	= c;
		this.func 		= f;
		this.timeout 		= (!t) ? 3000 : t;
		this.count 		= 0;
		this.timer;
		
		this.count += 30;

		var con 	= this.condition;
		var func 	= this.func;
		var tmt	= this.timeout;
		var cnt 	= this.count;
		var tmr	= this.timer;

		this.step = function() {
			
			cnt += 30;
			
			if(typeof con == 'string') {
				if(eval(con)) cnt = tmt + 1;	
			}
			else
			if(con) cnt = tmt + 1;
			
			if(cnt > tmt) {
				clearInterval(tmr);
				func();
			}
			
		}
		
		tmr = setInterval(this.step, 30);
		
	}
	
	
	window.is_array = function(input){
		return typeof(input) == "object" && (input instanceof Array);
	}
	
	
	window.tryout = function(func_to_try, func_if_fail) {
		if(!func_if_fail) func_if_fail = function(){};
		try {
			func_to_try();
			return SUCCESS;
		}
		catch(err) {
			func_if_fail();
			return FAIL;
		}
	}
	
	
	window.tryoutloud = function(func_to_try, func_if_fail) {
		if(!func_if_fail) func_if_fail = function(){};
		try {
			func_to_try();
			return SUCCESS;
		}
		catch(err) {
			alert(err);
			func_if_fail();
			return FAIL;	
		}
	}
	
	
	window.disableSel = function(target) {
		if (typeof target.onselectstart != "undefined") // Retarded Internet Exploerer
			target.onselectstart = function() { return false; };
		else if (typeof target.style.MozUserSelect != "undefined") // Firefox and Chrome
			target.style.MozUserSelect = "none";
	}
	
	
	window.enableSel = function(target) {
		if (typeof target.onselectstart != "undefined") // Retarded Internet Exploerer
			target.onselectstart = function() { return true; };
		else if (typeof target.style.MozUserSelect != "undefined") // Firefox and Chrome
			target.style.MozUserSelect = "all";
	}
		
	
	/*
 	# ----------------------------------------------------------------------------- #
 	# Find Layer													 	#
	# ----------------------------------------------------------------------------- #*/
	
	
	window.$ = function(id, origset) {
	
		var returnObject;
		if(!origset) origset = document;
		
		function findLayer(id, set, asType) {
	
			if(typeof id == "object")
				return id;
			else
			if(typeof id == "string") {
				if(!asType) {
					if(id.contains(":")) {
						if(id.charAt(0) == ":") {
							var newset = set.getElementsByName(id.substring(1));
						} else {
							var newset = set.getElementsByTagName(id.substring(0, id.indexOf(":")));
						}
						return findLayer(id.substring(id.indexOf(":") + 1), newset, "name");
					} else
					if(id.contains("#")) {
						var newset = set.getElementsByTagName(id.substring(0, id.indexOf("#")));
						return findLayer(id.substring(id.indexOf("#") + 1), newset, "id");
					} else
					if(id.contains(";")) {
						var newset = set.getElementsByTagName("form");
						var currform;
						for(var i = 0; i < newset.length; i++) {
							if(newset[i].name && newset[i].name == id.substring(0, id.indexOf(";"))) {
								currform = newset[i];
								break;
							}
						}
						return findLayer(id.substring(id.indexOf(";") + 1), currform, "form");
					} else {
						try {
							if(set.getElementById)
								return set.getElementById(id);
							else if(set.layers)
								return set.layers[id];
							else if(set.all)
								return set.all[id];
						}
						catch(err) {
							return null;	
						}
					}
				}
				else {
					if(asType == "id") {
						for(var i = 0; i < set.length; i++) {
							//if(typeof set[i].children != "undefined") return findLayer(id, set[i].children, "id");
							if(set[i].id == id) return set[i];
						}
						return null;
					}
					else
					if(asType == "name") {
						for(var i = 0; i < set.length; i++) {
							if(set[i].name == id) return set[i];
						}
						return null;
					}
					else
					if(asType == "form") {
						if(set == null) return null;
						for(var i = 0; i < set.elements.length; i++) {
							if(set.elements[i].name == id) return set.elements[i];
						}
						return null;
					}
					else {
						try {
							if(set.getElementById)
								return set.getElementById(id);
							else if(set.layers)
								return set.layers[id];
							else if(set.all)
								return set.all[id];
						}
						catch(err) {
							return null;	
						}
					}
				}
			}

		}
		
		return findLayer(id, origset);
		
	}
	
	
	window.page_redir = function(redir) {
		targetObject = new DC.Layer(":" + redir);
		if(targetObject.obj && targetObject.obj.name != "")
			smoothScrollTo(targetObject.getTop());
	}


	/*
 	# ----------------------------------------------------------------------------- #
 	# Script importing												 	#
	# ----------------------------------------------------------------------------- #*/
	
	
	var head_cont = document.getElementsByTagName("head")[0];


	var ScriptHandler = {
	
		numberofscripts: 0,
		includedStyles: [],
	
		requireScript: function(file, num) {
			var script = document.createElement("script");
			script.setAttribute("type", "text/javascript");
			script.setAttribute("language", "JavaScript");
			script.setAttribute("charset", "utf-8");
			if(num == null) {
				file += ((file.contains("?")) ? "&" : "?") + "scriptnum=" + ScriptHandler.numberofscripts;
				script.setAttribute("id", "script_" + ScriptHandler.numberofscripts);
				ScriptHandler.numberofscripts++;
			} else
			if(num > 0) {
				file += ((file.contains("?")) ? "&" : "?") + "scriptnum=" + num.toString();
				script.setAttribute("id", "script_" + num.toString());	
			}
			script.setAttribute("src", file);
			head_cont.appendChild(script);
		},
		
		
		removeScript: function(id, byfile) {
			if(!byfile) {
				head_cont.removeChild($("script#" + id));
			} else {
				ScriptHandler.includedScripts = document.getElementsByTagName("script");
				var i = ScriptHandler.includedScripts.length;
				while(i--) {
					if(ScriptHandler.includedScripts[i].src.toString() == byfile) {
						head_cont.removeChild(ScriptHandler.includedScripts[i]);
					}
				}
			}
		},
		
		
		requireStyle: function(file) {
			ScriptHandler.removeStyle(file);
			var script = document.createElement("link");
			script.setAttribute("rel", "stylesheet");
			script.setAttribute("type", "text/css");
			script.setAttribute("href", file);
			head_cont.appendChild(script);
		},
		
		
		removeStyle: function(file) {
			ScriptHandler.includedStyles = document.getElementsByTagName("link");
			var i = ScriptHandler.includedStyles.length;
			while(i--) {
				if(ScriptHandler.includedStyles[i].href.toString() == file) {
					head_cont.removeChild(ScriptHandler.includedStyles[i]);
				}
			}
		},
		
		
	}
	
	
	window.requireScript 	= ScriptHandler.requireScript;
	window.removeScript		= ScriptHandler.removeScript;
	window.requireStyle 	= ScriptHandler.requireStyle;
	window.removeStyle		= ScriptHandler.removeStyle;
	
	
	/*
 	# ----------------------------------------------------------------------------- #
 	# Page dimensions												 	#
	# ----------------------------------------------------------------------------- #*/
	
	
	window.window_width = function() {
		return (window.innerWidth) ? window.innerWidth : document.documentElement.clientWidth;
	}
	
	
	window.window_height = function() {
		return (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
	}
	
	
	window.doc_width = function() {
		return (document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.documentElement.clientHeight) ? document.documentElement.clientWidth : document.documentElement.width;
	}
	

	window.doc_height = function() {
		return (document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.documentElement.height;
	}
	
	
	window.smoothScrollTo = function(scrollPoint) {
		
		if(scrollPoint + window_height() > doc_height())
			scrollPoint = Math.abs(doc_height() - window_height());
			
		var scrollInterval = null;
		
		if(scrollEventActive) {
			clearInterval(scrollInterval);
			scrollEventActive = NO;
		}	
		
		function loopScroll() {

			var dy = scrollPoint - document.body.scrollTop;
			var ab = Math.abs(dy);

			if(scrollEventActive) {
				scrollEventActive = NO;
				windowIsAdjustingScroll = NO;
				clearInterval(scrollInterval);
			} else
			if(ab <= 8) {
				document.body.scrollTop = scrollPoint;
				windowIsAdjustingScroll = NO;
				clearInterval(scrollInterval);
			} else
			if(ab > 8) {
				document.body.scrollTop += dy / 8;
			}
			
		}
		
		windowIsAdjustingScroll = YES;
		scrollInterval = setInterval(loopScroll, 30);
		
	}
	
	
	windowIsAdjustingScroll = NO;
	scrollEventActive = NO;
	userDidStartScrollEvent = function(e) {
		scrollEventActive = YES;
	}


	checkForScrollEvent = function(e) {

		if(!e) return;
		
		if(e.which) {
			if(
				e.which == 33 || // pageup
				e.which == 34 || // pagedown
				e.which == 32 || // spacebar
				e.which == 38 || // up 
				e.which == 40 || // down
				(e.ctrlKey && e.which == 36) || 	// ctrl + home 
				(e.ctrlKey && e.which == 35) 		// ctrl + end 
			) {
				userDidStartScrollEvent(e);
			}
		} else
		if(window.event) {
			if(
				e.keycode == 33 || // pageup
				e.keycode == 34 || // pagedown
				e.keycode == 32 || // spacebar
				e.keycode == 38 || // up 
				e.keycode == 40 || // down
				(e.ctrlKey && e.keycode == 36) || 	// ctrl + home 
				(e.ctrlKey && e.keycode == 35) 	// ctrl + end 
			) {
				userDidStartScrollEvent(e);
			}
		}
	
	}


	/*
 	# ----------------------------------------------------------------------------- #
 	# Debug control												 	#
	# ----------------------------------------------------------------------------- #*/
	
	
	window.dumpProps = function(returnObject, parent) {
		// Go through all the properties of the passed-in object 
		for (var i in returnObject) {
			// if a parent (2nd parameter) was passed in, then use that to 
			// build the message. Message includes i (the object's property name) 
			// then the object's property value on a new line 
			if (parent) { var msg = parent + "." + i + "\n" + returnObject[i]; } else { var msg = i + "\n" + returnObject[i]; }
			// Display the message. If the user clicks "OK", then continue. If they 
			// click "CANCEL" then quit this level of recursion 
			if (!confirm(msg)) { return; }
			// If this property (i) is an object, then recursively process the object 
			if (typeof returnObject[i] == "object") {
				if (parent) { dumpProps(returnObject[i], parent + "." + i); } else { dumpProps(returnObject[i], i); }
			}
		}
	}
