$(document).ready(
		function() {

			$.datepicker.regional['nl'] = {
				closeText : 'Sluiten',
				prevText : '←',
				nextText : '→',
				currentText : 'Vandaag',
				monthNames : [ 'januari', 'februari', 'maart', 'april', 'mei',
						'juni', 'juli', 'augustus', 'september', 'oktober',
						'november', 'december' ],
				monthNamesShort : [ 'jan', 'feb', 'maa', 'apr', 'mei', 'jun',
						'jul', 'aug', 'sep', 'okt', 'nov', 'dec' ],
				dayNames : [ 'zondag', 'maandag', 'dinsdag', 'woensdag',
						'donderdag', 'vrijdag', 'zaterdag' ],
				dayNamesShort : [ 'zon', 'maa', 'din', 'woe', 'don', 'vri',
						'zat' ],
				dayNamesMin : [ 'zo', 'ma', 'di', 'wo', 'do', 'vr', 'za' ],
				weekHeader : 'Wk',
				dateFormat : 'dd-mm-yy',
				firstDay : 1,
				isRTL : false,
				showMonthAfterYear : false,
				yearSuffix : ''
			};
			$.datepicker.setDefaults($.datepicker.regional['nl']);

			$('div#modal').dialog({
				autoOpen : false,
				modal : true,
				width : 700,
				height : 400,
				resizable : false,
				closable : true
			});

			// Datepicker
			$('.datepicker').datepicker({
				minDate : new Date(),
				beforeShowDay : noSundays
			});
			
			// Model filter pulldown
			loadModels();
		});

$(function() {

	$('div.newsblock').click(function() {
		$(this).find('a[href]').eq(0).each(function() {
			document.location.href = $(this).get(0).href;
		});
		return false;
	});

	$('a.new_window').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	$('ul li').click(function(){
		if($(this).children('a').length > 0){
			if(!$(this).children('a').hasClass('new_window')){
				window.location.href = $(this).children('a').attr('href');
			} else {
				window.open($(this).children('a').attr('href'));
				return false;
			}
		}
	});

	$('ul li').each(function(){
		if($(this).children('a').length > 0){
			if($(this).children('a').text() == 'Saab'){
				$(this).children('a').addClass('new_window');
			}
		}
	});
	
	
	
	// Buttons
	$('.button').wrapInner('<span>');
	
	// Submit buttons
	$('a.submit').click(function() {
		$(this).closest('form').submit();
	});
	
	$('div.filter ul.filter_options input').click(function() {
		searchAndFilter();
	});

	$('.autoclear').focusin(
			function() {
				var defaultval = $(
						'input[name=' + $(this).attr('name') + '_default]')
						.val();
				if ($(this).val() == defaultval) {
					$(this).val('');
				}
			});
	$('.autoclear').focusout(
			function() {
				var defaultval = $(
						'input[name=' + $(this).attr('name') + '_default]')
						.val();
				if ($(this).val() == '') {
					$(this).val(defaultval);
				}
			});


	$('div#menu ul > li').hover(function() {
		$(this).addClass('active');
	},function(){
		$(this).removeClass('active');
	});
	

});

function deselectFilter(filtername) {
	$('input.' + filtername).each(function() {
		$(this).attr('checked', false);
	});
}

function urlencode(str) {
	return escape(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g,
			'%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g,
			'+');
}

function toggleActionPanel(action) {
	var show = false;
	if ($('div#action_' + action + '_content').css('display') == 'none') {
		show = true;
	}
	$('div.details_menu_content').css('display', 'none');
	$('div.details_menu').removeClass('active');
	if (show) {
		$('div#action_' + action + '_content').css('display', 'block');
		$('div#action_' + action).addClass('active');
	}
}

function openModal(title, width, height, url) {
	$('div#modal_content').html('');
	$('div#modal').dialog('option', 'title', title);
	$('div#modal').dialog('option', 'width', width);
	$('div#modal').dialog('option', 'height', height);
	$('div#modal_content').load(
			url,
			function(response, status, xhr) {
				if (status == 'error') {
					$('div#modal_content').html(
							'Er is iets mis gegaan. Probeer het nogmaals.');
				}
			});
	$('div#modal').dialog('open');
}

/** Method for datepicker to exclude sundays * */
function noSundays(date) {
	if (date.getDay() == 0)
		return [ false ];
	return [ true ];
}
