
 jQuery(document).ready(function() {
     jQuery('a[rel*=facebox]').facebox();
 })
 
function validaTamanho(obj,meet) {
	if(obj.value.length > 139) {
		$(obj).css('border','solid 1px red');
		$(obj).attr('value',obj.value.substr(0,140));
	} else {
		$(obj).css('border','solid 1px #ccc');
	}
	$('#contador-'+meet).html((140 - obj.value.length) + ' restantes');
}

function buscaEstado() {
	//chamada do AJAX
	 $.ajax({
					 	type: "POST",
						//arquivo que vai executar a requisição
				   		url: "/do/buscaEstado",
						//dados para requisição
				    	data: "estado_id= "  + 	$('#estado').attr('value') ,
						//após a conclusão adiciona os dados ao elemento
				  		success: function(msg){
							//elemento que vai receber o retorno
						   $('#cidades').html(msg);
				   		}
				 });
}
function Trim(str){return str.replace(/^\s+|\s+$/g,"");}
function limpaCaracteresEspeciais(obj) { 
	//chamada do AJAX
	 $.ajax({
					 	type: "POST",
						//arquivo que vai executar a requisição
				   		url: "/do/limpatexto",
						//dados para requisição
				    	data: "texto="+obj.value ,
						//após a conclusão adiciona os dados ao elemento
				  		success: function(msg){
							//elemento que vai receber o retorno
						   $(obj).attr('value',Trim(msg));
				   		}
				 });
}


 function loadXMLDoc(url,dado,valor)
{
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url+"?"+dado+"="+valor, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET",url+"?"+dado+"="+valor, true);
            req.send();
        }
    }
}


function processReqChange()
{
    if (req.readyState == 4) {
        if (req.status == 200) {
            document.getElementById(_div).innerHTML = req.responseText;
        } else {
           // alert("Error:\n" + req.statusText);
        }
    }

}

function ajax(valor,dado,div,url)
{
	_div = div;
    loadXMLDoc(url,dado,valor);
}

function mostrarAcoes(atividade_id) {
	obj = document.getElementById('barraAcao'+atividade_id);
	obj.style.display = 'block';
	
	obj = document.getElementById(atividade_id);
	obj.style.backgroundColor = '#f7f7f7';
}

function ocultarAcoes(atividade_id) {
	obj = document.getElementById('barraAcao'+atividade_id);
	obj.style.display = 'none';
	
	obj = document.getElementById(atividade_id);
	obj.style.backgroundColor = '#FFFFFF';
	
}


function mudaImg(obj,img) {
	obj.src = img;
}

function habilitaComment(obj,id) {
	obj.rows = 3;
	obj.value = '';
	obj2 = document.getElementById('acoes-'+id);	
	obj2.style.display = 'block';
	
	obj3 = document.getElementById('avatarlogado-'+id);	
	obj3.style.display = 'block';
}

function irjunto(id) {
	
	ajax(id,'meet','divajax','/do/join');
	
	objir =  document.getElementById('ir-'+id);	
	objdeixar =  document.getElementById('deixar-'+id);	
	
	objir.style.display = 'none';
	objdeixar.style.display = 'block';
	
	//obj = document.getElementById('comentar-'+id);	
	//obj.style.display = 'block';
	
	$('#comentar-'+id).show("slow");
	
}

function deixar(id) {
	
	ajax(id,'meet','divajax','/do/leave');
	
	objir =  document.getElementById('ir-'+id);	
	objdeixar =  document.getElementById('deixar-'+id);	
	
	objir.style.display = 'block';
	objdeixar.style.display = 'none';
	
//	obj = document.getElementById('comentar-'+id);	
//	obj.style.display = 'block';
	$('#comentar-'+id).show("slow");
	
}
function enviaComentario(id) {
	obj =  document.getElementById('textocomentario-'+id);	
	ajax(obj.value,'texto','alerta','/do/add-comment?meet='+id+'&');
	
	$('#alerta').show("slow");
	 
	 setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
	obj2 = document.getElementById('comentar-'+id);	
	obj2.style.display = 'none';
}

