// Encontrando as dimensões da página visível
// Netscape: window.innerHeight (Altura) e window.innerWidth (Largura)
// Internet Explorer: document.body.clientHeight (Altura) e document.body.clientWidth (Largura)
// 
//
// findDOM( objectID, withStyle): Encontra o correspondente endereco do objeto DOM com estilo  (withStyle = 1) ou sem estilo (withStyle =0)
//
// getLivePageHeight(): Encontra a altura visível da página;
// getLivePageWidth() : Encontra a largura visível da página;
// getWidth(objectID) : Encontra a largura do objeto;
// getHeight(objectID): Encontra a altura do objeto;
// getTop(objectID)   : Encontra a posição superior do objeto;
// getLeft(objectID)  : Encontra a posição esquerda do objeto;
// getRight(objectID) : Encontra a posição direita do objeto;
// GetDALttom(objectID): Encontra a posição inferior do objeto; 
// getVisibility(objectID): Obtém o valor visibilidade para o objeto;
// getStyle(objectID, styleName); Obtém o valor de um determinado objeto
// getLayer(objectID): Obtém o valor da camada do objeto, seu "zIndex";
// getObject(evt): Identificando o ID do objeto de onde o evento se originou.
// getXCoord(evt): Encontrando sua localização na tela;
// getYCoord(evt): Encontrando sua localização na tela;
// getClipArray(clipStr): Obtém os valores superior, inferior, esquerda e direita colocando em um vetor;
// getClipTop(objectID): Obtém o valor superior para o recorte;
// getClipRight(objectID): Obtém o valor direita para o recorte;
// getClipLeft(objectID): Obtém o valor esquerda para o recorte;
// getClipBottom(objectID): Encontra a posição inferior do objeto;
// getClipWidth(objectID): Obtém o valor da largura do objeto;
// getClipHeight(objectID): Obtém o valor da altura do objeto;

// setWidth(objectID, newValue) : Modifica a largura do objeto;
// setHeight(objectID, newValue): Modifica a altura do objeto;
// setVisibility(objectID, state): Coloca dinamicamente a visibilidade para o objeto;
// setLayer(objectID, layerNum): Coloca dinamicamente o valor para a camada; state assume os valores "visible" ou "hidden"
// setClip(objectID, clipTop, clipRight, clipBottom, clipLeft): Coloca dinamicamente o recorte para o objeto; clipTop, clipRight, clipBottom, clipLeft: valores, em pixel, para o topo, lado direito, rodape, lado esquerdo.
// setClass(objectID, newClass): Alterando a classe de um objeto;
// setImage(objectID, newImage) : Altera o arquivo de imagem;
// setStyle(objectID, styleName, newVal): Alterando uma definição de CSS;

// moveObjectToX(objectID, x ) // Move o objeto na direção horizontal
// moveObjectToY(objectID, x ) // Move o objeto na direção vertical
// moveObjectTo(objectID, x, y): Movendo Objetos de um ponto a outro;
// moveObjectBy(objectID, deltaX, deltaY): Movendo Objetos por uma distância determinada;
// moveTo(x, y): move a janela para a posicao x e y da tela;
// moveBy(deltaX, deltaY): movimenta a janela pelo incremento deltaX em X e deltaY em Y;
// swapVisibility(objectID): Fazendo objetos aparecerem e desaparecerem;
// swapLayer(objectID): Trocando Posições entre Objetos 3D;

// Identifica o Navegador para obter o DOM correspondente
var isDHTML = isLayers = isAll = isID = false;

function getNavigator() 
{
	if (document.getElementById) 
		isID = isDHTML = true; 
	else {
		if (document.all)
			isAll = isDHTML = true;
		else { 
			browserVersion = parseInt(navigator.appVersion);
			if ((navigator.appName.indexOf('Netscape') != -1) && 
				(browserVersion == 4))
				isLayers = isDHTML = true;
		}
	}
}

getNavigator();

