function gebi(ide){
		return document.getElementById(ide);
}

$(document).ready(function() {
        $("li a img").error(function(){
                $(this).removeAttr('src').attr('src','http://www10.xpg.uol.com.br/templates/default/images/foto_user.jpg');
        });
        
	var page_seguidores = 0;
	var page_seguindo = 0;
	
	var liToGrid = '<li class="onlyImg"><a href="{$url}" target="_blank"><img src="{$img_profile}" alt="{$username}" title="{$username}" /></a></li>';

	var img_out_xpg = $('#img-btn-meu-xpg').attr('rel');
	var img_out_conta = $('#img-btn-minha-conta').attr('rel');
	var img_out_conta_login = $('#img-btn-minha-conta-login').attr('rel');
	
	var img_on_xpg = $('#img-btn-meu-xpg').attr('rev');
	var img_on_conta = $('#img-btn-minha-conta').attr('rev');
	var img_on_conta_login = $('#img-btn-minha-conta-login').attr('rev');
	
	$("#lform").validationEngine({
		promptPosition: "topRight",
		scroll:false,
		boxtype: "alert"
	});
	
	$('#senha').keyup(function(e) {
		if(e.keyCode == 13) {
			$('#lform').submit();
		}
	});

	$('#listagem-tipo-post').bind('change',function() {
		var link = $('#listagem-tipo-post').val();
		
		if( link != '' ) {
			location.href = '/?load='+link;
		} else {
			location.href = '/';
		}
	});
	
	$('#btn-minha-conta').click(function(){
		if( $('#drop-minha-conta').is(':visible') ) {
			$('#drop-minha-conta').hide();
			$('#btn-minha-conta').removeClass('drop-on-click');
			$('#img-btn-minha-conta').attr('src',img_out_conta);
			$('#btn-minha-conta').addClass('btn-conta-barra-page-usuario');
		} else {
			$('#drop-meu-xpg').hide();
			$('#btn-meu-xpg').removeClass('drop-on-click');
			$('#btn-meu-xpg').addClass('btn-meu-xpg-barra-page-usuario');
			$('#img-btn-meu-xpg').attr('src',img_out_xpg);
			$('#btn-minha-conta').addClass('drop-on-click');
			$('#img-btn-minha-conta').attr('src',img_on_conta);
			$('#drop-minha-conta').show();
		}
	});
	
	$('#btn-minha-conta-login').click(function() {
		if( $('#drop-minha-conta-login').is(':visible') ) {
			$('#drop-minha-conta-login').hide();
			$('#btn-minha-conta-login').removeClass('drop-on-click');
			$('#img-btn-minha-conta-login').attr('src',img_out_conta_login);
			$('#btn-minha-conta-login').addClass('btn-conta-barra-page-usuario');
			$('.loginformError').hide();
			$('.senhaformError').hide();
		} else {
			$('#btn-minha-conta-login').addClass('drop-on-click');
			$('#img-btn-minha-conta-login').attr('src',img_on_conta_login);
			$('#drop-minha-conta-login').show();
		}
	});
	
	$('body').click(function(event) {
	    if (!$(event.target).closest('#drop-meu-xpg').length && !$(event.target).closest('#btn-meu-xpg').length) {
	        $('#drop-meu-xpg').hide();
	        $('#btn-meu-xpg').removeClass('drop-on-click');
	        $('#img-btn-meu-xpg').attr('src',img_out_xpg);
			$('#btn-meu-xpg').addClass('btn-meu-xpg-barra-page-usuario');
	    };
	    
	    if (!$(event.target).closest('#drop-minha-conta').length && !$(event.target).closest('#btn-minha-conta').length) {
	        $('#drop-minha-conta').hide();
	        $('#btn-minha-conta').removeClass('drop-on-click');
	        $('#img-btn-minha-conta').attr('src',img_out_conta);
			$('#btn-minha-conta').addClass('btn-conta-barra-page-usuario');
	    };
	    
	    if (!$(event.target).closest('#drop-minha-conta-login').length && !$(event.target).closest('#btn-minha-conta-login').length) {
	        $('#drop-minha-conta-login').hide();
	        $('#btn-minha-conta-login').removeClass('drop-on-click');
	        $('#img-btn-minha-conta-login').attr('src',img_out_conta_login);
			$('#btn-minha-conta-login').addClass('btn-conta-barra-page-usuario');
			$('.loginformError').hide();
			$('.senhaformError').hide();
	    }; 
	});
	
	$('#btn-meu-xpg').click(function() {
		if( $('#drop-meu-xpg').is(':visible') ) {
			$('#drop-meu-xpg').hide();
			$('#btn-meu-xpg').removeClass('drop-on-click')
			$('#btn-meu-xpg').addClass('btn-meu-xpg-barra-page-usuario');
			$('#img-btn-meu-xpg').attr('src',img_out_xpg);
		} else {
			$('#drop-minha-conta').hide();
			$('#btn-minha-conta').removeClass('drop-on-click');
			$('#btn-minha-conta').addClass('btn-conta-barra-page-usuario');
			$('#img-btn-minha-conta').attr('src',img_out_conta);
			$('#btn-meu-xpg').addClass('drop-on-click');
			$('#img-btn-meu-xpg').attr('src',img_on_xpg);
			$('#drop-meu-xpg').show();
		}
	});
	
	$('#seta-direita-seguidores').click(function(){
		/*
		var maxScroll = $('#drop-seguidores').attr('scrollHeight') - $('#drop-seguidores').height();
		if($('#drop-seguidores').scrollTop()==maxScroll) {
		*/
			var blogid = $('#drop-seguidores').attr('rel');
			page_seguidores++;
			$.ajax({
				  type: 'POST',
				  url: '/seguidores.html',
				  data: 'page='+page_seguidores+'&blog='+blogid,
				  beforeSend: function() { $('#loading-seguidores').show(); },
				  success: function(json) {
					if(json.retorno){

						if(parseInt( json.quantidade ) <=8){
							gebi('seta-esquerda-seguidores').style.height='23px';
							gebi('seta-direita-seguidores').style.height='23px';
							gebi('setinha_left1').style.height='23px';
							gebi('setinha_left1').style.width='10px';
							gebi('setinha_left1').style.paddingTop='7px';
							gebi('setinha_right1').style.height='23px';
							gebi('setinha_right1').style.width='10px';
							gebi('setinha_right1').style.paddingTop='7px';
							mostrar_setas('seguidores');
							esconder_seta_direita('seguidores');
						}else{
							gebi('seta-esquerda-seguidores').style.height='40px';
							gebi('seta-direita-seguidores').style.height='40px';
							gebi('setinha_left1').style.height='40px';
							gebi('setinha_left1').style.width='10px';
							gebi('setinha_left1').style.paddingTop='20px';
							gebi('setinha_right1').style.height='40px';
							gebi('setinha_right1').style.width='10px';
							gebi('setinha_right1').style.paddingTop='20px';
							mostrar_setas('seguidores');
							if(page_seguidores==0){
								esconder_seta_esquerda('seguidores');
							}
						}
						
						if(json.html.toString() != ''){
							//$('#drop-listagem-seguidores').html(json.html);
							
							var quantidade = json.html.length;
							var html = "";
							for(i=0; i<quantidade ; i++){
								htmlTmp = liToGrid.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$url}",json.html[i]['url']);
								htmlTmp = htmlTmp.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$img_profile}",json.html[i]['img_profile']);
								html = html + htmlTmp;
							}
							$('#drop-listagem-seguidores').html(html);

							$('#loading-seguidores').hide();
						}
					} else {
						page_seguidores--;
						$('#loading-seguidores').hide();
					}
				  },
				  dataType: 'json'
				});
	});

	$('#seta-esquerda-seguidores').click(function(){
		/*
		var maxScroll = $('#drop-seguidores').attr('scrollHeight') - $('#drop-seguidores').height();
		if($('#drop-seguidores').scrollTop()==maxScroll) {
		*/
			var blogid = $('#drop-seguidores').attr('rel');	
			page_seguidores--;
			$.ajax({
				  type: 'POST',
				  url: '/seguidores.html',
				  data: 'page='+page_seguidores+'&blog='+blogid,
				  beforeSend: function() { $('#loading-seguidores').show(); },
				  success: function(json) {
					if(json.retorno) {
						
						if(parseInt( json.quantidade ) <= 8){
							gebi('seta-esquerda-seguidores').style.height='23px';
							gebi('seta-direita-seguidores').style.height='23px';
							gebi('setinha_left1').style.height='23px';
							gebi('setinha_left1').style.width='10px';
							gebi('setinha_left1').style.paddingTop='7px';
							gebi('setinha_right1').style.height='23px';
							gebi('setinha_right1').style.width='10px';
							gebi('setinha_right1').style.paddingTop='7px';
							mostrar_setas('seguidores');
							esconder_seta_direita('seguidores');
						}else{
							gebi('seta-esquerda-seguidores').style.height='40px';
							gebi('seta-direita-seguidores').style.height='40px';
							gebi('setinha_left1').style.height='40px';
							gebi('setinha_left1').style.width='10px';
							gebi('setinha_left1').style.paddingTop='20px';
							gebi('setinha_right1').style.height='40px';
							gebi('setinha_right1').style.width='10px';
							gebi('setinha_right1').style.paddingTop='20px';
							mostrar_setas('seguidores');
							if(page_seguidores==0){
								esconder_seta_esquerda('seguidores');
							}
						}
						if(json.html.toString() != ''){
							
							var quantidade = json.html.length;
							var html = "";
							for(i=0; i<quantidade ; i++){
								htmlTmp = liToGrid.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$url}",json.html[i]['url']);
								htmlTmp = htmlTmp.replace("{$img_profile}",json.html[i]['img_profile']);
								html = html + htmlTmp;
							}
							$('#drop-listagem-seguidores').html(html);
							
							$('#loading-seguidores').hide();
						}
						
					} else {
						page_seguidores++;
						$('#loading-seguidores').hide();
					}
				  },
				  dataType: 'json'
				});
	});
	
	$('#seta-direita-seguindo').click(function(){
			var blogid = $('#drop-seguindo').attr('rel');
			page_seguindo++;
			$.ajax({
				  type: 'POST',
				  url: '/seguindo.html',
				  data: 'page='+page_seguindo+'&blog='+blogid,
				  beforeSend: function() { $('#loading-seguindo').show(); },
				  success: function(json) {
					if(json.retorno) {
						if(parseInt( json.quantidade ) <=8){
							gebi('seta-esquerda-seguindo').style.height='23px';
							gebi('seta-direita-seguindo').style.height='23px';
							gebi('setinha_left2').style.height='23px';
							gebi('setinha_left2').style.width='10px';
							gebi('setinha_left2').style.paddingTop='7px';
							gebi('setinha_right2').style.height='23px';
							gebi('setinha_right2').style.width='10px';
							gebi('setinha_right2').style.paddingTop='7px';
							mostrar_setas('seguindo');
							esconder_seta_direita('seguindo');
						}else{

							gebi('seta-esquerda-seguindo').style.height='40px';
							gebi('seta-direita-seguindo').style.height='40px';
							gebi('setinha_left2').style.height='40px';
							gebi('setinha_left2').style.width='10px';
							gebi('setinha_left2').style.paddingTop='20px';
							gebi('setinha_right2').style.height='40px';
							gebi('setinha_right2').style.width='10px';
							gebi('setinha_right2').style.paddingTop='20px';
							mostrar_setas('seguindo');
							if(page_seguindo==0){
								esconder_seta_esquerda('seguindo');
							}
						}
						if(json.html.toString() != ''){
							var quantidade = json.html.length;
							var html = "";
							for(i=0; i<quantidade ; i++){
								htmlTmp = liToGrid.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$username}",json.html[i]['username']);
								htmlTmp = htmlTmp.replace("{$url}",json.html[i]['url']);
								htmlTmp = htmlTmp.replace("{$img_profile}",json.html[i]['img_profile']);
								html = html + htmlTmp;
							}
							$('#drop-listagem-seguindo').html(html);
							$('#loading-seguindo').hide();
						}
					} else {
						page_seguindo--;
						$('#loading-seguindo').hide();
					}
				  },
				  dataType: 'json'
				});
			
	});
	
	$('#seta-esquerda-seguindo').click(function(){
		var blogid = $('#drop-seguindo').attr('rel');
		page_seguindo--;
		$.ajax({
			  type: 'POST',
			  url: '/seguindo.html',
			  data: 'page='+page_seguindo+'&blog='+blogid,
			  beforeSend: function() { $('#loading-seguindo').show(); },
			  success: function(json) {
				if(json.retorno) {
					
					if(parseInt( json.quantidade ) <=8){
						gebi('seta-esquerda-seguindo').style.height='23px';
						gebi('seta-direita-seguindo').style.height='23px';
						gebi('setinha_left2').style.height='23px';
						gebi('setinha_left2').style.width='10px';
						gebi('setinha_left2').style.paddingTop='7px';
						gebi('setinha_right2').style.height='23px';
						gebi('setinha_right2').style.width='10px';
						gebi('setinha_right2').style.paddingTop='7px';
						mostrar_setas('seguindo');
						esconder_seta_direita('seguindo');
					}else{
						gebi('seta-esquerda-seguindo').style.height='40px';
						gebi('seta-direita-seguindo').style.height='40px';
						gebi('setinha_left2').style.height='40px';
						gebi('setinha_left2').style.width='10px';
						gebi('setinha_left2').style.paddingTop='20px';
						gebi('setinha_right2').style.height='40px';
						gebi('setinha_right2').style.paddingTop='20px';
						gebi('setinha_right2').style.width='10px';
						mostrar_setas('seguindo');
						if(page_seguindo==0){
							esconder_seta_esquerda('seguindo');
						}
					}
					if(json.html.toString() != ''){
						var quantidade = json.html.length;
						var html = "";
						for(i=0; i<quantidade ; i++){
							htmlTmp = liToGrid.replace("{$username}",json.html[i]['username']);
							htmlTmp = htmlTmp.replace("{$username}",json.html[i]['username']);
							htmlTmp = htmlTmp.replace("{$url}",json.html[i]['url']);
							htmlTmp = htmlTmp.replace("{$img_profile}",json.html[i]['img_profile']);
							html = html + htmlTmp;
						}
						$('#drop-listagem-seguindo').html(html);
						$('#loading-seguindo').hide();
					}
				} else {
					page_seguindo++;
					$('#loading-seguindo').hide();
				}

			  },
			  dataType: 'json'
			});
	});
	
	
	
	$('#drop-ballon').click(function() {
		$('#drop-ballon').hide();
	});
	
	
	$('#btn-follow').click(function() {
		$.ajax({
			  type: 'GET',
			  url: '/seguir-deixar.html',
			  success: function(json) {
				if(json.retorno==true) {
					$.notifyBar({ cls: "success", html: json.message, close:true });
					//$('#btn-follow').html(json.html);
					$('#btn-follow').hide();
					$('#perfil_').removeClass('btn-usuario-right-bar').addClass('btn-usuario-right-bar-extended');
				} else {
					$.notifyBar({ cls: "error", html: json.message, close:true });
				}
			  },
			  dataType: 'json'
			});
	});
			
	if(gebi('seta-esquerda-seguidores')){
		esconder_seta_esquerda("seguidores");
	}
	if(gebi('seta-esquerda-seguindo')){
		esconder_seta_esquerda("seguindo");
	}

	c = contaLiSeguindo();
	if(parseInt(c) > 0){
		if(parseInt(c) <=8){
			gebi('seta-esquerda-seguindo').style.height='23px';
			gebi('seta-direita-seguindo').style.height='23px';
			gebi('setinha_left2').style.height='23px';
			gebi('setinha_left2').style.width='10px';
			gebi('setinha_left2').style.paddingTop='7px';
			gebi('setinha_right2').style.height='23px';
			gebi('setinha_right2').style.width='10px';
			gebi('setinha_right2').style.paddingTop='7px';
		}
		if(parseInt(c) < 16){
			esconder_setas("seguindo");
		}
	}
	
	d = contaLiSeguidores();
	if(parseInt(d) > 0){
		if(parseInt(d) <=8){
			gebi('seta-esquerda-seguidores').style.height='23px';
			gebi('seta-direita-seguidores').style.height='23px';
			gebi('setinha_left1').style.height='23px';
			gebi('setinha_left1').style.width='10px';
			gebi('setinha_left1').style.paddingTop='7px';
			gebi('setinha_right1').style.height='23px';
			gebi('setinha_right1').style.width='10px';
			gebi('setinha_right1').style.paddingTop='7px';
		}
		if(parseInt(d) < 16){
			esconder_setas("seguidores");
		}
	}
	
	$("a[id*='setinha']").css('color', $("div[class*='bottom-boxs-right-bar'] a").css("color") );
	
});


