/**
 * Ex:
 * var id_img = splash_add_img( 'teste4.jpg', 500, 600, 'Nome do Teste', 'Bla bla bla bla bla bla bla.' );
 * show_splash( id_img );
 */


add_load_event( splash_load_imgs );

var navegador     = verif_navegador();
var lista_imagens = new Array();
var lista_pos     = -1;


splash_write();


function splash_write() {
    
    document.write('<div id="splash_esconde_fundo"></div>');
    document.write('<div id="splash_centro">');
    document.write('  <div id="splash_nav_bar">');
    document.write('    <div class="prev"><div id="splash_nav_bar_prev"><a href=\"javascript: show_splash( lista_pos-1 );\"></a></div></div>');
    document.write('    <div class="next"><div id="splash_nav_bar_next"><a href=\"javascript: show_splash( lista_pos+1 );\"></a></div></div>');
    document.write('    <div class="close"><a href=\"javascript: splash_close();\"></a></div>');
    document.write('  </div>');
    document.write('  <div id="splash_nome"></div>');
    document.write('  <div id="splash_principal">');
    document.write('    <img id="splash_img" src="" class="unload" onload="splash_loaded();">');
    document.write('  </div>');
    document.write('  <div id="splash_texto"></div>');
    document.write('</div>');
    
    document.getElementById('splash_centro').style.maxWidth  =  1000;
    
}


function show_splash( pos ) {
    
    lista_pos = pos;
    
    if ( !splash_verif_existe_img( pos ) ) {
        return;
    }
    
    splash_show_img( pos );
    
    splash_ativacao_nav_bar( pos );
    splash_open();
    
    splash_resize();
    window.onresize = splash_resize;
    
}


function splash_show2( path ) {
    pos = splash_get_pos_by_path( path );
    if ( pos > -1 ) {
        show_splash( pos );
    }
}


function splash_open() {
    document.getElementById('splash_esconde_fundo').style.display  =  'block'; 
    document.getElementById('splash_centro').style.display         =  'block';
}


function splash_close() {
    document.getElementById('splash_esconde_fundo').style.display  =  'none'; 
    document.getElementById('splash_centro').style.display         =  'none';
}


function splash_resize() {
    
    var fundo = document.getElementById('splash_esconde_fundo');
    if ( fundo == null ) {
        return;
    }
    
    fundo.style.height = '100%';
    var maior_altura = ( document.body.clientHeight > document.body.scrollHeight ) ? document.body.clientHeight : document.body.scrollHeight ;
    
    fundo.style.width  = document.body.clientWidth;
    fundo.style.height = maior_altura;
    
    splash_show_img( lista_pos );
    
}


function splash_loaded() {
    splash_set_dim( lista_pos, true );
    document.getElementById('splash_img').className = 'loaded';
}


function splash_unload() {
    document.getElementById('splash_img').className = 'unload';
}


function splash_ativacao_nav_bar( pos ) {
    
    if ( lista_imagens.length <= 1 ) {
        document.getElementById('splash_nav_bar_prev').style.display  =  'none'; 
        document.getElementById('splash_nav_bar_next').style.display  =  'none';
        return;
    }
    
    document.getElementById('splash_nav_bar_prev').style.display  =  'block'; 
    document.getElementById('splash_nav_bar_next').style.display  =  'block';
    
    if ( pos == 0 ) {
        document.getElementById('splash_nav_bar_prev').style.display  =  'none'; 
    } else if ( pos == (lista_imagens.length-1) ) {
        document.getElementById('splash_nav_bar_next').style.display  =  'none'; 
    }
    
}


function splash_reset_imgs() {
    lista_imagens = new Array();
    lista_pos     = -1;
}


