var URLBASE = document.getElementsByTagName ('base')[0].href;


/** Retorna un elemento por su ID
*/

function e( id ){
    return document.getElementById (id);
}

/** Retorna un array de elementos por nombre del tag
*/
function t ( name ) {
    return document.getElementsByTagName ( name );
}

/** Crea un elemento
*/

function c( element ) {
    return document.createElement ( element );
}

/** Obtiene las coordenadas de un elemento. Tomado de
    http://www.quirksmode.org/js/findpos.html
*/
function xy ( element ) {
    var x = y = 0;
    
    if ( element.offsetParent ) {
        do {
            x += element.offsetLeft;
            y += element.offsetTop;
        } while ( element = element.offsetParent );
    }
    
    return [x,y]
}

/** Convierte de decimal a hexadecimal, usado para colores
*/

function d2h ( d ) {
    ss = d.toString ( 16 );
    
    if ( ss.length < 2 ) 
        ss = '0' + ss;
    
    return ss;
}



/** Muestra el mensaje msg, y de aceptar, va a la url "url" :)
 */
function confirmar (msg, url) {
    if ( window.confirm (msg) ) {
        window.location.href = URLBASE + url;
    }
}

function cerrar_sesion () {
    if ( window.confirm ('Esto cerrará tu sesión en este sitio web')  )
        ir ( 'admin/sesion/salir' );
}


/** Cambia la dirección a la direccion url
 */
function ir (url, openwindow, opts) {

    // Si la URL no tiene http:// , entonces le añadimos la ruta base.
    
    if ( url.substr (0, 7) != 'http://' )
        url = URLBASE + url;

    if ( openwindow ) 
        return window.open (url, openwindow, opts);
    else
        window.location.href = url;
}


/** Publica un mensaje, y da el foco al elemento
*/
function mensaje (msg, objeto, no_restaltar ) {
    if ( typeof (objeto) == 'string')  {
        o = e (objeto);
    }
    else {
        o = objeto;
    }
    
    o.focus();
    
    color_back = o.style.background;
    color_fore = o.style.color;
    
    o.style.color = 'white';
    o.style.background = 'red';

    window.alert ( msg );
    
    o.style.color = color_fore;
    o.style.background = color_back;
    o.focus()
}

/** Esta funcion muestra un popup bonito.
 */
function mostrar_busqueda (script, id, valor, busqueda) {

    id = window.open (script + "/" + "/id/" + id + "/valor/" + valor + "/busqueda/" + busqueda, "listado", "resizable=no,scrollbars=yes,width=320,height=480,toolbar=no,location=no,status=no");
    
   if ( !id  ) {
        window.alert ("El navegador no permite ventanas 'popup'. No puedo mostrar el listado mientras no permitas que este sitio muestre ventanas 'popup'.");
    }
}

function checkbox_muestra_oculta ( checkbox, control ) {

    if ( e(checkbox).checked )
        e(control).style.display='block';
    else
        e(control).style.display='none';        

}

// Prototipo de una funcion trim
String.prototype.trim = function(){
    return this.replace(/^\s+|\s+$/g,'')
}

function opacidad ( element, op ) {
    if ( typeof ( element ) == "string" )
        element = e (element);
        
    element.style.opacity = op / 100;
    try {
        element.filters.alpha.opacity = op;
    }
    catch (err) {
    
    }
}


// Variables globales para manter el 
rE = false;
rP = false;
rH = false;

function remover_elemento ( e, p ) {
    
    GOTIMEOUT = true;
    
    // Guardamos las variables
    if ( e ) {
        rE = e;
        rP = p;
        rH = rE.clientHeight;
    }
    
    if (  rE.opacidad === undefined )
        rE.opacidad = 100;

    if ( rE.opacidad > 0 ) {
        // En cada llamada, le bajamos un poquito
    
        rE.opacidad -= 2;
        
        opacidad ( rE, rE.opacidad );
    }
    else {
        // Y ahora le reducimos el tamaño
        rH -= 4;
        rE.style.height = rH + "px"
        
        if ( rH < 0 ) { 
            GOTIMEOUT = false 
            
            // Borramos el elemento
            rE.parentNode.removeChild ( rE );
        }
    }

    if ( GOTIMEOUT )
    setTimeout ( 'remover_elemento()', 10 );

}
