var W3CDOM = (document.createElement && document.getElementsByTagName && document.getElementById && Array.prototype.push);
var is_ie = (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent));
var is_ie5 = (is_ie && /msie 5\.0/i.test(navigator.userAgent));
var is_opera = /opera/i.test(navigator.userAgent);
var is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);

String.prototype.find=function(what){
	return(this.indexOf(what)>=0 ? true : false);
};

Array.prototype.each = function(f){
	for(var i=0;i<this.length;i++){
		f(this[i]);
	}
};

function CreateEl(x){
	return(document.createElement(x));
};

function $(selector){
	var i;
	var j;
	var selid = "";
	var selclass = "";
	var tag = selector;
	var tag2 = "";
	var v2;
	var k;
	var vf;
	var a;
	var s = [];
	var objlist = [];
	var c;
	if(selector.find("#")){
		if(selector.find(" ")){
			s = selector.split(" ");
			var fs = s[0].split("#");
			if(fs.length == 1)
				return(objlist);
			f = $$(fs[1]);
			if(f){
				v = f.getElementsByTagName(s[1]);
				for(i=0;i<v.length;i++)
					objlist.push(v[i]);
			};
			return(objlist);
		} else {
			s = selector.split("#");
			tag = s[0];
			selid = s[1];
			if(selid!=""){
				f = $$(selid);
				if(f)
					objlist.push(f);
				return(objlist);
			}
		}
	};
	if(selector.find(".")){
		s = selector.split(".");
		tag = s[0];
		selclass = s[1];
		if(selclass.find(" ")){
			s = selclass.split(" ");
			selclass = s[0];
			tag2 = s[1];
		}
	};
	var v = document.getElementsByTagName(tag);
	if(selclass==""){
		for(i=0;i<v.length;i++)
			objlist.push(v[i]);
		return(objlist);
	};
	for(i=0;i<v.length;i++){
		c = v[i].className.split(" ");
		for(j=0;j<c.length;j++){
			if(c[j] == selclass){
				if(tag2 == "")
					objlist.push(v[i]);
				else{
					v2 = v[i].getElementsByTagName(tag2);
					for(k=0;k<v2.length;k++)
						objlist.push(v2[k]);
				}
			}
		}
	};
	return(objlist);
};

function $$(id){
	var i;
	var elm;
	var arr;
	if(arguments.length>1){
		arr = [];
		for(i=0;i<arguments.length;i++)
			arr.push(arguments[i]);
	};
	if(id instanceof Array){
		arr = id;
	};
	if(arr instanceof Array){
		elm = [];
		for(i=0;i<arr.length;i++)
			elm[i] = document.getElementById(arr[i]);
	} else {
		elm = document.getElementById(id);
	};
	return elm;
};

function addEvent(elm, evType, fn, useCapture) {
	if(elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if(elm.attachEvent){
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
	};
};

function remEvent(elm,evType,fn){
	if(document.detachEvent){
		elm.detachEvent('on'+evType,fn,true);
		return true;
	};
	if(elm.removeEventListener){
		elm.removeEventListener(evType,fn,true);
		return true;
	};
};

function addClass(elm,classe){
	if(!elm.className.find(classe))
		elm.className += " "+classe;
};

function remClass(elm,classe){
	elm.className = elm.className.replace(classe,"");
};

Array.prototype.inArray = function (value) {
	var i;
	for(i=0;i<this.length;i++){
		if(this[i] === value){
			return true;
		};
	};
	return false;
};

function addExtLinks() {
    if(W3CDOM){
        var links = $('a.externo');
		var i;
		var link;
        for(i=0;i<links.length;i++){
            link = links[i];
            if(link.getAttribute("href")){
                link.target='_blank';
                link.title += ' (Este link abre uma nova janela)';
            };
        };
    };
};

addEvent(window,"load",addExtLinks);

function $newTN(cont){
	if(typeof cont=="string"){
		return document.createTextNode(cont);
	} else {
		return false;
	};
};

function $new(elm,attr,cont){
	var i;
	var novo = CreateEl(elm);
	var attrs;
	if(typeof cont=="undefined"){
		cont = attr;
		attr=null;
	};
	if(attr instanceof Array){
		for(i=0;i<attr.length&&(attrs=attr[i].split("="));i++){
			novo[attrs[0]] = attrs[1];
		};
	} else {
		if(typeof attr=="string"&&(attrs=attr.split("="))){
			novo[attrs[0]] = (attrs.length==2) ? attrs[1] : "";
		};
	};
	if(cont instanceof Array){
		for(i=0;i<cont.length;i++){
			(typeof(cont[i])=="string") ? $append($newTN(cont[i]),novo) : $append(cont[i],novo);
		};
	} else {
		$append(cont,novo);
	};
	return novo;
}

function $before(elm,pai){
	return pai.parentNode.insertBefore(elm,pai);
};

function $after(elm,pai){
	return pai.parentNode.insertBefore(elm,pai.nextSibling);
};

function $replace(novo,old){
	if(old.parentNode){
		return old.parentNode.replaceChild(novo,old);
	} else {
		return false;
	};
};

function $append(elm,pai){
	var i;
	if(typeof pai=="undefined"){
		pai=document.body;
	};
	if(elm==""||elm==null){
		return true;
	};
	if(elm instanceof Array){
		for(i=0;i<elm.length;i++){
			$append(elm[i],pai);
		};
	} else {
		if(typeof elm=="string"){
			pai.appendChild($newTN(elm));
		} else {
			pai.appendChild(elm);
		};
	};
	return pai.childNodes.length;
};

function $remove(elm){
	if(elm && elm.parentNode){
		elm.parentNode.removeChild(elm);
	};
};

function getSrc(_1){
	if(typeof _1=="undefined"){
		_1=window.event;
	};
	var _2 = _1.target ? _1.target : _1.srcElement;
	if(_2.nodeType == 3){
		_2 = _2.parentNode;
	};
	return _2;
};

function cEvt(_1){
	try{
		_1.preventDefault();
	} catch(e){
		event.returnValue = false;
	};
	return false;
}

function getPosition(e){
    e = e||window.event;
	var arrPos = {x:0,y:0};
    if(e.pageX||e.pageY){
		arrPos.x=e.pageX;
		arrPos.y=e.pageY;
	} else {
        arrPos.x=e.clientX+(document.documentElement.scrollLeft||document.body.scrollLeft)-document.documentElement.clientLeft;
        arrPos.y=e.clientY+(document.documentElement.scrollTop||document.body.scrollTop)-document.documentElement.clientTop;
    };
    return arrPos;
};

function getPageScroll(){
	var yScroll = (self.pageYOffset) ? self.pageYOffset : (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	return arr = ['',yScroll];
};

function getPageSize(){
	var xScroll = (document.body.scrollWidth) ? document.body.scrollWidth : document.body.offsetWidth;
	var yScroll = (window.innerHeight + window.scrollMaxY) ? window.innerHeight + window.scrollMaxY : (document.body.scrollHeight) ? document.body.scrollHeight : document.body.offsetHeight;
	var windowWidth = (self.innerWidth) ? self.innerWidth : (document.body.clientWidth) ? document.body.clientWidth : document.documentElement.clientWidth;
	var windowHeight = (self.innerHeight) ? self.innerHeight : (document.body.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
	var pageHeight = (yScroll < windowHeight) ? windowHeight : yScroll;
	var pageWidth = (xScroll < windowWidth) ? windowWidth : xScroll;
	return arr = [pageWidth,pageHeight,windowWidth,windowHeight];
}

