var GaleriaFotosApp = function()
{
	this.Extends = UIComponent;
	
	/*filhos*/

	this.btNext/*Element*/=null;
	this.btPrev/*Element*/=null;

	this.ulListaFotos/*Element*/=null;
	this.imagemAmpliada/*Element*/=null;
	this.btCloseGaleria/*Element*/=null;
	this.iconeMaisfotos/*Element*/=null;

	this.btEstrutura/*Element*/=null;
	this.btlazer/*Element*/=null;
	this.btAcomodacoes/*Element*/=null;

	this.btnPousadaEstrutura/*Element*/=null;
	this.btnPousadaLazer/*Element*/=null;
	this.btnPousadaAcomodacao/*Element*/=null;

	this.btnGaleriaHome/*Element*/ = null;

	this.btGaleriasMenu/*Element*/=null;
	this.btGaleriasRodape/*Element*/=null;

	this.bgGaleriaLayer/*Element*/=null;
	this.loadingDiv/*Element*/=null;
	
	this.initFirst/*Boolean*/

	/*fim filhos*/
	
	/*state*/
	this.idGaleria/*String*/=null;
	
	/*fim state*/
	
	// Public
	
	this.initialize = function(idGaleria){
		
		//if(idGaleria == "") return;

		this.parent("galery");

		this.setStyle("display","none");

		this.onClickNext = this.onClickNext.bindWithEvent(this);
		this.onClickPrev = this.onClickPrev.bindWithEvent(this);
		this.onClickThumb = this.onClickThumb.bindWithEvent(this);
		this.onClickCloseGaleria = this.onClickCloseGaleria.bindWithEvent(this);
		this.onClickMaisFotos = this.onClickMaisFotos.bindWithEvent(this);
		this.onResizeWindow= this.onResizeWindow.bindWithEvent(this);
		this.onLoadImagem= this.onLoadImagem.bindWithEvent(this);

		this.onClickBtEstrutura = this.onClickBtEstrutura.bindWithEvent(this);
		this.onClickBtLazer = this.onClickBtLazer.bindWithEvent(this);
		this.onClickBtAcomodacoes = this.onClickBtAcomodacoes.bindWithEvent(this);

		this.onClickBtMenuGalerias= this.onClickBtMenuGalerias.bindWithEvent(this);
		
		this.onClickTampaPreta= this.onClickTampaPreta.bindWithEvent(this);
		
		
	}
	
	this.startup=function(idGaleria/*String*/)/*void*/{

		//if(idGaleria == "") return;
		
		this.idGaleria = idGaleria;
		
		this.facade = GaleriaFotosFacade.getInstance(GaleriaFotosApp.NAME);
		
		this.initializeChildren();
		this.childrenInitialized();
		this.initializationComplete();
	}
	
	this.initializeChildren = function(){
		
		this.btNext = document.id("btNextPaginaGaleria");
		this.btPrev = document.id("btPrevPaginaGaleria");
		this.ulListaFotos = document.id("ulListaFotos");
		this.imagemAmpliada = document.id("imagemAmpliada");
		this.btCloseGaleria = document.id("btCloseGaleria");
		this.iconeMaisfotos = document.id("iconeMaisfotos");
		
		this.btEstrutura = document.id("bt-estrutura");
		this.btlazer = document.id("bt-lazer");
		this.btAcomodacoes = document.id("bt-acomodacoes");
		
		this.btnPousadaEstrutura = document.id("btnPousadaEstrutura");
		this.btnPousadaLazer = document.id("btnPousadaLazer");
		this.btnPousadaAcomodacao = document.id("btnPousadaAcomodacao");
		
		this.btnGaleriaHome = document.id("btnGaleriaHome");
		
		this.btGaleriasMenu = document.id("btGaleriasMenu");
		this.btGaleriasRodape = document.id("btGaleriaRodape");

		this.bgGaleriaLayer = document.id("layer-galery");
		this.loadingDiv = document.id("loadingDiv");
	}
	
	this.childrenInitialized = function(){

		this.btNext.addEvent("click",this.onClickNext);
		this.btPrev.addEvent("click",this.onClickPrev);
		this.btCloseGaleria.addEvent("click",this.onClickCloseGaleria);
		//this.iconeMaisfotos.addEvent("click",this.onClickMaisFotos);

		this.btEstrutura.addEvent("click",this.onClickBtEstrutura);
		this.btlazer.addEvent("click",this.onClickBtLazer);
		this.btAcomodacoes.addEvent("click",this.onClickBtAcomodacoes);
		this.btGaleriasMenu.addEvent("click",this.onClickBtMenuGalerias);
		this.btGaleriasRodape.addEvent("click",this.onClickBtMenuGalerias);
		this.bgGaleriaLayer.addEvent("click",this.onClickTampaPreta);

		if(this.btnPousadaEstrutura != null) this.btnPousadaEstrutura.addEvent("click", this.onClickBtEstrutura);
		if(this.btnPousadaLazer  != null) this.btnPousadaLazer.addEvent("click", this.onClickBtLazer);
		if(this.btnPousadaAcomodacao  != null) this.btnPousadaAcomodacao.addEvent("click", this.onClickBtAcomodacoes);

		if(this.btnGaleriaHome != null) this.btnGaleriaHome.addEvent("click", this.onClickBtEstrutura);
		window.addEvent("resize",this.onResizeWindow);
		
		this.imagemAmpliada.addEvent("load",this.onLoadImagem);
	}
	
	this.initializationComplete = function(){
		this.facade.startup(this);
		
		this.center();
	}
	
	this.updateImagemAmpliada=function(foto/*Foto*/)/*void*/{
		var prefixo =(foto.nome).toLowerCase();
		
		var fotoPath/*String*/ = "img/galeria/"+prefixo+"/"+foto.id+".jpg";
		//fotoPath = "lay/ampliada-galeria.jpg";
		//alert(fotoPath);
		
		this.imagemAmpliada.set("src",fotoPath);
		this.imagemAmpliada.setStyle("display","block");
		
		this.imagemAmpliada.setStyle("display","none");
		this.loadingDiv.setStyle("display","block");
		
	}
	
	this.updateListaFotos=function(fotos/*Array*/,plotFirst/*Boolean*/)/*void*/{

		if(plotFirst!=true) plotFirst = false;
		
		var buffer/*String*/ = "";
		var id/*String*/=null;
		var foto/*Foto*/ = null;
		var thumbPath/*String*/=null;
		var prefixo/*String*/=null;
		
		for(var i=0;i<fotos.length;i++){
			
			foto = fotos[i];
			prefixo=(foto.nome).toLowerCase();
			id = foto.id;
			thumbPath = Constants.path("img/galeria/"+prefixo+"/"+id+"_thumb.jpg");
			
			//thumbPath = "lay/thumb-home.jpg"
			
			buffer += '<li><a id="fotoGaleriaThumb'+id+'" href="javascript:void(0)"><img class="b-img" src="'+thumbPath+'" width="97" height="70" border="0" alt="" /></a></li>';
		}
		
		this.ulListaFotos.set("html",buffer);
		
		var elementoThumb/*Element*/ = null;
		var idFoto,idElemento;
		
		for(var i=0;i<fotos.length;i++){
			
			foto = fotos[i];
			
			idFoto = foto.id;
			idElemento = "fotoGaleriaThumb"+idFoto;
			
			elementoThumb = document.id(idElemento);
			
			elementoThumb.store("fotoVO",foto);
			elementoThumb.addEvent("click",this.onClickThumb);
		}
		
		if(plotFirst==true) this.updateImagemAmpliada(fotos[0]);
	}
	
	this.center = function(){
		
		var size/*Object*/ = window.getSize();
		
		var left/*Number*/ = size.x/2 - this.getStyle("width").toInt()/2;
		var top/*Number*/ = size.y/2 - this.getStyle("height").toInt()/2;
		
		this.setStyle("left",left);
		this.setStyle("top",top);
		
	}

	this.show=function(){
		var body_height = document.body.offsetHeight;
		var window_height = document.getSize().y;
		
		var bg_height = (body_height > window_height) ? body_height : window_height;

		this.setStyle("display","block");

		this.bgGaleriaLayer.setStyle("height", bg_height + "px");
		this.bgGaleriaLayer.setStyle("display", "block");
	
		this.center();
		
	}
	
	this.unselectAll=function(){
		
		var prefixo = "";
		var item = null; 
		
		for(var i=1;i<=3;i++){
			switch(i){
			
			case 1:
				prefixo = "acomodacoes";
				break;
			
			case 2:
				prefixo = "estrutura";
				break;
				
			case 3:
				prefixo = "lazer";
				break;
		
			}
			item = document.id("item-galeria-"+prefixo);
			bt = document.id("bt-"+prefixo);
			
			item.set("class","iten-on");
			bt.set("class","item-"+prefixo+"-on");
		}
		
	}
	
	this.selectGaleria=function(galeria/*int*/){
		this.unselectAll();
		var prefixo = "";
		var item = null;
		
		switch(galeria){
		
			case 1:
				prefixo = "acomodacoes";
				break;
			
			case 2:
				prefixo = "estrutura";
				break;
				
			case 3:
				prefixo = "lazer";
				break;
		
		}
		item = document.id("item-galeria-"+prefixo);
		bt = document.id("bt-"+prefixo);
		
		item.set("class","iten-off");
		bt.set("class","item-"+prefixo+"-off");
		
	}
	
	
	this.showSetas=function(){
		this.btNext.set("href","#");
		this.btPrev.setStyle("href","#");
	}

	this.hideSetaPrev=function(){
		this.btPrev.erase("href");
	}
	
	this.hideSetaNext=function(){
		this.btNext.erase("href");
	}
	
	// Private
	
	this.facade/*GaleriaFotosFacade*/ = null;

	//Handlers
	
	this.onClickTampaPreta=function(){
		this.onClickCloseGaleria();
	}

	this.onClickBtMenuGalerias=function(){
		this.show();
	}

	this.onLoadImagem=function(){
		this.imagemAmpliada.setStyle("display","block");
		this.loadingDiv.setStyle("display","none");
		
	}
	
	this.onClickMaisFotos=function(e){
		//this.show();
	}

	this.onClickCloseGaleria=function(e){
		this.setStyle("display","none");
		this.bgGaleriaLayer.setStyle("display", "none");
	}
	
	this.onResizeWindow=function(e){
		this.center();
	}
	
	this.onClickThumb = function(e/*Event*/)/*void*/{
		
		var elemento/*Element*/ = e.target.getParent();
		
		var foto/*Foto*/ = elemento.retrieve("fotoVO");
		
		this.fireEvent(GaleriaFotosApp.CLICK_THUMB_FOTO,foto);
		
	}
	
	this.onClickNext = function(e/*Event*/)/*void*/{
		this.fireEvent(GaleriaFotosApp.CLICK_NEXT_PAGINA);
		
	}

	this.onClickPrev = function(e/*Event*/)/*void*/{
		this.fireEvent(GaleriaFotosApp.CLICK_PREV_PAGINA);
	}
	
	this.onClickBtEstrutura = function(e/*Event*/)/*void*/{
		/*this.idGaleria = "2";
		this.facade.sendNotification(GaleriaFotosFacade.LOAD_XML_GALERIA,{pagina:1,galeria:2,init:true});*/
		
		this.fireEvent(GaleriaFotosApp.CLICK_GALERIA,2);
		this.show();
	}
	
	this.onClickBtLazer = function(e/*Event*/)/*void*/{
		/*this.idGaleria = "3";
		this.facade.sendNotification(GaleriaFotosFacade.LOAD_XML_GALERIA,{pagina:1,galeria:3,init:true});*/
		
		this.fireEvent(GaleriaFotosApp.CLICK_GALERIA,3);
		this.show();
	}
	
	this.onClickBtAcomodacoes = function(e/*Event*/)/*void*/{
		/*this.idGaleria = "1";
		this.facade.sendNotification(GaleriaFotosFacade.LOAD_XML_GALERIA,{pagina:1,galeria:1,init:true});*/
		
		this.fireEvent(GaleriaFotosApp.CLICK_GALERIA,1);
		this.show();
	}
}

GaleriaFotosApp = new Class(new GaleriaFotosApp());
//consts

GaleriaFotosApp.NAME = "galeriaFotosApp";

//eventos

GaleriaFotosApp.CLICK_NEXT_PAGINA/*String*/ = "clickNextPagina";
GaleriaFotosApp.CLICK_PREV_PAGINA/*String*/ = "clickPrevPagina";
GaleriaFotosApp.CLICK_THUMB_FOTO/*String*/ = "clickThumbFoto";
GaleriaFotosApp.CLICK_GALERIA/*String*/ = "clickGaleria";



