$(function(){

	/**
	  * GUI modifications
	  */
	$('#downloads li:nth-child(4n+1)').addClass('first');
	$('ul.achtredenen li:even').addClass('oneven');
	$('.birthday').datepicker({changeMonth: true, changeYear: true, minDate: '-60y', maxDate: '-17y', dateFormat: 'dd/mm/yy'});
	$('.datepick').datepicker({dateFormat: 'dd/mm/yy'});
	$('.lesson').datepicker({minDate: 0});
	$('.last').datepicker({maxDate: 0});

	/**
	  * Twitter
	  */
	$('.tweet .bottom p').tweet({
		loading_text: 'Loading tweets...',
		template: '{text} &dash; {time}',
		username: 'RS_CARIN',
		count: 1
	});

	/**
	  * Admin > Flashmessages
	  */
	$('.message').hide().append('<span class="close" title="Dismiss"></span>').fadeIn('slow');
	$('.message .close').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); });
	$('.message .close').click(function() { $(this).parent().fadeOut('slow', function() { $(this).remove(); }); });

	/**
	 * Admin > AJAX updating
	 */
	$('.section').change(function() {
		$.get('/admin/index/progress/format/json?pupil_id=' + $('#pupil_id').val() + '&' + $(this)[0].name + '=' + $(this)[0].checked);
	});

	$('.cbr').change(function() {
		$.get('/admin/index/cbr/format/json?pupil_id=' + $('#pupil_id').val() + '&field=' + $(this).attr('name') + '&value=' + $(this).val());
	});

	/**
	 * Admin > Sidebar tabs
	 */
	$('.sidebar_content').hide();

	if (window.location.hash && window.location.hash.match('tab')) {
		$('ul.sidemenu li a[href=' + window.location.hash + ']').parent().addClass('active').show();
		$('.sidebar_content#' + window.location.hash).show();
	} else {
		$('ul.sidemenu li:first-child').addClass('active').show();
		$('.edit .sidebar_content:first').show();
	}

	$('ul.sidemenu li').click(function() {
		var activeTab = $(this).find('a').attr('href');
		window.location.hash = activeTab;

		$(this).parent().find('li').removeClass('active');
		$(this).addClass('active');

		$('.edit').find('.sidebar_content').hide();
		$(activeTab).show();

		return false;
	});

});
