﻿$namespace('System.Page.Util');

var GaleriaAtual = 1;
var FotoAtual = 1;
var NomeFotoAtual = "";
var tituloFotos;
var listaFotos = null;

function openFotonoCelular(_logo, _img , _uid, _pwd){
var width = 350;
var height = 600;
var left = screen.width/2 - width/2;
var top = screen.height/2 - height/2;
var url = "http://fotonocelular.ringtones.com/stream.step1.aspx?img="+_img+"&logo="+_logo+"&uid="+_uid+"&pwd="+_pwd
window.open(url, "FotoNoCelular", "fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
};

function fotoCelular(){
    openFotonoCelular('http://www.dreamcam.com.br/images/barraCelular3.jpg', 
        'http://www.dreamcam.com.br/' + NomeFotoAtual, 'dream', 'cam001')
};

function AtivaGaleria(NovaGaleria, qtdeFotos){
    $("imgGaleriaSel"+GaleriaAtual).style.display = "none";
    $("imgGaleria"+GaleriaAtual).style.display = "";
    $("imgGaleria"+NovaGaleria).style.display = "none";
    $("imgGaleriaSel"+NovaGaleria).style.display = "";
    GaleriaAtual = NovaGaleria;
    var res = ensaio.Galeria(NovaGaleria);
    Galeria_callback(res);
    //Assincrono:
    //ensaio.Galeria(NovaGaleria, Galeria_callback);
};

function setFoto(numFoto){
    var i = numFoto - 1;
    var thumb = $("Thumb"+FotoAtual)
    if (thumb != null) thumb.src = listaFotos[FotoAtual-1].Thumb;
    
    thumb = $("Thumb"+numFoto)
    if (thumb != null) thumb.src = listaFotos[i].ThumbSel;

    var res = ensaio.GaleriaLiberada(GaleriaAtual);
    
    var liberaGaleria = false;
    if(res.value != null) 
        liberaGaleria = res.value;

    if (liberaGaleria)
    {
        NomeFotoAtual = listaFotos[i].Path;
        $("imagemFoto").src = NomeFotoAtual;
        $("imagemFoto").style.display = "";
    }
    else
    {
        location.href = listaFotos[i].Path;
    }
    
    FotoAtual = numFoto;
};

Galeria_callback = function(res){
    try {
        if (res.value==null)
            try {
                res.value = eval(res.text.replace(/;/g,''));
                res.error = null;
            }
            catch (err) { res.error = err; }
        
        if(res.error == null){
            listaFotos = res.value;
            initFotos();
        }
        else
            alert(res.error.message + '\n' + res.error.stackTrace);
    } catch (ex) { alert(ex.message); }
};

initFotos = function(){
    var items = tituloFotos;
    if (listaFotos!=null) {
        for (var i = 0; i < listaFotos.length; i++){
            items += "<li onclick=\"javascript:setFoto(" + (i+1) + ");\" style=\"cursor:pointer\">"
            items += "<img id=\"Thumb" + (i+1) + "\" src=\"" + listaFotos[i].Thumb + "\""
            items += " width=\"57\" height=\"30\" border=\"0\" /></a></li>\r\n";
        }
    }
    $("listaFotos").innerHTML = items;
    setFoto(1);
};

init = function(){
    tituloFotos = $("listaFotos").innerHTML;
    AtivaGaleria(GaleriaAtual);
};

$load(init);