// JavaScript Document


$(document).ready(function() {

// PNG FIX

	$("FORM").submit(function() {
		if ($("#disclaimer-check").length > 0) {
			if (!$("#disclaimer-check").attr('checked')) {
				alert('Je hebt de verklaring van bescherming persoonsgegevens nog niet geaccepteerd');			
				return false;	
			}
		}
	});

	$.pngfix('/style/laesienet/images/common/pixel.gif');
	$('img[@src$=.png], #header DIV, .almanak-contact-image-frame, .profile-avatar-frame').pngfix();


// Menu rollover

	$(".menu-item").hover(function() {
		if (!$(this).parent().hasClass('active')) {
			var tmp = $(this).attr('src');
			tmp = tmp.replace('normal','active');
			$(this).attr('src', tmp);
		}
	},
	function() {
		if (!$(this).parent().hasClass('active')) {
			var tmp = $(this).attr('src');
			tmp = tmp.replace('active','normal');
			$(this).attr('src', tmp);
		}
	});	
	
	
// Accordion starten
	
	/*
	$("#accordion").accordion({ 
    	header: 'span', 
		autoheight:false
	});
	*/
	$("#accordion > DIV").css('background','url(/style/laesienet/images/backgrounds/column-small-content-gray.png) left bottom');
	$("#accordion > DIV:last").css('background','url(/style/laesienet/images/backgrounds/back-accordion.png) left bottom no-repeat');
	$("#accordion > SPAN:first").css('background','url(/style/laesienet/images/backgrounds/column-small-header-green.png) no-repeat');
	
	

// Uitloggen

	$(".logout").click(function() {
		if (confirm('Weet je zeker dat je uit wilt loggen?')) {
			return true;
		}
		return false;
	});
	
// Iemand aanklikken uit contactenlijst 

	$(".almanak-contact-image-frame").click(function() {
		location = $(this).prev().attr('href');
	});
	
	$(".profilePhoto").click(function() {
		$("#profileContent").html('<img src="'+$(this).attr('href')+'" />');
		return false;
	});
		
		
//	Maak een geboortedatum

	$(".birthDateSelect").change(function() {
		var dDay = $("#birthDay OPTION:selected").eq(0).val();
		var dMonth = $("#birthMonth OPTION:selected").eq(0).val();
		var dYear = $("#birthYear OPTION:selected").eq(0).val();
		
		$("#birthDate").val(dDay+'-'+dMonth+'-'+dYear);
	});
	
	
//	Verwijder een foto

	$(".delete-photo").click(function() {
		if (confirm('Wil je deze foto verwijderen?')) {
			return true;	
		}
		return false;
	});
	
	
//	Reageren op nieuws

	$("#trigger-news-reaction").click(function() {
		if ($("#reactions").css('display') == 'none') {
			$("#reactions").show();
			$("#react").hide();
			$(this).text('Reageer');
		}
		else {
			$("#reactions").hide();
			$("#react").show();
			$(this).text('Toon reacties');
		}
		return false;
	});
			
			

	
// Iemand toevoegen aan contactenlijst

	$("#addToContacts").click(function() {
		if (confirm('Wil je '+$(this).attr('rel')+' toevoegen aan je contactenlijst?')) {
			var response = $.ajax({url: $(this).attr('href') , async: false}).responseText;
			if (response == 'true') {
				alert($(this).attr('rel')+' ontvangt een uitnodiging om zich toe te voegen aan je contactlijst');
				$(this).parent().remove();
			}
			else {
				alert($(this).attr('rel')+' kan niet worden toegevoegd aan je contactenlijst');
			}
		}
		
		return false;
	});
	
// Klik op een video

	$(".accordionLink").click(function() {
		location = $(this).attr('rel');
		return false;
	});
	
// avatar wijzigen

	$("#edit-profile-avatar").click(function() {
		if ($("#edit-profile-avatar-change").css('display') == "none") {
			$("#edit-profile-avatar-image").hide();
			$("#edit-profile-avatar-change").show();
			$(this).text('Annuleren');
		}
		else {
			$("#edit-profile-avatar-change").hide();
			$("#edit-profile-avatar-image").show();
			$(this).text('Wijzig foto');
		}
		
		return false;
	});
	
	
// Zoeken

	$("#basicSearch").submit(function() {
		location = '/almanak/search/'+$("#basicSearchInput").val();
		return false;
	});
	
	
// Forum topic toevoegen

	$("#topic-preview-trigger").click(function() {
		if ($("#add-topic-subject").val() > '' && $("#add-topic-content").val() > '') {
			
			$("#topic-preview-content").html($("#add-topic-content").val().replace(/([^>])\n/g, '$1<br />\n'));
			$("#topic-preview-subject").html($("#add-topic-subject").val());
			$("#topic-preview").show();
		}
		else {
			alert('Vul een onderwerp en bericht in.');
		}
		return false;
	});
	
// Forum reageren

	
	$("#reaction-preview-trigger").click(function() {
		if ($("#add-reaction-content").val() > '') {
			
			$("#reaction-preview-content").html($("#add-reaction-content").val().replace(/([^>])\n/g, '$1<br />\n'));
			$("#reaction-preview").show();
		}
		else {
			alert('Vul eerst een bericht in.');
		}
		return false;
	});
	
// Forum topic wijzigen

	$("#topic-change").change(function() {
		location = $("OPTION:selected",this).eq(0).val();
	});
	
// Relaties

	$("#accept-relation").click(function() {
		if (confirm('Wil je de uitnodiging accepteren?')) {
			return true;
		}
		return false;
	});
	
	$("#reject-relation").click(function() {
		if (confirm('Wil je de uitnodiging weigeren?')) {
			return true;
		}
		return false;
	});
	
	$("#delete-relation").click(function() {
		if (confirm('Wil je de contactpersoon verwijderen uit je contactenlijst?')) {
			return true;
		}
		return false;
	});
	
	
	
// Forum / topic mouseover

	$(".forum-topic-table TR").hover(function() {
		var tmp = $("IMG",this).eq(0).attr('src');
		$("IMG",this).attr('src', tmp.replace('normal','active'));
	},
	function() {
		var tmp = $("IMG",this).eq(0).attr('src');
		$("IMG",this).attr('src', tmp.replace('active','normal'));
	});
	
	
	$('form').submit(function(){					
		var txt = '';


		// standaard form check
		$(this).find('input, textarea').each(function(){
			if( $(this).attr('title') && $(this).val().length == 0 ){
				txt += ' - ' + $(this).attr('title') + '\n';
			}
		});
		
		
		if(txt && txt.length > 0){
			var header = '___________________________________________________________\n\n De volgende velden zijn verplicht: \n\n';
			var footer = '___________________________________________________________\n\n ';
			alert(header + txt + footer);
			return false;
		}
	});
	
	
//	Bericht verwijderen

	$(".delete-message").click(function() {
		if (confirm('Weet je zeker dat je dit bericht wilt verwijderen?')) {
			return true;
		}
		return false;
	});
	
	
//	Contacten filteren

/*
	$("#filterRevalidationCentre, #filterGender, #filterBirthDate").change(function() {
		showContacts();
	});

	
	$("#filterCity, #filterName").blur(function() {
		showContacts();
	});

*/

	$("#searchForumInput").focus(function() {
		if ($(this).val() == 'Zoekwoord') {
			$(this).val('');	
		}
	});
	
	$("#searchForumInput").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Zoekwoord');	
		}
	});
	
										  
	
	$("#filterCity").focus(function() {
		if ($(this).val() == 'Zoekwoord') {
			$(this).val('');	
		}
	});
	
	$("#filterCity").blur(function() {
		if ($(this).val() == '') {
			$(this).val('Zoekwoord');	
		}
	});
	

	$("#filterForm").submit(function() {
		showContacts();
		return false;								 
	});
	
	
	$("#navigate-previous").click(function() {
		$("#filterStart").val($("#filterPrevious").val());
		showContacts();
		return false;
	});
	
	$("#navigate-next").click(function() {
		$("#filterStart").val($("#filterNext").val());
		showContacts();
		return false;
	});
	
	
	$(".navigate-page").click(function() {
		$("#filterStart").val($(this).attr('href'));
		showContacts();
		return false;
	});
	

});