function enviaMsgDireta(id,uid) {
	obj =  document.getElementById('textocomentario-'+id);	
	ajax(obj.value,'texto','divajax','/do/add-message?uid='+uid+'&meet='+id+'&');
	
	obj2 = document.getElementById('comentar-'+id);	
	obj2.style.display = 'none';
}

function mostraOpcoesMeet() {
	//obj2 = document.getElementById('add-atividade-opcoes');	
	//obj2.style.display = 'block';	
	 $('#add-atividade-opcoes').show("slow");
}

function selecionacidade(id,nome) {
	obj = document.getElementById('meetcidade');	
	obj.value = nome;	
	
	obj2 = document.getElementById('meetcidadeid');	
	obj2.value = id;	
	
	obj3 = document.getElementById('lista-cidades');	
	obj3.innerHTML = '';

}
 
 function enviaMeet(url) {	
	if($('#meetcidade').attr('value') == '') {
		alert('Por favor escolha uma cidade');
	} else {
			if($('#textarea1').attr('value') != '') {
				if($('#categoria_id').attr('value') == 0) {
					alert('Por favor escolha uma categoria');
				} else {
					$('#loadermeet').show();
					$.ajax({
					   type: "POST", 
					   url: "/do/addmeet",
						data: "city=" 		+ 	$('#meetcidade').attr('value') +
							  "&city_id= " 	+ 	$('#meetcidadeid').attr('value') +
							  "&texto= "		+ 	$('#textarea1').attr('value') +
							  "&dia= "			+	$('#inputDate').attr('value') +
							  "&part= "		+	$('#participantes').attr('value') +
							  "&qqr= "			+	$('#qualquerdia_input').attr('value') +
							  "&horas= "		+	$('#horas').attr('value') +
							  "&categoria_id= "	+	$('#categoria_id').attr('value') +
							  "&nivel= "		+	$('#privacidade').attr('value') ,
					  
					  success: function(msg){
							$('#loadermeet').hide();
							
							if($('#abaselecionada').attr('value') == 1) {
								$('#ctaba1').html(msg);
							} else {
								window.location = '/page/'+url;
							}
					   }
				 });
				 $('#add-atividade-opcoes').hide("slow");
				 $('#textarea1').attr('value','');
				 $('#categoria_id').attr('value','');
				 $('#inputDate').attr('value',$('#dataoriginal').attr('value'));
			     $('#meetcidade').attr('value',$('#cidadeoriginal').attr('value'));
				 $('#meetcidadeid').attr('value',$('#cidadeoriginalid').attr('value'));
				 fechaBoxData();
				 
				 $('#participantes').attr('value',0);
				 $('#qualquerdia_input').attr('value',0);
				 $('#horas').attr('value','00:01');
				 $('#privacidade').attr('value',0);
				 
				 count = document.getElementById('countsprytextarea1');
				 count.innerHTML = '140';
				}
			} else {
				alert('Preencha a atividade');
			}
			
	}
}


 function enviaMeetConvidado(user) {
	
	city_var = document.getElementById('meetcidade');	
 	city_id_var = document.getElementById('meetcidadeid');	
	txt_var	=  document.getElementById('textarea1');
	dia_var  =   document.getElementById('inputDate'); 
	nivel_var =  document.getElementById('privacidade'); 
	participante_var =  document.getElementById('participantes');
	qqr_var = document.getElementById('qualquerdia_input');
	horas_var = document.getElementById('horas');

	if(txt_var.value != '') {
		ajax(user,'user','alerta','/do/addmeetinvited?city='+city_var.value+'&city_id='+city_id_var.value+'&texto='+txt_var.value+'&dia='+dia_var.value+'&nivel='+nivel_var.value+'&part='+participante_var.value+'&qqr='+qqr_var.value+'&horas='+horas_var.value+'&');
		$('#alerta').show("slow");
	   	setTimeout(function(){ $("#alerta").hide("normal");},2000);	
	}
	
	
	 $('#add-atividade-opcoes').hide("slow");


	txt_var.value = '';
	 count = document.getElementById('countsprytextarea2');
	 count.innerHTML = '140';
	
}

