Galeria=new Class({
    Implements: [Options],
    options: {
        contenedor: '',
        albumes: [],
        categorias: [],
        filas: 3,
        columnas: 3,
        divFondoAlbum: null,
        divContenedorAlbum: null,
        indiceMostrandoAlbum: -1,
        indiceMostrandoFoto: -1,
        categoriaSeleccionada: -1,
        div_albumes: null,
        div_todo: null,
        div_pie_de_foto: null,
        indice_album: 0,
        padding_left_select: 0,
        padding_right_botones: 0
    },
    initialize: function(options){
        this.setOptions(options);
        Object.append(this,this.options);
        this.options=null;
        this.crearDOM();
    },
    crearDOM: function(){
        var div_todo,div_herramientas,div_albumes,div_album,div_titulo_album;
        var div_fila,div_tabla,div_celda,div,img;
        var div_fila_2,select_categoria,option;
        var yo=this;
        var i,itop,filas,columnas;
        this.div_todo=new Element('div',{
        }).inject(this.contenedor);
        div_todo=this.div_todo;
        div_todo.addClass('galeria_contenedor');
        div_herramientas=new Element('div',{
            styles:{
                'display':'table',
                'width':'100%'
            }
        }).inject(div_todo);
        div_herramientas.addClass('galeria_contenedor_herramientas');
        div_fila=new Element('div',{
            styles:{
                'display':'table-row'
            }
        }).inject(div_herramientas);
        div_celda=new Element('div',{
            styles:{
                'display':'table-cell',
                'text-align':'left',
                'padding-left':this.padding_left_select+'px'
            }
        }).inject(div_fila);
        select_categoria=new Element('select',{
        }).inject(div_celda);
        select_categoria.addEvent('change', function(event){
            yo.categoriaSeleccionada=parseInt(this.value);
            yo.recargarAlbumes();
        });
        select_categoria.addClass('galeria_select');
        option=new Element('option',{
            value: -1
        }).inject(select_categoria);
        option.innerHTML='Buscar por categor&iacute;a';
        itop=this.categorias.length;
        for(i=0;i<itop;i++){
            option=new Element('option',{
                value: this.categorias[i].id
            }).inject(select_categoria);
            option.innerHTML=this.categorias[i].nombre;
        }
        if((this.filas*this.columnas)<this.albumes.length){
            div_celda=new Element('div',{
                styles:{
                    'display':'table-cell',
                    'text-align':'right',
                    'padding-right':this.padding_right_botones+'px'
                }
            }).inject(div_fila);
            div=new Element('div',{
            }).inject(div_celda);
            div.addClass('galeria_boton');
            div.innerHTML=' Anterior ';
            div.addEvent('click', function(event){
                yo.anteriorAlbum();
            });
            div=new Element('div',{
            }).inject(div_celda);
            div.addClass('galeria_separador_boton');
            div.innerHTML=' | ';
            div=new Element('div',{
            }).inject(div_celda);
            div.addClass('galeria_boton');
            div.innerHTML=' Siguiente ';
            div.addEvent('click', function(event){
                yo.siguienteAlbum();
            });
            //falta van x paginas de X
        }
        this.indice_album=0;
        this.recargarAlbumes();
    },
    siguienteAlbum: function(){
        var num_albumes_vista=this.filas*this.columnas;
        if((this.indice_album+num_albumes_vista)<(this.albumes.length)){
            this.indice_album+=num_albumes_vista;
        }else{
            this.indice_album=0;
        }
        this.recargarAlbumes();
    },
    anteriorAlbum: function(){
        var num_albumes_vista=this.filas*this.columnas;
        if((this.indice_album-num_albumes_vista)>=0){
            this.indice_album-=num_albumes_vista;
        }else{
            var ultima_pagina=Math.floor(this.albumes.length/num_albumes_vista);
            this.indice_album=ultima_pagina*num_albumes_vista;
        }
        this.recargarAlbumes();
    },
    recargarAlbumes: function(){
        var div_todo,div_herramientas,div_albumes,div_album,div_titulo_album;
        var div_fila,div_tabla,div_celda,div,img;
        var div_fila_2,select_categoria,option;
        var yo=this;
        var esta=false;
        div_todo=this.div_todo;
        var i,filas,columnas,itop,j,jtop;
        if(this.div_albumes!=null){
            this.div_albumes.nix({duration:0},true);
        }
        this.div_albumes=new Element('div',{
            styles:{
                'display': 'table'
            }
        }).inject(div_todo);
        div_albumes=this.div_albumes;
        div_albumes.addClass('galeria_contenedor_albumes');
        i=this.indice_album;
        itop=this.albumes.length;
        for(filas=0;filas<this.filas&&i<itop;filas++){
            div_fila_2=new Element('div',{
                styles:{
                    'display': 'table-row'
                }
            }).inject(div_albumes);
            for(columnas=0;columnas<this.columnas&&i<itop;columnas++){
                if(this.categoriaSeleccionada!=-1){
                    esta=false;
                    while(!esta&&i<itop){
                        jtop=this.albumes[i].categorias.length;
                        esta=false;
                        for(j=0;j<jtop;j++){
                            if(this.albumes[i].categorias[j]==this.categoriaSeleccionada){
                                esta=true;
                                break;
                            }
                        }
                        if(!esta){
                            i++;
                        }
                    }
                    if(!esta){
                        break;
                    }
                }
                div_album=new Element('div',{
                    styles:{
                        'display': 'table-cell'
                    }
                }).inject(div_fila_2);
                div_album.addClass('galeria_contenedor_album');
                div_titulo_album=new Element('div',{
                    styles:{
                        'cursor': 'pointer'
                    }
                }).inject(div_album);
                div_titulo_album.set('id',''+i);
                div_titulo_album.addEvent('click', function(event){
                    yo.abrirAlbum(this.get('id'));
                });
                div_titulo_album.addClass('galeria_titulo_album');
                div_tabla=new Element('div',{
                    styles:{
                        'display': 'table',
                        'margin-left': 'auto',
                        'margin-right': 'auto',
                        'width': '100%'
                    }
                }).inject(div_titulo_album);
                div_fila=new Element('div',{
                    styles:{
                        'display': 'table-row'
                    }
                }).inject(div_tabla);
                div_celda=new Element('div',{
                    styles:{
                        'display': 'table-cell',
                        'text-align': 'left'
                    }
                }).inject(div_fila);
                div_celda.innerHTML=this.albumes[i].nombre;
                div_celda=new Element('div',{
                    styles:{
                        'display': 'table-cell',
                        'text-align': 'right'
                    }
                }).inject(div_fila);
                div_celda.innerHTML=this.albumes[i].fotos.length;
                div=new Element('div',{
                    styles:{
                        'cursor': 'pointer'
                    }
                }).inject(div_album);
                div.set('id',''+i);
                div.addEvent('click', function(event){
                    yo.abrirAlbum(this.get('id'));
                });
                div.addClass('galeria_contenido_album');
                img=new Element('img',{
                    styles:{
                        'display': 'inline'
                    },
                    src: 'administrador/img_site/galeria/'+this.albumes[i].id+'/thumb/'+this.albumes[i].fotos[0].archivo
                }).inject(div);
                img.addClass('galeria_imagen');
                i++;
            }
        }
    },
    abrirAlbumId: function(idAlbum){
        var i,itop;
        itop=this.albumes.length;
        for(i=0;i<itop;i++){
            if(this.albumes[i].id==idAlbum){
                this.abrirAlbum(i);
                return;
            }
        }
    },
    abrirAlbum: function(elAlbum){
        this.cerrarAlbum();
        this.indiceMostrandoFoto=0;
        this.indiceMostrandoAlbum=elAlbum;
        var div,div_herramientas,div2,div3;
        this.divFondoAlbum=new Element('div',{
        }).inject(document.body);
        this.divFondoAlbum.addClass('fondoLightbox');
        this.divFondoAlbum.innerHTML='&nbsp;';
        this.divFondoAlbum.fade(0.4);
        this.divContenedorAlbum=new Element('div',{
        }).inject(document.body);
        this.divContenedorAlbum.addClass('contenedorLightbox');
        var divContenedorImagen;
        var yo=this;
        div=new Element('div',{
            styles: {
                    'height': '100%',
                    'margin': '0 auto',
                    'display': 'table'
            }
        }).inject(this.divContenedorAlbum);
        div2=new Element('div',{
            styles: {
                'vertical-align': 'middle',
                'display': 'table-cell'
            }
        }).inject(div);
        div3=new Element('div',{
            styles: {
                'text-align':'right'
            }
        }).inject(div2);
        div=new Element('div',{
        }).inject(div3);
        div.addClass('galeria_boton_cerrar');
        div.innerHTML='X';
        div.addEvent('click', function(event){
            yo.cerrarAlbum();
        });
        divContenedorImagen=new Element('div',{
        }).inject(div2);
        divContenedorImagen.addClass('galeria_contenedor_imagen');
        var img=new Element('img',{
            id: 'imagen_galeria_album',
            src: 'administrador/img_site/galeria/'+this.albumes[elAlbum].id+'/img/'+this.albumes[elAlbum].fotos[0].archivo
        }).inject(divContenedorImagen);
        this.div_pie_de_foto=new Element('div',{
        }).inject(divContenedorImagen);
        this.div_pie_de_foto.innerHTML=this.albumes[elAlbum].fotos[0].nombre;
        this.div_pie_de_foto.addClass('galeria_pie_de_foto');
        div_herramientas=new Element('div',{
        }).inject(divContenedorImagen);
        div_herramientas.addClass('galeria_contenedor_herramientas');
        //x foto de X
        div=new Element('div',{
        }).inject(div_herramientas);
        div.addClass('galeria_boton');
        div.innerHTML='Anterior';
        div.addEvent('click', function(event){
            yo.anteriorFoto();
        });
        div=new Element('div',{
        }).inject(div_herramientas);
        div.addClass('galeria_separador_boton');
        div.innerHTML=' | ';
        div=new Element('div',{
        }).inject(div_herramientas);
        div.addClass('galeria_boton');
        div.innerHTML='Siguiente';
        div.addEvent('click', function(event){
            yo.siguienteFoto();
        });
        div=new Element('div',{
        }).inject(div_herramientas);
        div.addClass('galeria_separador_boton');
        div.innerHTML=' | ';
        div=new Element('div',{
            id: 'boton_galeria_album_reproducir'
        }).inject(div_herramientas);
        div.addClass('galeria_boton');
        div.innerHTML='Reproducir';
        div.addEvent('click', function(event){
            yo.reproducir();
        });
    },
    cerrarAlbum: function(){
        if(this.divFondoAlbum!=null){
            this.divFondoAlbum.nix({duration:0},true);
            this.divContenedorAlbum.nix({duration:0},true);
        }
    },
    siguienteFoto: function(){
        this.indiceMostrandoFoto++;
        if(this.indiceMostrandoFoto<this.albumes[this.indiceMostrandoAlbum].fotos.length){
            this.div_pie_de_foto.innerHTML=this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].nombre;
            $('imagen_galeria_album').src='administrador/img_site/galeria/'+this.albumes[this.indiceMostrandoAlbum].id+'/img/'+this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].archivo;
        }else{
            this.indiceMostrandoFoto=0;
            this.div_pie_de_foto.innerHTML=this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].nombre;
            $('imagen_galeria_album').src='administrador/img_site/galeria/'+this.albumes[this.indiceMostrandoAlbum].id+'/img/'+this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].archivo;
            
        }
    },
    anteriorFoto: function(){
        this.indiceMostrandoFoto--;
        if(this.indiceMostrandoFoto>=0){
            this.div_pie_de_foto.innerHTML=this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].nombre;
            $('imagen_galeria_album').src='administrador/img_site/galeria/'+this.albumes[this.indiceMostrandoAlbum].id+'/img/'+this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].archivo;
        }else{
            this.indiceMostrandoFoto=this.albumes[this.indiceMostrandoAlbum].fotos.length-1;
            this.div_pie_de_foto.innerHTML=this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].nombre;
            $('imagen_galeria_album').src='administrador/img_site/galeria/'+this.albumes[this.indiceMostrandoAlbum].id+'/img/'+this.albumes[this.indiceMostrandoAlbum].fotos[this.indiceMostrandoFoto].archivo;
            
        }
    }
});
Galeria.album=new Class({
    Implements: [Options],
    options: {
        nombre: '',
        fecha: '',
        id: -1,
        fotos: [],
        categorias: []
    },
    initialize: function(options){
        this.setOptions(options);
        Object.append(this,this.options);
        this.options=null;
    }
});
Galeria.foto=new Class({
    Implements: [Options],
    options: {
        nombre: '',
        archivo: ''
    },
    initialize: function(options){
        this.setOptions(options);
        Object.append(this,this.options);
        this.options=null;
    }
});
Galeria.categoria=new Class({
    Implements: [Options],
    options: {
        nombre: '',
        id: -1
    },
    initialize: function(options){
        this.setOptions(options);
        Object.append(this,this.options);
        this.options=null;
    }
});
Galeria.simple=new Class({
    Implements: [Options],
    options: {
        fotos: [],
        div_fotos: [],
        contenedor: '',
        indiceFoto: -1,
        idAlbum: 0,
        temporizador: null,
		duracion: 3000
    },
    initialize: function(options){
        this.setOptions(options);
        Object.append(this,this.options);
        this.options=null;
        this.crearDOM();
    },
    crearDOM: function(){
        var div_todo,i,itop;
        var yo=this;
        itop=this.fotos.length;
        div_todo=new Element('div',{
            styles:{
                'position':'relative',
                'left': '0',
                'top': '0',
                'width': '592px',
                'height': '434px'
            }
        }).inject(this.contenedor);
        for(i=0;i<itop;i++){
            this.div_fotos[i]=new Element('div',{
                styles:{
                    'background-image':'url("administrador/img_site/galeria/'+this.idAlbum+'/img/'+this.fotos[i].archivo+'")',
                    'left': '7px',
                    'top': '0',
                    'position': 'absolute',
                    'background-position': 'center',
                    'background-repeat': 'no-repeat',
                    'visibility': 'hidden',
                    'opacity': '0',
                    'width': '578px',
                    'height': '434px'
                }
            }).inject(div_todo);
        }
        /*new Element('div',{
            styles:{
                'background-image':'url("calendario/years_historia.png")',
                'left': '-23px',
                'top': '-32px',
                'position': 'absolute',
                'background-position': 'center',
                'background-repeat': 'no-repeat',
                'width': '174px',
                'height': '150px'
            }
        }).inject(div_todo);*/
        this.siguienteFoto();
        this.temporizador=setInterval('siguienteFotoSimple()',this.duracion);
    },
    siguienteFoto: function(){
        if(this.indiceFoto!=-1&&this.div_fotos[this.indiceFoto]!=null){
            this.div_fotos[this.indiceFoto].fade('out');
        }
        if(this.indiceFoto<(this.fotos.length-1)){
            this.indiceFoto++;
            this.div_fotos[this.indiceFoto].fade('in');
        }else{
            this.indiceFoto=0;
            this.div_fotos[this.indiceFoto].fade('in');
        }
    }
});
function siguienteFotoSimple(){
    la_galeria_simple.siguienteFoto();
}
