/***************************************************************************
 * Copyright (C) 2006 by Korgault Studios
 * http://www.kgstudios.net
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 **************************************************************************/

/**
 * Métodos agregados al objeto 'Window' de JavaScript
 *
 * @package	phppr0.Shared.js
 * @author Korvus
 * @date 01-01-2007
 * @version 1.0
 */

/**
 * Realizamos una copia del método de sistema 'window.open' para implementar un 
 * método propio con el mismo nombre
 */
 
window._open = window.open;

/**
 * Abre una nueva ventana
 *
 * @function window.open
 * @param sUrl ( string )
 * @param [ sIdentifier ] ( String )
 * @param [ sParameters ] ( String )
 * @param [ bIgnoreSession ] ( Boolean )
 */

window.open = function( sUrl, sIdentifier, sParameters, bIgnoreSession )
{
 /**
  * Añade el identificativo de la sesión para presevarla
  * @see APP_VARS
  */
	
 if( typeof( bIgnoreSession ) == "undefined" || bIgnoreSession == false )
  sUrl += ( sUrl.indexOf( "?" ) == -1 ? "?" : "&" ) + APP_VARS[ "SESSION" ][ 0 ] + "=" + APP_VARS[ "SESSION" ][ 1 ];
	
 return( this._open( sUrl, sIdentifier, sParameters ) );
}

/**
 * Evento lanzado al finalizar la carga de la página. Se encarga de buscar las funciones
 * asociadas a cada elemento, y de llamar al evento 'onloaded' de dichos elementos
 *
 * @function window.onload
 * @return ( void ) 
 */

window.onload = function()
{
 var oElements = document.getElementsByTagName( "*" );
 var oEvents = [ "click", "dblclick", "change", "focus", "blur", "mouseover", "mouseout", "mousemove", "keypress", "keydown", "keyup", "beforesubmit", "submit", "load" ];
 
 var sCurrentFunction = new String();
 var oCustomObject = new Object();
 
 /**
  * Aplicamos los eventos a aquellos objetos que tengan alguna funcion relacionada
  * al estilo VB ( nombreobjeto_evento ). P.e: cmdOK_click() ...
  * Los elementos de los formularios cuyo nombre contenga el carácter '[' serán ignorados
  *
  * p.e: TABLES[ noticias ][ values ][ 0 ][ idNoticia ]
  * @see phppr0.Shared.php.formHandler
  */
 
 for( var i = 0; i < oElements.length; i++ )
 {
  /**
   * Objeto relacionado con el elemento actual, que poseerá valores para aquellos atributos
   * no reconocidos por los estándares XHTML ( onloaded, onsubmit, onbeforesubmit, etc ... )
   *
   * @var oCustomObject ( Object )
   * @see phppr0.Advanced.HTML :: createElement
   */
	 
  if( oCustomObject = getObject( oElements[ i ].getAttribute( "id" ) ) )
  {
   for( var j in oCustomObject )
	oElements[ i ].setAttribute( j, oCustomObject[ j ] );
  }  

  if( oElements[ i ].getAttribute( "onloaded" ) != null )
   new Function( oElements[ i ].getAttribute( "onloaded" ) ).call( oElements[ i ] );
  
  /**
   * El elemento no tiene atributo 'name' o su valor posee el carácter '['
   */
  
  if( oElements[ i ].getAttribute( "name" ) == null || oElements[ i ].getAttribute( "name" ).indexOf( "[" ) != -1 )
   continue;
  
  /**
   * Aplica las funciones encontradas al objeto actual en 'oElements[ i ]' para el evento actual en 'oEvents[ j ]'
   */
  
  for( var j = 0; j < oEvents.length; j++ )
  {
   sCurrentFunction = oElements[ i ].getAttribute( "name" ) + "_" + oEvents[ j ];
   eval( "if( typeof( " + sCurrentFunction + " ) == 'function' ){ oElements[ i ].on" + oEvents[ j ] + " = " + sCurrentFunction + "; }" ); 
  }
 }
 
 document.body.onmousemove = function( e )
 { 
  /**
   * Mozilla :_)
   */
 
  if( !window.event )
  {
   APP_VARS[ "EVENT" ].type = e.type.toLowerCase();
   APP_VARS[ "EVENT" ].clientX = e.pageX;
   APP_VARS[ "EVENT" ].clientY = e.pageY;
   APP_VARS[ "EVENT" ].scrollX = this.scrollLeft;
   APP_VARS[ "EVENT" ].scrollY = this.scrollTop;
   
   /**
    * Ajusta la posición del cursor al desplazamiento del scroll
	*/
   
   if( this.scrollTop > 0 )
    APP_VARS[ "EVENT" ].clientY %= this.scrollTop;
  }
  
  /**
   * Explorer ¬¬'
   */
  
  else
  {
   APP_VARS[ "EVENT" ].type = window.event.type.toLowerCase();
   APP_VARS[ "EVENT" ].clientX = window.event.clientX;
   APP_VARS[ "EVENT" ].clientY = window.event.clientY;
   APP_VARS[ "EVENT" ].scrollX = this.scrollLeft;
   APP_VARS[ "EVENT" ].scrollY = this.scrollTop;
  }
 };
 
 if( typeof( this.onloaded ) == "function" )
  this.onloaded();
}

/**
 * Cambia el texto de la barra de estado
 * 
 * @function window.setStatus
 * @param sStatusBarText ( String )
 * @param [ nRestoreInterval ] ( Integer )
 * @return ( void )
 */

window.setStatus = function( sStatusBarText, nRestoreInterval )
{
 if( typeof( nRestoreInterval ) == "number" )
  this.setTimeout( "window.status = unescape( '" + escape( this.status ) + "' );", nRestoreInterval );
 
 this.status = sStatusBarText;
}