$(document).ready(function(){
	loadCommentsComunidades(false);
	
	$(".bb-md-noticia-subcom-com a, .bb-md-noticia-subcom-end a").click(function(event){
		//event.preventDefault();
		$(this).blur();
		
		esconderMateria();
	});
});

function mostrarMateria()
{
	if(typeof(oReload) != "undefined")
		oReload.start();
	if($("div.texto-noticia").css("display") == "none"){
		$("div.texto-noticia").show("blind", 1000);
		$(".bb-md-noticia-tab-main li.bb-active").removeClass("bb-active");
		$(".bb-md-noticia-tab-main li#tab-materia").addClass("bb-active");
	}
		
	loadCommentsComunidades(false);
}

function esconderMateria()
{
	if(typeof(oReload) != "undefined")
		oReload.stop();
	if($("div.texto-noticia").css("display") == "block"){
		$("div.texto-noticia").hide("blind", 1000);
		$(".bb-md-noticia-tab-main li.bb-active").removeClass("bb-active");
		$(".bb-md-noticia-tab-main li#tab-comentarios").addClass("bb-active");
		
		loadCommentsComunidades(true);
	}
	else
		mostrarMateria();
}

/*function loadComments(full,start){
	if(typeof(mostraComentarios) != "undefined" && mostraComentarios == false){
		return;
	}
	//if(typeof(hashtag) != "undefined" && hashtag != null){
	//	if(hashtag != "")
			loadCommentsComunidades(full);
		return;
	//}

	var quantity;
	if(!full)
	{
		start = null;
		quantity = 3;
		
		$(".bb-md-noticia-subcom-end a").html("ler todos os coment&aacute;rios <img src=\"/estadao/novo/img/ic-next.gif\"/>");
	}
	else
	{
		start = start || 0;
		quantity = 20;

		$(".bb-md-noticia-subcom-end a").html("leia a not&iacute;cia <img src=\"/estadao/novo/img/ic-next.gif\"/>");
	}
	
	if(typeof(guid) == "undefined")
		return;
	
	$.get("/estadao/novo/includes/comentarios/listar.php",{guid: guid, i:start, q:quantity, t:token()},function(data){
		var qtde = $(data).find("#hTotalComentarios").val();
		
		if(qtde > 0){
			$(".bb-md-noticia-subcom-com a").html("coment&aacute;rios para este post <sub>" + $(data).find("#hTotalComentarios").val() + "</sub>");
			$("h4.bb-md-noticia-subcom-next a:first").html("comente tamb&eacute;m <img src=\"/estadao/novo/img/ic-next.gif\"/>");
			$(".bb-md-noticia-subcom-com a").html("coment&aacute;rios para este post <sub>" + $(data).find("#hTotalComentarios").val() + "</sub>");
			$("h4.bb-md-noticia-subcom-next a:first").html("comente tamb&eacute;m <img src=\"/estadao/novo/img/ic-next.gif\"/>");
			$(".bb-md-not-commnents_count").text($(data).find("#hTotalComentarios").val());
			
			$(".bb-md-noticia-subcom-end").show();
			
			if(start > 0){
				$(".bb-md-noticia-coment_comentarios a.more").remove();
				
				$(".bb-md-noticia-coment_comentarios").append($(data).html());
			}else {
				$(".bb-md-noticia-coment_comentarios").html($(data).html());
			}
		} else {
			$("h4.bb-md-noticia-subcom-com").html("nenhum coment&aacute;rio para este post");
			$("h4.bb-md-noticia-subcom-next a:first").html("seja o primeiro a comentar <img src=\"/estadao/novo/img/ic-next.gif\"/>");
			$(".bb-md-noticia-subcom-end").hide();
		}
		if($("#bb-md-noticia-subcom").css("display") == "none")
			$("#bb-md-noticia-subcom").show("slide",{direction: "up"}, 500);
	},"html");
}
*/
function loadCommentsComunidades(full,page){
	if(hashtag == "null")
		hashtag = "";
	
	var quantity = "3";
	if(full){
		quantity = "20";
		page = page || 1;
		
		//$(".discussion .show_all").html("leia a not&iacute;cia <img src=\"/estadao/novo/img/ic-next.gif\"/>");
	}else{
		//$(".discussion .show_all").html("ler todos os coment&aacute;rios <img src=\"/estadao/novo/img/ic-next.gif\"/>");
	}
	
	if($(".discussion .more").length > 0)
		$(".discussion .more").replaceWith("<span class='more loading'>Carregando...</span>");
	else
		$(".discussion .show_all").before("<span class='more loading'>Carregando...</span>");
	
	var url = "/estadao/novo/includes/comentarios/carregar.php";
	
	$.get(url,{g:guid,h: hashtag,q:quantity,p:page || "",t:token()},function(data){
		if(!page || page == 1){
			$(".discussion").html(data);
		} else {
			$(".discussion .comments").append(data);
		}
		
		if($(data).find(".totalComentarios").length > 0)
			$(".bb-md-not-commnents_count").text($(data).find(".totalComentarios").text());
		
		if($(".discussion").css("display") == "none")
			$(".discussion").show("slide",{direction: "up"}, 500);
			
		/*if($(".discussion .show_all").length == 0){
			if(full){
				$(".discussion").append("<a class='show_all' href='#noticia'>leia a not&iacute;cia <img src=\"/estadao/novo/img/ic-next.gif\"/></a>");
			}else{
				$(".discussion").append("<a class='show_all' href='#noticia'>ler todos os coment&aacute;rios <img src=\"/estadao/novo/img/ic-next.gif\"/></a>");
			}
			
			
		}*/
		
		$(".discussion .show_all").unbind("click").click(function(event){
			//event.preventDefault();
			$(this).blur();
			
			esconderMateria();
		});
	
		if(page > 0){
			$(".discussion .more").replaceWith("<a class='more' href='#'>Exibir mais coment&aacute;rios</a>");
			$(".discussion .more").unbind('click').click(function(event){
				event.preventDefault();
				$(this).blur();
				
				loadCommentsComunidades(true,page + 1);
			});
		}
		
		if($(data).find(".comment.last").length > 0)
			$(".discussion .more").hide();
				
		document.domain = "estadao.com.br";
		
		//$(".discussion").append('<a href="" class="more">Pr&oacute;ximos <span class="count">20</span> coment&aacute;rios de <span class="count">1000</span></a>');
	});
}

