//    por diego nunes - dnunes[em]gmail.com
//  (cc) Conteúdo sob licença Creative Commons
//http://creativecommons.org/licenses/by-sa/2.0/

function gE(tI) { var d=document, r, n, i;
  if (tI instanceof Array) {
    for (n=tI.length, i=0; i<n; i++) r[r.length]=d.getElementById ? d.getElementById(tI) : d.all ? d.all[tI] : false; return r;
  } else { return (d.getElementById) ? d.getElementById(tI) : (d.all) ? d.all[tI] : false; }
}
function gFETN(tTN, tFs, isER) { //getFilteredElements[by]TagName: [str] TagName, [arr] theFilters, [bool] isERFilter
  var tEs, i, n, ii, nn, tF, tER, tR=new Array(); tEs=document.getElementsByTagName(tTN);
  if (!tFs) { return (tEs.length) ? tEs : false; }
  for (n=tEs.length, i=0; i<n; i++) { passed=1;
    for (nn=tFs.length, ii=0; ii<nn; ii++) { tF=tFs[ii].split('=');
      if (isER) { tER=new RegExp(tF[1], 'g'); if (!tER.test(tEs[i][tF[0]])) passed=0; }
      else { if (tEs[i][tF[0]] != tF[1]) passed=0; }
    } if (passed) { tR[tR.length]=tEs[i]; /* compatível com IE5.1 */ }
  }
  return (tR.length) ? tR : false;
}
function cE(tN, tPs, tC) { /* createElement 3.0 -- <str tagName> [, arr tProperties] [, (str | obj) conteúdo] */
  //Essa função, se chamada com 2 parâmetros, os assumirá como "tN" e "tC", e não "tN" e "tPs".
  if (!document.createElement) return false; var nE, tER, i, n, tP; nE=document.createElement(tN);
  if (arguments.length > 1) { if (arguments.length == 2) { tC=tPs; tPs=[]; }
    if (typeof(tPs) == 'string') { tPs=[tPs]; }
    for (n=tPs.length, i=0; i<n; i++) { tP=tPs[i].split('='); nE[tP[0]]=tP[1]; }
    if (tC) { i=0;
      if (tC instanceof Array) { for (n=tC.length; i<n; i++) { nE.appendChild( (typeof(tC[i])=='string')?cTN(tC[i]):tC[i] ); } }
      else { nE.appendChild( (typeof(tC)=='string')?cTN(tC):tC ); }
    }
  } return nE;
}
function cTN(tV) { /* theValue */ return (tV && document.createTextNode) ? document.createTextNode(tV) : false; }
function rEs(tO) { var i, n; //removeElements. Recebe um objeto ou uma coleção deles, para remover do documento
  if (!tO) { return false; } if (!tO.nodeName) { if (tO.length) for (n=tO.length; n--;) rEs(tO[n]); }
  else tO.parentNode.removeChild(tO);
}

//Bloco baseado nas funções da biblioteca prototype.js - http://prototype.conio.net/
function $A(tE) { var r=[], i; for (i in tE) { r[i]=tE[i]; } return r; }
Function.prototype.bind=function(obj) { var metodo=this, a=$A(arguments), obj=a.shift(); return function() { return metodo.call(obj, a); } }
Function.prototype.bindAsEventListener=function(obj) { var metodo=this; return function(e) { return metodo.call(obj, e); } }

