﻿/** BY WELLINGTON RIBEIRO
/** wellington_mdd@yahoo.com.br
/** EZ LEARN 2008
**/

// FUNÇÃO PARA CHECAR SE UMA STRING DE E-MAIL É VÁLIDA

$('document').ready(function(){
	png2gif();
});

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return mail; } else { return false; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
            return mail;
        } else { return false; }
    }else{
        return false;
    }
}

//Função para carregar o sistema de apagar comentários
function loadBtnApagar(){
	$(".bt_apagar").click(function(){
	var id=$(this).attr("id");
		idCom=id.split("_");
		$('#comment_'+idCom[2]).css('border','1px solid red');
		if(confirm("Você realmente deseja apagar este comentário?")){
			if (typeof(thisURL) == 'undefined'){thisURL = '';}
			$.post(thisURL + "/lessons_ajax.php",{
				excluir: true,
				idcomment: idCom[2]
			}, function(data) {
				data = data.split('|');
				ids = data[1].split(',');
				if (data[0] == "true"){
					$('#comment_'+idCom[2]).fadeOut("slow", function(){
						if (ids != ''){
							for (i=0; i<ids.length; i++){
								el = $('#comment_' + ids[i]);
								el.css('margin-left', '0');
								el.find('img.replyImage').attr('src', el.find('img.replyImage').attr('src').replace('resizew=50&resizeh=50', 'resizew=75&resizeh=75'));
								el.find('div.replyTextDiv').css('width', '418px');
							}
						}
					});
				}
			});
		}
		$('#comment_'+idCom[2]).css('border','none');
	});
}

//Função para carregar o sistema de resposta de comentários
function loadReplySystem(ajax){
	window.replyForm = false;
	window.lastReplyBtn = false;
	$('.btn_reply').each(function(n, el){
		if (!$(el).hasClass('got_event')){
			$(el).click(function(){
				if (typeof(frm) != 'undefined' && window.replyForm){
					if (confirm('Você já está respondendo a outro comentário. Deseja descartá-lo?')){
						window.lastReplyBtn.css('display', 'block');
						$($(window.lastReplyBtn).parent().find('div')[1]).css('display', 'block');
						frm.remove();
						window.replyForm = false;
					} else {
						return false;
					}
				}
				frm = $('#frm_reply').clone().attr('id', 'frm_reply_').css('display', 'block');
				frm.find('input[type=hidden]').attr('value', $(el).attr('id'));
				frm.insertAfter($(el));
				window.replyForm = true;
				window.lastReplyBtn = $(el);
				$(el).css('display', 'none');
				frm.find('textarea').focus();
				$('.btn_cancel_reply').click(function(){
					frm.remove();
					window.replyForm = false;
					$(el).css('display', 'block');
					$($(el).parent().find('div')[1]).css('display', 'block');
					return false;
				});
				frm.find('input.btn_submit_reply').click(function(){
					if (frm.find('textarea').attr('value') == ''){
						alert('Digite o comentário');
						frm.find('textarea');
						return false;
					} else if (ajax) {
						comment = frm.find('textarea').attr('value');
						thisData = $(el).attr('id').split(',');
						thisData = 'idcomentario=' + thisData[0] + '&idpublicacao=' + thisData[1] + '&reply_to=' + thisData[3] + '&comentario=' + frm.find('textarea').attr('value');
						frm.find('textarea').attr({
							'value':'Aguarde, enviando...',
							'disabled':'disabled'
						});
						$(this).attr('disabled', 'disabled');
						gotoY = document.documentElement.scrollTop;
						$(frm.find('input.btn_submit_reply')).css('visibility', 'hidden');
						if (!window.editingComment){
							$.post(getUrlSite() + '/lessons_ajax.php', thisData, function(data){
								frm.css('display', 'none');
								$("#comments").fadeOut("slow",function(){
									$("#comments").html(data);
								});
								$("#comments").fadeIn("slow", function(){
									setTimeout(function(){
										scrollTo(0, gotoY);
									}, 50);
								});
								$('.btn_cancel_reply').trigger('click');
							});
						} else {
							idcomentario = $(el).attr('class').replace(/^.* meuid_([0-9]+) .*$/, '$1').toInt();
							thisData = 'edit_comment=' + idcomentario + '&comentario=' + comment;
							$.post(getUrlSite() + '/lessons_ajax.php', thisData, function(data){
								frm.css('display', 'none');
								setTimeout(function(){
									scrollTo(0, gotoY);
								}, 50);
								el = $(el);
								texto = $(el.parent().find('div')[1]);
								texto.html(data).css('display', 'block');
								$('.btn_cancel_reply').trigger('click');
								window.editingComment = false;
							});
						}
						return false;
					}
				});
				rebuildEditor();
			});
			$(el).addClass('got_event');
		}
	});
	$('.btn_reply_disabled').each(function(n, el){
		if (!$(el).hasClass('got_event')){
			$(el).click(function(){
				return false;
			});
		}
	});
	
	//Ao clicar num link do e-mail de resposta, automaticamente abrir a caixa flutuante de resposta
	/*
	commentAnchor = window.location.href.match('#comment_[0-9]+');
	if (commentAnchor){
		$(commentAnchor[0]).find('.btn_reply').trigger('click');
	}
	*/
}