function splash_add_img( path_img, width, height, nome, descricao ) {
    
    if ( path_img == null )
        return false;
    if ( width == null || width < 0 )
        return false;
    if ( height == null || height < 0 )
        return false;
    if ( nome == null )
        nome = '';
    if ( descricao == null )
        descricao = '';
    
    var pos = lista_imagens.length;
    /*var width_janela  = document.body.clientWidth  - 20;
    var height_janela = document.body.clientHeight - 20;
    
    if ( width > width_janela ) {
        width  = width_janela;
        height = parseInt( (height/width) * height );
    }
    
    if ( height > height_janela ) {
        height = height_janela;
        width  = parseInt( (width/height) * width );
    }*/
    
    lista_imagens[pos] = new Array();
    
    lista_imagens[pos][0] = path_img;
    lista_imagens[pos][1] = width;
    lista_imagens[pos][2] = height;
    lista_imagens[pos][3] = nome;
    lista_imagens[pos][4] = descricao;
    
    return pos;
    
}


function splash_verif_existe_img( pos ) {
    
    if ( pos>=lista_imagens.length ) {
        return false;
    }
    return true;
}


function splash_get_img_path( pos ) {
    return lista_imagens[pos][0];
}
function splash_get_img_width( pos ) {
    return lista_imagens[pos][1];
}
function splash_get_img_height( pos ) {
    return lista_imagens[pos][2];
}
function splash_get_img_nome( pos ) {
    return lista_imagens[pos][3];
}
function splash_get_img_descricao( pos ) {
    return lista_imagens[pos][4];
}


function splash_get_pos_by_path( path ) {
    for (i=0; i<lista_imagens.length; i++) {
        if ( splash_get_img_path(i) == path ) {
            return i;
        }
    }
    return -1;
}


function splash_show_img( pos ) {
    
    splash_unload();
    
    if ( !splash_verif_existe_img( pos ) ) {
        return false;
    }
    
    splash_set_dim( pos, false );
    
    var divPrincipal = document.getElementById('splash_principal');
    var imgSplash    = document.createElement('img');
    
    document.getElementById('splash_nome').innerHTML   =  splash_get_img_nome( pos );
    document.getElementById('splash_texto').innerHTML  =  splash_get_img_descricao( pos );
    document.getElementById('splash_centro').title     =  splash_get_img_nome( pos );
    document.getElementById('splash_img').src          =  splash_get_img_path( pos ); 
    
}


function splash_set_dim( pos, loaded ) {
    
    splash_width  = 400;    // Default
    splash_height = 250;    // Default
    
    width  = splash_get_img_width( pos );
    height = splash_get_img_height( pos );
    
    if ( loaded && ( (width <= 0) && (height <= 0) ) ) {
        width  = document.getElementById('splash_img').width;
        height = document.getElementById('splash_img').height;
    }
    
    if ( (width > 0) && (height > 0) ) {
        splash_width  = width + 40;
        splash_height = height + 100;
    }
    
    if ( splash_width >= document.body.clientWidth ) {
        splash_width = document.body.clientWidth - 20;
    }
    
    splash_left = ( document.body.clientWidth  / 2 ) - ( splash_width / 2 );
    splash_top  = ( document.body.clientHeight / 2 ) - ( splash_height / 2 ) + document.body.scrollTop;
    
    if ( splash_top < (document.body.scrollTop + 10) ) {
        splash_top = document.body.scrollTop + 10;
    }
    
    document.getElementById('splash_centro').style.width  = splash_width;
    document.getElementById('splash_centro').style.height = splash_height;
    
    document.getElementById('splash_centro').style.left =  splash_left;
    document.getElementById('splash_centro').style.top  =  splash_top;
    
}


/** 
* Carrega todas as imagens de splash da página
* @author Alexandre Franco Marquez <afm.com@gmail.com> 
* @version 1.0 
*/
function splash_load_imgs() {
    if ( !document.getElementById )
        return;
    var elemA = document.getElementsByTagName("a");
    for (var i=0; i<elemA.length; i++)  {
        atualElemA = elemA.item(i);
        if ( atualElemA.getAttribute("target") == 'splash' ) {
            elemImg = atualElemA.getElementsByTagName("img");
            if ( elemImg.length != 1 ) {
                continue;
            }
            nome        = elemImg.item(0).getAttribute("alt");
            path_img    = atualElemA.getAttribute("href");
            descricao   = atualElemA.getAttribute("title");
            splash_add_img( path_img, 0, 0, nome, descricao );
            atualElemA.onclick = function() { splash_show2(this.href); return false; };
        }
    }
}