function deixarComentario(editoria, subeditoria, idPagina, GUIDPagina){
	var guid = GUIDPagina.substring(0,8) + "-" + GUIDPagina.substring(8,12) + "-" + GUIDPagina.substring(12,16) + "-" + GUIDPagina.substring(16,20) + "-" + GUIDPagina.substring(20);
	var url = "http://cadastro.estadao.com.br/comentar?guid=" + guid + "&editoria="+editoria+"&titulo_pg=" + encodeURIComponent(title);
	
	if(typeof(hashtag) != "undefined" && hashtag != null)
		url += "&hashtag=" + hashtag;
	
	url += "&url_pg=" + document.location;
	
	window.open(url,"comentarios","width=500, height=600, top=30, left=300, scrollbars=yes");
}

function enviarEmail(){
	var url = "http://sites.estadao.com.br/enviar_noticia/formularioEnviar.php?linkNoticia=" + escape(window.location) + "&tituloNoticia=" + escape(title) + "&editoria=" + editoria;
	window.open(url, 'envio_email', 'toolbars=no,status=no,width=656,height=620,location=no');
}

function imprimir(){
	window.open("/noticia_imp.php?req=" + url, 'impressao', 'width=690, height=480, resizable=yes, scrollbars=yes');
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

//BOX VIDEO ESPN
$(function() {
	function handler1(event) {
		var $target = $(event.target);
		$target = $target.parents('.videoEspnContent');
		$target.children('.boxVideoClosed').fadeOut('slow', function() {
			$target.css("height","330px");
			$target.children('.boxVideoOpen').fadeIn('slow');
		});
	}

	function handler2(event) {
		var $target = $(event.target);
		$target = $target.parents('.videoEspnContent');
		var $video = $target.find('.videoESPN object').attr("id");
		if (sliderPlayersArray.indexOf($video)!=-1) jwplayer($video).pause();
		$target.children('.boxVideoOpen').fadeOut(100, function() {
			$target.css("height","auto","width", "635px");
			$target.children('.boxVideoClosed').fadeIn(3000);
		  });
	}
	$(".boxVideoClosed a").click(handler1);
	$(".btCloseVideo").click(handler2);

});
//BOX VIDEO ESPN
