//----- GALERIA ESPECIAL
function escreveInfo(obj){
	var a = obj
	var li = jQuery('#nav-fotos li.selected')
	var info = jQuery('#foto #info')
	var credito = li.find('.credito')
	
	info.find('.ativo').trigger('click');
	if(credito.length > 0){ info.find('.credito').css('display','inline').find('span').html(credito.html());
	} else { info.find('.credito').css('display','none'); }
	
	var url_img = window.location.href.split("#")[0]+'#'+a.attr('id')
	info.find('.url input').val(url_img).hide();
	info.find('.incorporar input').val('<a href="'+url_img+'"><img alt="'+a.attr('title')+' - Adoro Cinema" src="'+a.attr('href')+'" /></a>').hide();
	
	info.find('.titulo').hover(function(){
		jQuery('span', this).css({opacity: 0, display: 'block'}).stop().animate({opacity: 1}, 300);
	}, function(){
		jQuery('span', this).stop().animate({opacity: 0}, 300);
	});
	
	//Add classe para liberar uso das setas do teclado dentro do input
	info.find('input').focus(function(){ jQuery(this).prev().addClass('s3-off'); }).blur(function(){ jQuery(this).prev().removeClass('s3-off'); });
	
	info.show();
}

function clickinfo_acao(obj){
	if(obj.hasClass('ativo')){ obj.removeClass('ativo').parent().find('input').hide(); return; }
	obj.addClass('ativo s3-off').parent().find('input').show().select();
}

function foto_show(){
	var foto = jQuery('#foto img')
	foto.css({opacity: 0}).load(function(){
		var a = jQuery(this)
		var fw = foto.width()
		
		alert(fw);
		
		jQuery('#foto #img').stop().animate({width: fw}, 500, function(){
			jQuery('#foto img').stop().animate({opacity: '1'}, 700);
		});
	});
}