function slide_setas ( btn, drop) {
	if( $('#'+drop).is(':visible') ) {
		$('#'+drop).slideUp(400);
		$('#'+btn).html(':::::<span>▼</span>:::::');
	} else {
		$('#'+btn).html(':::::<span>▲</span>:::::');
		$('#'+drop).slideDown(400);
	}
}

function contaLiSeguindo(){
	var ul = gebi('drop-listagem-seguindo');
	
	if(ul){
		var i=0, c =0;
		while(ul.getElementsByTagName('li')[i++]) c++;
		return c;
	}else{
		return 0;
	}
	
}

function contaLiSeguidores(){
	var ul = gebi('drop-listagem-seguidores');
	
	if(ul){
		var i=0, c =0;
		while(ul.getElementsByTagName('li')[i++]) c++;
		return c;
	}else{
		return 0;
	}
}


function esconder_setas(deQuem){
	if(deQuem=="seguidores"){
		gebi('seta-esquerda-seguidores').style.display='none';
		gebi('drop-seguidores').style.width='298px';
		gebi('drop-seguidores').className='content-boxs-right-bar-seg box-scroll icons-vert';
		gebi('seta-direita-seguidores').style.display='none';
	}else{
		gebi('seta-esquerda-seguindo').style.display='none';
		gebi('drop-seguindo').style.width='298px';
		gebi('drop-seguindo').className='content-boxs-right-bar-seg box-scroll icons-vert';
		gebi('seta-direita-seguindo').style.display='none';
	}
}