function showContacts() {
	var d = {
			revalidationCentre : $("#filterRevalidationCentre OPTION:selected").eq(0).val(),
			group : $("#filterGroup OPTION:selected").eq(0).val(),
			birthPeriod: $("#filterBirthDate OPTION:selected").eq(0).val(),
			keyword: $("#filterCity").val(),			
			gender: $("#filterGender OPTION:selected").eq(0).val()
	};
	
	var start = $("#filterStart").val();
	var url = "/almanak/ajaxViewContacts/"+start;
	
	$.ajax({
  		url: url,
  		cache: false,
		async: false,
		success: function(data) {			
			$("#almanak-contacts").empty();
			
			
			$("#filterStart").val(data.navigation.start);
			$("#filterNext").val(data.navigation.next);
			$("#filterPrevious").val(data.navigation.previous);
			
			$("#navigate-page").html(data.navigation.currentPage);
			$("#navigate-pages").html(data.navigation.pages);

			if (!data.navigation.previous) {
				$("#navigate-previous").hide();
			}
			else {
				$("#navigate-previous").show();
			}
					
			if (!data.navigation.next) {
				$("#navigate-next").hide();
			}
			else {
				$("#navigate-next").show();
			}			
			
			$.each(data.records, function(i,item) {
				fillContact(item);
			});
			
			$('.almanak-contact-image-frame').pngfix();
		},
		data: d,
		dataType: "json",
		type: "POST"
	});
	
}

// Vul een contact

function fillContact(obj) {
	var avatar = (obj.avatar > 0)?obj.avatar:29;
	var name = (obj.nickName > '')?obj.nickName:obj.name;
	
	var html = '<div class="almanak-contact"><div class="almanak-contact-image"><div class="container">';
	html += '<a href="/almanak/profile/'+obj.id+'" title="'+name+'">';
	html += '<img src="/docs/thumb/'+avatar+'/98/80" alt="'+name+'" />';
	html += '</a><div class="almanak-contact-image-frame" style="cursor:pointer;"></div>';
	html += '</div></div>';
	
	html += '<div class="almanak-contact-name"><a href="/almanak/profile/'+obj.id+'" title="'+name+'">'+truncate(name, 15)+'</a></div></div>';
	
	$(html).appendTo('#almanak-contacts');
}


function truncate(trunc, len) {
  	if (trunc.length > len) {
   		trunc = trunc.substring(0, len);
    	trunc = trunc.replace(/\w+$/, '');	
		trunc = trunc+'...';
   }
   return trunc;
}