/* util.js JavaScript library Functions by Lucas Ferreira - http://www.lucasferreira.com/
*  Bugs and Reports: contato@lucasferreira.com
*  Version: 2.1b */

var Browser = {
	isIE		:	function(){ return (window.ActiveXObject && document.all && navigator.userAgent.toLowerCase().indexOf("msie") > -1  && navigator.userAgent.toLowerCase().indexOf("opera") == -1) ? true : false; },
	isOpera		:	function(){ return (window.ActiveXObject == undefined && navigator.userAgent.toLowerCase().indexOf("opera") > -1) ? true : false; },
	isMozilla	:	function(){ return (navigator.userAgent.indexOf('Mozilla') > -1 && parseInt(navigator.appVersion.substring(0, 1)) >= 5); }
}

Object.extend = function(obj, ext){
	for (var p in ext) obj[p] = ext[p];
	return obj;
}

if(!Array.prototype.push) {
	Array.prototype.push = function(a){
		this[this.length] = a;
	}
}

var BodyLoad = {
	onloads: new Array(),
	add: function(f){ if(typeof f == "function") BodyLoad.onloads.push(f); },
	onLoad: function(){ for(var i=0; i<BodyLoad.onloads.length; i++) BodyLoad.onloads[i].call(this); },
	init: function(){
		if(!window.onload){
			window.onload = BodyLoad.onLoad;
		} else {
			var oldLoad = window.onload;
			window.onload = function(){
				oldLoad();
				Delegate.create(window, BodyLoad.onLoad)();
			}
		}
	}
}

Delegate = { version: "1.1b" };
Delegate.create = function(obj, func, args){
	var f = function(){
		var target = arguments.callee.target;
		var func = arguments.callee.func;
		var args = arguments.callee.args;
		return func.apply(target, (args.length < 1 ? arguments : args));
	};
	Object.extend(f, {
		args: (args != undefined && args.length > 0 ? args : new Array()),
		target: obj, func: func
	});
	return f;
}
window.Delegate = Delegate;

String.prototype.trim = function() {
	var s = this.toString().replace(/^\s*|\s*$/g, ""), r = "", i = 0;   
	for (i=0; i <= s.length; i++){
		if(!(s.charCodeAt(i) == Key.SPACE ||
		  s.charCodeAt(i) == Key.CARRIAGE ||
		  s.charCodeAt(i) == Key.LINEFEED ||
		  s.charCodeAt(i) == Key.TAB)) r = r + s.charAt(i);
	}  
	return unescape(r.replace(/\+/g, " "));
}
Object.extend(String.prototype, {
	
	replaceAll: function(f, r){
		var s = this.toString();
		while(s.indexOf(f) > -1) s = s.replace(f, r);
		return s;
	},
	
	toNumber: function(){
		var s = this.toString().replaceAll(".", "").replace(",", ".").replaceAll(",", "");
		return isNaN(new Number(s)) ? 0 : new Number(s);
	},
	
	toMoeda: function(c){
		if(c == undefined) var c = 2;
		var s = this.toString().split(".");
		if(s.length > 1) s[1] = s[1].substr(0, c); else s[1] = "00";
		while(s[1].length < c) s[1] += "0";
		return s.join(",");
	},
	
	toObject: function(){
		return eval('(' + this.toString() + ')');
	}
	
});

Object.extend(Number.prototype, {
	
	toNegative: function(){
		return (this < 0) ? this : new Number(this * -1);
	},
	
	modulo: function(){
		return (this < 0) ? this * -1 : this;
	}
	
});

Array.prototype.each = function(f){
	var a = this || [];
	for(var i=0; i<a.length; i++){
		Delegate.create(a, f, [i])();
	}
}
Object.extend(Array.prototype, {
	
	find: function(e){
		var a = this || [];
		for(var i=0; i<a.length; i++){
			if(a[i] == e) return true;
		}
		return false;
	},
	
	remove: function(i){
		this.splice(i, 1);
	},
	
	toObject: function(){
		var a = this, o = new Object();
		for(var i=0; i<a.length; i++){
			o[i.toString()] = a[i];
		}
		return o;
	}
	
});

