/* 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); }
}

function require(src, special){ 
	if(!special) var src = src.split(".").join("/");
	src = src.indexOf(".js") < src.length - 3 ? src + ".js" : src;
	document.write( '<sc'+'ript src="' + src + '" type="text/javascript"><\/sc'+'ript>' );
}

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;
}

if(!window.Event){
	var Event = {version: "1.2"};
}

Object.extend(Event, {
	add: function(obj, evType, fn){
		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.attachEvent) obj.attachEvent("on" + evType, fn);
		else if(obj.addEventListener) obj.addEventListener(evType, fn, true);
		else obj["on" + evType] = fn;
	},
	remove: function(obj, evt, func){
		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.detachEvent) obj.detachEvent("on" + evt, func);
		if(obj.removeEventListener) obj.removeEventListener(evt, func, true);
		if(obj["on" + evt]){
			obj["on" + evt] = function(){};
			delete obj["on" + evt];
		}
	}
});

window.addEvent = Event.add;

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";
		}
	}
}

function fts(form) {
    var el = form.length, formObj = {}, postString = "";
    for(var i = 0; i<el; i++){
		if(form[i].name != undefined && form[i].name != "" && formObj[form[i].name] == undefined){
			formObj[form[i].name] = $v(form[i].name, form);
		}
    }
	for(var i in formObj){
        var name = i, val = formObj[i];
        if(val != undefined){
			postString += name + "=" + val + "&";
		}
	}
    return postString.substring(0, postString.length-1);
}
var formToString = fts;


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;
	}
}

var Key = {
	TAB: 9, LINEFEED: 10, ENTER: 13, CARRIAGE: 13, SPACE: 32,
	LEFT: 37,RIGHT: 39, DOWN: 40, UP: 38, ALT: 18, CTRL: 17, SHIFT: 16,
	addListener: function(o){ for(var i in o) document[i] = o[i]; },
	removeListener: function(){ for(var i in o) delete document[i];	}
}

var Mouse = {
	//from http://hartshorne.ca/2006/01/23/javascript_cursor_position/
	getPosition: function(e) {
		e = e || window.event;
		var cursor = {x:0, y:0};
		if (e.pageX || e.pageY) { cursor.x = e.pageX; cursor.y = e.pageY; } 
		else {
			cursor.x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
			cursor.y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) -  document.documentElement.clientTop;
		}
		return cursor;
	},
	addListener: function(o){ for(var i in o) document[i] = o[i]; },
	removeListener: function(){ for(var i in o) delete document[i];	}
}

function chr(n){
	return String.fromCharCode(n) || null;
}

function sleep(s){
	var st = (new Date()).getSeconds(), s = new Number(s);
	while(1){
		var at = (new Date()).getSeconds();
		if((at-st) >= s) break;
	}
}

function getTargetByEvent(e){
	if(typeof(e) == "undefined") var e = window.event;
	source = e.target ? e.target : e.srcElement;
	return source;
}

function swapImage(obj, newSrc, restore){
	if(typeof obj == "string") var obj = $(obj);
	if(restore){
		var f = function(e){
			var e = getTargetByEvent( e || window.event );
			swapImage( e , e.oldSrc , false );
		}
		obj.oldSrc = obj.src;
		Event.remove(obj, "mouseout", f);
		Event.add(obj, "mouseout", f);
	}
	obj.src = newSrc;
}

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;
}

function $fa(n, t, f){
	if(typeof f == "string") var f = document[f];
	var tags = new Array();
	for(var i=0; i<f.childNodes.length; i++){
		var c = f.childNodes[i];
		if(c.nodeName=="INPUT"&&c.type!=undefined&&c.type==t)
			if(c.name.toString().indexOf(n) > -1) tags.push(c);
	}
	return (tags.length > 0 ? tags : null);
}

/* funções de getTextValue e normalizeString by BrainJar.com */
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");
function normalizeString(s) {
	return s.replace(whtSpMult, " ").replace(whtSpEnds, "");
}
function getTextValue(el, f) {
	var i, s = "";
	for (i = 0; i < el.childNodes.length; i++){
		if (el.childNodes[i].nodeType == 3) s += el.childNodes[i].nodeValue;
		else if (el.childNodes[i].nodeType == 1
					&& el.childNodes[i].tagName == "BR") s += " ";
		else s += getTextValue(el.childNodes[i]);
	}
	if(el.childNodes == undefined) s = el.innerHTML;
	return f == true ? s : normalizeString(s);
}

function $v(o, form){
	if(form != undefined && form) {
		var o = (typeof form == "string") ? document[form][o] : form[o];
		if(o.length && o.length > 0 && o.type == undefined && !o.type) o.type = o[0].type;
	} else var o = typeof o == "string" ? $(o) : o;
	if(o == undefined || !o) return false;
	if(o.type && o.type.toString().indexOf("select") > -1){
		if(o.type.toString().indexOf("mult") < 0) return o.options[o.selectedIndex].value;
		else {
			var values = new Array();
			for(var i=0; i<o.options.length; i++) if(o.options[i].selected) values.push(o.options[i].value);
			return values;
		}
	} else if(o.type && o.type.toString().indexOf("radio") > -1){
		for(var i=0; i<o.length; i++) if(o[i].checked) return o[i].value;
	} else if(o.type && o.type.toString().indexOf("check") > -1){
		var values = new Array();
		for(var i=0; i<o.length; i++) if(o[i].checked) values.push(o[i].value);
		return values;
	} else if(o.type) return o.value.toString();
	else return getTextValue(o);
}

var StatusBar = {
	msg: new String(),
	set: function(msg){
		if(msg != undefined && msg.length > 0){
			StatusBar.msg = window.defaultStatus = msg;
		}
	},
	get: function(){
		return window.defaultStatus.toString();
	}
}

function debbug(v, a){
	if(document.getElementById("debbuger") != undefined){
		if(!a || a == undefined) $("debbuger").innerHTML = "";
		$("debbuger").innerHTML += v + (a ? "<BR />" + String.fromCharCode(13) : "");
	}
}

debbug.createObject = function(f){
	if(document.getElementById("debbuger") == undefined){
		this.htmlString = new String('<span id="debbuger" style="padding: 10px; border: 1px solid #000000; display: block;"></span>');
		if(f) this.htmlString = "<div style='position: absolute; width: 400px; z-index: 1000; background: white;'>" + this.htmlString + "</div>";
		document.write( this.htmlString );
	}
}