// Encontra o correspondente endereco do objeto DOM (com ou sem estilo)
function findDOM( objectID, withStyle) 
{
	if (withStyle == 1) {
		if (isID) 
			return (document.getElementById(objectID).style); 
		else {
			if (isAll) 
				return (document.all[objectID].style); 
			else if (isLayers)
				return (document.layers[objectID]);
  		}
	} 
	else {
		if (isID) 
			return (document.getElementById(objectID)); 
		else {
			if (isAll) 
				return (document.all[objectID]); 
			else if (isLayers) 
				return (document.layers[objectID]); 
		}
	}
}

// Encontra a altura visível da página
function getLivePageHeight()
{
	if (window.innerHeight != null) 
		return window.innerHeight;
	if (document.body.clientHeight != null) 
		return document.body.clientHeight;
	return null;
}

// Encontra a largura visível da página
function getLivePageWidth()
{
	if (window.innerWidth != null) 
		return window.innerWidth;
	if (document.body.clientWidth != null) 
		return document.body.clientWidth;
	return null;
}

// Encontrando as dimensões de um objeto
// IE 4/5 e NE6: offsetHeight (Altura) e offsetWidth (Largura)
// NE4: clip.height (Altura) e clip.width (largura)
// Encontra a largura do objeto
function getWidth(objectID)
{
	var dom=findDOM(objectID, 0);
	if (dom.offsetWidth) 
		return dom.offsetWidth;
	if (dom.clip.width) 
		return dom.clip.width;
	return null;
}

// Encontra a altura do objeto
function getHeight(objectID)
{
	var dom=findDOM(objectID, 0);
	if (dom.offsetHeight) 
		return dom.offsetHeight;
	if (dom.clip.height) 
		return dom.clip.height;
	return (null);
}


// Encontrando as posições superior, esquerda, inferior e direita de um objeto
// IE 4/5: pixelTop (superior), pixelLeft (esquerda) 
// NE4: top (superior) e left (esquerda)
// NE6: offsetTop (superior), offsetLeft (esquerda)

// Encontra a posição superior do objeto
function getTop(objectID)
{
	var domStyle=findDOM(objectID, 1);
	var dom = findDOM(objectID, 0);
	if (domStyle.top) 
		return domStyle.top;  // NE4
	if (domStyle.pixelTop) 
		return domStyle.pixelTop; // IE4/5
	if (dom.offsetTop) 
		return dom.offsetTop; // NE6
	return null;
}

// Encontra a posição esquerda do objeto
function getLeft(objectID)
{
	var domStyle=findDOM(objectID, 1);
	var dom = findDOM(objectID, 0);
	if (domStyle.left) 
		return domStyle.left;  // NE4
	if (domStyle.pixelLeft) 
		return domStyle.pixelLeft; // IE4/5
	if (dom.offsetLeft) 
		return dom.offsetLeft; // NE6
	return (null);
}

// Encontra a posição direita do objeto
function getRight(objectID){
  var domStyle=findDOM(objectID, 1);
  var dom = findDOM(objectID, 0);
  if (domStyle.left) return (domStyle.left + domStyle.clip.width);  // NE4
  if (domStyle.pixelLeft) return (domStyle.pixelLeft + dom.offsetWidth); // IE4/5
  if (dom.offsetLeft) return (dom.offsetLeft + dom.offsetWidth); // NE6
  return (null);
}

// Encontra a posição inferior do objeto
function GetDALttom(objectID){
  var domStyle=findDOM(objectID, 1);
  var dom = findDOM(objectID, 0);
  if (domStyle.top) return (domStyle.top + domStyle.clip.height);  // NE4
  if (domStyle.pixelTop) return (domStyle.pixelTop + dom.offsetHeight); // IE4/5
  if (dom.offsetTop) return (dom.offsetTop + dom.offsetHeight); // NE6
  return (null);
}

// Encontrando a posição de um objeto 3D
// O IE 4/5 e o Netscape 6 não podem ver o índice z até que ele seja definido dinamicamente.
// Para contornar esse pequeno problema, é preciso utilizar o JavaScript para definir o índice z
// de cada objeto quando a página é carregada.
// Coloca dinamicamente o valor para a camada
function setLayer(objectID, layerNum){
  var domStyle = findDOM(objectID, 1);
  domStyle.zIndex = layerNum;
}

