function makeemail(emailadres, naam) {
 	var emailadres = emailadres.replace(/_AT_/i, '@', emailadres);
 	while(emailadres.search('_punt_') > 0) {
		var emailadres = emailadres.replace(/_punt_/i, '.', emailadres);
	}
	document.write('<a href="mailto:'+ emailadres +'">'+ naam +'</a>');
}

function setNewPhoto(direction){
	var currentPhoto = document.getElementById('frontpage_image').style.backgroundImage;
	var newPhoto;
	
	if(currentPhoto.indexOf('dnw-frontpage') >= 0){
		var currentPhotoParts = currentPhoto.split("-");
		var currentNumber = currentPhotoParts[3].split(".")[0];
	} else {
		var currentNumber = currentPhoto.split("/")[3].split(".")[0];
	}
		if(direction == 'prev'){
		currentNumber--;
		if(currentNumber == 0){
			currentNumber = 34;
		}
	} else {
		currentNumber++;
		if(currentNumber == 35){
			currentNumber = 1;
		}
	}
	
	document.getElementById('frontpage_image').style.backgroundImage ='url(/img/HeaderPhotos/'+currentNumber+'.jpg)';
}

$(document).ready(function() {
	//Update bar is een klein vierkantje aan de linkerkant die aangezet kan worden via de UNDER_CONTRUCTION constante in de config	
	$('#update_bar').hover(
		function(){
			$(this).addClass('update_bar_over');
			$(this).find('span').css("display", "block");
		},
		function(){
			$(this).removeClass('update_bar_over');
			$(this).find('span').css("display", "none");
		}
	);	
	
	//Ververs de chatberichten elke 30 seconden.
	if ( $('#ChatBoxContainer').length ) {
		window.setInterval(updateChatPage, 30000);
		initChatPageEvents();
	}
	
	function updateChatPage(){
		$('img:.loading_image').css("opacity", "0.6");
		$('#chat_loading').fadeIn(600);;
		//$('#sidebar_loading_1').fadeIn(600);
		//$('#sidebar_loading_2').fadeIn(600);
		
		//Het pad + query string + ajax parameter geven de content van een module terug.
		//Met jQuery kunnen we de content downsizen tot alleen de berichten (anders wordt bijv ook de textarea getoond)
		$.get('/direct.php'+location.search+'&ajax=true', function(data) {
			//Update Chat content
			$('#ChatBoxContainer').html($(data).find('div:#ChatBoxContainer'));
			initChatPageEvents();
			
			//Update wie is online
			$('div:#sidebar_1').html($(data).find('div:#sidebar_1'));
			
			//Update nieuwsfeeds
			$('div:#sidebar_2').html($(data).find('div:#sidebar_2'));
			
			$('#chat_loading').hide();
			//$('#sidebar_loading_1').hide();
			//$('#sidebar_loading_2').hide();
			
			//Update Live Chat 
			AjaxIM.client.updateFriendCount();
		});
		
		
	}
	
	//Omdat live() met AJAX niet werkte, wordt deze functie per refresh aangeroepen.
	function initChatPageEvents(){
		$('img:.chat_reply').css("opacity", "0.2");
		$('img:.chat_reply').fadeIn(200);
	
		$('.chat_row').click(function(){
			$(this).find('.chat_reply').fadeTo(200, 1).fadeTo(200, 0.5).fadeTo(200, 1);
			$('#chat_bericht').html($('#chat_bericht').html() + $(this).find('.chat_reply').attr("title") + ' ' ).focus();
			$(this).unbind();
		});
		
		$('.chat_row').mouseenter(function(){
			$(this).find('.chat_count').addClass('chat_count_hover');
			$(this).find('.chat_reply').fadeTo(50, 1);
		});
		
		$('.chat_row').mouseleave(function(){
			$(this).find('.chat_count').removeClass('chat_count_hover');
			$(this).find('.chat_reply').fadeTo(50, 0.2);
		});
	}
	
	//getLiveChats();
	$('<div id="active_chats" style="display: none"></div>').appendTo('body');
	
	
	
});	

AjaxIM.loaded(function() { 
	AjaxIM.client.login(); 
});