jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

$(document).ready(function() {
	$('.sabia div').cycle({'timeout':5000, 'delay':1000, 'cleartypeNoBg': true});
	
	$('.loader, .conteudo-transportes').hide();
	
	$('a.transportes').live('click', function() {
		$('.conteudo-transportes').slideUp('fast');
		var rel = $(this).attr('rel');
		$('.div-'+rel).slideToggle('slow');
		return false;
	});
	
	$('.input-newsletter').attr('value', 'eMail');
	
	$(".input-newsletter").blur(function() { if((this.value=='')) { this.value='eMail';  } });
	$(".input-newsletter").focus(function() { if((this.value=='eMail')) { this.value=''; } });
	
	$('a.fancy').fancybox({
		'padding': 0,
		'opacity': true,
		'overlayOpacity': '0.5',
		'overlayColor': '#000000'
	});
	
	$('#form-newsletter').live('submit', function() {
		var dados = $(this).serializeArray();
		$.ajax({
			type: "post",
			url: $(this).attr('action'),
			data: dados,
			cache: false,
			beforeSend: function() {
				$('#form-newsletter').fadeOut('slow', function(){$('.loader').fadeIn('slow');});
			},
			success: function(data) {	
				$('.loader').fadeOut('slow', function(){$(this).after('<p>&nbsp;</p><p>'+data+'</p>');});
			},
			error:function (xhr, ajaxOptions, thrownError){
				alert('Erro: '+xhr.statusText);
			}
		});
		return false;
	});
	
	$('#nav').superfish({
		autoArrows:    false,
		animation:   {opacity:'slide',height:'show'}
	});
	
	$('#apoios img').hover(function () {
		src = $(this).attr('src').split('.png',1);
		$(this).attr('src', src+'_h.png');
	}, 
	function () {
		src = $(this).attr('src').split('_h.png',1);
		$(this).attr('src', src+'.png');
	});

	function gettweet(user) {
		$.ajax({
			'url': 'http://twitter.com/statuses/user_timeline/' + user + '.json',
			'data': { count: 10 },
			'success': showtweet,
			'dataType': 'jsonp'
		});
	}

	function showtweet(tweet) {
		$.each(tweet, function() {
			if(!this["in_reply_to_user_id"]) {
				$("#twitter").html('<ul class="twitter"><li class="twitter-item"><a href="http://twitter.com/' + this["user"]["screen_name"] + '/status/' + this["id_str"] + '" class="twitter-link">' + this["text"] + '</a> <span class="twitter-timestamp">' +  DateHelper.time_ago_in_words_with_parsing(this["created_at"]) + '</span></li></ul>').fadeIn();
				if(window.console) {
					console.debug(this["id"]);
				}
				return false;
			}
		});
	}

	$("#twitter").hide();
	gettweet("Aquameeting");

	var DateHelper = {
		  // Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
		  // Ruby strftime: %b %d, %Y %H:%M:%S GMT
		  time_ago_in_words_with_parsing: function(from) {
		    var date = new Date; 
		    date.setTime(Date.parse(from));
		    return this.time_ago_in_words(date);
		  },

		  time_ago_in_words: function(from) {
		    return this.distance_of_time_in_words(new Date, from);
		  },

		  distance_of_time_in_words: function(to, from) {
		    var distance_in_seconds = ((to - from) / 1000);
		    var distance_in_minutes = Math.floor(distance_in_seconds / 60);
		    if (distance_in_minutes == 0) { return 'h&agrave; menos de um minuto'; }
		    if (distance_in_minutes == 1) { return 'h&agrave; 1 minuto'; }
		    if (distance_in_minutes < 45) { return 'h&agrave; '+distance_in_minutes + ' minutos'; }
		    if (distance_in_minutes < 90) { return 'h&agrave; uma hora'; }
		    if (distance_in_minutes < 1440) { return 'h&agrave; ' + Math.floor(distance_in_minutes / 60) + ' horas'; }
		    if (distance_in_minutes < 2880) { return '1 day ago'; }
		    if (distance_in_minutes < 43200) { return 'h&agrave; '+Math.floor(distance_in_minutes / 1440) + ' dias'; }
		    if (distance_in_minutes < 86400) { return 'h&agrave; 1 mês'; }
		    if (distance_in_minutes < 525960) { return 'h&agrave; '+Math.floor(distance_in_minutes / 43200)+ ' meses'; }
		    if (distance_in_minutes < 1051199) { return 'h&agrave; 1 ano'; }
		    return 'h&agrave; ' + Math.floor(distance_in_minutes / 525960) + ' anos';
		  }
		};

	$('#bolhas-1, #bolhas-2').css({opacity: 0});

	function bolhas () {
		
		
		$('#bolhas-1').animate({'opacity':1, 'bottom':'100px'}, 5500, function() {
			$(this).animate({'bottom':'150px','opacity':0}, 1500, function() {
				$(this).css({'bottom':'-100px'})
			});
		});

		$('#bolhas-2').delay(1500).animate({'opacity':1, 'bottom':'50px'}, 2500, function() {
			$(this).animate({'bottom':'100px','opacity':0}, 2500, function() {
				$(this).css({'bottom':'-50px'})
			});
		});
		
	}

	bolhas ();

	setInterval(bolhas, 12500);
	
	$('.roteiro-conteudo').hide();
	
	$('.roteiro-title').click(function() {
		$('.roteiro-conteudo').slideUp();
		$(this).next('.roteiro-conteudo').is(":visible") ? $(this).next('.roteiro-conteudo').slideUp('fast') : $(this).next('.roteiro-conteudo').slideDown('fast');

	  return false;
	});

});