function showAvatarForm() {
	obj = document.getElementById('avatarForm');	
	obj.style.display = 'block';	
}

function limpaCampo(obj) {
	obj.value = '';
}

//function limpaCidade(obj) {
	//document.getElementById('lista-cidades').style.visibility = "hidden"
//}
function limpaCampoSenha(obj) {
	obj.value = '';
	obj.type = "password";
}

function mudaCategoria(categoria) {
		$('#cat'+categoria +' a').css('color','#000');
		$('#cat'+categoria +' a').css('font-weight','bold');
		$('#cat'+categoria +' a').css('cursor','default');
		$('#cat'+categoria +' a').css('text-decoration','none');
		$('#cat'+categoria +' a').attr('href','#');
		
		$('#cat'+$('#categoria_id').attr('value')+' a').css('color','#FF0084');
		$('#cat'+$('#categoria_id').attr('value')+' a').css('font-weight','normal');
		$('#cat'+$('#categoria_id').attr('value')+' a').css('cursor','pointer');
	//	$('#cat'+$('#categoria_id').attr('value')+' a').css('text-decoration','underline');
		$('#cat'+$('#categoria_id').attr('value')+' a').attr('href','javascript:mudaCategoria('+$('#categoria_id').attr('value')+')');
		
		$('#categoria_id').attr('value',categoria);
		$('#titulopg').html('Resultado da busca');
		buscameets();
}
function pesquisarmeets() {
	$('#titulopg').html('Resultado da busca');
	buscameets();
}

function mudaOrdenacao(ordem) {
	if(ordem == 1) {
		$('#ordena2').css('color','#FF0084');
		$('#ordena2').css('font-weight','normal');
		$('#ordena2').css('cursor','pointer');
		
		$('#ordena1').css('color','#666666');
		$('#ordena1').css('font-weight','bold');
		$('#ordena1').css('cursor','default');
	} else {
		$('#ordena1').css('color','#FF0084');
		$('#ordena1').css('font-weight','normal');
		$('#ordena1').css('cursor','pointer');
		
		$('#ordena2').css('color','#666666');
		$('#ordena2').css('font-weight','bold');
		$('#ordena2').css('cursor','default');
	}
	$('#ordenacao').attr('value',ordem);
	buscameets();
}
function buscameets() {
	$('#atividades').css('display','none');
	$('#imgloader').css('display','block');

	
	$('#atividades').css('text-align','center');
	$('#atividades').css('margin-top','50px');
	
	$.ajax({
	   type: "POST",
	   url: "/do/find-meets",
	    data: "meet=" 		+ 	$('#meet').attr('value') +
		 	  "&cidade= " 	+ 	$('#meetcidadeid').attr('value') +
			  "&data= "		+ 	$('#inputDate').attr('value') +
			  "&qualquer= "	+	$('#qualquerdia_input').attr('value') +
		      "&categoria_id= "	+	$('#categoria_id').attr('value') +
			  "&ordenacao= "	+	$('#ordenacao').attr('value') +
			  "&qualquer= "	+	$('#qualquerdia_input').attr('value') +
			  "&sexo= "		+	$('#sexvalue').attr('value') ,
	  
	  success: function(msg){
		   $('#atividades').css('text-align','left');
		   $('#atividades').css('margin-top','0px');
		   $('#atividades').html(msg);
		   
		   $('#atividades').css('display','block');
		   $('#imgloader').css('display','none');
		  
	   }
	 });
}

function remover(id) {
	 $("#"+id).hide("normal");
	 ajax(id,'meet','divajax','/do/remove-meet');
}

function remove_message(id) {
	$("#box-"+id).hide("slow");
    ajax(id,'message','alerta','/do/remove-message');
	$('#alerta').show("slow");
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
}