jQuery(document).ready(function(){
	//alert('ok');
	jQuery(document.documentElement).keyup(function (event) {
	  // handle cursor keys
	  if (event.keyCode == 37) {
		  if(jQuery('#info .titulo.s3-off').length > 0){ return; }
		  var btn = jQuery('.seta3.esq')
		  btn.trigger('click');
	  } else if (event.keyCode == 39) {
		  if(jQuery('#info .titulo.s3-off').length > 0){ return; }
		  var btn = jQuery('.seta3.dir')
		  btn.trigger('click');
	  } else if (event.keyCode == 27) {
		  if( jQuery('#fechar').hasClass('inativo') ){ return; }
		  jQuery('#fechar').trigger('click');
	  }
	});
						   
	//----- GALERIA ESPECIAL
	//Dados Body
	var wTela = jQuery(window).width()
	var hTela = jQuery(window).height()
	var margTop = jQuery('body').css('marginTop');
	
	//Prepara div#galeria-especial - container para o conteudo
	//left:'+wTela+'px; width:'+wTela+'px; height:'+jQuery('body').height()+'px;
	
	jQuery('#galeria-especial').css({width: wTela+'px', height: hTela+'px'});
	jQuery('#nav-fotos').css({width: wTela+'px'});
	
	//Body reset
	jQuery('body').attr('id', 'galeria-body');
	
	//Div de container para as Imagens
	//jQuery('body').append('<div id="galeria-especial" style="display:none; left:'+wTela+'px; width:'+wTela+'px; height:'+jQuery('body').height()+'px;"><div id="fechar" class="inativo">fechar</div> <div class="seta3 esq"></div> <div class="seta3 dir"></div> <div id="titulo"></div> <div id="foto" style="display:none;"><div id="img"></div><div id="ac-video" style="display:none;"></div><div id="info"> <span class="credito">Fonte: <span></span></span> <span class="url"><span class="titulo">URL<span>Ver URL da Imagem</span></span> <input type="text" value="" /></span> <span class="incorporar"><span class="titulo">Publique em seu Site<span>Obter o código da Imagem para inserir no seu Site</span></span> <input type="text" value="" /></span></div></div> <div id="nav-fotos" style="display:none; width:'+wTela+'px;"><span id="min"><span class="btn"><strong>Esconder</strong> <strong></strong></span></span><span class="seta2 esq"></span><div class="container"><ul></ul></div><span class="seta2 dir"></span></div></div>');
	
	var galeria = jQuery('#galeria-especial');
	if(wTela > 1200){ galeria.addClass('bg1280'); }
		
	function acoes(obj, tipo){
		var a = obj
		var hTela = jQuery(window).height()-190
		var wTela2 = jQuery(window).width()
		
		if (tipo == 'imagens'){
		
			a.parent().parent().addClass('selected');
			
			//Foto			
			jQuery('#galeria-especial #foto').css({display: 'block', width: wTela2}).find('#img').css({display:'block',opacity:0}).html('<img src="'+a.attr('href')+'" />');
			
			//Mostra Foto
			var foto = jQuery('#foto img')
			foto.css({opacity: 0}).load(function(){
				
				//verifica altura da imagem em relacao a tela do usuario
				if(foto.height()>hTela){
					jQuery('#foto #img').height(hTela).find('img').height('100%');
				}
				
				var fh = foto.height()
				var fw = foto.width()
				jQuery('#foto #img').stop().animate({height: fh, width: fw, opacity:1}, 500, function(){
					foto.stop().animate({opacity: '1'}, 700);
				});
				
			});
			
			//Menu - copiando
			jQuery('#nav-fotos').css('width',wTela2).attr('class','foto').css({display:'block',opacity:'0'}).find('div.container').width(wTela2-42);
			
			//Posicionando Setas Grandes
			jQuery('#galeria-especial .seta3').css({top: hTela/2, display: 'block', opacity: '0'});
			
		} else {
			a.parent().addClass('selected');
			var id_video = a.attr('id');
			
			//Prepara container vídeo
			jQuery('#galeria-especial #foto').css({display:'block'}).find('#ac-video').css({opacity:'1', width:'610px', height:'380px', display:'block'});
			
			//Mostra Vídeo
			jQuery('#ac-video').show().find('div#'+id_video).show();
			
			//Menu - copiando itens
			jQuery('#nav-fotos').attr('class','video').css({display:'block',opacity:'0'}).find('div.container').width(wTela-42);
						
		}
		
		var navFotos = jQuery('#nav-fotos')
		
		if(jQuery('#f-'+tipo+' li.bloco').length > 0){
			jQuery('#f-'+tipo+' li.bloco ul').each(function(){
				navFotos.find('.container ul').append(jQuery(this).html());
			});
		} else {
			navFotos.find('.container ul').append(jQuery('#f-'+tipo+' ul').html());
		}
		
		var li = navFotos.find('li:not(li.bloco)')
		var wLi = li.outerWidth()
		navFotos.find('ul:first').width(li.length*wLi);
		
		if(navFotos.find('ul:first').width() < navFotos.find('.container').width()){ navFotos.find('.seta2.dir').addClass('inativo'); }
		
		//Fixa opacity dos thumbs e play-icone
		navFotos.find('li.selected img, li.selected .play-icone').css('opacity','1');
		
		clearTimeout();
		setTimeout(function(){
			var exibir = jQuery('#nav-fotos')
			if(tipo == 'imagens'){ var exibir = jQuery('#nav-fotos, #galeria-especial .seta3') }
			
			exibir.animate({opacity: '1'}, 500);
                        
			//Ativa Fechar
			jQuery('#fechar').css({display:'block'}).removeClass('inativo');
			
		}, 1000);
				
		exibirSelecionado();
		
		clickfechar();
		clickimg();
		hovers();
	}
	
	jQuery('#f-imagens a').click(function(){
		var a = jQuery(this)
		
		jQuery('#galeria-especial').css({opacity:'0', display:'block', left:'0'}).stop().animate({opacity:1}, 500, function(){
			var img = a.find('img')
			
			//Titulo
			jQuery('#galeria-especial #titulo').html(a.attr('title')).css({display: 'block'});
			
			acoes(a,'imagens');
			
			//Escreve INFOs
			escreveInfo(a);
		});
		return false;
	});
	
	jQuery('#f-videos a').click(function(){
		var a = jQuery(this)
		
		jQuery('#galeria-especial').css({opacity:'0', display:'block', left:'0'}).stop().animate({opacity:1}, 500, function(){
			var id_video = a.attr('id')
			
			jQuery('#foto, #ac-video').show();
			
			//Titulo
			jQuery('#galeria-especial #titulo').html(a.attr('title')).css({display: 'block'});
			
			acoes(a,'videos');
			
			//Esconder INFO
			jQuery('#info').hide();
		});
		return false;
	});
	
	function clickimg(){
		jQuery('#nav-fotos.foto a').click(function(){
			var cFoto = jQuery('#foto #img')
			
			var li = jQuery(this).parent().parent()			
			if(li.hasClass('selected')){ return false; }
			
			//Dimensoes da Tela
			var wTela = jQuery(window).width()
			var hTela = jQuery(window).height()-190
			
			jQuery('#foto').width(wTela);
			
			//Escreve Título
			var titulo = jQuery(this).attr('title')
			jQuery('#titulo').css({opacity:0, width:wTela-300}).html(titulo).stop().animate({opacity: 1}, 500);
			
			//Prepara Container da Foto
			cFoto.find('img').remove();
			cFoto.html('<img src="'+jQuery(this).attr('href')+'" style="display:none;" />');
			
			cFoto.find('img').css({opacity: 0,display: 'block'}).load(function(){
				
				var nImg = cFoto.find('img')
				var fw = nImg.width();
				var fh = nImg.height();
				
				if(fh>hTela){
					jQuery(this).height(hTela);
					var nImg = jQuery('#foto img')					
					cFoto.stop().animate({width: nImg.width(), height: nImg.height()}, 400);
				} else {
					cFoto.stop().animate({width: fw, height: fh}, 400);
				}
				
				setTimeout(function(){ jQuery('#foto img').stop().animate({opacity: '1'}, 500); }, 500);
							
			});
			
			//Posiciona Seta3 (grandes)
			//jQuery('#galeria-especial .seta3').stop().animate({top: fh/2}, 500);
			
			clicksetas();
			
			jQuery('#nav-fotos li.selected').removeClass('selected').find('img').css('opacity','0.7');
			li.addClass('selected').find('img').css('opacity','1');
			
			//Escreve INFOs
			escreveInfo(jQuery(this));
			
			return false;
		});
		
		jQuery('#nav-fotos.video a').click(function(){
			var cVideo = jQuery('#foto #ac-video')
			var id_video = jQuery(this).attr('id');
			
			//Retorna se item ja esta clicado
			var li = jQuery(this).parent()			
			if(li.hasClass('selected')){ return false; }
						
			//Escreve Título
			var titulo = jQuery(this).attr('title')
			jQuery('#titulo').stop().animate({opacity: 0}, 500, function(){
				jQuery('#titulo').html(titulo).animate({opacity: '1'}, 500);
			});
			
			//Mostra video referente ao item clicado
			cVideo.find('div:visible').hide();
			cVideo.find('div#'+id_video).show();

			//Marca item do Menu
			jQuery('#nav-fotos li.selected').removeClass('selected').find('img, .play-icone').css('opacity','0.7');
			li.addClass('selected').find('img, .play-icone').css('opacity','1');
			
			clicksetas();
			
			return false;
		});
	}
	
	function hovers(){		
		jQuery('#f-imagens a img, #f-videos a img, #f-videos a .play-icone').css({opacity:0.7,display:'block'});
		
		jQuery('#nav-fotos.foto li, #f-imagens li:not(li.bloco), #nav-fotos.video li, #f-videos li:not(li.bloco)').hover(function(){
			jQuery('img, .play-icone', this).stop().animate({opacity:'1'},300);
		},function(){
			if(jQuery(this).hasClass('selected')){ return; }
			jQuery('img, .play-icone', this).stop().animate({opacity:'0.7'},300);	
		});
	}
	hovers();
	
		
	function clickfechar(){
		jQuery('#fechar').click(function(){
			jQuery(this).addClass('inativo');
			jQuery('#fechar, #nav-fotos, .seta3, #foto').css({display: 'none'});
			jQuery('#galeria-especial').stop().animate({opacity:0}, 500, function(){
				//jQuery(this).css({opacity: 1, left: wTela+'px'});
				jQuery(this).css({display: 'none'});
				jQuery('#nav-fotos .seta2, #galeria-especial .seta3').removeClass('inativo');
				
				jQuery('#foto #img, #nav-fotos .container ul:first').html('').attr('style','');
				jQuery('#titulo, #foto, #foto div').css({display: 'none'});
				jQuery('#f-imagens .selected, #f-videos .selected').removeClass('selected');
			});
		});
	}
	
	jQuery('#min .btn').click(function(){
		var nav = jQuery('#nav-fotos')
		var a = jQuery(this)
		if(jQuery(this).hasClass('ativo')){
			nav.css({bottom: 0});
			a.removeClass('ativo').find('strong:first').html('Esconder');
			return false;
		}
		a.addClass('ativo').find('strong:first').html('Exibir');
		var h = nav.height()
		nav.css({bottom: '-110px'});
	});
	
	function clicksetas(){
		jQuery('#nav-fotos .seta2').click(function(){
			var a = jQuery(this)
			if(a.hasClass('ativo') || a.hasClass('inativo')){ return; }
			a.addClass('ativo');
			jQuery('#nav-fotos .seta2').removeClass('inativo');
			
			var wC = jQuery('#nav-fotos .container').width()
				var ul = jQuery('#nav-fotos .container ul:first')
			var wUl = ul.width()
			var margUl = parseFloat(ul.css('marginLeft'))
			
			if(a.hasClass('dir')){
				var margMov = (wUl+margUl)-wC
				
				if(margMov > wC){ var margMov = wC }

				ul.stop().animate({marginLeft: margUl-margMov}, 700, function(){
					a.removeClass('ativo');
					var margUl = parseFloat(ul.css('marginLeft'))
					if(margUl-wC == -wUl){ a.addClass('inativo'); }
				});
			} else {
				var margMov = margUl+wC
				if(margMov > 0){ var margMov = 0 }
				
				ul.stop().animate({marginLeft: margMov}, 700, function(){
					a.removeClass('ativo');
					var margUl = parseFloat(ul.css('marginLeft'))
					if(margUl == 0){ a.addClass('inativo'); }
				});
			}
			
		});
	}
	clicksetas();
	
	function setas_grandes(){
		jQuery('.seta3').click(function(){
			var li = jQuery('#nav-fotos li.selected')
			//Pra retornar qndo a seta do teclado for usada mas a Galeria nao estiver ativa
			if(li.length == 0){ return; }
			
			var margUl = parseFloat(jQuery('#nav-fotos ul:first').css('marginLeft'))
			var wC = jQuery('#nav-fotos .container').width()
						
			if(jQuery(this).hasClass('dir')){
				var liClick = li.next()
				if (liClick.position().left+liClick.width() > wC){
					var margMov = margUl-liClick.position().left
					var wUl = jQuery('#nav-fotos ul:first').width()
					var falta = wUl+margUl-wC
					if( falta < wC){ var margMov = margUl-falta }
					jQuery('#nav-fotos ul:first').animate({marginLeft: margMov}, 700);
					
					//Desativa Seta2 - Dir
					if(-margMov+wC >= wUl){ jQuery('#nav-fotos .seta2.dir').addClass('inativo'); }
					
					jQuery('#nav-fotos .seta2.esq').removeClass('inativo');
				}				
			} else {
				var liClick = li.prev()
				
				if (liClick.position().left < 0){
					var margMov = margUl+wC-(liClick.width()+liClick.position().left)
					//var margMov = margUl+wC
					if(-margMov < liClick.width()){ jQuery('#nav-fotos .seta2.esq').addClass('inativo'); margMov = 0  }
					jQuery('#nav-fotos ul:first').animate({marginLeft: margMov}, 700);
										
					jQuery('#nav-fotos .seta2.dir').removeClass('inativo');
				}			
			}
			
			liClick.find('a').trigger('click');			
			
		});
	}
	setas_grandes();
	
	function exibirSelecionado(){
		
		var ul = jQuery('#nav-fotos ul:first')
		var li = ul.find('li.selected')
		var margUl = parseFloat(ul.css('marginLeft'))
		var margMov = li.position().left-margUl
		var wC = jQuery('#nav-fotos .container').width()
		
		setTimeout(function(){
			if (margMov+li.width() > wC){
				
				if( wC+margMov > ul.width() ){
					margMov = ul.width()-wC
				}
				
				ul.animate({marginLeft: -margMov}, 700);
				//Problema aqui na desativação da seta Dir
				if(li.position().left+wC > ul.width()){ jQuery('#nav-fotos .seta2.dir').addClass('inativo'); }
			} else {
				jQuery('#nav-fotos .seta2.esq').addClass('inativo');
			}
		}, 1000);
		
	}
	
	jQuery('#info span.titulo').click(function(){ clickinfo_acao(jQuery(this)); });
	
	//Faz ancora da URL clicar na IMG
	var ancora = window.location.href.split("#")[1];
	if(ancora) {
		//alert(ancora);
		jQuery('#'+ancora).trigger('click');
	}
	
});