function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;
    if(document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }
    if(document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }
    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;
    var x = Math.max(x1, Math.max(x2, x3)), y = Math.max(y1, Math.max(y2, y3));
    window.scrollTo(Math.floor(x / 1.5), Math.floor(y / 1.5));
    if(x > 0 || y > 0)  window.setTimeout("window.backToTop()", 30);
}
window.goToTop = window.backToTop = backToTop;

function getQuery(s){
	v = window.location.href.toString();
	vGV = v.split("?"), strGV = vGV[1] ? vGV[1].indexOf(s + "=") : -1;
	sGV = (strGV!=-1) ? vGV[1].substring(strGV+(s.length+1), vGV[1].length) : null;
	if(sGV) sGV = (sGV.indexOf("&")!=-1) ? sGV.substring(0, sGV.indexOf("&")) : sGV;
	return (sGV) ? unescape(sGV) : null;
}

var Event = {

	version: "1.1b",
	unloadAdded: false,
	aEvts: new Array(),
	
	add: function(obj, evType, fn, useCapture)
	{
		useCapture = typeof useCapture == "undefined" ? true : useCapture;
		if(typeof obj == "array")
		{
			obj.each(function(i){ Event.add(this[i], evType, fn); });
			return true;
		}
		var obj = (typeof obj == "string") ? $(obj) : obj;
		if(!obj || obj == null) return false;
		if(obj.attachEvent) obj.attachEvent("on" + evType, fn);
		else if(obj.addEventListener) obj.addEventListener(evType, fn, useCapture);
		else obj["on" + evType] = fn;
		
		Event.aEvts.push(typeof Event.add.arguments == "array" ? Event.add.arguments : [obj, evType, fn, useCapture]);
		
		if(!Event.unloadAdded)
		{
			Event.unloadAdded = true;
			Event.add(window, "unload", Event.unloadEvents, false);
		}
	},
	
	remove: function(obj, evType, func, useCapture)
	{
		useCapture = typeof useCapture == "undefined" ? true : useCapture;
		if(typeof obj == "array")
		{
			obj.each(function(i){ Event.remove(this[i], evType, fn); });
			return true;
		}
		var obj = (typeof obj == "string") ? $(obj) : obj;
		if(!obj || obj == null) return false;		
		if(obj.detachEvent) obj.detachEvent("on" + evType, func);
		if(obj.removeEventListener) obj.removeEventListener(evType, func, useCapture);
		if(obj["on" + evType])
		{
			obj["on" + evType] = function(){};
			delete obj["on" + evType];
		}
	},
	
	unloadEvents: function(){
		for(var i=0; i<(Event.aEvts.length); (Event.remove.apply(this, Event.aEvts[i]), Event.aEvts[i][0]=null), i++);
	}
	
}

//atalhos de compatibilidade com versoes anteriores...
window.addEvent = Event.add;
window.remEvent = Event.remove;

function initValue(o){
	var o = getTargetByEvent(o || window.event);
	if(o.initVal == undefined) {
		o.initVal = o.value;
		Event.add(o, "blur", function(e){
			var o = getTargetByEvent(e);
			if(o.value.length < 1) o.value = o.initVal;
		});
	}
	if(o.value == o.initVal) o.value = "";
}

function alternateRows(o, a, st){
	var iColor = 0, l = o.childNodes.length, oc = o.childNodes;
	for(var i=0; i<l; i++){
		if(oc[i].nodeType == 1){
			var obj = oc[i];
			if(st != undefined) obj = $t("a", oc[i])[0];
			obj.style.backgroundColor = a[iColor];
			iColor = (iColor == 0) ? 1 : 0;
		}
	}
}

