/*
	Zorg voor vrijheid
	******************
	
	Javascript by pasz.nl. Magic by jQuery.
*/

$(document).ready(function() {
	
	// Global (vermijd global variables)
	window.ie6 = ($.browser.msie && parseInt($.browser.version) <= 6);
	window.ie7 = ($.browser.msie && parseInt($.browser.version) == 7);
	
	// Upgrade script voor IE6
	upgrade.init();
	
	/*	Cufon
		*****
		Font replacement	*/
	
	Cufon.replace('#nav a', {hover: true});
	Cufon.replace('div.banner .content h3');
	Cufon.replace('#nieuwsbrief h3');
	Cufon.replace('#sitemap li a.level_0', {hover: true});
	Cufon.replace('#homeLeftCol h2');
	Cufon.replace('#homeRightCol h2');
	Cufon.replace('#leftSide h2');
	Cufon.replace('#leftSide h3');
	Cufon.replace('#rightSide h2');
	Cufon.replace('#rightSide h3');
	Cufon.replace('#ballon p');
	Cufon.replace('#intro h1');
	Cufon.replace('#pageKB #kb_catgs li .descr h3 a', {hover: true});
	
	/*	Focus on load
		*************
		Kan er maar 1 op de pagina zijn!	*/
	
	$('.focusOnLoad').focus();
	
	
	/*	Partnerlogo's
		*************
		De partnerlogo's onderaan de pagina die afwisselen	*/
	
	var logos = function() {
		var $li = $('#logos li'),
			timeOut = 5000,
			transTime = 500,
			max = 6,
			current = 0;
		
		// Eerst li's inline maken zodat ze horizontaal gecentreerd kunnen worden
		$li.css({'display' : 'inline'});
		
		// Workaround voor rare bug in Safari
		if ($.browser.safari) $li.css({'display' : 'inline-block'});
		
		// Als er maar 1 set logo's is, dan stoppen
		if ($li.length <= max) {
			return false;
		}
		
		// Eerst verbergen
		$li.hide()
		
		// Deze functie wordt om de timeOut seconden aangeroepen
		function update() {
			if (current >= $li.length) current = 0;
			$li.fadeOut(transTime);
			
			setTimeout(function() {
				$li.each(function(index) {
					if (index >= current && index < (current + max)) {
						// Fade in
						var displayMode = 'inline-block';
						if (window.ie7) displayMode = 'inline'
						$(this)
							.css({display : displayMode, opacity : 0})
							.animate({opacity : 1}, transTime)
					}
				});
				
				current = current + max;
				setTimeout(function(){logos.update()}, timeOut)
			}, transTime)
		}
		
		// Begin
		update();
		
		// Maak public
		return {
			update: update,
			current: current
		}
	}()
	
	
	
	/*	Tooltips
		********
		De tooltips die bij de vraagtekens in een formulier horen	*/
	
	var tooltips = function() {
		var $tt = $('.i');
		
		function refresh() {
			$tt.each(function() {
				$(this)
					.wrapInner('<span></span>')
					.css('overflow', 'visible')
					.hoverIntent(function() {
						$(this).find('> span').css('display', 'block')
					}, function() {
						$(this).find('> span').css('display', 'none')
					})
			})
		}
		
		refresh();
		
		// Public
		return {
			refresh : refresh
		}
	}()
	
	
	
	/*	Empty inputs
		************
		Inputs met css class 'makeEmpty' krijgen als value
		wat er in de title attribuut staat als de input leeg is.	*/
		
	$(".makeEmpty").emptyfields();
	
	
	/*	Formulieren
		***********/
	
		$('#formError').animate({
			backgroundColor : 'white'
		}, 3000);
		
		if (window.ie7) {
			$('input.text, textarea, #keyword_kennisbank').focus(function() {
				$(this).addClass('focus')
			}).blur(function() {
				$(this).removeClass('focus')
			})
		}
	
	
	/*	Datepicker
		**********
		Maakt gebruik van de datePicker jQuery plugin	*/
	var datePickers = function() {
		// initialise the "Select date" link
		$('.date-pick').each(function() {
			var $options = $(this).parent().find('.y option'),
				startYear = $options.eq(0).val(),
				endYear =  $options.eq($options.length-1).val();
			
			$(this).datePicker({
				// associate the link with a date picker
				createButton:false,
				startDate:'01/01/'+startYear,
				endDate:'31/12/'+endYear
				
			}).bind('click', function() {
				// when the link is clicked display the date picker
				updateSelects($(this));
				$(this).dpDisplay();
				return false;
				
			}).bind('dateSelected', function(e, selectedDate, $td, state) {
				// when a date is selected update the SELECTs
				updateSelects($(this),selectedDate);
				
			}).bind('dpClosed', function(e, selected) {
				updateSelects($(this),selected[0]);
			});
		
			// default the position of the selects to today
			var today = new Date();
			updateSelects($(this), today.getTime());

			// and update the datePicker to reflect it...
			$(this).find('.d').trigger('change');
		})
		
		// listen for when the selects are changed and update the picker
		$('.inputs .d, .inputs .m, .inputs .y').bind('change', function() {
			var $parent = $(this).parent(),
				$btn = $parent.find('.date-pick'),
				d = new Date();
				
			d.setFullYear(
				$parent.find('.y').val(),
				$parent.find('.m').val()-1,
				$parent.find('.d').val()
			);
			
			$btn.dpSetSelected(d.asString());
			updateSelects($btn, d)
			
		});
		
		function updateSelects($btn,selectedDate)
		{
			var selectedDate = new Date(selectedDate);
			$btn.parent().find('.d').attr('value',selectedDate.getDate());
			$btn.parent().find('.m').attr('value',selectedDate.getMonth()+1);
			$btn.parent().find('.y').attr('value',selectedDate.getFullYear());
		}
	}();
});