/*<![CDATA[*/
//******************************************************************************
// @name:        galeria.js
// @purpose:     scripts que sao utilizado na galeria de fotos.
//
//
// @author:      Ivanir Duarte - ivanir@2xt.com.br
// @created:     05/03/2008
//******************************************************************************
function galeria_abrir(id_galeria){
    url = 'portlets/galeria_mostrar?id_galeria=' + id_galeria
    var discard = window.open(URLGALERIAPOPUP + '?id_galeria='+ id_galeria ,  '_blank', 'width=700, height=610,toolbar=no, menubar=no, personalbar=no, scrollbars=no, resizable=no, dependent=yes, z-lock=yes, modal=yes')
    
}

function ir_para(){
    var valor = document.getElementById('ir_para').value;
    var total_paginas = document.getElementById('total_paginas').value;
    var limit = document.getElementById('limit').value;
    var union = '';
    if(window.location.href.indexOf('?') == -1 ) union = '?'
    else {
        if(window.location.href.indexOf('b_start') != -1) union = '?';
        else union = '&' 
    } 
  
    if(valor == '') {
        alert('informe a pÃ¡gina que vocÃª deseja visualizar','erro');
        return;
    }  
    if(valor == 0 ){
        alert('o nÃºmero da pÃ¡ginda de ser de 1 Ã  ' +  total_paginas,'erro');
        return;
    }

    if(parseInt(valor) > parseInt(total_paginas) ){
        alert('o nÃºmero da pÃ¡ginda deve ser  menor ou igual ' +  total_paginas,'erro');
        return;
    }
 
    if (valor == 1) i = 0;
    else if(valor == 2) i = parseInt(limit);
    else {
        calc = parseInt(valor) -1;
        i = calc * parseInt(limit);  
    }
    window.location.href= URLGALERIA + union + 'b_start:int='+i+'&-C=';
}

function ir_para_topo(){
    var union = '';
    if(window.location.href.indexOf('?') == -1 ) union = '?'
    else{
       if(window.location.href.indexOf('b_start') != -1) union = '?';
       else union = '&' 
    } 
    window.location.href= URLGALERIA + union + 'b_start:int=0&-C=';
}

var intNavegador = -1;

function getFriend(){ 
    var IE = document.all;
    var url = URLGALERIA +  '?id_galeria='+ document.getElementById('id_galeria').value; 

    var url_friend = URLENVIE + '?url=' + url + '&titulo=' + document.getElementById('tituloGaleria').innerHTML ;

    var winl = (screen.width - 438)/2;
    var wint = (screen.height - 390)/2;
    if (winl < 0) winl = 0;
    if (wint < 0) wint = 0;

    if (!IE) {
        var attributes = 'width=394, height=330, scrollbars=yes';
    } else {
        var attributes = 'width=410, height=353, scrollbars=yes';
    }

    attributes += ", top=" + wint + ", left=" + winl;
    window.open(url_friend, 'envieAmigo', attributes);
}

function proximaFoto()
{
    var lstThumbs = getFotosLista();
    
    if(intNavegador == -1)intNavegador = 0;
    if (intNavegador<(lstThumbs.length-1)) {
        intNavegador++;
        lstThumbs[intNavegador].onclick();
    }
}

function voltarFoto()
{
     var lstThumbs = getFotosLista();
    if (intNavegador>0) {
      intNavegador--;
      lstThumbs[intNavegador].onclick()
    } 
}

function retornarArquivoFoto(objA) {
    objA.onclick();
    var strArquivo = new String(objA.getAttribute("onclick"));
    strArquivo = new String(strArquivo.match(/src='.+'/));
    return strArquivo.replace("src=", "").replace(/'/g, "");
}

function getFotosLista(){
    return document.getElementsByName("fotosThumbs");
}

function showFoto(url, credito, descricao, link) {
    /*
    var objCredito = document.getElementById('autor');
    objCredito.value = credito
    with (document.forms['figrama']) {
        fotodescricao.value = descricao;
        linkFoto.value = link;
        urlFoto.value = url;
        submit();
    }
    */    
    document.getElementById('autor').innerHTML = credito || '';    
    document.getElementById('imagem_principal').src = url;    
    //document.getElementById('a_link').setAttribute('href', link);
    document.getElementById('a_link').href =  link;
    document.getElementById('descricao').innerHTML = descricao || '';
    // Efetuando um page view
    pageTracker._trackPageview();
}

function abrirLink(){
    window.open(URLENVIE,'nome_janela','width=384,height=320,scrollbars=no'); 
}

function getGaleriasPublicadas() {
    var handler = new XMLHandler();
    var xmlreq = new XMLClient(URL_GET_GALERIAS);
    xmlreq.addParam('null', '');
    handler.onError = function(e) { throw('Ocorreu um erro ao tentar apanhar as galerias relacionadas.'); }
    handler.onProgress = function() {};
    handler.onInit = function() {};
    handler.onLoad = function (dados) {
        dados = JSON.decode(unescape(dados));
        // Inserindo as galerias relacionadas no select
        if(dados) {
            var flag = false;
            var e_select = document.getElementById('id_galeria_relacionada');
            e_select.innerHTML = '';
            for(var i=0; i<dados.length; i++) {
                var e_option = document.createElement('option');
                e_option.setAttribute('value', dados[i]['link']);
                e_option.innerHTML = dados[i]['formatado'] + ' - ' + dados[i]['titulo'];
                e_select.appendChild(e_option);
                // Verificando se e a galeria atual
                if(parseInt(dados[i]['id_galeria']) == parseInt(ID_GALERIA)) {
                    flag = true;
                    e_option.setAttribute('selected', 'selected');
                }
            }
            // Inserindo um selecione caso esta galeria nÃ£o esteja na listagem
            if(!flag) {
                var e_option = document.createElement('option');
                e_option.innerHTML = 'Selecione';
                e_option.setAttribute('selected', 'selected');
                e_select.insertBefore(e_option, e_select.options[0]);
            }
        }
    }
    xmlreq.query(handler);    
}

/* Mostra a primeira foto da galeria no onload */
function galeriaIniciar() {

    var e_table = document.getElementById('tabela_de_thumbs');
    var e_l_thumbs = e_table.getElementsByTagName('img');

    e_l_thumbs[0].parentNode.onclick();
}
/*]]>*/
