/**
 *  ----------------------------------------------------------------------  *
 *  arquivo: appWeb.class.js
 *  versão: 1.0
 *  revisão: 018
 *  data criação: 12/11/2007
 *  data atualização: 20/03/2008
 *  dependência: ECMAscript 1.0
 *  categoria: Biblioteca de Classe JS  
 *  modulo: CORE
 *  since: 0.0.001
 *  autor: Ricardo Othuki    <ricardoothuki@realweb.com.br>
 *  copyleft: Realweb Soluções  2007-2008
 *  @versao 
 *  ----------------------------------------------------------------------  *
 */


classTeste = {
	versao: "1.0",
	setAttr: function()
	{},
	getAttr: function()
	{}
}
 
/**
 *  ----------------------------------------------------------------------  *
 *  ClassAjax()
 *  ----------------------------------------------------------------------  *
 *  Classe para chamada RPC - AJAX
 *
 */
 
function classAjax( url, processa, metodo, modo, params )
{
  if( arguments.length == 0 )
  {
    this.url = '';
    this.metodo = 'GET';
    this.params = null;
    this.processaResultado = '';
    this.modo = 'T';
  }else
  {
    this.url = url;
    this.metodo = (metodo) ? metodo : 'POST';
    this.params = params;
    this.processaResultado = processa;
    this.modo = (modo) ? modo : 'T';
  }
  this.handleObj = null;
  
  if( this.modo != 'T' && this.modo != 'X' )
  {
    this.modo = 'T';
  }// End
  
  //this.conectar();
}// End Class