function adEvento(tO, tE, tF, tPs) { //3.1 - <str/arr tObject(s)>, <str tEvent>, <func tFunction> [, arr tParams]
  if (tO instanceof Array) { for (n=tO.length; n>0; ) { if (!adEvento(tO[--n], tE, tF, tPs)) return false; } return true; }
  var tnF=(tPs) ? function(e) { tF.apply(tO, Array(e).concat(tPs)); } : tF;
  /* Iniciar arrays, caso não existam */ if (!tO.eventos) { tO.eventos=[]; } if (!tO.eventos[tE]) { tO.eventos[tE]=[]; }
  tO.eventos[tE][tO.eventos[tE].length]={tE: tE,   tF: tF,   tPs: tPs };
  if (tO.attachEvent) { tO.attachEvent('on'+tE, tnF); return true; }
  if (tO.addEventListener) { tO.addEventListener(tE, tnF, true); return true; }
  /* alert('Erro!'); */ return false;
}
function remEventos(tO, tE, tF, tPs) {
  //Recebendo apenas objeto e nome do evento
  if (arguments.length == 2) { if (!tO.eventos) return false;
    var tCEv; if (!(tE instanceof Array)) tE=[tE];
    for (n=tE.length, i=0; i<n; i++) { tCEv=tE[i]; tCEvs=tO.eventos[tCEv];
      for (nn=tCEvs.length, ii=0; ii<nn; ii++) {
        remEventos(tO, tCEvs[ii]['tE'], tCEvs[ii]['tF'], tCEvs[ii]['tPs'] );
      } tO.eventos[tCEv]=[];
    } return true;
  }
  //Recebendo informações sobre o evento
  var tnF=(tPs) ? function(e) { tF.apply(tO, Array(e).concat(tPs)); } : tF;
  if (document.detachEvent) { tO.detachEvent('on'+ tE, tnF, true); return true; }
  if (tO.removeEventListener) { tO.removeEventListener(tE, tnF, true); return true; }
  tO["on"+ tE] = null;
}
function getSrc(e) { /* event */ return (e.target) ? e.target : (window.event && event.srcElement) ? event.srcElement : false; }
function getKeyCode(e) { /* event */ return (e.which) ? e.which : (window.event && event.keyCode) ? event.keyCode : false; }
function poin(e) { try { e.preventDefault(); } catch(e) { event.returnValue=false; } return false; }
function valorValido(tV) { /* theValue */ return new RegExp('[a-z0-9]','gi').test(tV); }
function RETest(tER, tV, tF) { /* tRegularExpression, tValue, tFlags */ return new RegExp(tER, (tF)?tF:'').test(tV); }
function insertAfter(nN, tN) { /* newNode, tNode */ var tP=tN.parentNode;
  while ((tN=tN.nextSibling) && tN.nodeType != 3) { /* ... */ }
  if (tN) { tP.insertBefore(tN, nN); } else { tP.appendChild(nN); } return tP.childNodes.length;
}
function childsOfType(tO, tT) { /* tParentObject, <string/arr tTag(s)> */ var i, n, tCs, tR=[];
  tT=new RegExp( (tT instanceof Array)?tT.join('|') : tT , 'i');
  for (i=0, n=(tCs=tO.childNodes).length; i<n; i++) {
    if (tCs[i].nodeType==1 && tT.test(tCs[i].nodeName.toLowerCase())) tR[tR.length]=tCs[i];
  } return (tR.length) ? tR:false;
}
function firstParentOfType(tO, tT) { /* <node tObject>, <string/arr tTag(s)> */
  tT=new RegExp( (tT instanceof Array)?tT.join('|') : tT , 'i');
  while (tO && (tO.nodeType != 1 || !tT.test(tO.nodeName.toLowerCase()))) { tO=tO.parentNode; }
  return (tO) ? tO : false;
}
function sW(tS, sS) { /* (Bool) startsWith: theString, subString */
  return (tS && sS && tS.substring(0,sS.toString().length) == sS.toString()) ? true : false;
}
function addTok(tokList, tTok, tDiv) { return (tokList + tDiv + tTok); }
function hasTok(tokList, tTok, tDiv) { var n, i, tToks=tokList.split(tDiv);
  for (n=tToks.length, i=0; i<n; i++) { if (tToks[i]==tTok) { return 1; } } return 0;
}
function remTok(tokList, tTok, tDiv) { var n, i, tRs=[], tToks=tokList.split(tDiv);
  for (n=tToks.length, i=0; i<n; i++) { if (tToks[i]!=tTok) { tRs[tRs.length]=tToks[i]; } } return tRs.length ? tRs.join(tDiv) : [];
}
function getStyle(tO, tS) { /* tObject, tStylename */
  if (window.getComputedStyle) { return window.getComputedStyle(tO, null).getPropertyValue(tS); }
  else if (tO.currentStyle) { var tER=new RegExp('^([^\-]+)\-([a-z])(.*)$');
    tS=tS.replace(tER, '$1') + tS.replace(tER, '$2').toUpperCase() + tS.replace(tER, '$3');
    return tO.currentStyle[tS];
  }
  return false;
}
function round(tN, n) { tP=Math.pow(10, n); return Math.round(tN * tP) /tP; }

//Criação de objeto XMLHTTPRequest cross-browser
function XMLHTTPRequest() { var tXHR=0;
  if (window.XMLHttpRequest) { tXHR=new XMLHttpRequest(); } //Objeto nativo (FF/Safari/Opera7.6+)
  else {
    try { tXHR=new ActiveXObject("Msxml2.XMLHTTP"); } //activeX (IE5.5+/MSXML2+)
    catch(e) {
      try { tXHR=new ActiveXObject("Microsoft.XMLHTTP"); } //activeX (IE5+/MSXML1)
      catch(e) { /* O navegador não tem suporte */ tXHR=false; }
    }
  }
  return tXHR;
}