﻿/**
* Inicializa as funções assim que os elementos (DOM) são carregados
* @author Leonardo Souza <leonardo.souza@agenciaclick.com.br>
*/
jQuery(function() {
	Post._init();
});


var Post = {
	/**
	* Função de chamada das outras funções que inicializam o site
	* author Leonardo Souza <leonardo.souza@agenciaclick.com.br>
	*/
	_init: function() {
		//try {
			Post._comentarios();
			Post._topicos();
			Post._validate();
		//} catch (e) {
		//	console.log('Error: ' + e.description);
		//}
	},
	
	
	/**
	* Validate dos comentarios
	* author: Danilo Augusto <danilo.martins@agencliaclick.com.br >
	*/
	_validate: function() {
		$('#comentario_post').validate();
	},
	
	
	/**
	* Controle de comentários
	* author: Danilo Augusto <danilo.martins@agencliaclick.com.br >
	*/
	_comentarios: function() {
	
	
		if(readCookie('user_nicename')==null){		
			$('.comentario_fechado').show();
		} else {
			$('.comentario_aberto').show();			
		
			// VALIDAÇÂO
			$('#comentario_post').submit(function(){
				if($('#comment').val()==''){
					$('#comment').after('<span class="error">'+traducao.este_campo_e_obrigatorio+'</span>');
					$('#comment').keydown(function(){
						if($('#comment').next().hasClass('error')){
							$('#comment').next().remove();
						}
						$('#comment').unbind();
					});
					return false;
				}
			});
			
			// PROCURANDO COOKIE
			var idPost = $('input[name="comment_post_ID"]').val();
			var erro_comentario = 'error_comment_'+idPost;
			if(readCookie(erro_comentario)!=null){
				$('#comment').val(unescape(readCookie(erro_comentario)).replace(/\+/g,' '));
				createCookie(erro_comentario,'',-1);
			}
			
			
			Facebook._postarComentario();
			
			
		}
		
		
		
		if(readCookie('user_avatar')!=null){
			var src_avatar = readCookie('user_avatar').toString().replace(/%2F/g,'/');
			var htm_avatar = '';
			htm_avatar += '<div class="autor_imagem">';
			htm_avatar += '		<img src="'+src_avatar+'" alt="Seu avatar"/>';
			htm_avatar += '</div>';
			
			$('.comentario_aberto').append(htm_avatar);
		}
		
		

		$('#tipo_comentario a').click(function(){
			$('#tipo_comentario a').removeClass();
			$(this).addClass('selected');
			var estilo_form = $(this).attr('id');
			$('#estilo_formulario').removeClass().addClass(estilo_form);
			
			if($('#estilo_facebook').hasClass('selected')){
				$('#confirmar_post_facebook').attr('checked','checked');
			} else {
				$('#confirmar_post_facebook').attr('checked','');			
			}
			return false;
		});
		
		
		$('#confirmar_post_facebook').click(function(){
			if($(this).is(':checked')){
				$('#postar').hide();
				$('#comentario_post a.fb_button').show();
			} else {
				$('#postar').show();
				$('#comentario_post a.fb_button').hide();			
			}
		});
		
		if(readCookie('fbs_'+Facebook.API_ID)!=null){	
			$('#estilo_facebook').click();
			$('.logado_como').show();
			
		} else {
			$('#estilo_normal').click();		
		}
	},
	

	
	/**
	* author: Danilo Augusto <danilo.martins@agencliaclick.com.br >
	*/
	_topicos: function() {
		$('a.toogle_repostas').click(function(){
			var topico = $(this).parents('.topico');
			if(topico.hasClass('comentarios_aberto')){
				topico.removeClass('comentarios_aberto').find('.respostas_do_topico').slideUp();
				$(this).addClass('abre_respostas').removeClass('fecha_respostas');				

			} else {
				topico.addClass('comentarios_aberto').find('.respostas_do_topico').slideDown();
				$(this).removeClass('abre_respostas').addClass('fecha_respostas');				
			}
			return false;
		});
	}
	
};

