$(function() {
	
	$('.front .views-table-actueel caption').addClass('active');
	$('.front .views-table-actueel tbody').show();
	
	$('.section-actueel .views-table-actueel caption').addClass('active');
	$('.section-over-westerlo .views-table-over-westerlo caption').addClass('active');
	$('.section-besturen .views-table-besturen caption').addClass('active');
	$('.section-dienstverlening .views-table-dienstverlening caption').addClass('active');
	$('.section-vrije-tijd .views-table-vrije-tijd caption').addClass('active');
	$('.section-tot-z-gids .views-table-a-tot-z-gids caption').addClass('active');
	$('.section-adressen .views-table-adressen caption').addClass('active');
	
	$('.section-actueel .views-table-actueel tbody').show();
	$('.section-over-westerlo .views-table-over-westerlo tbody').show();
	$('.section-besturen .views-table-besturen tbody').show();
	$('.section-dienstverlening .views-table-dienstverlening tbody').show();
	$('.section-vrije-tijd .views-table-vrije-tijd tbody').show();
	$('.section-tot-z-gids .views-table-a-tot-z-gids tbody').show();
	$('.section-adressen .views-table-adressen tbody').show();
	
	$(".view-page-structure .views-field-link-title a").each(function(i, val) {
		var splitted = $(this).attr('href').split('/');
		$(this).attr('rel', 'address:/' + splitted[splitted.length-1]);
		$(this).attr('href', '#/' + splitted[splitted.length-1]);
	}).address();
	
	var page_setted = false;
	
	$.address.init(function(event) {
    	$(".view-page-structure .views-field-link-title a").each(function(i, val) {
    		if ($(this).attr('rel') == ("address:" + event.value)) {
    			$(".view-page-structure .node").hide().eq(i).show();
    			page_setted = true;
    		}
    	});
    }).change(function(event) {
    	var selection = $('a[rel=address:' + event.value + ']'); 
    	if (selection.text() != "") {
    		$.address.title($.address.title().split('|')[0] + ' | ' + selection.text());
    	}
    	$(".view-page-structure .views-field-link-title a").each(function(i, val) {
    		if ($(this).attr('rel') == ("address:" + event.value)) {
    			$(".view-page-structure .node").hide().eq(i).show();
    		}
    	});
    });
	
	if (page_setted == false) {
		$(".view-page-structure .node").eq(0).show();
	}

	$(".view-agenda-tags .field-content a").each(function(i, val) {
		var splitted = $(this).attr('href').split('/');
		$(this).attr('rel', 'address:/' + splitted[splitted.length-1]);
		$(this).attr('href', '#/' + splitted[splitted.length-1]);
	}).address();
	
	var agenda_setted = false;
	
	$.address.init(function(event) {	
		$(".view-agenda-tags .field-content a").each(function(i, val) {
			if ($(this).attr('rel') == ("address:" + event.value)) {
				$(".agenda-rows").hide().eq(i).show();
				agenda_setted = true;
			}
		});
	}).change(function(event) {
    	var selection = $('a[rel=address:' + event.value + ']'); 
    	if (selection.text() != "") {
    		$.address.title($.address.title().split('|')[0] + ' | ' + selection.text());
    	}
    	$(".view-agenda-tags .field-content a").each(function(i, val) {
    		if ($(this).attr('rel') == ("address:" + event.value)) {
    			$(".agenda-rows").hide().eq(i).show();
    		}
    	});
	});
	
	if (agenda_setted == false) {
		$(".agenda-rows").eq(0).show();
	}
	
	
	$(".view-menu-structure caption").each(function(i, val) {
		$(this).click(function(event) {
			event.preventDefault();
			$(".view-menu-structure caption").removeClass('active');
			$(".view-menu-structure tbody").hide();
			$(this).parent().find('tbody').show();
			$(this).addClass("active");
		});
	});
});