﻿function abre_popup(id) {
	window.open("http://www.estadao.com.br/interatividade/Multimidia/ShowGaleria.action?idGaleria=" + id ,"","width=740, height=690, top=100, left=100, scrollbars=no");
};

var originalTextSize;
function changeTextSize(selector, amount, type)
{
	var size = $(selector).css("fontSize");
	
	if(!type) type = "%";
	
	if (size == "" || size.indexOf(type) < 0){
		size = 100;
		type = "%";
	}else{ 
		size = parseInt(size.replace(type,""));
		if(!originalTextSize) originalTextSize = size;
	}
	
	if(amount){
		size += amount;
	}else if(originalTextSize){
		size = originalTextSize;
	}
	
	$(selector).css("fontSize", size + type);
}

function buscar(){
	if($("#txtBusca").val() != ""){
		window.location = "http://www.estadao.com.br/busca/" + $("#txtBusca").val();
	}
}

function token(){
	var date = new Date();
	return Date.UTC(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds(),date.getMilliseconds());
}

function truncateString(text, limit, suffix)
{
	if (text.length > limit)
	{
		text = text.substr(0, limit - suffix.length);
		
		if(!text.charAt(text.length-1).match(/[\s\,\.\!\?]/))
			text = text.substr(0, text.lastIndexOf(" "));
			
		while(text.charAt(text.length-1).match(/[\s\,]/))
			text = text.substr(0, text.length-1);
		
		if(!text.charAt(text.length-1).match(/[\.\!\?]/))
			text += suffix;
	}
	
	return text
}

function montaUrlSEO(str)  {	
	str = removeAccents(str).replace(/ /g, "-").replace(/[^-a-zA-Z0-9]/, "");

	return str.toLowerCase();
}

function removeAccents(strAccents){
    strAccents = strAccents.split('');
    strAccentsOut = new Array();
    strAccentsLen = strAccents.length;
    var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽžÆæŒœß';
    var accentsOut = ['A','A','A','A','A','A','a','a','a','a','a','a','O','O','O','O','O','O','O','o','o','o','o','o','o','E','E','E','E','e','e','e','e','e','C','c','D','I','I','I','I','i','i','i','i','U','U','U','U','u','u','u','u','N','n','S','s','Y','y','y','Z','z','Ae','ae','Oe','oe','ss'];
    for (var y = 0; y < strAccentsLen; y++) {
        if (accents.indexOf(strAccents[y]) != -1) {
            strAccentsOut[y] = accentsOut[accents.indexOf(strAccents[y])];
        }
        else
            strAccentsOut[y] = strAccents[y];
    }
    strAccentsOut = strAccentsOut.join('');
    return strAccentsOut;
}

