/***************************************************************************
 * 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 'Document' de JavaScript
 *
 * @package	phppr0.Shared.js
 * @author Korvus
 * @date 01-01-2007
 * @version 1.0
 */

/**
 * Realiza una copia del método nativo 'document.getElementById' y lo sustituye por un método propio
 * que buscará los elementos por su atributo 'name' y no 'id', ya que éste se reservará para el uso interno
 *
 * @function document.getElementByName
 * @param sElementId ( String )
 * @return ( Object | null )
 */ 

document.getElementByName = function( sElementId )
{ return( this.getElementsByName( sElementId )[ 0 ] ); }

/**
 * Devuelve información sobre el estado del ratón
 * @function document.mouse
 * @param [ sIndex ] ( string )
 * @return ( mixed )
 */

document.mouse = function( sIndex )
{
 var oMouseInfo = new Array();
  
 oMouseInfo[ "x" ] = APP_VARS[ "EVENT" ].clientX;
 oMouseInfo[ "y" ] = APP_VARS[ "EVENT" ].clientY;
 oMouseInfo[ "pageX" ] = oMouseInfo[ "x" ] + APP_VARS[ "EVENT" ].scrollX;
 oMouseInfo[ "pageY" ] = oMouseInfo[ "y" ] + APP_VARS[ "EVENT" ].scrollY;
 
 return( typeof( sIndex ) == "undefined" ? oMouseInfo : oMouseInfo[ sIndex ] );
}

/**
 * Inicializa una variable conservando el valor del resto de variables
 *
 * @function document.setVariable
 * @param mNames ( Array | String )
 * @param mValues ( Array | String )
 * @param [ sFragment ] ( String )
 * @return ( void )
 */
 
document.setVariable = function( mNames, mValues, sFragment )
{ 
 /**
  * Fuerza el tipo de los 2 primeros parámetros a 'Array'
  */
 
 if( [ "string", "number" ].indexOf( typeof mNames ) != -1 ){ mNames = new Array( mNames.toString() ); }
 if( [ "string", "number" ].indexOf( typeof mValues ) != -1 ){ mValues = new Array( mValues.toString() ); }

 /**
  * Listado de variables y valores de la página actual
  * @var oVariableList ( Array )
  */

 var oVariableList = this.getVariable();

 for( var i = 0; i < mNames.length; i++ )
 {
  /**
   * Elimina aquellas variables para las que se haya indicado un valor NULO o VACÍO
   */
	 
  if( [ null, "" ].indexOf( mValues[ i ] ) != -1 )
  { delete( oVariableList[ mNames[ i ] ] ); continue; }
 
  /**
   * Modifica el valor de la variable, al indicado en 'mValues'
   */
 
  oVariableList[ mNames[ i ] ] = escape( mValues[ i ] );
 }
 
 this.location.href = "?" + oVariableList.ajoin( "=", "&" ) + ( typeof( sFragment ) == "string" ? "#" + sFragment : "" );
}

/**
 * Devuelve el valor de una variable enviada mediante GET
 * Si no se define un valor para el primer parámetro, devuelve un array asociativo con los valores de las variables
 *
 * @function document.getVariable
 * @param [ sKey ] ( string )
 * @return ( String )
 */

document.getVariable = function( sKey )
{
 /**
  * Listado de elementos del tipo "variable=valor"
  * @var oVariableContentList ( Array )
  */
 
 var oVariableList = new Array();
 var oVariableContentList = this.location.search.toString().replace( "?", "&" ).split( "&" );
 
 /**
  * Añade el elemento al array asociativo 'oVariableList' si éste tiene valor
  */
 
 for( var i = 0; i < oVariableContentList.length; i++ )
 {
  if( oVariableContentList[ i ].length > 0 )
   oVariableList[ oVariableContentList[ i ].split( "=" )[ 0 ] ] = oVariableContentList[ i ].split( "=" )[ 1 ];
 }
  
 return( typeof( sKey ) == "undefined" ? oVariableList : oVariableList[ sKey ] );
}

/**
 * Filtra elementos según los atributos y sus valores
 *
 * @function document.getElementsByAttribute
 * @param sName ( string ) Nombre del atributo que se desea buscar
 * @param sValue ( string )
 * @param [ sTag ] ( string ) Filtro por nombre de nodo
 * @param [ bSeekByPattern ] ( bool ) Indica si debemos comparar el valor estrictamente
 * @return ( Array )
 */

document.getElementsByAttribute = function( sName, sValue, sTag, bSeekByPattern )
{
 var oMatches = new Array();
 var oElements = this.getElementsByTagName( sTag || "*" );
 
 for( var i = 0; i < oElements.length; i++ )
 {
  if( oElements[ i ][ sName ] == sValue || ( oElements[ i ][ sName ] && !sValue ) || ( oElements[ i ][ sName ].indexOf( sValue ) != -1 && bSeekByPattern == true ) )
   oMatches.push( oElements[ i ] );
 }
 
 return( oMatches );
}

/**
 * Inicializa una cookie
 *
 * @function document.setCookie
 * @param sName ( string ) Nombre de la cookie
 * @param sValue ( string ) Valor de la cookie
 * @return ( void )
 * @see phppr0.js.document :: getCookie
 */

document.setCookie = function( sName, sValue )
{ this.cookie = sName + "=" + escape( sValue ); }

/**
 * Devuelve el valor de una cookie
 *
 * @function document.getCookie 
 * @param sName ( string ) Nombre de la cookie
 * @return ( string )
 * @see phppr0.js.document :: setCookie
 */

document.getCookie = function( sName )
{
 if( ( nStart = this.cookie.indexOf( sName + "=" ) ) != -1 )
 {
  nStart += sName.length + 1;
  nEnd = this.cookie.indexOf( ";", nStart );
  nEnd = nEnd == -1 ? this.cookie.length : nEnd;
  
  return( unescape( this.cookie.substring( nStart, nEnd ) ) );
 }
}

/**
 * @function document.open
 * @param sUrl ( String )
 * @param [ bIgnoreSession ] ( Boolean )
 */
 
document.open = function( sUrl, 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 ];

 /**
  * Redirecciona a la URL especificada
  */

 this.location.href = sUrl; 
}

/**
 * Evento lanzado al seleccionar un texto del documento
 * @function document.onselectstart
 * @return ( void )
 */

document.onselectstart = function()
{ return( APP_VARS[ "DEBUG" ] == true || [ "textarea", "input" ].indexOf( window.event.srcElement.tagName.toLowerCase() ) != -1 ); }

/**
 * Evento lanzado al mostrar el menú contextual
 *
 * @function document.oncontextmenu
 * @see phppr0.js.document :: onselectstart
 */

document.oncontextmenu = function()
{ return( this.onselectstart() ); }