function update_mitmachen_stuff()
{
	if($('#mitmachen_tnb').get(0).checked)
	{
		$('#login_stuff').show();
	}
	else
	{
		$('#login_stuff').hide();
	}
}

function facebook_onlogin()
{
	$("#facebook_login").attr("value", "1");
	$("#form_mitmachen").submit();
}

$(document).ready(function()
{
	update_countdown();
	
	if($('#form_mitmachen').length)
	{
		update_mitmachen_stuff();
		
		$('#mitmachen_tnb').change(function()
		{
			update_mitmachen_stuff();
		})
	}
	
	$('a.youtube').click(function(){
		
		var width = ($(this).hasClass("size_4_3")) ? 480 : 640;
		
		$.fancybox({
			'padding'	: 10,
			'autoScale'	: false,
			'width'		: width,
			'height'	: 385,
			'titlePosition' : 'inside',
			'title'		: '<span class="title">'+this.title+'</span><a href="http://www.youtube.com/lzpbnrw" class="link" target="_blank">Zum YouTube Channel der LZPB »</span>',
			'href'		: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'		: 'swf',
			'swf'		: {
				'wmode'	: 'transparent',
				'allowfullscreen': 'true'
			}
		});
		
		return false;
	});
	
	$('.mitmachen').click(function()
	{
		$.fancybox({
			'padding'	: 0,
			'autoScale'	: false,
			'width'		: 759,
			'height'	: 480,
			'href'		: this.href,
			'type'		: 'iframe'
		});
		
		return false;
	});
	
	$('#reason').keyup(function()
	{
		var remaining = 140 - this.value.length;
		
		$('#reason_count span').text(remaining);
		
		if(remaining < 0)
		{
			$('#reason_count span').css("color", "red");
			$('#reason').css("color", "red");
			
			$("#next").hide();
			$("#notice_140").show();
		}
		else
		{
			$('#reason_count span').css("color", "");
			$('#reason').css("color", "");
			
			$("#next").show();
			$("#notice_140").hide();
		}
	})
	
	if($('#facebook_stream_container').length)
	{
		get_facebook_stream(0);
	}
});

var pages = false;

function get_facebook_stream(page)
{
	/* get page count*/
	var per_page = 4;
	
	if(page == 0 && pages == false)
	{
		$.getJSON("/mitmachen/json_get_post_pages/", function(data)
		{
			pages = data;
			get_facebook_stream(0);
		})
	}
	else
	{
		var start = page * per_page;

		var next_page = page + 1;
		if(next_page == pages) next_page = 0;

		var previous_page = page - 1;
		if(previous_page < 0) previous_page = pages-1;

		$.get('/mitmachen/posts/'+start+'/', function(data)
		{
			var html = $(data);
			html.hide();

			$('#facebook_stream_container').append(html);

			html.fadeIn();

			if($('#page_'+previous_page).length)
			{
				$('#page_'+previous_page).animate({ 'margin-left': -570 }, 1000, function()
				{
					$('#page_'+previous_page).remove();
					setTimeout('get_facebook_stream('+next_page+')', 8000);
				});
			}
			else
			{
				setTimeout('get_facebook_stream('+next_page+')', 8000);
			}
		});
	}
	
	
	
}

function update_countdown()
{
	
	var end = new Date(2010, 4, 9, 18);
	var today = new Date();
	
	if(today.getTime() < end.getTime())
	{
		var dd = end-today;

		var dday=Math.floor(dd/(60*60*1000*24)*1)
		var	dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
		var dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
		var dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)

		var day_zero = (dday < 10) ? "0" : "";
		var hour_zero = (dhour < 10) ? "0" : "";
		var min_zero = (dmin < 10) ? "0" : "";
		var sec_zero = (dsec < 10) ? "0" : "";

		$('#days').text(day_zero+dday);
		$('#hours').text(hour_zero+dhour);
		$('#minutes').text(min_zero+dmin);
		$('#seconds').text(sec_zero+dsec);

		setTimeout("update_countdown()", 1000)
	}
	else
	{
		$('#countdown_container').fadeOut();
	}
}
