
function showDialog(dialogId)
{
	$.getJSON('/ajax', {	action: 'dialog', dialog: dialogId}, function(oResponse)
	{
		$('#lightbox-body').html(oResponse.html);
		
		if (dialogId == 'lightbox-login')
		{
			if (loginNetwork == 'twitter')
				$('.lightbox-body #holder-content').html($('.lightbox-body #holder-twitter').html());
			else if (loginNetwork == 'mobypicture')
				$('.lightbox-body #holder-content').html($('.lightbox-body #holder-mobypicture').html());
		}
		if($('form.uniForm').length > 0)
			jQuery('form.uniForm').uniform();
		$('#screen_dim').show();
		$('#lightbox-body').fadeIn(200);
		
	}, 'json');
}

function hideDialog()
{
	$('#lightbox-body').hide();
	$('#screen_dim').hide();
	
	if (signupRegisterSuccess == true)
		window.location = '/';
}

function homeReloadTweets()
{
	sListString = '';
	if ($('ul.filter #btn-rabo-pt').attr('class') == 'btn-com active')
		sListString = sListString + ',rabo-pt';
	if ($('ul.filter #btn-rabo-ct').attr('class') == 'btn-com active')
		sListString = sListString + ',rabo-ct';
	if ($('ul.filter #btn-rabo-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',rabo-fans';
	if ($('ul.filter #btn-rabo-ot').attr('class') == 'btn-com active')
		sListString = sListString + ',rabo-ot';
	if ($('ul.filter #btn-rabo-org').attr('class') == 'btn-com active')
		sListString = sListString + ',rabo-org';

	if ($('ul.filter #btn-tnw-editors').attr('class') == 'btn-com active')
		sListString = sListString + ',tnw-editors';
	if ($('ul.filter #btn-tnw-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',tnw-fans';
	if ($('ul.filter #btn-tnw-speakers').attr('class') == 'btn-com active')
		sListString = sListString + ',tnw-speakers';
	if ($('ul.filter #btn-tnw-press').attr('class') == 'btn-com active')
		sListString = sListString + ',tnw-press';
	if ($('ul.filter #btn-tnw-bloggers').attr('class') == 'btn-com active')
		sListString = sListString + ',tnw-bloggers';
		
	if ($('ul.filter #btn-tmf-artists').attr('class') == 'btn-com active')
		sListString = sListString + ',tmf-artists';
	if ($('ul.filter #btn-tmf-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',tmf-fans';
	if ($('ul.filter #btn-tmf-crew').attr('class') == 'btn-com active')
		sListString = sListString + ',tmf-crew';
	if ($('ul.filter #btn-tmf-vips').attr('class') == 'btn-com active')
		sListString = sListString + ',tmf-vips';
	if ($('ul.filter #btn-tmf-press').attr('class') == 'btn-com active')
		sListString = sListString + ',tmf-press';
		
	if ($('ul.filter #btn-ned-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-fans';
	if ($('ul.filter #btn-ned-press').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-press';
	if ($('ul.filter #btn-ned-vips').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-vips';
	if ($('ul.filter #btn-ned-agis').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-agis';
	if ($('ul.filter #btn-ned-spelers').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-spelers';
	if ($('ul.filter #btn-ned-sponsor').attr('class') == 'btn-com active')
		sListString = sListString + ',ned-sponsor';
				
	if ($('ul.filter #btn-ilse-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',ilse-fans';
	if ($('ul.filter #btn-ilse-herself').attr('class') == 'btn-com active')
		sListString = sListString + ',ilse-herself';
	if ($('ul.filter #btn-ilse-fanclub').attr('class') == 'btn-com active')
		sListString = sListString + ',ilse-fanclub';
	if ($('ul.filter #btn-ilse-band').attr('class') == 'btn-com active')
		sListString = sListString + ',ilse-band';

	if ($('ul.filter #btn-53j8-jagers').attr('class') == 'btn-com active')
		sListString = sListString + ',53j8-jagers';
	if ($('ul.filter #btn-53j8-bewijs').attr('class') == 'btn-com active')
		sListString = sListString + ',53j8-bewijs';
	if ($('ul.filter #btn-53j8-mrx').attr('class') == 'btn-com active')
		sListString = sListString + ',53j8-mrx';
		
	if ($('ul.filter #btn-idf-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',idf-fans';
	if ($('ul.filter #btn-idf-intel').attr('class') == 'btn-com active')
		sListString = sListString + ',idf-intel';
	if ($('ul.filter #btn-idf-speakers').attr('class') == 'btn-com active')
		sListString = sListString + ',idf-speakers';
	
	if ($('ul.filter #btn-aifw-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',aifw-fans';
	if ($('ul.filter #btn-aifw-media').attr('class') == 'btn-com active')
		sListString = sListString + ',aifw-media';
	if ($('ul.filter #btn-aifw-vips').attr('class') == 'btn-com active')
		sListString = sListString + ',aifw-vips';
	if ($('ul.filter #btn-aifw-model').attr('class') == 'btn-com active')
		sListString = sListString + ',aifw-model';
		
	if ($('ul.filter #btn-oxfam-fans').attr('class') == 'btn-com active')
		sListString = sListString + ',oxfam-fans';
		
	setCookie('filter', sListString, 365);

	$.getJSON('/ajax', {	action: 'getLiveStream', lists: sListString}, function(oResponse)
	{
		$('#tweets').html(oResponse.html);
	}, 'json');
	
	$.getJSON('/ajax', {	action: 'getMediaStream', lists: sListString}, function(oResponse)
	{
		$('.top-gallery').html(oResponse.html);
	}, 'json');

	
	oMap.googleMap.clearOverlays();
	oMap.filters = sListString;
	oMap.sync();
}

var signupRegisterSuccess = false;
var loginNetwork = 'twitter';

$(document).ready(function()
{
	$('#site-filter').live('click', function()
	{
		showDialog('lightbox-filter');
		return false;
	});
	
	$('#site-welcome').live('click', function()
	{
		showDialog('lightbox-welcome');
		return false;
	});

	$('#site-login-twitter').live('click', function()
	{
		loginNetwork = 'twitter';
		showDialog('lightbox-login');
		return false;
	});
	
	$('#map-filter').live('click', function()
	{
		showDialog('lightbox-mapfilter');
		return false;
	});
	
	$('.lightbox-body #login-twitter-link').live('click', function()
	{
		$('#holder-content').html($('#holder-twitter').html());
		jQuery('form.uniForm').uniform();
		return false;
	});
	
	
	$('#site-login-mobypicture').live('click', function()
	{
		loginNetwork = 'mobypicture';
		showDialog('lightbox-login');
		return false;
	});
	$('.lightbox-body #login-mobypicture-link').live('click', function()
	{
		$('#holder-content').html($('#holder-mobypicture').html());
		jQuery('form.uniForm').uniform();
		return false;
	});
	
		
	
	$('#site-logout').live('click', function()
	{
		$.post('/ajax', {	action: 'logoutUser'}, function(oResponse)
		{
			if (oResponse.result == 'OK')
			{
				window.location.reload();
			}
		}, 'json');
		
		return false;
	});
	
	
	$("#lightbox-body").delegate('form#form-login-twitter', 'submit', function()
	{
		$('.errorField').hide();
		
		$.post('/ajax', {	action: 'loginUser', 
							username: $('#login-twitter-username').val(), 
							password: $('#login-twitter-password').val(), 
							network: 'twitter'}, function(oResponse)
		{
			if (oResponse.result == 'OK')
			{
				window.location.reload();
			}
			else
			{
				$('#login-twitter-username-error').html('Ongeldige gebruikersnaam of wachtwoord. Probeer het opnieuw.');
				$('#login-twitter-username-error').show();
			}
		}, 'json');
		
		return false;
	});
	
	$("#lightbox-body").delegate('form#form-login-mobypicture', 'submit', function(ev)
	{
		$('.errorField').hide();
		
		$.post('/ajax', {	action: 'loginUser', 
							username: $('#login-mobypicture-username').val(), 
							password: $('#login-mobypicture-password').val(), 
							network: 'mobypicture'}, function(oResponse)
		{
			if (oResponse.result == 'OK')
			{
				window.location.reload();
			}
			else
			{
				$('#login-mobypicture-username-error').html('Ongeldige gebruikersnaam of wachtwoord. Probeer het opnieuw.');
				$('#login-mobypicture-username-error').show();
			}
		}, 'json');
		
		ev.preventDefault();
		return false;
	});
	
	$('#load-more-tweets').live('click', function()
	{
		toggle_livestream_reload(false);
		
		sLastTweetId = $('#tweets .comment:last-child .sub-info .media-id').val();
		
		sListString = '';
		if ($('#btn-team').attr('class') == 'btn-com active')
			sListString = sListString + ',rabo-pt';
		if ($('#btn-sport').attr('class') == 'btn-com active')
			sListString = sListString + ',rabo-ct';
		if ($('#btn-fans').attr('class') == 'btn-com active')
			sListString = sListString + ',rabo-fans';
			
		$.getJSON('/ajax', { action: 'getLiveStream', max_id: sLastTweetId, lists: sListString}, function(oResponse)
		{
			$('#tweets').append(oResponse.html);
		}, 'json');
		
		return false;
	});
	
	$('.fromvreporter .floater a').live('click', function()
	{
		sLastTweetId = $('#from-vgz-list .comment:last-child .sub-info .media-id').val();

		$.getJSON('/ajax', { action: 'getLiveStream', max_id: sLastTweetId, lists: 'vgz-people'}, function(oResponse)
		{
			$('#from-vgz-list').append(oResponse.html);
		}, 'json');
		
		return false;
	});
	

	
	$('#load-more-tweets-ilse').live('click', function()
	{
		toggle_livestream_reload(false);
		
		sLastTweetId = $('.fromilse .comment:last-child .sub-info .media-id').val();

		$.getJSON('/ajax', { action: 'getLiveStream', max_id: sLastTweetId, lists: 'ilse-herself'}, function(oResponse)
		{
			$('.fromilse .list').append(oResponse.html);
		}, 'json');
		
		return false;
	});
	
	$('#load-more-tweets-ilseuk').live('click', function()
	{
		toggle_livestream_reload(false);
		
		sLastTweetId = $('.fromilseuk .comment:last-child .sub-info .media-id').val();

		$.getJSON('/ajax', { action: 'getLiveStream', max_id: sLastTweetId, lists: 'ilse-uk'}, function(oResponse)
		{
			$('.fromilseuk .list').append(oResponse.html);
		}, 'json');
		
		return false;
	});
		
	$('#load-more-tweets-ilsenl').live('click', function()
	{
		toggle_livestream_reload(false);
		
		sLastTweetId = $('.fromilseuk .comment:last-child .sub-info .media-id').val();

		$.getJSON('/ajax', { action: 'getLiveStream', max_id: sLastTweetId, lists: 'ilse-nl'}, function(oResponse)
		{
			$('.fromilseuk .list').append(oResponse.html);
		}, 'json');
		
		return false;
	});
	
	$('.button-lightbox-close').live('click', function()
	{
		hideDialog();
		return false;
	});
		
	$('.button-signup').live('click', function()
	{
		showDialog('lightbox-signup');
		return false;
	});
	
	$('#button-signup-login').live('click', function()
	{
		loginNetwork = 'mobypicture';
		hideDialog();
		showDialog('lightbox-login');
		return false;
	});
	
	$("#lightbox-body").delegate('.form-register', 'submit', function()
	{
		
		// Perform checks

		$('.errorField').hide();
		
		if ($('#lightbox-body #register-redirection').is(':checked') == true)
			redirection = '1';
		else
			redirection = '0';
		
		if ($('#lightbox-body #register-agreeterms').is(':checked') == true)
			agreeTerms = '1';
		else
			agreeTerms = '0';
		
		$.post('/ajax', {	action: 'registerUser', 
							username: $('#lightbox-body #register-username').val(), 
							name: $('#lightbox-body #register-fullname').val(), 
							email: $('#lightbox-body #register-email').val(),
							pin: $('#lightbox-body #register-pin').val(),
							countryId: $('#lightbox-body #register-country').val(),
							redirection: redirection,
							agreeterms: agreeTerms}, function(oResponse)
		{
			if (oResponse.result == 'OK')
			{
				signupRegisterSuccess = true;
				hideDialog();
				
				window.location.reload();
			}
			else
			{
				$.each(oResponse.errors, function(i, item)
				{
					$('#register-'+item.field+'-error').html(item.error);
					$('#register-'+item.field+'-error').show();
				});
				
				signupRegisterSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	
	$("#lightbox-body").delegate('.form-firstlogin', 'submit', function()
	{
		
		// Perform checks
		$('.errorField').hide();
		
		if ($('#lightbox-body #firstlogin-redirection').is(':checked') == true)
			redirection = '1';
		else
			redirection = '0';
		
		if ($('#lightbox-body #firstlogin-agreeterms').is(':checked') == true)
			agreeTerms = '1';
		else
			agreeTerms = '0';
		
		if( $('#lightbox-body #firstlogin-stream').length > 0)
			stream_id = $('#lightbox-body #firstlogin-stream').val();
		else
			stream_id = 0;
		
		if( $('#lightbox-body #register-bier').length > 0)
			bierrekening = $('#lightbox-body #register-bier').val();
		else
			bierrekening = 0;
		
		$.post('/ajax', {	action: 'agreeTerms', 
							redirection: redirection,
							agreeterms: agreeTerms,
							stream: stream_id,
							bierrekening: bierrekening
						}, 
		function(oResponse)
		{

			if (oResponse.result == 'OK')
			{
				signupRegisterSuccess = true;
				hideDialog();
				
				window.location.reload();
			}
			else
			{
				$.each(oResponse.errors, function(i, item)
				{
					$('#firstlogin-'+item.field+'-error').html(item.error);
					$('#firstlogin-'+item.field+'-error').show();
				});
				
				signupRegisterSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	

	
	$('#map-filter-dialog ul.filter a').live('click', function()
	{
		if (this.className == 'btn-com active')
			this.className = 'btn-com';
		else
			this.className = 'btn-com active';
			
		mapReload();
	
		return false;
	});
	
	/*$('.comment-filter a').live('click', function()*/

	$('ul.filter a').live('click', function()
	{
		if (this.className == 'btn-com active')
			this.className = 'btn-com';
		else
			this.className = 'btn-com active';
			
		homeReloadTweets();
		
		return false;
	});
	
	$('#reload-homepage').live('click', function()
	{
		$.getJSON('/ajax', {	action: 'getLiveStream' }, function(oResponse)
		{
			$('#tweets').html(oResponse.html);
		}, 'json');
		
		$.getJSON('/ajax', {	action: 'getMediaStream' }, function(oResponse)
		{
			$('.top-gallery').html(oResponse.html);
		}, 'json');
		
		return false;
	});
	
	autoReloadMediastream = false;
	
	function toggle_mediastream_reload(toggle) {
		if(toggle) {
			autoReloadMediastream = true;
		}
		else {
			autoReloadMediastream = false;
		}
	}
	
	autoReloadLivestream = false;
	
	function toggle_livestream_reload(toggle) {
		if(toggle) {
			$('#reload-homepage-toggle').html( $('#reload-homepage-toggle').html().replace("Enable", "Disable") );
			$('#reload-homepage-toggle').html( $('#reload-homepage-toggle').html().replace(" aan", " uit") );
			autoReloadLivestream = true;
		}
		else {
			$('#reload-homepage-toggle').html( $('#reload-homepage-toggle').html().replace("Disable", "Enable") );
			$('#reload-homepage-toggle').html( $('#reload-homepage-toggle').html().replace(" uit", " aan") );
			autoReloadLivestream = false;
		}
	}
	
	$('#reload-homepage-toggle').live('click', function()
	{
		if (this.className == 'toggle active') {
			this.className = 'toggle';
			toggle_livestream_reload(false);
		}
		else {
			this.className = 'toggle active';
			toggle_livestream_reload(true);
		}
		
		return false;
	});
	
	
	setInterval( function() {
		if(autoReloadMediastream) {
			$.getJSON('/ajax', {	action: 'getMediaStream' }, function(oResponse)
			{
				$('.top-gallery').html(oResponse.html);
			}, 'json');
		}
	}, 7500);
	
	setInterval( function() {
		if(autoReloadLivestream) {
			sfirstTweetId = $('#tweets .comment:last-child .sub-info .media-id').val();
			
			$.getJSON('/ajax', { action: 'getLiveStream', min_id: sfirstTweetId }, function(oResponse)
			{
				if(oResponse.html) {				
					//prepend new HTML
					$('#tweets').prepend(oResponse.html);
					
					//remove items > 10
					$('#tweets .comment').slice(10).remove();
				}
				
			}, 'json');
		}
	}, 7500);
	
	
	$('.comment-tweet a.send').live('click', function()
	{
		return false;
	});
	
	$('.comment .sub-info .btn-comm').live('click', function()
	{
		toggle_livestream_reload(false);
		
		$('.comment-moby').hide();
		$('.comment-tweet').hide();
		$('.retweet-tweet').hide();
		$('#comment-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').val('@'+$(this).attr('username')+' ');
		$('#comment-tweet-'+$(this).attr('tweetid')).show();
		$('#comment-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').focus();
		$(this).addClass('active');
		
		return false;
	});
	
	$('.comment .sub-info .btn-comm-moby').live('click', function()
	{
		toggle_livestream_reload(false);
		
		$('.comment-moby').hide();
		$('.comment-tweet').hide();
		$('.retweet-tweet').hide();
		$('#comment-moby-'+$(this).attr('mobyid')+' .comment-area textarea').val('@'+$(this).attr('username')+' ');
		$('#comment-moby-'+$(this).attr('mobyid')).show();
		$('#comment-moby-'+$(this).attr('mobyid')+' .comment-area textarea').focus();
		$(this).addClass('active');
		
		return false;
	});
	
	$('.comment .sub-info .btn-t').live('click', function()
	{
		toggle_livestream_reload(false);
		
		$('.comment-moby').hide();
		$('.comment-tweet').hide();
		$('.retweet-tweet').hide();
		$('#retweet-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').val($(this).attr('tweet'));
		$('#retweet-tweet-'+$(this).attr('tweetid')).show();
		$('#retweet-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').focus();
		return false;
	});
	
	
	$('.comment').live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
		// do something on mouseover
			$(this).find('.share').show();
		} else {
	    // do something on mouseout
			$(this).find('.share').hide();
	  }
	});
	
	$('.comment .sub-info .btn-star').live('click', function()
	{
		$.post('/ajax', {	action: 'toggle_favorite', 
							type_id: $(this).attr('tweetid')
						},
						function(oResponse)
		{
			
			if (oResponse.result == 'OK')
			{
				
				
				if(oResponse.toggle == 'added') {
					$(this).addClass('active');
				}
				else
					$(this).removeClass('active');
			}
			else
			{
				$.each(oResponse.errors, function(i, item)
				{
					alert(item.error);
				});
				
				commentSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	
	$('.comment-tweet .verzend').live('click', function()
	{
		$.post('/ajax', {	action: 'addCommentTweet', 
							tweet_id: $(this).attr('tweetid'),
							comment: $('#comment-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').val()
						},
						function(oResponse)
		{
			
			if (oResponse.result == 'OK')
			{
				commentSuccess = true;
				$('.comment-tweet').hide();
			}
			else
			{
				$.each(oResponse.errors, function(i, item)
				{
					alert(item.error);
				});
				
				commentSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	
	$('.comment-moby .verzend').live('click', function()
	{
		$.post('/ajax', {	action: 'addComment', 
							moby_id: $(this).attr('mobyid'),
							comment: $('#comment-moby-'+$(this).attr('mobyid')+' .comment-area textarea').val()
						},
						function(oResponse)
		{
			
			if (oResponse.result == 'OK')
			{
				commentSuccess = true;
				$('.comment-moby').hide();
			}
			else
			{
				alert('error');
				$.each(oResponse.errors, function(i, item)
				{
					alert(item.error);
				});
				
				commentSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	
	$('.blog-list .hide_ellerdale').live('click', function()
	{
		ellerdaleId = $(this).attr('ellerdaleid');
		
		$.post('/ajax', {	action: 'hideEllerdale', 
							ellerdale_id: $(this).attr('ellerdaleid')
						},
						function(oResponse)
				{
			
			if (oResponse.result == 'OK')
			{
				$('#ellerdale_'+ellerdaleId).fadeOut();
			}
			else
			{
				alert('Kon dit item niet verbergen.');
			}
		}, 'json');

		return false;
	});
	
	$('.retweet-tweet .verzend').live('click', function()
	{
		$.post('/ajax', {	action: 'retweet', 
							tweet: $('#retweet-tweet-'+$(this).attr('tweetid')+' .comment-area textarea').val()
						},
						function(oResponse)
				{
			
			if (oResponse.result == 'OK')
			{
				commentSuccess = true;
				$('.retweet-tweet').hide();
			}
			else
			{
				$.each(oResponse.errors, function(i, item)
				{
					alert(item.error);
				});
				
				commentSuccess = false;
			}
		}, 'json');
		
		return false;
	});
	

	$('#gallery-page-next, #gallery-page-previous').live('click', function() {
		//traverse mediastream, but turn off the autmoatic reloading first
		toggle_mediastream_reload(false);
	
		$('.top-gallery').load('/ajax?action=getMediaStream&page='+$(this).attr('page')+'&format=html');
		
		return false;
	});
	
	$('.fotostream #page-previous, .fotostream #page-next').live('click', function() {
		$('.fotostream-stream').load('/ajax?action=getMediaStreamSRDJ&page='+$(this).attr('page')+'&format=html');
		
		return false;
	});
	
	$('.brievenbus #page-previous, .brievenbus #page-next').live('click', function() {
		$('.brievenbus-stream').load('/ajax?action=getMediaStreamSRBrief&page='+$(this).attr('page')+'&format=html');
		
		return false;
	});
	
	$('#user-browse .post-moby #page-previous, #user-browse .post-moby #page-next').live('click', function() {
		userId = $('#user-browse').attr('data-user-id');
	
		$('#user-browse .post-moby').load('/ajax?action=getUserMediaStream&user-id='+userId+'&page='+$(this).attr('page')+'&format=html');
		
		return false;
	});
	
	$('.user-navigation #page-previous, .user-navigation #page-next').live('click', function() {
		userId = $('.user-navigation').attr('data-user-id');
	
		$('.user-navigation-wrapper').load('/ajax?action=getUserNavigation&user-id='+userId+'&page='+$(this).attr('page')+'&format=html');
		
		return false;
	});
	
	$('#next-step-1').live('click', function() {
		$('#welcome-step-2').show();
		$('#welcome-step-1').hide();
	});
	
	$('#next-step-2').live('click', function() {
		$('#welcome-step-3').show();
		$('#welcome-step-2').hide();		
	});
	
	$('#shoutbox .input-media').change(function() 
	{
		if ($(this).val().length > 0)
		{
			$('#shoutbox-services ul li').show();
		}
	});
	
		/*
	$('#shoutbox .input-media').live('propertychange', function(event) 
	{
		if (event.propertyName=='value')
			alert('hoi');
	});*/
		
	if ($('.block-facts').length > 0)
	{
		$('.block-facts .thumbnail a').lightBox({fixedNavigation:true});
	}
		
	
	$('#shoutbox form').bind('submit', function() 
	{
		if ($('#shoutbox .input-media').val().length > 0)
		{
			return true;
		}
		else
		{
			$('#shoutbox .shoutbox-error').hide();
			$('#shoutbox .shoutbox-success').hide();
			//$('#shoutbox input[type="submit"]').attr('disabled', 'disabled');
			
			servicesString = '';
			$('#shoutbox-services ul input[type="checkbox"]:checked').each(function()
			{
				serviceName = $(this).attr('data-service-name');
				if ((serviceName == 'hyves') || (serviceName == 'twitter') || (serviceName == 'facebook'))
					servicesString = servicesString + ',' + $(this).attr('data-service-id');
			});
			
			statusMessage = $('#shoutbox #shoutbox-message').val();
			
			// Check whether 
			/*previousStatusMessage = cookie('lastmessage');
			
			if (statusMessage == previousStatusMessage)
			{
				$('#shoutbox .shoutbox-error').html('Dit bericht heb je al eerder gestuurd.');
				$('#shoutbox .shoutbox-error').show(400);
				return;
			}*/
			
			$.post('/ajax', {
					action: 'addMediaText', 
					message: statusMessage,
					service: servicesString
				},
				function(oResponse)					
				{
					if (oResponse.result == 'OK')
					{
						$('#shoutbox .shoutbox-success').html('Je bericht is verstuurd!');
						$('#shoutbox .shoutbox-success').show(400);
						$('#shoutbox #shoutbox-message').val('');
						$('#shoutbox #shoutbox-message').attr('isdirty', '');
						$('#shoutbox #shoutbox-message').trigger('blur');
						$('#shoutbox-message').bind('keydown',shoutboxMessageKeydown);
						toggle_livestream_reload(true);
						//document.cookie = 'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'

					}
					else
					{
						$('#shoutbox .shoutbox-error').html(oResponse.result);
						$('#shoutbox .shoutbox-error').show(400);
					}
					$('#shoutbox input[type="submit"]').removeAttr('disabled');
				}, 
				'json'
			);
			
			return false;
		}
	});
	
	function shoutboxMessageKeydown()
	{
		$(this).attr('isdirty', '1');
		$(this).unbind('keydown');
	}

	$('#shoutbox-message').bind('keydown',shoutboxMessageKeydown);
	
	/*$('#shoutbox-message').bind('keydown', function() 
	{

	});*/
	
	$('#shoutbox-message').bind('focus', function() 
	{
		if ($('.log-out-btn').length == 0)
		{
			$('#shoutbox-message').hide();
			$('#shoutbox-overlay').show();
			$(this).blur();
		}
		else
		{
			$(this).css('font-style', 'normal');
			$(this).css('color', 'black');
			$(this).css('height', '40px');
			$(this).css('overflow', 'auto');
			if ($(this).attr('isdirty') != '1')
				$(this).val('');
	
			$('#shoutbox .extra').show();
			$('#shoutbox #shoutbox-services').show();
			$('#shoutbox .shoutbox-send').show();
		}
	});
	
	$('#shoutbox-message').bind('blur', function() 
	{
		if ($(this).attr('isdirty') != '1')
		{
			$(this).css('font-style', 'italic');
			$(this).css('color', '#777777');
			$(this).val($(this).attr('data-placeholder'));
		}
	});

	$('#shoutbox .button-login').bind('click', function()
	{
		loginNetwork = 'mobypicture';
		showDialog('lightbox-login');
		return false;
	});
	
	$('#shoutbox-services .header-expand').bind('click', function()
	{
		$('#shoutbox-services ul').show(400);
		$('#shoutbox-services .header-expand').hide();
		$('#shoutbox-services .header-collapse').show();
		return false;
	});

	$('#shoutbox-services .header-collapse').bind('click', function()
	{
		$('#shoutbox-services ul').hide(400);
		$('#shoutbox-services .header-collapse').hide();
		$('#shoutbox-services .header-expand').show();
		return false;
	});

	$('#shoutbox-services-all').bind('click', function()
	{
		if ($(this).is(':checked') == true)
			$('#shoutbox-services ul li input[type="checkbox"]').attr('checked', 'checked');
		else
			$('#shoutbox-services ul li input[type="checkbox"]').removeAttr('checked');
	});
	
	$('.fotostream li a').live('click', function() {
		$('#dj-last-moby').attr('src', $(this).attr('data-large-img'));
		$('#dj-last-moby-link').attr('href', $(this).attr('href'));
		$('.fotostream h2').html( $(this).attr('title') );
		return false;
	});
	
	$('.brievenbus li a').live('click', function() {
		$('#brievenbus-last-moby').attr('src', $(this).attr('data-large-img'));
		
		return false;
	});

});

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString()) + "; path=/";

}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