function mudarGaleria(obj, itemXML){
	var gal = $(obj).parent();
	while(!gal.is("#gal_estadao_tabs"))
		gal = gal.parent();
	
	var list = gal.find("ul.bb-md-gal_estadao_selector");
	
	gal.find("li.selected").removeClass("selected");
	$(obj).parent().addClass("selected");

	if(!itemXML){
		var imgurl = $(obj).css("background-image");
		
		if(typeof($(obj).attr("largeimg")) == "undefined"){
			imgurl = imgurl.replace("/P", "/G");
		} else {
			var imgurl = "url(" + $(obj).attr("largeimg") + ")";
		}
		
		var target = gal.find(".bb-md-gal_estadao_tab");
		target.find("a").css("background-image", imgurl);
		target.find("a").attr("href", $(obj).attr("url"));
		target.find("p").html($(obj).attr("title"));
		target.find("a").attr("title", $(obj).attr("title"));
	} else {
		var item = {urlXML:'',url:'',titulo:''};
		if(itemXML.jquery){
			item.urlXML = itemXML.find("urlXML").text();
			item.url = itemXML.find("url").text();
			item.titulo = itemXML.find("titulo").text();
		} else {
			item = itemXML;
		}
		
		var urlXML = item.urlXML;
		var paramXML = urlXML.replace(/.*videos\//i, "").replace(/.xml/,".jsonp");
		$.ajax({
			url: 'http://img.estadao.com.br/videos/proxy.php?url='+paramXML,
			dataType: "jsonp",
			success: function(data) {
				jwplayer("dvPlayerTVEstadao").setup({
					playlist:data,
					controlbar: 'bottom',
					repeat: 'list',
					width: '292',
					height: '240',
					flashplayer: '/estadao/novo/jwplayer/player.swf',
					skin: '/estadao/novo/jwplayer/skins/classic/classic.xml'
				});
			}
		});
		
		
		//swfPlayer.params["flashvars"] = "item=0&repeat=list&fullscreen=true&file=" + item.urlXML;
		//swfPlayer.write("dvPlayerTVEstadao");
		
		$("#gal_estadao-tabs-1 p").html("<a href=\"" + item.url + "\">" + item.titulo + "</a>");

	}
}

// Joga o Super Banner / Expansivel para o topo
function appendBanner(){
	$(".dvBannerExpansivel").append("<div id=\"dvBannerExpansivelHolder\"></div>");

	if($("#dvBannerExpansivelNoVisible").hasClass("bannerComSelo"))
		$("#dvBannerExpansivelHolder").addClass("bannerComSelo");
		
	$("#dvBannerExpansivelHolder").append($("#dvBannerExpansivelNoVisible div"));
}

// Exibe o selo de publicidade ao lado do Super Banner
function exibeSeloPublicidade(nomeDiv)
{
	if(document.getElementById(nomeDiv)){
		if(document.getElementById(nomeDiv).className == "")
			document.getElementById(nomeDiv).className = "bannerComSelo";
		else
			document.getElementById(nomeDiv).className += " bannerComSelo";
	}
}

// Função declarada apenas por questões de retrocompatibilidade
function setDvBannerExpansivelHolder()
{
	exibeSeloPublicidade();
}

function computeMoonPhase(year, month, day, hours) {
	var MOON_PHASE_LENGTH = 29.530588853;
	
	// Convert the year into the format expected by the algorithm
	var transformedYear = year - Math.floor((12 - month) / 10);
	
	// Convert the month into the format expected by the algorithm
	var transformedMonth = month + 9;
	if (transformedMonth >= 12) {
		transformedMonth = transformedMonth - 12;
	}
	
	// Logic to compute moon phase as a fraction between 0 and 1
	var term1 = Math.floor(365.25 * (transformedYear + 4712));
	var term2 = Math.floor(30.6 * transformedMonth + 0.5);
	var term3 = Math.floor(Math.floor((transformedYear / 100) + 49) * 0.75) - 38;
	var intermediate = term1 + term2 + (day + (hours - 1) / 24) + 59;
	if (intermediate > 2299160) {
		intermediate = intermediate - term3;
	}
	var normalizedPhase = (intermediate - 2451550.1) / MOON_PHASE_LENGTH;
	normalizedPhase = normalizedPhase - Math.floor(normalizedPhase);
	if (normalizedPhase < 0) {
		normalizedPhase = normalizedPhase + 1;
	}
	
	// Return the result as a value between 0 and MOON_PHASE_LENGTH
	return normalizedPhase * MOON_PHASE_LENGTH;
}

function nomeImagemTempo(clima, cidade)
{
	var lua = "";
	
	var data = new Date();
	
	if(data.getHours() < 6 || data.getHours() >= 18)
	{
		var phase = computeMoonPhase(data.getUTCFullYear(), 
									 data.getUTCMonth() + 1,
									 data.getUTCDate(),
									 data.getUTCHours());
		
		var truncPhase = Math.floor(phase) % 30;
		
		if(truncPhase >= 0 && truncPhase < 7){
			lua = "nova";
		} else if(truncPhase >= 7 && truncPhase < 15){
			lua = "crescente";
		} else if(truncPhase >= 15 && truncPhase < 23){
			lua = "cheia";
		} else { 
			lua = "minguante";
		}
	}
	
	var nome = cidade + "/" + cidade + "_" + clima + (lua != "" ? "_" + lua : "") + ".png";
	
	return nome;
}

function showSelo(){
	$(".pub157x27").show();
}

function posicionaSelo(id){
	$("#" + id).addClass("comSelo");
}