function more(start,type, param) {
	$('#more'+type).hide();
	$('#loader'+type).show();

	$.ajax({
	   type: "GET",
	   url: "/do/more-meets",
	   data: "start=" 		+ 	start  +
		 	  "&type= " 		+ 	type +
			  "&param= "		+ 	param  ,
	   success: function(msg){
		  	 $('#'+type+start).html(msg);
		  }
	 });
	
}

function alerta(msg) {
	ajax(msg,'msg','alerta','/do/alert');
}

function adicionarFavorita(id) {
	 //PEGA O ELEMENETO DA IMAGEM DA ESTRELA
	 obj = document.getElementById('estrela'+id);
	 //TODO MUDAR ENDERECO NA PRODUÇÃO 
	 if(obj.src == 'http://www.meetango.com/images/ic_favorito_cor.png') {
		 // REMOVE DOS FAVORITOS
		obj.src = '/images/ic_favorito_pb.png';
		ajax(id,'meet','alerta','/do/delete-favorite');
	
		$('#alerta').show("slow");
		setTimeout(function(){ $("#alerta").hide("normal");},3000);
		if(window.location.pathname == '/page/my-favorites') {
			$('#'+id).hide("slow");
		}
	} else {
		 //ADICIONA AOS FAVORITOS
		 obj.src = '/images/ic_favorito_cor.png';
		 ajax(id,'meet','alerta','/do/add-favorite');
	
		 $('#alerta').show("slow");
		 setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
	}
} 
 
function exibeBoxResposta(id) {
	$('#responder-'+id).show("slow");
}

function removeMensagem(id) {
	ajax(id,'meet','divajax','/do/remove-message');
	$('#box-'+id).hide("slow"); 	
} 


function changeSex(valor) {
	obj = document.getElementById('sexvalue');	
	obj.value = valor;
	
	_ich = document.getElementById('ich');	
	_icm = document.getElementById('icm');	
	_ichm = document.getElementById('ichm');	

	switch(valor) {
		case 'h' :
			_ich.src = '/images/hs.png';
			_icm.src  = '/images/mn.png';
			_ichm.src  = '/images/hmn.png';
		break;
		case 'm' :
				_ich.src = '/images/hn.png';
			_icm.src  = '/images/ms.png';
			_ichm.src  = '/images/hmn.png';
		break;
		case 'hm' :
			_ich.src = '/images/hn.png';
			_icm.src  = '/images/mn.png';
			_ichm.src  = '/images/hms.png';
		break;
	}
	
}


function favoritoIcone(valor) {
	obj = document.getElementById('sexvalue');	
	obj.value = valor;
	
	_ich = document.getElementById('ich');	
	_icm = document.getElementById('icm');	
	_ichm = document.getElementById('ichm');	

	switch(valor) {
		case valor :
			_ich.src = '/images/hs.png';
			_icm.src  = '/images/mn.png';
			_ichm.src  = '/images/hmn.png';
		break;
	}
	
}

function exibeBoxData() {
	$('#qualquerdia').hide();
	$('#emumdia').show();
	
	obj = document.getElementById('qualquerdia_input');	
	obj.value = '1';

}
function fechaBoxData() {
	$('#qualquerdia').show();
	$('#emumdia').hide();

	obj = document.getElementById('qualquerdia_input');	
	obj.value = '0';
}

function enviaContato() {

	ajax(texto.value,'texto','alerta','/do/add-contact?assunto='+assunto.value+'&');
	
	texto.value = '';
	assunto.value = '';

	 $('#alerta').show("slow");
	 
	 setTimeout(function(){ $("#alerta").hide("normal");},3000);

}

function reenviaSenha() {
	
	ajax($('#login').attr('value'),'login','alerta','/do/resend-pass');
	
	$('#login').attr('value','');

	$('#alerta').show("slow");
	 
	 setTimeout(function(){ $("#alerta").hide("normal");},3000);
}

function seguir(uid) {
	ajax(uid,'uid','alerta','/do/follow');
	$('#alerta').show();
	 
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
	
	$('#botaoseguir').hide(); 
	$('#botaounfollow').show();
	
		
}



