var cidades = new Array;
var documento = $("");

$(document).ready(function() {

	//$.preloadCssImages();
	
	//transicoes do banner
	$("#banners_home").coinslider({
		width: 960, // width of slider panel
		height: 354, // height of slider panel
		spw: 7, // squares per width
		sph: 5, // squares per height
		delay: 10000, // delay between images in ms
		sDelay: 30, // delay beetwen squares in ms
		opacity: 0.7, // opacity of title and navigation
		titleSpeed: 500, // speed of title appereance in ms
		effect: '', // random, swirl, rain, straight
		navigation: true, // prev next and buttons
		links : false, // show images as links 
		hoverPause: false // pause on hover							  
	});
	
	//audio na home
	$("#player").jPlayer({
		ready: function () {
			this.element.jPlayer("setFile", SITE_URL+"/media/jingle_aline_correa_forro.mp3").jPlayer("play");
		},
		volume: 70,
		oggSupport: false,
		customCssIds: true
	})
	.jPlayer("cssId", "pause", "player_ctr_stop")
	.jPlayer("cssId", "play", "player_ctr_play");
	
	$("#player_ctr_stop").click(function(){
		$("#player_ctr_play").show();
		$(this).hide();
	});
	$("#player_ctr_play").click(function(){
		$("#player_ctr_stop").show();
		$(this).hide();
	});
	//fotos no menu lateral
	var timer;
	clearTimeout(timer);
	povoaGaleriaFlickr();
	
	//animar abas 
	$("#formspring").css('opacity', 0);
	$("#twitter").css('opacity', 1);
	
	$("#aba_twitter").click(function(){
		$(this).css('backgroundPosition', 'left bottom');
		$("#aba_formspring").css('backgroundPosition', 'left top');
		$("#formspring").animate({opacity: 0}, 1000, function(){
					$("#twitter").css('display', 'block').animate({opacity: 1}, 1000);
					$("#formspring").css({display: 'none', opacity: 0});
		});			
	});
	
	$("#aba_formspring").click(function(){
		$(this).css('backgroundPosition', 'left bottom');
		$("#aba_twitter").css('backgroundPosition', 'left top');
		$("#twitter").animate({opacity: 0}, 1000, function(){
					$("#formspring").css('display', 'block').animate({opacity: 1}, 1000);	
					$("#twitter").css({display: 'none', opacity: 0});
		});			
	});
	
	
	/** Twitter Functions **/
	
	//Send tweet
	$('#twitter_send_button').button();
	$('#twitter_send_button').click(function(){
		if($('#sign_in_with_twitter').html() != '') {
			$('#sign_in_with_twitter').dialog('open');
		} else {
			$("#twitter_load").css("display", "block");
			var tweet = $('#twitter_textarea').serialize();
			$.post('index.php', tweet, function(data){
				if(data) {
					if(!data.error) {
						$('#twitter_textarea').val(twitter_user);
						$('#twitter_char_counter').html(twitter_max-$('#twitter_textarea').val().length);
						$('#twitter_msg').html('Mensagem enviada com sucesso!');
					} 
				} 
				$("#twitter_load").css("display", "none");
			}, "json");
		}
	});
	
	//Reply
	$('.reply a').click(function() {
		var in_reply_to = $(this).attr('in_reply_to');
		$('#twitter_textarea').val(in_reply_to);
		$("#aba_twitter").click();
		return false;
	});
	
	//Retweet
	$('.retweet_link a').click(function() {
		var tweet_id = $(this).attr('status_id');
		var tweet_link = $(this);
		$.post('index.php', {"tweet_id": tweet_id}, function(data,status) {
			if(data) {
				if(!data.error) {
					tweet_link.replaceWith('<span class="retweeted">Retweeted</span>');
				}
			}
		}, "json");
		return false;
	});	
	
	//Follow user
	$('#twitter_follow').click(function() {
		$.post('index.php', {"follow": true}, function(data) {
			if(data) {
				if(!data.error) {
					$('#twitter_follow').replaceWith('<span class="is-following"><i></i><strong>Following</strong></span>');
				}
			}
		}, "json");
	});	
	
	//Character counter
	var twitter_max = 140;
	var twitter_user = '';
	$('#twitter_textarea').keyup(function(){
		var len = $(this).val().length;
		
		var count = twitter_max-len;
		
		if(count < 20 && count >= 10) {
			$('#twitter_char_counter').css("color", "#5C0002");
		} else if(count < 10) {
			$('#twitter_char_counter').css("color", "#D40D12");
		} else {
			$('#twitter_char_counter').css("color", "");
		}
		
		$("#twitter_char_counter").html(count);
	});
	
	//Inicialize
	twitter_user = $('#twitter_textarea').val();
	$('#twitter_char_counter').html(twitter_max-$('#twitter_textarea').val().length);
	$('#sign_in_with_twitter').dialog(
			{
				autoOpen: false,
				resizable: false,
				buttons: {
					"Ir Para o Twitter" : function() {
						$('#twitter_go_form input[name=tweet]').val($('#twitter_textarea').val());
						$('#twitter_go_form').submit();
					},
					"Cancelar" : function() {
						$(this).dialog("close");
					}
				}
			
			});
	
	/** End Twitter Functions **/
	
	//tirar overlay explicativo do formspring
	$("#formspring_overlay").hover(function(){
		$(this).hide("fast");
	})

	//pegar perguntas do formspring
	$('#home_perguntas ul').rssfeed('http://www.formspring.me/profile/DepAlineCorrea.rss', {
		limit: 5,
		header: false,
		titletag: "h4",
		date: false,
		snippet: false,
		errormsg: "Desculpe, o Formspring está fora do ar."
	  });
	
	//abrir pergunta do formspring ao clicar no titulo
	$(".box_content").live('click', function(){
		$(this).children(".resposta_formspring").slideToggle("slow");
		$(this).children(".botao_mais_single").toggleClass("fechar");
	});
	
	//dialog aprovado
	$('#dialog_aprovado').dialog(
			{
				autoOpen: true,
				resizable: false,
				buttons: {
					"OK" : function() {
						$(this).dialog("close");
						$(this).remove();
					}
				}
	});
			
});