(function() {
var lib = window.lib = function(thing) {
	if(!(this instanceof lib)) {
		return new lib(thing);
	}
	if(!isNaN(parseInt(thing.length,10))) {
		if(thing.join && thing.push) {
			var newThing = thing;
		}
		else {
			for(var i=0,newThing=[],len=thing.length;i<len;i++) {
				newThing.push(thing[i]);
			}
		}
	}
	else {
		newThing = [thing];
	}
	this.length = 0;
	Array.prototype.push.apply(this,newThing);
	return this;
};
var $ = window.$ = function(id) {
	return document.getElementById(id);
};
var $T = window.$T = function(elm,prnt,_fast) {
	var _return = (prnt || document).getElementsByTagName(elm || '*');
	if(_fast) {
		return _return;
	}
	return new lib(_return);
};
var $C = window.$C = function(cname,elm,prnt,_fast) {
	var re = new RegExp('(^|\\s)' + cname + '(\\s|$)');
	for(var i=0,newThing=[],elms=$T(elm,prnt,true),len=elms.length;i<len;i++) {
		var e = elms[i];
		if(re.test(e.className)) {
			newThing.push(e);
		}
	}
	if(_fast) {
		return newThing;
	}
	return new lib(newThing);
};
lib.x = function(from,to) {
/*
lib.x({
	nomeFuncao:lib.x({},funcao)
});
*/
	to = to || lib;
	for(var prop in from) {
		to[prop] = from[prop];
	}
	return to;
};
lib.xEach = function(from, to) {
	to = to || lib.prototype;
	for(var prop in from) {
		to[prop] = function(arg) {
			for(var i=0,len=this.length;i<len;i++) {
				from[prop](this[i],arg);
			}
			return this;
		};
	}
	return to;
};
lib.x({
	forEach:function(lambda) {
		for(var i=0,len=this.length;i<len;i++) {
			try {
				lambda(this[i],i);
			}
			catch(e) {
				if(e == 'break')break;
				eval(e);
			}
		}
		return this;
	},
	map:function(lambda) {// length == //!= values
		for(var i=0,len=this.length,newThing=[];i<len;i++) {
			try {
				newThing.push(lambda(this[i],i));
			}
			catch(e) {
				if(e == 'break')break;
				eval(e);
			}
		}
		return new lib(newThing);
	},
	filter:function(lambda) {// length != //== values
		for(var i=0,len=this.length,newThing=[];i<len;i++) {
			try {
				if(lambda(this[i],i)) {
					newThing.push(this[i]);
				}
			}
			catch(e) {
				if(e == 'break')break;
				eval(e);
			}
		}
		return new lib(newThing);
	},
	indexOf:function(search) {
		for(var i=0,len=this.length;i<len;i++)
			if(this[i] == search)
				return i;
		return -1;
	},
	concat:function(thing) {
		for(var i=0,len=thing.length;i<len;i++) {
			this[this.length++] = thing[i];
		}
		return this;
	},
	push:function(thing) {
		this[this.length++] = thing;
		return this;
	},
	toArr:function() {
		for (var i=0,_return=[],len=this.length; i < len; i++) {
			_return.push(this[i]);
		}
		return _return;
	}
},lib.prototype);
lib.x({
	binder:function(/*that,func,myArgs*/) {
		var myArgs = lib.prototype.toArr.apply(arguments),
			that = myArgs.shift(),
			func = myArgs.shift();
		return function() {
			func.apply(that,lib(myArgs).concat(arguments).toArr());
		};
	},
	createElement:function(elm,options) {
		var _return;
		if(lib.browser.indexOf('ie') != -1) {
			if('name' in options) {//IE tem problemas para gerar campos com o atributo 'name'
				_return = document.createElement('<' + elm + ' name="' + options['name'] + '">');
				delete options['name'];
			}
			else {
				_return = document.createElement(elm);
			}
				
			if('style' in options) {//IE trabalha com o style de maneira diferente
				_return.style['cssText'] = options['style'];
				delete options['style'];
			}
			
			//Opacidade no IE também é diferente
			//e pra ficar mais simples coloco
			//a opacidade separada do style
			if('opacity' in options) {
				_return.style.zoom = 1;//Todo elemento que terá sua opacidade alterada, deve ter a propriedade 'hasLayout'
				_return.style.filter = 'alpha(opacity=' + options['opacity'] * 100 + ')';
				delete options['opacity'];
			}
		}
		else {
			_return = document.createElement(elm);
		}
		
		if('opacity' in options) {//Retorno a opacidade junto aos estilos para os navegadores normais
			var style = 'opacity:' + options['opacity'] + ';';
			if(options['style']) {
				options['style'] += options['style'][options['style'].length - 1]!=';'?';'+style:style;
			}
			else {
				options['style'] = style;
			}
			delete options['opacity'];
		}
		
		var props = {
			'for': 'htmlFor',
			'class': 'className',
			value: 'value',
			disabled: 'disabled',
			checked: 'checked',
			readonly: 'readOnly',
			selected: 'selected',
			maxlength: 'maxLength',
			selectedIndex: 'selectedIndex',
			defaultValue: 'defaultValue'
		};
		
		for(var prop in options)
			if(prop in props)//Algumas propriedades ainda tem que usar o metodo DOM 0
				_return[props[prop]] = options[prop];
			else {//Outras podem usar o método 'normal'
				_return.setAttribute(prop,options[prop]);
			}
		
		return _return;
	},
	orderTable:function(table,column) {
		if(!table)
			throw 'Tabela não especificada.';
		column = column || 0;
		var storeRows = [],
			actionRows = [],
			tbody = $T('tbody',table,true)[0],
			rows = tbody.rows,
			i=0;
		
		while(rows[0]) {
			var cell = rows[0].cells[column];
			storeRows.push(rows[0]);
			actionRows[i] = [cell.textContent || cell.innerText,i];
			lib.removeChild(rows[0]);
			i++;
		}
		actionRows.sort(function(a,b) {return a==b?0:a>b?1:-1;});
		for(var j=0,len=actionRows.length;j<len;j++) {
			tbody.appendChild(storeRows[actionRows[j][1]]);
		}
	},
	firstChild:function(elm) {
		var _return;
		if(!elm || !(_return = elm.firstChild))
			return null;
		if(_return.nodeType === 1)
			return _return;
		else {
			return lib.nextSibling(_return);
		}
	},
	nextSibling:function(elm) {
		var _return;
		if(!elm || !(_return = elm.nextSibling))
			return null;
		do {
			if(_return.nodeType === 1)//ELEMENT_NODE
				return _return;
		}while(_return = _return.nextSibling);
		return null;
	},
	previousSibling:function(elm) {
		var _return;
		if(!elm || !(_return = elm.previousSibling))
			return null;
		do {
			if(_return.nodeType ===1)//ELEMENT_NODE
				return _return;
		}while(_return = _return.previousSibling);
		return null;
	},
	insertAfter:function(refElm,newElm) {
		var next = lib.nextSibling(refElm);
		if(!next)
			refElm.parentNode.appendChild(newElm);
		else {
			next.parentNode.insertBefore(newElm,next);
		}
	},
	getPos:function(obj) {
		var curleft  = 0,
			curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while ((obj = obj.offsetParent)) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return {left:curleft,top:curtop};
	},
	hasClass:function(elm,classname) {
		if(!elm)
			return false;
		classname = '(' + classname.replace(/\s+/g,'|') + ')';
		var re = new RegExp('(^|\\s)' + classname + '(\\s|$)');
		return re.test(elm.className);
	},
	//Deve-se buscar a propriedade em especifico com a nomenclatura em JS
	//Ex: marginTop
	getStyle:function(elm,prop) {
		if(elm.currentStyle) {
			return elm.currentStyle[prop];
		}
		else if(document.defaultView && document.defaultView.getComputedStyle) {
			document.defaultView.getComputedStyle(elm,'')[prop];
		}
		return null;
	},
	addEvent:function(elm,on,func) {//Tenho que melhorar, por enquanto, somente uso interno
		if(elm.addEventListener) {
			elm.addEventListener(on,func,false);
		}
		else if(elm.attachEvent) {
			elm.attachEvent('on' + on, (function() {return function(evt) {func.call(elm,evt||window.event);};})());
		}
		else {
			elm['on' + on] = (function() {return function(evt) {func.call(elm,evt||window.event);};})();
		}
	},
	stopPropagation:function(evt) {//Evitar de chamar o evento no elemento pai
		if(evt.stopPropagation)
			evt.stopPropagation();
		evt.cancelBubble = true;
	},
	preventDefault:function(evt) {//mesmo q return false
		if(evt.preventDefault)
			evt.preventDefault();
		evt.returnValue = false;
	}/*,
	removeEvent:function(elm,on,func) {
		if(elm.removeEventListener) {
			elm.removeEventListener(on,func,false);
		}
		else if(elm.detachEvent) {
			elm.detachEvent('on' + on,)
		}
	}*/
});
var changes = {
	removeElement:function(elm) {//auto deletar um elemento
		elm.parentNode.removeChild(elm);
	},
	clearHTML:function(elm) {
		if(elm.tagName == 'select')
			elm.options.length = 0;
		else {
			while(elm.firstChild)
				elm.removeChild(elm.firstChild);
		}
	},
	addClass:function(elm,classname) {
		elm.className = lib(classname.split(' '))
			.filter(function(cname) {return !lib.hasClass(elm,cname);})
			.concat(elm.className.split(' '))
			.toArr()
			.join(' ');
	},
	delClass:function(elm,classname) {
		classname = lib(classname.split(' '));
		elm.className = lib(elm.className.split(' '))
			.filter(function(cname) {return (classname.indexOf(cname)==-1);})
			.toArr()
			.join(' ');
	},
	show:function(elm,type) {
		elm.style.display = type || '';
		elm.style.visibility = '';
	},
	hide:function(elm,type) {
		if(type == 'v') {
			elm.style.visibility = 'hidden';
		}
		else {
			elm.style.display = 'none';
		}
	},
	toggle:function(elm) {
		if(elm.style.visibility == 'hidden') {
			elm.style.visibility = '';
			elm.style.display = elm.style.display!='none'?elm.style.display:'';
		}
		else if(elm.style.display == 'none') {
			elm.style.display = '';
		}
		else {
			elm.style.display = 'none';
		}
	},
	//Minibordas com 2px de curva
	//devem ser usados 2 div, 1 para o conteudo e 1 para receber a borda
	//o elemento a ser passado deve ser o div que recebera a borda
	//cores de fundo só poderão ser aplicadas ao div do conteudo
	miniBorders:function(elm,bg,brd) {
		bg = 'background-color:' + (bg || lib.getStyle(lib.firstChild(elm),'backgroundColor'));
		
		var borda = 'display:block;height:1px;overflow:hidden;';
		brd = brd<1||brd>2?brd=2:brd;
		for(var i=1;i<=brd;i++) {
			var brd = lib.createElement('span',{style:borda + 'margin:0 ' + i + 'px;' + bg});
			elm.appendChild(brd.cloneNode(false));
			elm.insertBefore(brd.cloneNode(false),lib.firstChild(elm));
		}
	}
};
lib.x(changes);
lib.xEach(changes);
lib.browser = (function() {
	var ua = navigator.userAgent.toLowerCase();
	if(/webkit/.test(ua))
		return 'safari';
	if(/opera/.test(ua))
		return 'opera';
	if(/msie/.test(ua) && !(/opera/.test(ua))) {
		var ie = /msie\s([0-9])/.exec(ua);
		return 'ie' + ie[1];
	}
	if(/mozilla/i.test(navigator.userAgent) && !(/(compatible|webkit)/i.test(navigator.userAgent)))
		return 'ff';
	return null;
})();
if(lib.browser == 'ie6') {
	try{
		document.execCommand("BackgroundImageCache",false,true);
	}catch(e){}
}
})();