// Obtém o valor da camada do objeto, seu "zIndex"
function getLayer(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.zIndex != null) return domStyle.zIndex;
  return (null);
}

// Encontrando o estado de visibilidade de um objeto
// O IE 4/5 e o Netscape 6 não podem acessar o estado de visibilidade até que ele seja definido 
// dinamicamente.
// Para contornar esse pequeno problema, é preciso utilizar o JavaScript para definir 
// a visibilidade de cada objeto quando a página é carregada.

// Coloca dinamicamente a visibilidade para o objeto
function setVisibility(objectID, state){
  var domStyle = findDOM(objectID, 1);
  domStyle.visibility = state;
}

// Obtém o valor visibilidade para o objeto
function getVisibility(objectID){
  var domStyle=findDOM(objectID, 1);
  if ((domStyle.visibility == 'show') || (domStyle.visibility == 'visible')) 
    return 'visible';
  return 'hidden';
}

// Encontrando a área visível de um objeto (clip)
// O IE 4/5 e o Netscape 6 não podem ler o valor de recorte até que ele seja definido 
// dinamicamente.
// Para contornar esse pequeno problema, é preciso utilizar o JavaScript para definir 
// o recorte de cada objeto quando a página é carregada.
// Coloca dinamicamente o recorte para o objeto
function setClip(objectID, clipTop, clipRight, clipBottom, clipLeft){
  var domStyle = findDOM(objectID, 1);
  if (domStyle.clip.left){
    domStyle.clip.top = clipTop;
    domStyle.clip.right = clipRight;
    domStyle.clip.bottom = clipBottom;
    domStyle.clip.left = clipLeft;   
  }
  else domStyle.clip = 'rect(' + clipTop + ' ' + clipRight + ' ' + clipBottom + ' ' + clipLeft + ')';
  
  alert( "clip=" + domStyle.clip);
  setVisibility( objectID, 'visible' );
}

// Obtém o valor superior para o recorte
function getClipTop(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.top) return domStyle.clip.top;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[0]);
  }
  return null;
}

// Obtém o valor direita para o recorte
function getClipRight(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.right) return domStyle.clip.right;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[1]);
  }
  return null;
}

// Obtém o valor esquerda para o recorte
function getClipLeft(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.left) return domStyle.clip.left;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[2]);
  }
  return null;
}

// Obtém o valor inferior para o recorte
function getClipBottom(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.bottom) return domStyle.clip.bottom;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[3]);
  }
  return null;
}


// Obtém o valor da largura do objeto
function getClipWidth(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.width) return domStyle.clip.width;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[1] - clip[3]);
  }
  return null;
}

// Obtém o valor da altura do objeto
function getClipHeight(objectID){
  var domStyle=findDOM(objectID, 1);
  if (domStyle.clip.height) return domStyle.clip.height;
  if (domStyle.clip != null){
    var clip = getClipArray(domStyle.clip);
    return (clip[2] - clip[0]);
  }
  return null;
}


// Obtém os valores superior, inferior, esquerda e direita colocando em um vetor
function getClipArray(clipStr){
  var clip=new Array();
  var i;
  i = clipStr.indexOf('(');
  clip[0] = parseInt(clipStr.substring(i + 1, clipStr.length), 10);
  i = clipStr.indexOf(' ', i+1);
  clip[1] = parseInt(clipStr.substring(i + 1, clipStr.length), 10);
  i = clipStr.indexOf(' ', i+1);
  clip[2] = parseInt(clipStr.substring(i + 1, clipStr.length), 10);
  i = clipStr.indexOf(' ', i+1);
  clip[3] = parseInt(clipStr.substring(i + 1, clipStr.length), 10);
  return clip;
}

function setWidth(objectID, newValue) {
	var dom = findDOM( objectID, 1 );
	dom.width = newValue;
}

function setHeight(objectID, newValue) {
	var dom = findDOM( objectID, 1 );
	dom.height = newValue;
}

