﻿var pontos = new Array();
ativarMenu = false;
var linguaAtual = 'pt';

jQuery(function($){
	$('html, body, #site').height($(document).height());
	$('input[type=submit]').addClass("botao");  
	$('input[type=text]').addClass("input");
	$('input[type=file]').addClass("inputArquivo"); 

	// Tweets
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline/wesleybs.json?include_rts=1&count=3&callback=?', function(data) {
		$(data).each(function() {
			$('ul.twits').append('<li><p class="fCor1">' + this.text.linkify().at().hash() + '</p></li>');
		});
	});	
	
	Cufon('.menuRodape a, #menuTopo li a, h1, h2, h3, h4, h5', {
		fontFamily: 'Dekar'
	});

	$('.slideGaleria').scrollable({
		items: '.itens',
		size: 1,
		circular: true,
		keyboard: false
	}).navigator(".navi").autoscroll({
		interval: 3000		
	})

	if (ativarMenu){
		$('#menu-menutopo a').click(function(){
			desvio = 18 // graus positivos, pra deslocar o mapa a esquerda;
			var id = $(this).parent().attr('id').replace('menu-item-','');
			$('#conteudo').html('').addClass('loading');
			$.ajax({
				url: $(this).attr('href'),
				success: function(html){
					// colocando o conteudo
					$('#conteudo').html($('#conteudo',html).html()).removeClass('loading');
					// refazendo o cufon
					Cufon.refresh();
					gapi.plusone.go();
					// refazendo o altura do site
					$('html, body, #site').height(0).height($(document).height());
					// Ativando o Shadowbox
					Shadowbox.setup();
				}
			})
			if (pontos[id]){
				lat = parseFloat(pontos[id][0]);
				// move a latitude marcada conforme desvio estabelecido
				lng = parseFloat(pontos[id][1]) + desvio;
				removePontos();
				pontos[id][2].setMap(map);
				// direciona o mapa
				mapaVai(lat,lng);
			}
			return false;
		});
	}

	$('.linguas a').click(function(e) {
		lingua = $(this).attr('lingua');
		$('#wpadminbar, #areaTwitter, .linguas').addClass("notranslate");
		$('body').translate({
			toggle: true,
			from: linguaAtual,
			to: lingua,
			not: '.notranslate',
			each: function(i){
				if(this.elements[i].tagName.toLowerCase() == "cufontext"){
					$(this.elements[i]).parent().parent().html(this.translation[i]);
					Cufon.refresh();
				}
			},
			complete: function (){
				linguaAtual = lingua;
			}
		});
		return false;
	});
});

timerMap = null;
function mapaVai(lat,lng) {
	clearTimeout(timerMap);
	ponto = new google.maps.LatLng(lat,lng);
	if (map.getBounds() == undefined) {
		map.panTo(ponto);
	} else {
		limite = 10;
		var ctr = map.getCenter();
		lat2 = ctr.lat();
		lng2 = ctr.lng();
		d1 = lat2 - lat;
		d2 = lng2 - lng;
		if (Math.abs(d1) < limite && Math.abs(d2) < limite) {
			map.panTo(ponto);	
		} else {
			timerMap = setTimeout(function() { mapaVai(lat,lng); },100);
			map.panTo(new google.maps.LatLng(lat2-(d1/2),lng2-(d2/2)));
		}
	}
}

function registraPonto(id, lat, lng){
	pontos[id] = new Array();
	pontos[id][0] = lat;
	pontos[id][1] = lng;
}

function removePontos(){
	for (var id in pontos){
		pontos[id][2].setMap(null);
	}
}


function criaPontos(){
	for (var id in pontos){
		criaPonto(id, pontos[id][0], pontos[id][1]);
		pontos[id][2].setMap(null);
	}
}

function criaPonto(id, lat, lng){
	var ponto = new google.maps.LatLng(lat, lng);
	var image = '/wp-content/themes/id5/imgs/mapa/mapa-' + id + '.png';
	var objMarcador = new google.maps.Marker({
		position: ponto,
		map: map,
		icon: image
	});
	pontos[id][2] = objMarcador;
	google.maps.event.addListener(objMarcador, 'click', function() {
		infowindow.open(map,objMarcador);
	});
}