classAjax.prototype = 
{
  setObj:
  function( _nodeObj )
  {
    this.handleObj = _nodeObj;
  },
  setURL:
  function( _url )
  {
    this.url = _url;
  },
	setMetodo:
	function( _mtd_ )
	{
		this.metodo = (_mtd_ ) ? _mtd_: 'POST';
	},
	setParams:
	function( _params )
	{
		this.params = _params;
	},
	setReturnTo:
	function( _nFunc )
	{
    this.processaResultado = _nFunc;
	},
  conectar: 
  function()
  {
    if( ( this.url == undefined || this.url == '' ) || 
        ( this.processaResultado == undefined ) )
    {
      return;
    }// End
    this.httpRequest = null;
//alert(this.url);    
    if( window.XMLHttpRequest ) // mozilla, safari
    {
      this.httpRequest = new XMLHttpRequest();
    }else if( window.ActiveXObject )  // IE
    {
      try
      {
        this.httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }catch ( e )
      {
        try
        {
          this.httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch ( e ) 
        {
        
        }// End 
      }// End
    }// End
    
    if( this.httpRequest != null && this.httpRequest != undefined )
    {
      var obj = this;
      this.httpRequest.onreadystatechange = 
      function() 
      {
        obj.processaRetorno.call(obj);
      }
      if( this.metodo == 'POST' )
      {
        var _url_ = this.url + '?_rnd=' + Math.random();
      }else
      {
        var _url_ = this.url + '?_rnd=' + Math.random() + '&_par=' + this.params;
        this.params = null;
      }// End if()
//alert(_url_);      
      this.httpRequest.open( this.metodo, _url_, true );
      this.httpRequest.send( this.params );
    }// End 
  },
  processaRetorno: 
  function()
  {
    if( this.httpRequest.readyState == 4 )
    {
      if( this.httpRequest.status == 200 )
      {
        var resp = ( this.modo == 'T' )? this.httpRequest.responseText:
                                         this.httpRequest.responseXML;
        if( this.processaResultado != null )
        {
          this.processaResultado( resp );
        }else
        {
          document.write( resp );
        }
      }else
      {
        this.processaErro();
      }
    }
  },
  processaErro: 
  function()
  {
//    alert( this.httpRequest.status + '-' + this.httpRequest.statusText + ' :-> ' + this.url );
  }
}// End Metodos

 
/**
 *  ----------------------------------------------------------------------  *
 *  classEsquema()
 *  ----------------------------------------------------------------------  *
 *  esta classe detecta e armazena a configuração do Browser que
 *  esta rodando a aplicação Client
 *  ----------------------------------------------------------------------  *
 */
function classClient()
{
  this.browser  = "W3C";
  this.isDHTML  = false;
  this.nSO      = 'outros';
  this.total_X  = screen.width;
  this.total_Y  = screen.height;
  
  this.display_X = screen.availWidth;
  this.display_Y = screen.availHeight;
  this.language  = navigator.language ? navigator.language : navigator.userLanguage;
  
  
/**
 *  ----------------------------------------------------------------------  *
 *  define o construtor da Classe
 *  ----------------------------------------------------------------------  *
 */
  this.construtor = function()
  {
    this.getBrowser();
    this.getSO();
    this.getDinamic();
  }
  this.construtor();
}

/**
 *  ----------------------------------------------------------------------  *
 *  Define os Metodos Públicos da Classe
 *  ----------------------------------------------------------------------  *
 */
classClient.prototype = 
{
  getBrowser:
  function()
  {
//  define a familia do Browser  
    if( navigator.userAgent.indexOf( 'MSIE' ) != -1 )
    {
      this.browser = "MSIE";
    }else if( navigator.userAgent.indexOf( 'Firefox' ) != -1 )
    {
      this.browser = "FF";
    }// End 
  },
  getSO:
  function()
  {
//  define a familia do sistema operacional
    if( navigator.appVersion.indexOf( 'Win' ) != -1 )
    {
      this.nSO = 'Win';
    }else if( navigator.appVersion.indexOf( 'Linux' ) != -1 )
    {
      this.nSO = 'Linux';
    }else if( navigator.appVersion.indexOf( 'MAC' ) != -1 )
    {
      this.nSO = 'MAC';
    }// End 
  },
  getDinamic:
  function()
  {
//  define se o browser  é DHTML
    if( document.getElementById )
    {
      this.isDHTML = "isID";
    }else
    {
      if (document.all)
      {
        this.isDHTML = "isAll";
      }// End 
    }// End 
  }  
}
 
/**
 *  ----------------------------------------------------------------------  *
 *  classEsquema()
 *  ----------------------------------------------------------------------  *
 *  esta classe define o esquema de cores da aplicação
 *  ----------------------------------------------------------------------  *
 */
function classEsquema()
{
  this.ActiveBorder      = 'ActiveBorder';
  this.ActiveCaption     = 'ActiveCaption';
  this.AppWorkSpace      = 'AppWorkSpace';
  this.BackGround        = 'BackGround';
  this.ButtonFace        = 'ButtonFace';
  this.ButtonHighlight   = 'ButtonHighlight';
  this.ButtonShadow      = 'ButtonShadow';
  this.ButtonText        = 'ButtonText';
  this.CaptionText       = 'CaptionText';
  this.GrayText          = 'GrayText';
  this.Highlight         = 'Highlight';
  this.HighlightText     = 'HighlightText';
  this.InactiveBorder    = 'InactiveBorder';
  this.InactiveCaption   = 'InactiveCaption';
  this.InfoBackground    = 'InfoBackground';
  this.InfoText          = 'InfoText';
  this.Menu              = 'Menu';
  this.MenuText          = 'MenuText';
  this.Scrollbar         = 'Scrollbar';
  this.ThreeDDarkShadow  = 'ThreeDDarkShadow';
  this.ThreeDFace        = 'ThreeDFace';
  this.ThreeDHighlight   = 'ThreeDHighlight';
  this.ThreeDLightShadow = 'ThreeDLightShadow';
  this.ThreeShadow       = 'ThreeShadow';
  this.Window            = 'Window';
  this.WindowText        = 'WindowText';
  this.FontFamily        = 'verdana, tahoma, "Courier New", times';
  this.FontSize          = '12';
}// End 

//  extende a Classe Client
classEsquema.prototype = new classClient;


/**
 *  ----------------------------------------------------------------------  *
 *  Definição da Classe
 *  ----------------------------------------------------------------------  *
 */
function classAppWeb( appName )
{
/**
 *  ----------------------------------------------------------------------  *
 *  atributos privados
 *  ----------------------------------------------------------------------  *
 */
	var varTeste = 1;
  
/**
 *  ----------------------------------------------------------------------  *
 *  atributos públicos
 *  ----------------------------------------------------------------------  *
 */
  this.nome    = appName;
  this.modelo  = 'vista';
  this.esquema = 'system';

  this.widthPage   = 800;
  this.heghtPage   = 600;
  
  this.zIndex      = 200;
  this.zIndexModal = 1000;
  
/**
 *  ----------------------------------------------------------------------  *
 *  controle de Projeto
 *  ----------------------------------------------------------------------  *
 */
  this.style   = new Array();
  this.libCORE = new Array();
  this.libUSER = new Array();
  this.lib     = new Array();
  this.objetos = new Array();
  this.projeto = new Array();
  
/**
 *  ----------------------------------------------------------------------  *
 *  sistema de identificação do Client
 *  ----------------------------------------------------------------------  *
 */
  this.uID     = new Object();
  this.IP      = new Object();
  this.DNA     = new Object();
  
/**
 *  ----------------------------------------------------------------------  *
 *  sistema de controle de Acesso
 *  ----------------------------------------------------------------------  *
 */
  this.loginUsr       = new Object();
  this.codigoUsr      = new Object();
  this.nameUsr        = new Object();
  this.nivelAcessoUsr = new Object();
  
/**
 *  ----------------------------------------------------------------------  *
 *  diretorios do sistema
 *  ----------------------------------------------------------------------  *
 */
  this.dirRoot      = '';
  this.dirLib       = 'core/libs/';
  this.dirImage     = 'images/';
  this.dirStyle     = 'styles/';
  this.dirAppServer = 'core/';
  
/**
 *  ----------------------------------------------------------------------  *
 *  Definição do tipo de moeda utilizada
 *  ----------------------------------------------------------------------  *
 */
  this.moeda = new Array();
  this.moeda[ 'nome' ]    = 'US';
  this.moeda[ 'simbolo' ] = '$';
  this.moeda[ 'milhar' ]  = '.';
  this.moeda[ 'decimal' ] = ',';
  
/**
 *  ----------------------------------------------------------------------  *
 *  inicializa os container para imagens
 *  ----------------------------------------------------------------------  *
 */
  this.gaugeImg = new Image();
  this.logoImg  = new Image();
  this.bgModal  = new Image();
  
/**
 *  ----------------------------------------------------------------------  *
 *  variáveis de controle interno do CORE
 *  ----------------------------------------------------------------------  *
 */
  this.nodeHead   = null;
  this.nodeBody   = null;
  this.nodePage   = null;
  this.nodeSplash = null;
  this.nodeModal  = null;
  this.objModal   = null;
  this.nodeAlert  = null;
  
  this.objMovDOM = null;    // controle de Drag & Drop do Core
  this.posX      = null;
  this.posY      = null;

  this.ajax       = null;
  this.exec       = false;
  this.CORE_ok    = false;
  this.startLib   = null;
  this.showSplash = false;
  
/**
 *  ----------------------------------------------------------------------  *
 *  Inicialização dos Parametros do controle dinâmico de carga (CLdo)
 *  ----------------------------------------------------------------------  *
 *
 *  nWait       = tempo de espera para uma nova tentativa (em milisegundos)
 *  nTentativas = numero maximo de tentativas antes de disparar a 
 *                rotina de tratamento de erros do CORE
 *  nTrecover   = numero maximo de tentativas de recuperação
 *                da carga dinamica de um arquivo
 *  nV          = utilizada para interação com o numero de tentativas
 *  loopError   = referencia a setInterval do metodo para recuperação
 *                de erros de garga dinamica
 *  ----------------------------------------------------------------------  *
 */
  this.nWait       = 50;
  this.nTentativas = 10; 
  this.nTrecover   = 3; 
  this.nV          = 0;
  this.nVrecover   = 0;
  this.loopError   = null;

/**
 *  ----------------------------------------------------------------------  *
 *  Definição das filas (FIFO) do controle dinâmico de carga (CLdo)
 *  ----------------------------------------------------------------------  *
 *  this.load     =>  nome dos arquivos carregados
 *  this.loading  =>  nome dos arquivos em carga (carga não completada)
 *  this.dinamic  =>  nome dos objetos com carga tipo dinamica
 *  this.readOBJ  =>  nome dos objetos com carga tipo dinamica ja carregados
 *  this.core     => 
 *  this.erroLoad =>  nome dos objetos com erro de carga  
 *  this.errCode  =>  nome dos arquivos com erro de sintaxe no codigo JS
 *  this.errFunc  =>  nome da Função com erro de sintaxe no codigo JS
 *  this.waitObj  =>  nome dos objetos aguardando serem startados pelo CORE
 *  ----------------------------------------------------------------------  *
 */ 
  this.load    = new Array();
  this.loading = new Array();
  this.dinamic = new Array();
  this.readOBJ = new Array();
  this.core    = new Array();
  this.errLoad = new Array();
  this.errCode = new Array();
  this.errFunc = new Array();
  this.waitObj = new Array(); // objetos aguardando Start
  
  
  
  
  
  
  
  
/**
 *  ----------------------------------------------------------------------  *
 *  Definição dos Metodos da Classe
 *  ----------------------------------------------------------------------  *
 */
  
  
/**
 *  ----------------------------------------------------------------------  *
 *  Definição dos Metodos Privados
 *  ----------------------------------------------------------------------  *
 *
 *  por definição todos os metodos declarados internamente a classe
 *  sera considerado metodo privado para efeito de organização
 *  ----------------------------------------------------------------------  *
 */
 
/**
 *  ----------------------------------------------------------------------  *
 *  start()
 *  ----------------------------------------------------------------------  *
 */
  this.start = function()
  {
    this.IP       = _IP_;
    this.uID      = '';
    this.DNA      =  this.nSO       + ':' + 
                     this.browser   + ':' + 
                     this.display_X + ':' + 
                     this.display_Y + ':' + 
                     this.language; 
    this.loginUsr = '';
    
    this.objetos = _objetos_;
    this.style   = _style_;
    this.libCORE = _libCORE_;
    this.libUSER = _libUSER_;
    
    this.gaugeImg.src = this.dirRoot + this.dirImage + 'gauge.gif';
    this.logoImg.src  = this.dirRoot + this.dirImage + 'logo.gif';
    this.bgModal.src  = this.dirRoot + this.dirImage + 'bgModal.png';
    
    //this.loopError = setInterval( this.recoverErrorLoad, 1000 );
    //setTimeout( this.recoverErrorLoad, 100 );
/**
 *  ----------------------------------------------------------------------  *
 *  cria o bgModal
 *  ----------------------------------------------------------------------  *
 */
    this.nodeModal = document.createElement( 'div' );
    this.nodeModal.id = 'bgModal';
    this.setAttribute( this.nodeModal, 'class', 'bgModal' );
    
    var nodeImg = document.createElement( 'img' );
    nodeImg.id = "bgImg";
    this.setAttribute( nodeImg, 'class', 'bgImage' );
    this.setWidth( nodeImg, ( appWeb.total_X - 25 ) );
    this.setHeight( nodeImg, 950 );
    
    nodeImg.src = this.bgModal.src;
  
    this.nodeModal.appendChild( nodeImg );
//    this.nodeBody.insertBefore( this.nodeModal, this.nodeBody.firstChild );
    
//alert('started: appWeb');
  
  }// End Metodo start()
  

  this.startObjeto = function( nClass, nObj )
  {
    try
    {
      var _cmd_ = nObj + '.start();';
      //eval( _cmd_ );
      
      var _cmd = 'var ' + nObj + ' = new class' + nClass + '( "' + nObj + '" );';
      alert( _cmd );
      //eval( _cmd );

    }catch( _err )
    {
      alert( 'erro: ' + _err.description );
      var _cmdo_ = 'this.starObjeto("' + nObj + '")';
      //setTimeout( _cmdo_, this.nWait );
      //alert(_cmdo_);
      //alert( 'erro: ' + _err.description );
    }// End try
  }// End Metodo
  

/**
 *  ----------------------------------------------------------------------  *
 *  displayMensage()
 *  ----------------------------------------------------------------------  *
 */  
  this.displayMensage = function( _msg )
  {
    var template = 
'<div id="alertBox_header">'+
'  <img id="alertBox_headerBoxLeft" src="images/alertBoxLT.png"  />'+
'  <img id="alertBox_headerBoxMiddle" src="images/alertBoxMiddle.png" />'+
'  <img id="alertBox_headerBoxRight" src="images/alertBoxRT.png" />'+
'  <div id="alertBox_titulo">Mensagem do Sistema</div>'+
  
'    <div id="alertBox_btFechar">'+
'      <img id="alertBox_btFecharBtn" src="images/alertBox_btn.png" />'+
'      <img id="alertBox_btFecharIcone" src="images/alertBox_close.png" />'+
'    </div>'+
'  </div>'+
  
'  <div id="alertBox_status"></div>'+
'</div>'+

'<div id="alertBox_box">'+
'  <img id="alertBox_boxLeft" />'+
'  <img id="alertBox_boxRight" />'+
'  <div id="alertBox_frameBox">'+
'    <div id="alertBox_frame">'+
_msg +
'    </div>'+
'  </div>'+
'</div>'+

'<div id="alertBox_foot">'+
'  <img id="alertBox_footBoxLeft" />'+
'  <img id="alertBox_footBoxMiddle" />'+
'  <img id="alertBox_footBoxRight" />'+
'</div>';

//    var nodeBox = document.createElement( 'div' );
//    nodeBox.id = 'alertBox';
    
    this.nodeAlert = document.createElement( 'div' );;
    this.nodeAlert.id = 'alertBox';
    
    this.setModalOn( this.nodeAlert );
    
    this.nodeBody.appendChild( this.nodeAlert );
    this.nodeAlert.innerHTML = template;
    
    var _top = ( (appWeb.display_Y / 2 ) - 200 ) +
               ( document.documentElement.scrollTop / 2 );
    
    appWeb.setTop( this.nodeAlert, _top );
    appWeb.setLeft( this.nodeAlert, ( appWeb.display_X / 2 ) - 200 );
    
  }// End metodo displayMensage()
  
  
/**
 *  ----------------------------------------------------------------------  *
 *  setModalOn()
 *  ----------------------------------------------------------------------  *
 */
  this.setModalOn = function( _objDOM )
  {
    this.objModal = _objDOM;
    
    this.setAttribute( this.nodeModal, 'visibility', 'visible' );
    this.setAttribute( this.nodeModal, 'zIndex', appWeb.zIndexModal );
    //this.setScrollBar( false );
  }// End metodo setModalOn()
  
/**
 *  ----------------------------------------------------------------------  *
 *  setModalOff()
 *  ----------------------------------------------------------------------  *
 */
  this.setModalOff = function()
  {
    if( this.objModal != null )
    {
      var _ID = this.objModal.id;
      if( _ID.indexOf( 'winPage' ) != -1 )
      {
        winPage.objects[ this.objModal.id ].close();
      }// End if()
    }// End if()
    
    this.objModal = null;
    this.setAttribute( this.nodeModal, 'visibility', 'hidden' );
    this.setAttribute( this.nodeModal, 'zIndex', '1' );
    
    //this.setScrollBar( true );
  }// End metodo setModalOn()
  
/**
 *  ----------------------------------------------------------------------  *
 *  setModal()
 *  ----------------------------------------------------------------------  *
 */
  this.set_Modal = function( _status )
  {
    if( _status == true )
    {
      this.setAttribute( this.nodeModal, 'visibility', 'visible' );
      this.setAttribute( this.nodeModal, 'zIndex', appWeb.zIndexModal );
      this.setScrollBar( false );
    }else
    {
      this.setAttribute( this.nodeModal, 'visibility', 'hidden' );
      this.setAttribute( this.nodeModal, 'zIndex', '1' );
      this.setScrollBar( true );
    }
  }// End metodo setModal

/**
 *  ----------------------------------------------------------------------  *
 *  getScrollLeft()
 *  ----------------------------------------------------------------------  *
 */
  this.getScrollLeft = function()
  {
    return parseInt( document.documentElement.scrollLeft );
  }// End metodo getScrollLeft()

/**
 *  ----------------------------------------------------------------------  *
 *  getScrollTop()
 *  ----------------------------------------------------------------------  *
 */
  this.getScrollTop = function()
  {
    return parseInt( document.documentElement.scrollTop );  
  }// End metodo getScrollTop()
  
/**
 *  ----------------------------------------------------------------------  *
 *  Definição do Construtor da Classe
 *  ----------------------------------------------------------------------  *
 */
  this._construtor = function()
  {
    this.startLib = this.dirRoot + this.dirLib + this.browser + '/start.lib.js';
    
  }// End construtor
  this._construtor();
  
  
}// EndClass

/**
 *  ----------------------------------------------------------------------  *
 *  Atributos privados da Classe
 *  ----------------------------------------------------------------------  *
 */
//var appWeb = new Object();

/**
 *  ----------------------------------------------------------------------  *
 *  Definição dos Metodos Públicos
 *  ----------------------------------------------------------------------  *
 *  metodos Setter
 *
 *  metodos Getter
 */
 
  
  
/**
 *  ----------------------------------------------------------------------  *
 *  Atributos privados da Classe
 *  ----------------------------------------------------------------------  *
 */




/**
 *  ----------------------------------------------------------------------  *
 *  Incorpora Classes Ancestrais
 *  ----------------------------------------------------------------------  *
 *  extend a classe classEsquema que por sua vez 
 *  extend a classe classClient
 */
classAppWeb.prototype = new classEsquema;

//alert('appWeb');