﻿function classebase() {
}
	
function attachevent(pObjControle, pStrEvento, pFuncao) {
	if (window.addEventListener)
		pObjControle.addEventListener(pStrEvento, pFuncao, true);
	else
		pObjControle.attachEvent("on" + pStrEvento, pFuncao);
}

function tamanhojanela() {
	var lTamanho = new this.classebase();
	if (document.all) {
		lTamanho.w = document.body.clientWidth;
		lTamanho.h = document.body.clientHeight;
	} else {
		lTamanho.w = window.innerWidth;
		lTamanho.h = window.innerHeight;
	}
	return lTamanho;
}
	
function posicaocontrole(pCtlInicial) {
	var lPosControle = new classebase();
	lPosControle.x = 0;
	lPosControle.y = 0;
	for (var lObjObjeto = pCtlInicial; lObjObjeto; lObjObjeto = lObjObjeto.offsetParent) {
		lPosControle.x += lObjObjeto.offsetLeft;
		lPosControle.y += lObjObjeto.offsetTop;
	}
	lPosControle.w = pCtlInicial.offsetWidth;
	lPosControle.h = pCtlInicial.offsetHeight;
	return lPosControle;
}
	
function controlenovaposicao(pPosPosicionar, pPosReferencia, pBytHor, pBytVer, pBolPosInverterX, pBolPosInverterY) {
   var lTamJanela = tamanhojanela();
   var lPos = new classebase();
	// Horizontal
	switch (pBytHor) {
		case 0:
			lPos.x = pPosReferencia.x;
           if (pBolPosInverterX)
			    lPos.x -= pPosPosicionar.w;
           break;
		case -1:
			lPos.x = pPosReferencia.x + pPosReferencia.w;
           if (pBolPosInverterX)
			    lPos.x -= pPosPosicionar.w;
           break;
       case 2:
			lPos.x = (pPosReferencia.x + (pPosReferencia.w / 2)) - (pPosPosicionar.w / 2);
           break;
       case 3:
		    lPos.x = (lTamJanela.w / 2) - (pPosPosicionar.w / 2);
           break;
   }
	// Vertical
   switch (pBytVer) {
       case 0:
			lPos.y = pPosReferencia.y;
           if (pBolPosInverterY)
			    lPos.y -= pPosPosicionar.h;
           break;
       case -1:
			lPos.y = pPosReferencia.y + pPosReferencia.h;
           if (pBolPosInverterY)
			    lPos.y -= pPosPosicionar.h;
           break;
       case 2:
			lPos.y = (pPosReferencia.y + (pPosReferencia.h / 2)) - (pPosPosicionar.h / 2);
           break;
       case 3:
			lPos.y = (lTamJanela.h / 2) - (pPosPosicionar.h / 2);
           break;
	}
   return lPos;
}

function animacao(pstrnome, pctldiv, pctdtable) {
    this.nome = pstrnome;
    this.div = document.getElementById(pctldiv);
    this.tabela = document.getElementById(pctdtable);
    this.tamanho = posicaocontrole(this.tabela);
    this.aniabrir = new OAT.AnimationSize(pctldiv, { width: this.tabela.offsetWidth, height: this.tabela.offsetHeight, delay: 0, speed: 25 });
    this.anifechar = new OAT.AnimationSize(pctldiv, { width: 1, height: 1, delay: 0, speed: 25 });
    
    // inicia animador
    this.tabela.style.visibility = "hidden";
    this.tabela.style.display = "none";
    this.div.style.visibility = "hidden";
    this.div.style.position = "absolute";
    this.div.style.width = "1px";
    this.div.style.height = "1px";
    eval("OAT.MSG.attach(this.aniabrir.animation, OAT.MSG.ANIMATION_STOP, function() { " +
       "" + pstrnome + ".tabela.style.visibility = \"visible\"; " +
       "" + pstrnome + ".tabela.style.display = \"block\"; " +
    "});");
    
    eval("OAT.MSG.attach(this.anifechar.animation, OAT.MSG.ANIMATION_STOP, function() { " +
       "" + pstrnome + ".div.style.visibility = \"hidden\";" +
    "});");
    
    // cancela fechamento
    this.fecharcancelar = function() {
        if (this.timer) {
            window.clearTimeout(this.timer);
            this.timer = null;
        }
    }
    
    // abrir caixa
    this.abrir = function(pctlpos) {
        this.fecharcancelar();
        var lPosControle = posicaocontrole(pctlpos);
        this.div.style.left = lPosControle.x;
        this.div.style.top = lPosControle.y + lPosControle.h;
        this.aniabrir.start();
    }
    
    // fechar caixa
    this.fechar = function() {
        this.fecharcancelar();
        this.tabela.style.visibility = "hidden";
        this.tabela.style.display = "none";
        this.anifechar.start();
    }
    
    // inicia fechamento
    this.fechariniciar = function() {
        this.fecharcancelar();
        eval("this.timer = window.setTimeout(function() { " + this.nome + ".fechar(); }, 50);");
    }
}

function menupopup(pstrnome, pctlcontrole) {
    this.nome = pstrnome;
    this.controle = document.getElementById(pctlcontrole);
    this.controle.position = "absolute";
    this.controle.style.visibility = "hidden";
    this.controle.style.display = "none";
    
    // cancela fechamento
    this.fecharcancelar = function() {
        if (this.timer) {
            window.clearTimeout(this.timer);
            this.timer = null;
        }
    }

    // abrir caixa
    this.abrir = function(pctlref) {
        var lposref = posicaocontrole(pctlref);
        this.fecharcancelar();
        this.controle.style.left = lposref.x - 10;
        this.controle.style.top = lposref.y + lposref.h;
        this.controle.style.visibility = "visible";
        this.controle.style.display = "block";
    }

    // fechar caixa
    this.fechar = function() {
        this.fecharcancelar();
        this.controle.style.visibility = "hidden";
        this.controle.style.display = "none";
    }

    // inicia fechamento
    this.fechariniciar = function() {
        this.fecharcancelar();
        eval("this.timer = window.setTimeout(function() { " + this.nome + ".fechar(); }, 50);");
    }
}

