$(document).ready(function() {
	
	FB.init({appId:'319019402903', status: true, cookie: true, xfbml: true});
	

	
	//var newYear = new Date(); 
	//newYear = new Date(2010, 5, 19, 16, 20); 

	//$("#countdown").countdown({until: newYear, format: 'dHMs'});
	
	var menuWidth = 30;
	$('#menu li').each(function(){
		menuWidth += $(this).width() + 30;
	});
	
	menuWidth = menuWidth + 16;
	$('#menu')
		.css('width', menuWidth)
		.css('margin-left', (860 - menuWidth) / 2);
	
	
						   
	$("#twitter-feed").getTwitter({
		userName: "cielitodrive",
		numTweets: 40,
		loaderText: "Encontrando pedazos de Cielito...",
		slideIn: true,
		showHeading: false,
		showProfileLink: false,
		showTimestamp: true
	});
	if (!$.browser.msie){
		$("#viajeroep-tweets").getTwitterSearch({
			q: "#viajeroEP OR @cielitodrive",
			numTweets: 20,
			loaderText: "Encontrando pedazos de Cielito...",
			slideIn: true,
			showHeading: false,
			showProfileLink: false,
			showTimestamp: true
		});
	}
	else {
		$('.tweets').hide();
	}
	
	$('.resultDiv_gsvb img').live('click', function(){
		$('.floatingPlayerBox_gsvb').css('top', '1103px').css('z-index', '10000');
		$('.floatingPlayer_gsvb').css('top', '1113px').css('z-index', '10000');
		$('.alldone_gsvb').html('cerrar');
		$('#overlay').width($(document).width()).height($(document).height()).show();
	});
	
	$('.alldone_gsvb').live('click', function(){
		$('#overlay').hide();
	});
	
	$('.flickr').find('.flickr_badge_image').each(function(){
		var img = $(this).find('img');
		var imgWidth =img.width();
		var imgHeight = img.height();
		
		img
			.attr('width', '160')
			.attr('height', (160 * (imgHeight/imgWidth)));
	});
	
	var flickrTableWidth =  $('.flickr_feed table').width();
	
	$('#astrophotos .prev').bind('click', function(){
		var left = $('.flickr_feed table').css('marginLeft').replace('px', '') * 1;
		if (left < 0){
			left = left + 100;
			if (left > 0) left = 0;
			$('.flickr_feed table').stop(true, true).animate({marginLeft: (left) + 'px'});
		}
		
		return false;
	});
	
	$('#astrophotos .next').bind('click', function(){
		var left = $('.flickr_feed table').css('marginLeft').replace('px', '') * 1;
		if (left - 100 > 600 - flickrTableWidth){
			$('.flickr_feed table').stop(true, true).animate({marginLeft: (left - 100) + 'px'});
		}
		return false;
	});
	
	$("#cover").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	200, 
		'speedOut'		:	200,
		'overlayOpacity':	0.5,	
		'centerOnScroll':	true
	});
	
	
	var carrousel = $('.carrousel');
	var carrouselTexts = new Array();
	var carrouselPointer = 1;
	
	carrousel.each(function(){
		carrouselTexts.push($(this).html());
	});
	
	var carrouselNext = function (){
		if (carrouselPointer > carrousel.length - 1 ){
			carrouselPointer = 0;
		}
		$('.carrousel').first().fadeOut(500, function(){
			$(this).html(carrouselTexts[carrouselPointer])
				.fadeIn();
			carrouselPointer++;
		});
	}
	
	setInterval ( carrouselNext, 4000 );

	
	
	/*$('#flickr_badge_wrapper').find('a').each(function(){
		var $this = $(this);
		$this.attr('href', $this.find('img').attr('src').replace('_m', '_o')).attr('rel', 'gallery')
			.fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	200, 
				'speedOut'		:	200,
				'overlayOpacity':	0.5,	
				'centerOnScroll':	true
			});
	});*/
	
	$('.astronauta_download').animate({
		right: '-400px'
	})
	.hover(
		function(){
			$(this).animate({
				right: '0px'
			})
		},
		function(){
			$(this).animate({
				right: '-400px'
			})
		}
	);
	
	
});

relative_time = function (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 = (relative_to.getTime() - parsed_date) / 1000;
  delta = delta + (relative_to.getTimezoneOffset() * 60);
  if (delta < 60) {
	return 'Hace menos de un minuto';
  } else if(delta < 120) {
	return 'Hace un minuto';
  } else if(delta < (60*60)) {
	return ('Hace ' + parseInt(delta / 60)).toString() + ' minutos';
  } else if(delta < (120*60)) {
	return 'Hace como una hora';
  } else if(delta < (24*60*60)) {
	return 'Hace como ' + (parseInt(delta / 3600)).toString() + ' horas';
  } else if(delta < (48*60*60)) {
	return 'Hace un d&iacute;a';
  } else {
	return ('Hace ' + parseInt(delta / 86400)).toString() + ' d&iacute;as';
  }
}
