function Galeria(){
    this.url = "";
    this.siteUrl = "";
    this.nome = "";
    this.lista = new Array();
	this.posicaoAtual = 0;
	this.fotoAtual = 0;
	this.numeroPorPagina = 5;
	this.timeout = null;
    
    this.iniciar = function( siteUrl, id ){
    	this.siteUrl = siteUrl;
    	this.url = this.siteUrl + "galeria/json/" + id;
        $( '#nomeGaleria' ).html( "<img src=\"" + this.siteUrl + "img/galeria/ajax-loader-galeria.gif" + "\"/>" );
        $.getJSON( this.url, function( data ){
			galeria.nome = data.nome;
			galeria.lista = data.fotos;
			
			$( '#nomeGaleria' ).text( galeria.nome );
			galeria.atualizarThumbs();
			galeria.mostrarFoto( 0 );
        });
    }    
	
	this.mostrarAnteriores = function(){
		this.posicaoAtual -= this.numeroPorPagina;
		if( this.posicaoAtual < 0 ){
			this.posicaoAtual = 0;
		}
		this.mostrarLista();
	}
	
	this.mostrarProximas = function(){
		this.posicaoAtual += this.numeroPorPagina;
		if( this.posicaoAtual >= this.lista.length ){
			this.posicaoAtual = ( Math.floor( ( this.lista.length - 1 ) / this.numeroPorPagina ) ) * this.numeroPorPagina;
		}
		this.mostrarLista();	
	}
	
	this.atualizarThumbs = function(){
		var html = "";
		var foto = null;
		
		var onClick = "";
		var src = "";
		
		for( i = 0; i < this.lista.length; i++ ){		
			foto = this.lista[ i ];
			onClick = "galeria.mostrarFoto(" + i + ");";
			src = this.siteUrl + "foto/thumb/" + foto.id;
			
			html += "<img onClick=\"" + onClick + "\" src=\"" + src + "\" width=\"75\" height=\"50\" >";
		}		
		
	    $( '#thumbsGaleria' ).html( html );
	    ajustaAlpha( 'carrossel', 0.5 );
	}
	
	this.alternarLista = function () {
		if( this.lista.length == 0 && this.timeout == null ){
			this.timeout = setTimeout( "galeria.alternarLista()", 2000 ); 
		}else{
			if( $( '#setinha' ).attr( 'src' ).match(/setinhaBaixo/) ){
				$( '#setinha' ).attr( 'src', this.siteUrl + "img/galeria/setinha.gif" );
			}else{
				$( '#setinha' ).attr( 'src', this.siteUrl + "img/galeria/setinhaBaixo.gif" );			
			}			
			$( '#carrossel' ).slideToggle( 'normal' );
		}
	}
	
	this.mostrarFoto = function( indice ){
		this.fotoAtual = indice;
		
		if( this.fotoAtual >= this.lista.length ){
			this.fotoAtual = 0;
		}else if( this.fotoAtual < 0 ){
			this.fotoAtual = this.lista.length - 1;
		}
		
		var foto = this.lista[ this.fotoAtual ];		
		var html = "";		
		
		var boxLegenda = $( '#legendaGaleria' );
		var elementoBaixarFoto = $( '#galeriaBaixarFoto' );
		var elementoFoto = $( '#foto' );
		var elementoLegenda = $( '#legendaGaleriaTxt' );
		
		boxLegenda.hide( 'normal' );
		
		if( foto ){
			elementoBaixarFoto.attr( "href", this.siteUrl + "foto/visualizar/" + foto.id );		
			elementoFoto.attr( "src", this.siteUrl + "foto/galeria/" + foto.id );		
			elementoLegenda.text( foto.legenda );
		}else{
			elementoBaixarFoto.attr( "href", "" );		
			elementoFoto.attr( "src", this.siteUrl + "img/galeria/sem_imagem.gif" );		
			elementoLegenda.text( "Galeria Vazia" );		
		}
		
	    ajustaAlpha( 'legendaGaleria', 0.5 );
	    ajustaAlpha( 'legendaGaleriaTxt', 0.75 );
		
		boxLegenda.slideToggle( 'normal' );	
	}
	
	this.mostrarProximaFoto = function(){
		this.fotoAtual++;
		this.mostrarFoto( this.fotoAtual );
	}
	
	this.mostrarFotoAnterior = function(){
		this.fotoAtual--;
		this.mostrarFoto( this.fotoAtual );
	}	
	
	this.movimentaCarrossel = function( area ){
		var thumbs = $('#thumbsGaleria');
		var indentacaoAtual = parseInt( thumbs.css( 'text-indent' ) );
		if( area.id == "esquerda" ){
		    indentacaoAtual += 100;
		    if( indentacaoAtual > 0 )
		     	indentacaoAtual = 0;
		}else{			
		    indentacaoAtual -= 100;
		    if( indentacaoAtual < ( this.lista.length - 5 ) * 90 * -1 )
		    	indentacaoAtual = ( this.lista.length - 5 ) * 90 * -1;
		}
		thumbs.animate( { textIndent: indentacaoAtual + "px" }, 750 );
	}
}	

function ajustaAlpha( id, alpha ){
	elemento = document.getElementById( id );
	if ( elemento.filters ) {
		if ( !elemento.style.filter.match(/alpha/i) ) {
			elemento.style.filter += ' alpha(opacity=' + ( alpha * 100 ) + ')';
		} else if ( elemento.filters.length && elemento.filters.alpha ) {
			elemento.style.filter = elemento.style.filter.replace(/alpha\(opacity=\d+\)/ig, 'alpha(opacity=' + (Math.floor( ( alpha * 100 ) )) + ')');
		}
	} else {      
		elemento.style.opacity = elemento.style.MozOpacity = alpha;
	}
} 

var galeria = new Galeria();