
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() {
	//Used to differentiate between single and double clicks on one element.
	jQuery.fn.single_double_click = function(single_click_callback, double_click_callback, timeout) {
	  return this.each(function(){
		var clicks = 0, self = this;
		jQuery(this).click(function(event){
		  clicks++;
		  if (clicks == 1) {
			setTimeout(function(){
			  if(clicks == 1) {
				single_click_callback.call(self, event);
			  } else {
				double_click_callback.call(self, event);
			  }
			  clicks = 0;
			}, timeout || 300);
		  }
		});
	  });
	}
	
	/* Wordt o.a. gebruikt om ID van een rij te halen. prod[x] */
	$.fn.extractNumber = function(e){
		return e.match(/\d+/)[0]; 
	};
	
	//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 randomString(){
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 12;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		
		return randomstring;
	}
	
	function updateChatPage(){
		//$('img:.loading_image').css("opacity", "0.6");
		$('#chat_loading').fadeIn(300);
		//$('#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) { // veranderd na mod_rewrite update
		
		$.post('/direct.php?ac=module&module=chat&ajax=true&unique='+randomString(), {search: $('#chat_search').val() }, function(data) {
			var newMessages = parseInt($(data).find('div:#chat-new-messages').html());
			if(newMessages >0){
				document.title = "["+newMessages+"] GSVA 'Petrus Plancius' - Babbelbox";
			} else {
					document.title = "GSVA 'Petrus Plancius' - Babbelbox";
			}
			
			//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').fadeOut(300);
			//$('#sidebar_loading_1').hide();
			//$('#sidebar_loading_2').hide();
			
			//Update Live Chat 
			//AjaxIM.client.updateFriendCount();
		});
	}
	
	
	$('#chat_search').bind("keyup change", function(){
		if ($(this).val().length > 2 || $(this).val().length == 0){
			updateChatPage();
		}
	});
		
		
	$('#chat_search').change(function(){
		updateChatPage();
	});
	
	$('#frm_chat_search').submit(function(event){
		updateChatPage();
		
		event.preventDefault();
	});
	
	
	//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').single_double_click(function(e){
			var $target = $(e.target);
			if($target.is("div")){
				$(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();
			}
		}, function(e){
			//double click
			var chat_id = $().extractNumber($(this).attr('id'));
			var $target = $(e.target);

			/*console.log($(this).children('.chat_likes'));
			console.log($target);
			console.log($target.closest('.chat_row'));
			*/
			var chat_div = $target.closest('.chat_row').find('.chat_likes');
			
			$('body').data('chat_like_dive', chat_div);
			
			$(chat_div).load('/public/libs/ajax.php?type=plus-een&q='+chat_id).show();

		}).disableSelection();
		
		
		$('.chat_likes').each(function(){
			var liking = $().extractNumber($(this).attr('class'));
			switch(true){
				case (liking < 5):
					$(this).css('opacity', 0.3);
				break;
				case (liking < 10):
					$(this).css('opacity', 0.5);
				break;
				case (liking < 20):
					$(this).css('opacity', 0.8);
				break;
				case (liking > 19):
					$(this).css('opacity', 1);
				break;
			}
		});
		
		$('.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');
	
	
	
});	

/*
Werkte een poos, maar teveel bugs onder verschillende browsers. Daarom uitgeschakeld.
AjaxIM.loaded(function() { 
	AjaxIM.client.login(); 
});
*/
