

var custom_carrousel = function (config)
{
	var id = config.id;
	var el = $('#' + id);
	var size = config.size;
	var pause_length = config.timer; //aantal seconden wachten per slide * 1000
	var page_list, thumb_list, timer;
	var page_current = 0;
	var after_swap = (config.after_swap)?config.after_swap:false;

	var busy = false;

	var duration = config.duration;
	function init()
	{
		page_list = $('#' + id + ' div.carrousel-item');
		thumb_list = $('#' + id + ' div.carrousel-thumb-item');

		el.bind('mouseenter', mouseenter);

		el.bind('mouseleave', mouseleave);

		//alert($('#' + id + ' #nav-left').length);
		$('#' + id + ' .nav-left').click(prev);
		$('#' + id + ' .nav-right').click(next);
		thumb_list.click(thumb_click);

		if (pause_length>0) timer = setInterval(next, pause_length);
	}

	function thumb_click(e)
	{
		if (busy) return false;busy = true;

		var el = $(e.currentTarget);
		if (!el.hasClass('active'))
		{
			var page_id = el[0].id.replace('-thumb-', '-page-');
			for (var i=0; page_list.length>i; i++)
			{
				if (page_list[i].id == page_id)
				{
					if (i<page_current)
					{
						$(page_list[page_current]).animate({left: size}, {duration: duration});
						page_current = i;
						$(page_list[page_current]).css('left', -size);
						$(page_list[page_current]).animate({left: 0}, {duration: duration, complete: state_ready});
					}
					else
					{
						$(page_list[page_current]).animate({left: -size}, {duration: duration});
						page_current = i;
						$(page_list[page_current]).css('left', size);
						$(page_list[page_current]).animate({left: 0}, {duration: duration, complete: state_ready});
					}
					set_thumb();
				}
			}
			el.addClass('active');
		}

		state_ready();
	}

	function mouseenter()
	{
		clearInterval(timer);
	}

	function mouseleave()
	{
		if (pause_length>0) timer = setInterval(next, pause_length);
	}

	function prev()
	{
		if (busy) return false;busy = true;

		$(page_list[page_current]).animate({left: size}, {duration: duration});
		page_current--;

		if (page_current < 0) page_current=page_list.length-1;

		$(page_list[page_current]).css('left', -size);
		$(page_list[page_current]).animate({left: 0}, {duration: duration, complete: state_ready});

		set_thumb();
	}

	function next()
	{
		if (busy) return false;busy = true;

		$(page_list[page_current]).animate({left: -size}, {duration: duration});
		page_current++;
		if (page_current >= page_list.length) page_current=0;
		$(page_list[page_current]).css('left', size);
		$(page_list[page_current]).animate({left: 0}, {duration: duration, complete: state_ready});

		set_thumb();
	}

	function state_ready()
	{
		busy = false;
	}

	function set_thumb()
	{
		//highlight a matching thumb
		thumb_list.removeClass('active');

		//var thumb_id = page_list[page_current].id.replace('-page-', '-thumb-');

		//if (thumb_id!="") $('#' + id + ' div#' + thumb_id).addClass('active');

		if (after_swap)
			setTimeout(function(){after_swap(page_list[page_current])}, duration);
	}

	init();
	$('div.faq-item').click(function(){$(this).find('div.answer').toggle();});

	//catch event for manager foto clicks
	$('div.photo-item').click(function(){
		var el = $(this);

		if ( el.hasClass('open'))
		{
			el.removeClass('open');
		}
		else
		{
			el.addClass('open');
		}
	});


	//preopen if needed
	if (document.location.hash)
	{
		$('div.photo-item').each(function(){
			var name = $.trim($(this).find("h2")[0].firstChild.data).toLowerCase().replace(/ /g, "_");
			if ('#' + name == document.location.hash)
			{
				$(this).addClass('open');
			}
		});
	}

	//focus in form
	$('#aanmeld_form :input[type=text]').focus(function(){
		$(this).removeClass('invalid');
	});

};

//signup form
function show_signup()
{
	$('#block-aanmelden').fadeIn('normal', function()
	{
		$('#block-aanmelden').removeClass('block-aanmelden-tiny');

		//attach close click event
		$(document).click(function(event){

			var el = $(event.target);
			if (el.parents('#block-aanmelden').length==0)
			{
				$('#block-aanmelden').fadeOut();
				$('#block-aanmelden').addClass('block-aanmelden-tiny');
				$(document).off('click');
			}
		});
	});

	return false;
}

function handle_submit()
{
	$('#aanmeld_form .invalid').removeClass('invalid');

	var fields = $('#aanmeld_form :input[type=text]').not('#aanmeld_form :input[name=email]');

	for (var i=0;i<fields.length;i++)
		if (fields[i].value.length==0)
			fields[i].className += " invalid";

	if ($('#aanmeld_form .invalid').length>0)
	{
		$('#aanmeld_form img').replaceWith('<p style="color:#DC3B3B">Niet alle verplichte velden zijn ingevuld</p>');
		return false;
	}

	return true;
}

function special_swap(el){
	var link  = $('#account_mgr_link');
	var name = $(el).find('p');

	if ( typeof link[0] !== 'undefined' && link[0].href )
	{
		link[0].href = link[0].href.replace(/\#.*/, "")
			+ "#"
			+ $.trim(name[0].firstChild.data).toLowerCase().replace(/ /g, "_");
	}
}

function toggle_radio_text()
{
	if (this.value.toLowerCase() == "ja")
	{
		$(this.parentNode).find("div").removeClass('hidden');
	}
	else
	{
		$(this.parentNode).find("div").addClass('hidden');
	}
}

$(document).ready(function() {

	var list = $('#block-photo div.carrousel-item');
	if (list.length>0) special_swap(list[0]);

	new custom_carrousel({id: 'carrousel', size: 950, timer: 8000, duration: 1500});


	if ($('#block-photo').length>0)
		new custom_carrousel({id: 'block-photo', size: 370, timer: -1, duration: 1000, after_swap: special_swap});


	// Ja/Nee radio knoppen met text veld
	$('.radio-with-text input[type=radio]').click(toggle_radio_text);
	$('.radio-with-text input[type=radio]:checked').each(toggle_radio_text);

	$('#register-worker-form textarea').bind('blur', function(){
		$(this).toggleClass('expand');
	});

	$('#register-worker-form textarea').bind('focus', function(){
		$(this).toggleClass('expand');
	});

});


