﻿function $DL(f){
	if ($DL.loaded) f();
	else window.addEvent('domready',f);
}

/* GLOBAL function to add functions to before unload event of window */
function $DUL(f)
{
    window.addEvent('beforeunload',f);
}

function $WL(f)
{
    window.addEvent('load',f);
}
onDomReady(function () { $DL.loaded=true; });

MooTools.init=function () {
	document.form=document.forms[0];
};

$DL(MooTools.init);

function addNamespace(ns) {
	if (!ns) return null;
	var levels=ns.split(".");
	var root=window;
	for (var i=0;i<levels.length;i++) {
		if (root[levels[i]]==undefined) root[levels[i]]={};
		root[levels[i]].__namespace=true; // for ms ajax
		root=root[levels[i]];
	}
	if (root[levels[0]]) window.__rootNamespaces.push(root[levels[0]]); // for ms ajax
	return root;
}

function returnFalse() { return false; }

// fixes ms ajax errors
Array._forEach=Array.forEach;
function $each(iterable, fn, bind){
	if (iterable && typeof iterable.length == 'number' && $type(iterable) != 'object') Array._forEach(iterable, fn, bind);
	else for (var name in iterable) fn.call(bind || iterable, iterable[name], name);
};

Element.extend({
	show:function () {
		this.setStyle("display","");
		return this;
	},
	hide:function () {
		this.setStyle("display","none");
		return this;
	},
	visible:function () {
		var p=this;
		while (p && p!=document.body && p.getStyle("display")!="none") p=p.getParent();
		return !p; // if there's a parent with display=none, this element isn't visible
	},

	toggle:function () {
		this.setStyle("display",this.getStyle("display")=="none" ? "" : "none");
	},

	getTextContent:function () {
		return this.innerText || this.textContent;
	},
	setTextContent:function (value) {
		this.empty().appendText(value);
		return this;
	},

	setSelection:function (b) {
		if (window.ie || window.opera) this[(b?"remove":"add")+"Event"]("selectstart",returnFalse);
		if (window.gecko) this.style.MozUserSelect=b?"":"none";
		if (window.webkit) this.style.KhtmlUserSelect=b?"":"none";
		return this;
	},
	
	removeTextNodes:function () {
		if (this._removedTextNodes) return;
		$A(this.childNodes).each(function (o) {
			if (o.nodeType===3) {
				if (/^\s+$/.test(o.data)) o.parentNode.removeChild(o);
			}
			else $(o).removeTextNodes();
		});
		this._removedTextNodes=true;
		return this;
	},

	removeChildren:function () {
		while (this.hasChildNodes()) this.removeChild(this.lastChild);
		return this;
	},
	
	setPosition:function (p) {
		if (typeof(p)=="object") {
			x=p.x;
			y=p.y;
		}
		else {
			x=arguments[0];
			y=arguments[1];
		}
		this.setStyles({left:x,top:y});
		return this;
	},
	
	setCoordinates:function (c) {
		this.setStyles({left:c.left,top:c.top,width:c.width,height:c.height});
	},
	
	findParent:function (predicate) {
		var el=this;
		while (!predicate($(el)) && el && el!=document.documentElement) el=el.parentNode;
		return el===document.documentElement ? null : el;
	}
});

String.extend({
	escapeHtml:function (isHtml) { 
		var str=this;
		if (!isHtml) str=str.replace(/>/g,"&gt;").replace(/</g,"&lt;");
		else str=str.replace(/\r?\n/g,"<br/>");
		str=str.replace(/"/g,"&quot;").replace(/'/g,"&#39;");
		return str;
	}
});

Object.inspect=function (o,funcs,showFuncContent) {var s=[];for (var i in o) {if (typeof o[i]=="function" && funcs) s.push(i+"\t\t"+(showFuncContent ? o[i] : o[i].toString().substr(0,o[i].toString().search(/[\n\{]/))));else if (typeof o[i]!="function") s.push(i+"\t\t"+o[i]);}return "\r\n"+s.join("\r\n")+"\r\n";};
var h=Object.inspect;