function unfollow(uid) {
	ajax(uid,'uid','alerta','/do/unfollow');
	
	$('#alerta').show();
	 
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
	$('#botaoseguir').show();
	$('#botaounfollow').hide()
	
		
}

function seguirusuario(uid) {
	ajax(uid,'uid','alerta','/do/follow');
	$('#alerta').show();
	 
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
	
	$('#botaoseguirpageusuario'+uid).hide(); 
	$('#botaounfollowpageusuario'+uid).show();
}

function unfollowusuario(uid) {
	ajax(uid,'uid','alerta','/do/unfollow');
	
	$('#alerta').show();
	 
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
	$('#botaoseguirpageusuario'+uid).show();
	$('#botaounfollowpageusuario'+uid).hide()
}

function unfollow_seguidores(uid) {
	ajax(uid,'uid','alerta','/do/unfollow');
	
	$('#alerta').show();
	 
	setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
	$('#botaoseguirpageusuario'+uid).show();
	$('#botaounfollowpageusuario'+uid).hide();
	$('#usuario-'+uid).hide('slow');
}

function alteraSenha() {
	
	obj = document.getElementById('senha');	
	
	
	ajax(obj.value,'senha','alerta','/do/change-pass');
	
	$('#alerta').show("slow");
	  
	 setTimeout(function(){ $("#alerta").hide("normal");},3000);
	 
}

function convidarUsuario(username) {

		
		obj = document.getElementById('atividades');
		obj2 = document.getElementById('alerta');

		if(obj.value == '') {
			ajax('Selecione uma atividade','alert','alerta','/do/show-alert');
			$('#alerta').show("slow");
	    	setTimeout(function(){ $("#alerta").hide("normal");},2000);	
		} else {
			ajax(username,'username','alerta','/do/invite-user?atividade_id='+obj.value+'&');
			$('#alerta').show("slow");
	    	setTimeout(function(){ $("#alerta").hide("normal");},2000);	
		}
		
}

function twitThis(meetid) {
	window.location = '/do/twitthis/?meet='+ meetid;
}

function validaComentario(){
	if(document.comentarios.textarea1.value == ""){
		alert('Campo de Comentario em Branco!!!')
	} else{
		document.comentarios.submit()	
	}	
}

function limparPreferencias(user){
	/* Resetando Cores */
	cor_bg = document.form2.cor_bg.value = 'FFFFFF'
	cor_txt = document.form2.cor_txt.value = '000000'
	cor_link = document.form2.cor_link.value = 'FF0084'
	cor_barra = document.form2.cor_barra.value = 'FCFCFC'
	cor_borda = document.form2.cor_borda.value = 'CCCCCC'
	
	window.location = '/do/reset-preferences';
	}
	
function enviaParaPreferencias() {
	window.location = '/page/preferences-interactive';
}	
	
	function marcar(objeto) {
		if(objeto.checked) {
			selecionar_tudo();
		} else {
			deselecionar_tudo();
		}
	}
	function marcar1(objeto) {
		if(objeto.checked) {
			selecionar_tudo1();
		} else {
			deselecionar_tudo1();
		}
	}
	function selecionar_tudo1(){ 
	   for (i=0;i<document.form2.elements.length;i++) 
		  if(document.form1.elements[i].type == "checkbox")	
			 document.form1.elements[i].checked=1 
	} 
	function deselecionar_tudo1(){ 
	   for (i=0;i<document.form2.elements.length;i++) 
		  if(document.form1.elements[i].type == "checkbox")	
			 document.form1.elements[i].checked=0 
	} 
	function selecionar_tudo(){ 
	   for (i=0;i<document.form2.elements.length;i++) 
		  if(document.form2.elements[i].type == "checkbox")	
			 document.form2.elements[i].checked=1 
	} 
	function deselecionar_tudo(){ 
	   for (i=0;i<document.form2.elements.length;i++) 
		  if(document.form2.elements[i].type == "checkbox")	
			 document.form2.elements[i].checked=0 
	} 