(function($) {
	
	// jQuery fixCheckBoxes 1.1
	$.fn.fixCheckBoxes=function(){return this.each(function(){var a=$(this);var b=$('<input type="hidden" />');b.attr({'id':a.attr('id')+'_hidden','name':a.attr('name')});if(a.attr('checked'))b.val('Ja');else b.val('Nee');a.after(b);a.removeAttr('name');a.click(function(){if(b.val()=='Nee')b.val('Ja');else b.val('Nee')})})};
	
	// jQuery classHandler 1.0
	$.fn.classHandler=function(a,b){var c={'mouseover':'mouseout','mouseenter':'mouseleave','focus':'blur'};var d=$(this);d.bind(a,function(){$(this).addClass(b)});d.bind(c[a],function(){$(this).removeClass(b)});return this};
	
})(jQuery);

function _enhanceTabs() {
	
	var tabs = $('.tabs'),
		id = tabs.attr('id'),
		buttons = tabs.find('h2 a');
		
	if(buttons.length > 1) {
		buttons
			.each(function(i) {
				$(this).data('index', i);
			})
			.click(function() {
				setCookie(id, $(this).data('index'));
			});
		start = getCookie(id) || 0;
	} else {
		start = 0;
	};
	
	tabs.tabs({
		addClass : 'js',
		start : start
	});

};

function _enhanceSlider() {
	
	var slider = $('#uitgelichte-ontwerpen .elements'),
		elements = slider.find('.element');
	
	if(elements.length > 1) {
		var timer = null;
		slider
			.serialScroll({
				items: '.element',
				force : true,
				interval : 5000,
				duration : 1000,
				start : 0,
				step: 1
			})		
			.hover(function() {
				if(timer) {				
					window.clearTimeout(timer);
					timer = null;
				};
				slider.trigger('stop');
			}, function() {
				timer = window.setTimeout(function() {				
					slider.trigger('start');
				}, 1000);
			});
	};

};

$(function() {
		   
	// Set up cycle
	var container = $('#right .news_view'),
		elements = container.children('.uitgelicht');
		
	if(elements.length > 1) {
		
		var navigation = $('<div class="navigation" />');
	
		elements
			.each(function(i) {
				$('<a href="#">&nbsp;</a>')
					.data('index', i)
					.click(function(event) {
						elements.showIndex($(this).data('index'))
						event.preventDefault();
					})
					.appendTo(navigation);
			})
			.cycle({
				speed : 1000,
				interval : 3000,
				keep : true,
				onCycle : function() {
					navigation.children()
						.removeClass('active')
						.eq(this.getCurrentIndex()).addClass('active');
				}
			});
		
		navigation
			.hover(function() {
				elements.pauseCycle();				
			}, function() {
				elements.startCycle();
			})
			.appendTo(container);
		
	};
	
	// Set up design cycle	
	$('.view_ontwerpen').each(function() {
		$(this).find('.section').cycle({
			keep : true
		});
	});

	// Step up scroll
	_enhanceSlider();
	
	// Assign classHandler to form fields
	$('input[type=text], input[type=password], textarea').classHandler('focus', 'focus');
	$('input[type=submit]').classHandler('mouseover', 'hover');
	
	// Tell a friend
	var taf = $('#taf');
	
	$('.taf-button').click(function(event) {
		taf.toggle();
		event.preventDefault();
	});
	
});