var Airbag = {
	init : function() {
		var c = Airbag;
		var p = c.Project;
		var u = c.Utility;

		u.addLoadEvent(function() {
			if (u.isCompatible()) {
				// List function(s) to fire onload here
				p.searchForm();
				p.tagIt();
				p.textFocus();
				p.translateSwitch("translate");

				if (typeof imgSizer != "undefined") {
					var oLogo = document.getElementById("asi").getElementsByTagName("img")[0];
					var oMac = document.getElementById("global").getElementsByTagName("img")[0];
					imgSizer.collate([oLogo, oMac]);
				}
			}
		});
	}

	,Config : {
		sHTMLtag : "can-has-js",
		sPNGsel : "#asi h1"
	}

	/*
		CLIENT-SPECIFIC FUNCTIONS
	*/
	,Project : {
		searchForm : function() {
			var c = Airbag;
			var u = c.Utility;

			var oForms = document.getElementsByTagName("form");

			for (var i = 0; i < oForms.length; i++) {
				if (u.wordFind("search", oForms[i].className)) {
					var oLabels = oForms[i].getElementsByTagName("label");

					for (var j = 0; j < oLabels.length; j++) {
						var sId = oLabels[j].getAttribute("for") || oLabels[j].htmlFor;
						if (sId) {
							var oTarget = document.getElementById(sId);
							oTarget.value = u.getInnerText(oLabels[j]);
							oLabels[j].parentNode.removeChild(oLabels[j]);
						}
					}
				}
			}
		}

		,tagIt : function() {
			var c = Airbag;
			var u = c.Utility;

			var oHtml = document.getElementsByTagName("html")[0];

			if (oHtml) {
				oHtml.className = u.safeAppend(oHtml.className, c.Config.sHTMLtag);
			}
		}

		,textFocus : function() {
			var isOldIE = (document.all && !window.opera && typeof document.body.style.maxHeight == "undefined") ? 1 : 0;
			if (!isOldIE) {
				var oInputs = document.getElementsByTagName("input");

				for (var i = 0; i < oInputs.length; i++) {
					if (oInputs[i].getAttribute("type") == "text") {
						oInputs[i].setAttribute("default", oInputs[i].value);
						oInputs[i].setAttribute("changed", false)

						oInputs[i].onfocus = function() {
							if (!this.changed) {
								this.value = "";
							}
						}

						oInputs[i].onblur = function() {
							if (this.value != "") {
								this.changed = true;
							} else {
								this.value = this.getAttribute("default");
							}
						}
					}
				}
			}
		}

		,translateSwitch : function(sClass) {
			var c = Airbag;
			var u = c.Utility;

			var oForms = document.getElementsByTagName("form");

			if (oForms) {
				for (var i = 0; i < oForms.length; i++) {
					if (u.wordFind(sClass, oForms[i].className)) {
						var oSel = oForms[i].getElementsByTagName("select")[0];
						if (oSel) {
							oSel.onchange = function() {
								window.location.href = this.options[this.selectedIndex].value;
							}
						}
					}
				}
			}
		}
	}

	/*
		UTILITY FUNCTIONS
	*/
	,Utility : {
		addLoadEvent : function(func) {
			var oldonload = window.onload;
			if (typeof window.onload != 'function') {
				window.onload = func;
			} else {
				window.onload = function() {
					oldonload();
					func();
				}
			}
		}

		,getInnerText : function(oEl) {
			if (typeof(oEl.textContent) != 'undefined') return oEl.textContent;
			if (typeof(oEl.innerText) != 'undefined') return oEl.innerText;
			if (typeof(oEl.innerHTML) == 'string') return oEl.innerHTML.replace(/<[^<>]+>/g,'');
		}

		,isCompatible : function() {
			if (document.getElementById && document.getElementsByTagName && document.createElement) {
				return true;
			} else {
				return false;
			}
		}

		,safeAppend : function(target, str) {
			target += (target.length > 0 ? " ": "") + str;
			return target;
		}

		,wordFind : function(needle, haystack) {
			return haystack.match(needle + "\\b");
		}
	}
};

Airbag.init();