function gMaps(lat, lng, disableDefaultUI){
	var latlng = new google.maps.LatLng(lat, lng);
	var MY_MAPTYPE_ID = 'id5';
	zoom = 4;
	var opts = {
		zoom: zoom,
		disableDefaultUI: disableDefaultUI,
		center: latlng,
		draggable: false,
		mapTypeId: MY_MAPTYPE_ID
	};

	map = new google.maps.Map(document.getElementById('mConteudo'),opts);
	var styledMapOptions = {
		name: MY_MAPTYPE_ID
	};
	var poaccMapType = new google.maps.StyledMapType(getStyle(), styledMapOptions);
	map.mapTypes.set(MY_MAPTYPE_ID, poaccMapType);
	
	map.mapTypes[MY_MAPTYPE_ID].minZoom = zoom;
	map.mapTypes[MY_MAPTYPE_ID].maxZoom = zoom;
}

function getStyle() {
	return [
		{
			featureType: "all",
			elementType: "labels",
			stylers: [
				{ visibility: "off" }
			]
		},
		{ 
			featureType: "road",
			elementType: "all",
			stylers: [ 
				{ visibility: "off" }
			]
		 },
		{
			featureType: "poi",
			elementType: "all",
			stylers: [
				{ visibility: "off" }
			]
		},
		{
			featureType: "transit",
			elementType: "all",
			stylers: [
				{ visibility: "off" }
			]
		},
		{
			featureType: "water",
			elementType: "labels",
			stylers: [
				{ visibility: "off" }
			]
		},
		{
			featureType: "water",
			elementType: "geometry",
			stylers: [
				{ visibility: "on" },
				{ hue: "#eaeaea" },
				{ gamma: 0.5 },
				{ saturation: 6 },
				{ lightness: 95 }
			]
		},
		{
			featureType: "landscape",
			elementType: "all",
			stylers: [
				{ gamma: 0.14 },
				{ hue: "#13222f" },
				{ lightness: -30 }
			]
		},
		{	
			featureType: "administrative",
			elementType: "labels",
			stylers:[ 
				{ visibility: "off" }
			] 
		}
	];
}




// Acrescenta o método linkfy no obj String. Este método transforma urls em links clicáveis
String.prototype.linkify = function() {
	return this.replace(/\b(((https*\:\/\/)|www\.)[^\"\']+?)(([!?,.\)]+)?(\s|$))/g, function(m, l, j, i, h) {
		var k = j.match(/w/) ? "http://" : "";
		return '<a class="twtr-hyperlink" target="_blank" href="' + k + l + '">' + k + l + "</a>" + h
	});
};

String.prototype.at = function(){
	return this.replace(/\B[@]([a-zA-Z0-9_]{1,20})/g,function(h,i){
		return '@<a target="_blank" class="twtr-atreply" href="http://twitter.com/'+i+'">'+i+"</a>"}
	)
}

String.prototype.hash = function () {
	return this.replace(/(^|\s+)#(\w+)/gi, function (h, i, j) {
		return i + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + j + '">#' + j + "</a>"
	})
}

// Função que retorna uma string com o tempo relativo a time_value
function relative_time(time_value) {
	var values = time_value.split(' ');
	time_value = values[1] + ' ' + values[2] + ', ' + values[5] + ' ' + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);

	var r = '';
	if (delta < 60) { r = 'um minuto atrás'; }
	else if(delta < 120) { r = 'alguns minutos atrás'; }
	else if(delta < (45*60)) { r = (parseInt(delta / 60)).toString() + ' minutos atrás'; }
	else if(delta < (90*60)) { r = 'uma hora atrás'; }
	else if(delta < (24*60*60)) { r = '' + (parseInt(delta / 3600)).toString() + ' horas atrás'; }
	else if(delta < (48*60*60)) { r = 'um dia atrás'; }
	else { r = (parseInt(delta / 86400)).toString() + ' dias atrás'; }

	return r;
}