//Funções para rotacionar os testimonials dinamicamente
function startTestimonialsTimer(){
	window.testimonialsTimer = setInterval(function(){
		getTestimonial();
	}, 8500);
}
function getTestimonial(){
	$.getJSON(getUrlSite() + '/ajax_testimonials.php?get_testimonials=true', function(r){
		$('#testimonial_text').fadeOut(function(){
			$('#testimonial_text').html(r.texto);
			$(this).fadeIn();
		});
		$('#testimonial_nickname').fadeOut(function(){
			$('#testimonial_nickname').html(r.nickname);
			$(this).fadeIn();
		});
	});
}



function getUrlSite(){
	return 'http://' + window.location.hostname;
}


//Função para encontrar a posição de um determinado elemento
function findPos(obj){
	if (typeof(obj) == 'string'){
		obj = $(obj)[0];
	} else if (typeof(obj.length) == 'number'){
		obj = obj[0];
	}
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return {x:curleft, y:curtop};
	}
}


//Função para carregar o botão de adicionar amigo
function loadAddFriendButton(){
	$('.addamigo').click(function(){
		//var idadd = $(this).attr("id"); // atributo
		//var idAmigo= idadd.split("_");
		
		spinner = $('#tpl_loading').clone().attr('id', 'tpl_loading_').insertAfter(this);
		$(this).fadeOut('fast', function(){
			spinner.fadeIn('fast');
		});
		
		
		id =$(this).attr("id");
		//idAmigo="4870";
		//alert(id);
		idAmigo=id.split("_");
		$.post(getUrlSite() + "/ajax_amigos.php",{
			id_amigo: idAmigo[1],
			origem: "1",
			acao: $(this).attr("rel")
		}, function(data) { 
			if (window.location.href.match('/perfil.php')){
				if (data == 'true'){
					confBtn = $('<img src="images/botoes/bt_amigoadded.png" alt="Amigo adicionado" title=" - Amigo adicionado"/>').hide().insertAfter($('#tpl_loading_'));
					$('#tpl_loading_').fadeOut('fast', function(){
						confBtn.fadeIn('fast');
					});
				} else {
					$('#tpl_loading_').html(data);
				}
			} else {
				if(data == "true"){
						$(".div_amigo[id="+idAmigo[1]+"]").html('<img src="images/botoes/bt_amigoadded.png" alt="Amigo adicionado" title=" - Amigo adicionado"/>');
				} else {
					$(".div_amigo[id="+idAmigo[1]+"]").html(data);
				}
			}
		});
	});
}
function png2gif(){
	if (navigator.appVersion.match('MSIE 6')){
		$('img.png2gif,input.png2gif').each(function(n, el){
			$(el).attr('src', $(el).attr('src').replace(/\.png$/, '.gif'));
		});
	}
}


function include(file){
	file = (file.match('https?://', file) ? file : getUrlSite() + '/' + file);
	$('<script>').attr({
		'type':'text/javascript',
		'language':'javascript',
		'src':file
	}).appendTo($('body'));
}


/*
 * Extendendo o jQuery
 */
String.prototype.remove = function(p){
	return this.replace(p, '');
}
String.prototype.toInt = function(){
	r = this.replace(/[^0-9]+/g, '');
	if (r == ''){
		r = 0;
	}
	return parseInt(r);
}
Function.prototype.delay = function(t){
	setTimeout(this, t);
}