function esconder_seta_direita(deQuem){
	if(deQuem=="seguidores"){
		gebi('drop-seguidores').style.width='289px';
		gebi('drop-seguidores').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-left';
		gebi('seta-direita-seguidores').style.display='none';
	}else{
		gebi('drop-seguindo').style.width='289px';
		gebi('drop-seguindo').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-left';
		gebi('seta-direita-seguindo').style.display='none';
	}
}

function esconder_seta_esquerda(deQuem){
	if(deQuem=="seguidores"){
		gebi('drop-seguidores').style.width='288px';
		gebi('drop-seguidores').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-right';
		gebi('seta-esquerda-seguidores').style.display='none';
	}else{
		gebi('drop-seguindo').style.width='288px';
		gebi('drop-seguindo').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-right';
		gebi('seta-esquerda-seguindo').style.display='none';
	}
}

function mostrar_setas(deQuem){
	if(deQuem=="seguidores"){
		gebi('drop-seguidores').style.width='279px';
		gebi('drop-seguidores').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-right sem-borda-left';
		gebi('seta-esquerda-seguidores').style.display='block';
		gebi('seta-direita-seguidores').style.display='block';
	}else{
		gebi('drop-seguindo').style.width='279px';
		gebi('drop-seguindo').className='content-boxs-right-bar-seg box-scroll icons-vert sem-borda-right sem-borda-left';
		gebi('seta-direita-seguindo').style.display='block';
		gebi('seta-esquerda-seguindo').style.display='block';
	}
}

/*Repostar*/
function repostar_conteudo(blogid){
	$.ajax({
	  type: 'GET',
	  url: '/repostar.html',
	  data: "repostar="+blogid,
	  success: function(json) {
		if(json.retorno) {
			$.notifyBar({ cls: "success", html: json.message, close:true });
			//$('#btn-follow').html(json.html);
		} else {
			$.notifyBar({ cls: "error", html: json.message, close:true });
		}
	  },
	  dataType: 'json'
	});
}
