function $(i){return document.getElementById(i)}
function $del(o){o.parentNode.removeChild(o)}
function $replace(n,o){o.parentNode.replaceChild(n,o)}
function $before(n,o){o.parentNode.insertBefore(n,o)}
function $new(t){return document.createElement(t)}
function $tags(t){return document.getElementsByTagName(t)}

/** Adiciona um trim() ao objeto String. **/
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };

function $exchange(a,b) {
	var tmp = document.createTextNode( "" );
	$replace(tmp,b);
	$replace(b,a);
	$replace(a,tmp);
}

//Adiciona um evento ao onload de forma nao-obstrusiva
function addInit(f){
 //Para os bons navegadores
 if(window.addEventListener)window.addEventListener("load", f, true)
 //Para o IE
 if(window.attachEvent)window.attachEvent("onload", f)
}

function addUnload( f ) {
  if ( window.addEventListener ) {
    window.addEventListener( "unload", f, false );
  } else if ( window.attachEvent ) {
    window.attachEvent( "onunload", f );
  } else {
    window.onunload = this.flush;
  }
}

/**
 * Simple way to add events to trigger after the page has loaded. 
 * This of course attaches all your events to the onload event handler 
 * which some still see as necessary, 
 * nevertheless it does exactly what it?s supposed to, and does it well.
 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * Retorna todos os filhos diretos de "tag" que possuam o nome "childTagName".
 * Usado, por exemplo, para recuperar uma lista de divs dentro de um certo
 * objeto: $children( myNode, "div" );
 */
function $children( tag, childTagName ) {
  var result = new Array();
  for ( var i = 0; i < tag.childNodes.length; i++ ) {
    var child = tag.childNodes[i];
    if ( ( child.nodeType == 1 ) && ( child.tagName.toUpperCase() == childTagName.toUpperCase() ) ) {
      result.push( tag.childNodes[i] );
    }
  }
  return result;
}

/**
 * Retorna o primeiro elemento cuja classe CSS eh a especificada. Este
 * metodo percorre a arvore DOM inteira a partir do no especificado,
 * nao apenas os seus filhos diretos.
 * 
 * @param {Object} tag
 * @param {Object} className
 */
function $tagWithClass( tag, className ) {
	for ( var c = tag.firstChild; c != null; c = c.nextSibling ) {
		if ( c.nodeType != 1 ) continue; // ignora nos que nao sao tags
		if ( c.className == className ) return c; // encontrou esta mesmo
		var r = $tagWithClass( c, className ); // recursao
		if ( r != null ) return r; // se retornou algo, propaga
	}
	return null; // chega aqui se nao encontrou sob esta tag
}

function $fixRange( x, min, max ) {
  if ( x < min ) return min;
  if ( x > max ) return max;
  return x;
}

function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  var result = { left: curleft, top: curtop };
  result.toString = function() { return this.left + ":" + this.top };
  return result;
}

function findRelativeBox( element, reference ) {
  var ePos = findPos( element );
  var refPos = findPos( reference );
  
  var result = { 
    left  : ePos.left - refPos.left,
    top   : ePos.top  - refPos.top,
    width : element.offsetWidth,
    height: element.offsetHeight,
    bottom: ePos.top - refPos.top + element.offsetHeight,
    right : ePos.left - refPos.left + element.offsetWidth,
    centerTop : ePos.top - refPos.top + ( element.offsetHeight / 2 ),
    centerLeft: ePos.left - refPos.left + ( element.offsetWidth / 2 ),
		toString : function() {
			return this.left + ":" + this.top + ", " + this.width + "x" + this.height + 
				", center " + this.centerLeft + ":" + this.centerTop;
		}
  }
  
  result.toString = function() { 
    return this.left + ":" + this.top + " --- " + this.width + "x" + this.height; 
  }
  
  return result;
}

/** Utilit?rios para cookies **/
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

function logado(){
	return (readCookie("pst_login")=="S" && readCookie("usuario")!="" && readCookie("usuario")+""!="null");
} 