function setClass(obj, nClass){
	if(nClass != undefined){
		obj.lastClass = obj.className || "";
		obj.className = nClass;
	} else {
		obj.className = obj.lastClass;
	}
}

function getStyle(e, p){
	if(typeof e == "string") var e = $(e);
	if(e.style && e.style[p]) return e.style[p];
	else if(e.currentStyle && e.currentStyle[p]) return e.currentStyle[p];
	else if(document.defaultView && document.defaultView.getComputedStyle){
		return document.defaultView.getComputedStyle(e, "")[p];
	}
	return false;
}

function changeSelects(){
	var s = $t("SELECT", document);
	if(s != null){
		for(var i=0; i<s.length; i++){
			s[i].style.visibility = s[i].style.visibility.toString().indexOf("hid") > -1 ? "visible" : "hidden";
		}
	}
}

if(Ajax == undefined){
	var Ajax = { version: "1.3" };
}
Ajax.getTransport = function(){
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	for(var i=0; i<prefixes.length; i++){
		try { return new ActiveXObject(prefixes[i] + ".XmlHttp"); } catch(e) { continue; }
	}
	try {
		return new XMLHttpRequest();
	} catch(e){
		return false;
	}
}

function chr(n){
	return String.fromCharCode(n) || null;
}

function getTargetByEvent(e){
	if(typeof(e) == "undefined") var e = window.event;
	source = e.target ? e.target : e.srcElement;
	return source;
}

function $e(el, t, i){
	if(!document.createElement) return false;
	var nEl = document.createElement(el.toString());
	if(t != undefined && t.length > 0 && !i) nEl.appendChild(document.createTextNode(t));
	else if(i == true) nEl.innerHTML = t;
	return nEl;
}

function $(){
	for(var i=0, elements=[], args=$.arguments; i<args.length;
		elements.push( ( (document.getElementById(args[i]) || (document.all && document.all[args[i]])) || document[args[i]] ) || null ), i++);
	return elements.length == 1 ? elements[0] : elements;
}

function $t(t, p){
	for(var i = 0, p = ( (typeof p == "string" ? $(p) : p) || document ), o = [],
		els = p.getElementsByTagName(t); i < els.length; o.push( els[i] ), i++); return o;
}

function $tc(c, t, p){
	for(var i=0, tgs=$t(t, p), o=[]; i < tgs.length; (tgs[i].className == c ? o.push(tgs[i]) : 0), i++); return o;
}

function $c(c, p){
	for(var i = 0, p = ( (typeof p == "string" ? $(p) : p) || document ), o = [],
		els = (p.getElementsByTagName("*") || document.all); i < els.length;
		( els[i].className == c ? o.push( els[i] ) : 0 ), i++); return o;
}

function getElementsBySelect(rule) {
	for(var i = 0, parts = rule.split(" "), nodes = [document];
		i < parts.length; nodes = getSelectedNodes(parts[i], nodes), i++);
	return nodes;
}

var $$ = getElementsBySelect;

function getSelectedNodes(select, elements) {
	var nodes = [], doc = document;
	var identify = (/\#([a-z0-9_-]+)/i).exec(select);
	if(identify) {
		var element = $(identify[1]);
		return element ? [element] : nodes;
	}
	var classname = (/\.([a-z0-9_-]+)/i).exec(select);
	var tagName = select.replace(/(\.|\#|\:)[a-z0-9_-]+/i, '');
	var classReg = classname ? new RegExp('\\b' + classname[1] + '\\b') : false;
	for(var i=0; i<elements.length; i++) {
		var result = tagName ? ( elements[i].getElementsByTagName(tagName) || (elements[i].all && elements[i].all.tags(tagName)) )
				: ( elements[i].getElementsByTagName("*") || elements[i].all ); 
		for(var j = 0; j < result.length; j++) {
			var node = result[j];
			if(classReg && !classReg.test(node.className)) continue;
			nodes[nodes.length] = node;
		}
	}	
	return nodes;
}