// Fazendo objetos aparecerem e desaparecerem
function swapVisibility(objectID){
  var dom = findDOM(objectID, 1);
  state = dom.visibility;
  if (state == 'hidden' || state == 'hide') dom.visibility = 'visible'
  else {
    if (state == 'visible' || state == 'show') dom.visibility = 'hidden';
    else dom.visibility = 'visible';
  }
}

// Movendo Objetos de um ponto a outro
function moveObjectTo(objectID, x, y)
{
	var domStyle = findDOM(objectID, 1);
	domStyle.left = x; 
	domStyle.top = y;
}


// Move o objeto na direção horizontal
function moveObjectToX(objectID, x ) {
	var domStyle = findDOM( objectID, 1 );
	domStyle.left = x;
}

// Move o objeto na direção vertical
function moveObjectToY(objectID, y ) 
{
	var domStyle = findDOM( objectID, 1 );
	domStyle.top = y;
}

// Movimentando Objetos em 3D
// Não esquecer de carregar os valores para as camadas no onLoad 
var prevObjectID = null;
var prevLayer = 0;
function swapLayer(objectID){
  if (prevObjectID != null)
    setLayer( prevObjectID, prevLayer);
  prevLayer = getLayer(objectID);
  prevObjectID = objectID;
  setLayer(objectID, 1000);
  alert("swapLayer");
}

// Encontrando sua localização na tela
// Todos os eventos de uma janela de broser geram determinadas informações sobre o que ocorreu,
// onde e como ocorreu. Estas informações podem ser passadas diretamente para uma função
// Javascript para que ela possa acessar o objeto sem ter que criar um DOM compatível com 
// Browsers diferentes.
function getXCoord(evt){
  if (evt.x) return evt.x;   // IE
  if (evt.pageX) return evt.pageX;  // Netscape
}

function getYCoord(evt){
  if (evt.y) return evt.y;   // IE
  if (evt.pageY) return evt.pageY;  // Netscape
}

// Identificando o ID do objeto de onde o evento se originou. 
var name=null
function getObject(evt){
  if (document.layers){
    var testObj;
    var xPos = evt.pageX;
    var yPos = evt.pageY;
    for( var i = document.layers.length -1; i>=0; i--){
      testObj = document.layers[i]
      if ((xPos > testObj.left) && 
          (xPos < testObj.left + testObj.clip.width) &&
          (yPos > testObj.top) &&
          (yPos < testObj.top + testObj.clip.height)){
        objetcID = testObj.name;
        return objectID; 
      }
    }
  } else {
    objectID = evt.target.name;
    return objectID;
  } 
}

// Alterando uma definição de CSS
// A convenção de nomeação em JavaScript para as definições CSS são:
// - Não podem incluir pontos, hífens, espaços ou qualquer outro separador
// - Todas as letras aparecem em minúsculas, exceto pela primeira letra de todas as palavras
//   que vêm depois da primeira palavra
//   Exemplo: font-size seria expressa por fontSize
function setStyle(objectID, styleName, newVal){
  var domStyle=findDOM(objectID, 1);
  domStyle[styleName] = newVal;
}

// Alterando a classe de um objeto
// Desta forma pode-se alterar mais de uma definição CSS ao mesmo tempo para uma dada classe
function setClass(objectID, newClass){
  var dom=findDOM(objectID, 0);
  dom.className=newClass;
}

// Obtendo a nome do tipo do estilo de um objeto
function getStyle(objectID, styleName) {
	var domStyle = findDOM(objectID, 1 );
	return domStyle[styleName];
}

// Movendo Objetos por uma distância determinada
function moveObjectBy(objectID, deltaX, deltaY){
  var domStyle = findDOM(objectID, 1);
  var dom = findDOM(objectID, 0);
  if (domStyle.pixelLeft) { domStyle.pixelLeft += deltaX; domStyle.pixelTop += deltaY; }
  else {
    if (dom.offsetLeft != null){
      var plusLeft = dom.offsetLeft;
      var plusTop = dom.offsetTop;
      domStyle.left = deltaX + plusLeft;
      domStyle.top = deltaY + plusTop;
    } else dom.moveBy( deltaX, deltaY );
  }
}

function setImage( objectID, newImage ) {
	var dom = findDOM( objectID, 0 );
	dom.src = newImage;
}