jQuery(window).resize(function() {
	
	jQuery('#galeria-especial').height(jQuery(window).height());
	
	var hTela = jQuery(window).height()-190
	var wTela2 = jQuery(window).width()
	
	//Width - Itens interface - container galeria e menu thumbs
	jQuery('#galeria-especial, #nav-fotos').css('width',wTela2).find('div.container').width(wTela2-42);
	
	//Height - Itens interface >> retorna se já está no máximo
	if(hTela == jQuery('#foto #img').height() || hTela < 300){ return; }
	
	//Posiciona Titulo e Setas Grandes
	jQuery('#foto').width(wTela2);
	jQuery('#titulo').width(wTela2-300);
	jQuery('#galeria-especial .seta3').stop().animate({top: hTela/2}, 500);
		
	//Reload Img
	var img = jQuery('#foto img').attr('src')
	var cImg = jQuery('#foto #img')
	cImg.find('img').remove();
	cImg.html('<img src="'+img+'" style="opacity: 0;" />');
	
	jQuery('#foto img').load(function(){
		var a = jQuery(this)
		var nH = hTela
		if(hTela > a.height()){
			var nH = a.height();
		} else if(hTela < 300){
			var nH = 300
		}
		
		cImg.height(nH).find('img').height('100%');
		
		cImg.stop().animate({width: a.width()}, 500);
		setTimeout(function(){ a.stop().animate({opacity: '1'}, 700); }, 500);
	
	});
	
});

/* Carregar box de comentarios 

O QUE ISSO ESTA FAZENDO AQUI???

*/
jQuery(function($){
    jQuery('span.coment-responder.logado').click(function(){
        
        var comentId = jQuery(this).parent().parent().attr('id');

        if(jQuery('div#'+comentId+' #box-form').length == 0){
            jQuery('div#'+comentId+' span.coment-responder.logado:eq(0)').before('<div id="box-form" class="box-form clearfix">'+jQuery('#comentar div.box-form').html()+'</div>');
            $('div#' + comentId + ' #box-form > form').append('<input type="hidden" name="id_comentario" value="' + comentId + '" />');
        }
        estrelasHover();
    });
})
