/*
 * Copyright 2006 AgênciaClick - http://www.agenciaclick.com.br
 * $Date: 2006/11/20 12:12:06 $
 * $Revision: 1.1 $
*/

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

function getElm(id) {
	var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;

    elements.push(element);
  }
  return elements;
}

if (!window.Element) var Element = new Object();

Object.extend(Element, {
	remove: function(element) {
		element = getElm(element);
		element.parentNode.removeChild(element);
	},

	find: function(element, what) {
		element = getElm(element)[what];
		while (element.nodeType != 1) element = element[what];
		return element;
	}
});


//useful array functions
Array.prototype.iterate = function(func){
	for(var i=0;i<this.length;i++) func(this[i], i);
}
if (!Array.prototype.each) Array.prototype.each = Array.prototype.iterate;

function $c(array){
	var nArray = [];
	for (var i=0;i<array.length;i++) nArray.push(array[i]);
	return nArray;
}

/**
 * Retorna true se um objeto existe
 */
function isDef(S) {
	return(eval('typeof('+S+')') != 'undefined' && eval('typeof('+S+')') != 'unknown');
}

/*
 * Retorna as dimensões da página ou janela
 */

function docW() {return(window.innerWidth?window.innerWidth-20:document.body.clientWidth)}
function docH() {return(document.documentElement.scrollHeight)}
function winW() {return(document.body.clientWidth?document.body.clientWidth:document.body.offsetWidth)}
function winH() {return(document.body.clientHeight)}


/*
 * Preload de imagens
 * Cria objeto da seguinte forma:
 * pI('/img/nome.gif') -> nome
 */
function pI(src) {
	obj = src.substring(src.lastIndexOf('/')+1,src.lastIndexOf('.'));
	eval('i' + obj + ' = new Image()');
	eval('i' + obj + '.src = "' + src + '"');
}

/**
 * Função para abrir popup's
 */
function openPopup(u,n,w,h,o,c) {
	var l = t = 18;
	if (c) {
		l = (screen.availWidth-w) / 2;
		t = (screen.availHeight-h) / 2;
	}
	p = window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+',scrollbars='+((o)?o:'1'));
}


// Função utilizada para 'saltar' os campos quando o maxlength é atingido
function DFchangeField(o,e) {
	if(window.event)key=window.event.keyCode;
	else if(e)key=e.which;
	else return true;
	if (key==9||key==2||key==16) return false;
	// Vai
	if(o.value.length==o.maxLength){
  		for(var i=0;i<o.form.length;i++){
	    	if(o.form[i]==o&&o.form[i+1]){
	    		o.form[i+1].focus();
	    		break;
	    	}
		}
	}
	// Volta
	if(o.value.length == 0 && key == 8){
  		for(var i=0;i<o.form.length;i++){
    		if(o.form[i]==o && o.form[i-1]){
    			o.form[i-1].focus();
    			o.form[i-1].value = o.form[i-1].value;
    			break;
    		}
		}
	}
}
