/**
 * Script de controle de funções do site;
 * @author Julio Vedovatto <julio@juliovedovatto.com>
 */
;(function($){
	window.NaSocial = {
		init: function() {
			this.Duplas.init();
			this.FAQ.init();
		},
		Duplas: {
			init: function() {
				if (!$('div#body').is('.as-duplas'))
					return false;

				$('div#body.as-duplas div.dupla ul.menu li a').click(function(){
					var div_id = this.href.replace(/^.*#/g,'');
					if (($(this).parent().is('.ativo')) || ($.trim(div_id).length === 0))
						return false;

						$(this).parents('div.dupla')
							.find('div.participante')
							.hide()
							.filter('div#participante-' + div_id)
								.show();

					$(this).parents('ul').find('li').removeClass('ativo').filter($(this).parent()).addClass('ativo');
					this.blur();
					return false;
				});
			},
			getTwitter: function() {
				$.Juitter.start({
					searchType:'fromUser', // needed, you can use 'searchWord', 'fromUser', 'toUser'
					searchObject: this.id, // needed, you can insert a username here or a word to be searched for, if you wish multiple search, separate the words by comma.
					lang:'pt',
					live:'live-15', // the number after 'live-' indicates the time in seconds to wait before request the Twitter API for updates.
					placeHolder:'twitter-participante-' + this.id + '-tweets',
					loadMSG: 'image/gif',
					imgName: 'imagens/componentes/twitter/loader.gif',
					total: 5,
					readMore: 'veja no Twitter',
					nameUser:'image',
					openExternalLinks: 'newWindow',
			        filter:'sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*',  // insert the words you want to hide from the tweets followed by what you want to show instead example: 'sex->censured' or 'porn->BLOCKED WORD' you can define as many as you want, if you don't want to replace the word, simply remove it, just add the words you want separated like this 'porn,sex,fuck'... Be aware that the tweets will still be showed, only the bad words will be removed
			        details: false
				});
			}
		},
		FAQ: {
			init: function(){
				if (!$('div#body').is('.faq'))
					return false;

				this.submitForm();
				this.faqTree();

				$('input#email').keypress(function(event){
					return inputFilter(event, new RegExp(/[0-9a-z\@\.\+\-_]/));
				});
			},
			faqTree: function() {
				$('ol.perguntas-frequentes li a.pergunta')
					.click(function(){
						if ($(this).is('ativo'))
							return false;

						$(this).parents('ol.perguntas-frequentes').find('div.resposta').hide();
						$(this).parents('ol.perguntas-frequentes').find('a.pergunta.ativo').removeClass('ativo');
						$(this).addClass('ativo').next().show();

						return false;
					})
			},
			submitForm: function() {
				$('form#form-faq').submit(function(e){
					var form = this,
						ok = 1,
						nome = form['nome'].value,
						mail = form['email'].value,
						duvida	= form['duvida'].value;

					if (!nome) {
						popup_msg('Especifique o seu nome!');
						form['nome'].focus();
						ok = 0;
					} else if (!mail.match(/^([A-Za-z0-9_\.\-\+]+)@([0-9A-Za-z\.\-]+)\.([A-Za-z\.]{2,6})$/)) {
						popup_msg('Especifique o seu e-mail!');
						form['email'].focus();
						ok = 0;
					} else if (!duvida) {
						popup_msg('Especifique a sua dÃºvida!');
						form['duvida'].focus();
						ok = 0;
					}

					if (ok) {
						var post = '';

						post += 'nome=' + escape(nome);
						post += '&mail=' + escape(mail);
						post += '&duvida=' + escape(duvida);

						$.post('ajax/faq.php',post,function(o){popup_msg(o);});
					}

					if (typeof e.preventDefault != 'undefined')
						e.preventDefault();
					else
						e.returnValue = false;
					return false;
				});
			}
		}
	};

	$(function(){ NaSocial.init(); });

/** FUNCOES RECICLADAS DO SITE ANTIGO **/

function inputFilter(e, reg) {
	var key = window.event ? e.keyCode : e.which;
	switch (key) {
		case 0:
		case 8:
			return true;
	}

	var keychar = String.fromCharCode(key);
	if (!reg.test(keychar)) {
		if (typeof e.preventDefault != 'undefined')
			e.preventDefault();
		else
			e.returnValue = false;
		return false;
	} else
		return true;
}

function popup_msg(msg) {
	$('div#faq_status')
		.html(msg == 'ok'
				? '<strong>Dúvida enviada!</strong><br />Aguarde, responderemos em breve!'
				: '<strong>Erro no envio!</strong><br />' + msg
		);
}

/** /FUNCOES RECICLADAS DO SITE ANTIGO **/

})(jQuery);