var showLogin = false;

$(document).ready(function() {

	// Login box
	$('#loginBox').click(function(e) {showLogin = true;});
	$('body').click(function() {if($('#loginBox').is(':visible') && showLogin == false) $('a.login').click(); else showLogin = false;});
	$('a.login').click(function() {$('#loginBox').toggle();showLogin = true;});
	if ($('#forgotpassword').length) $('#forgotpassword').fancybox({
		href: '#newpasswordpopup-inner',
		onStart: function() {
			if (!$('#loginemail.loginboxusername').val().length) $('#noemail').show();
			else $('input[name=email][type=hidden]').val($('#loginemail.loginboxusername').val())
		},
		onComplete: function() {
			$('#fancybox-content').find('form').submit(function() {
				var data = {};
				$(this).find(':input').each(function() {
					data[$(this).attr('name')] = $(this).val();
				})
				var form = $(this);
				$.ajax({
					url: $(this).attr('action'),
					type: $(this).attr('method'),
					data: data,
					success: function(response) {
						if (parseInt(response.replace(/\s/g, '')) == 1) form.fadeOut(200, function() {
								form.siblings('#resetmessages').show().find('#resetmessagesuccess').show();
							})
						else form.fadeOut(200, function() {
								form.siblings('#resetmessages').show().find('#resetmessagesuccess').show();
							})
					}
				})
				return false;
			})
			
			$('#fancybox-content').find('#cancelNewPassword').click(function() {
				$.fancybox.close();
			})
		}
	});
	
	// FAQ
	if( $('.question').length && $('.answer').length ) {
		$('.question').click(function(e) {
			e.preventDefault();
			$(this).siblings('.show').removeClass('show');
			$(this).next().addClass('show');
		});
	}

	// Placeholder fallback
	if (!Modernizr.input.placeholder) {
		$('[placeholder]').focus(function(){
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('cPlaceholder');
			}
		}).blur(function(){
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('cPlaceholder');
				input.val(input.attr('placeholder'));
			}
		}).blur();
		$('[placeholder]').parents('form').submit(function(){
			$(this).find('[placeholder]').each(function(){
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
	}
	
	
	if ($('form[name=frmChangePassword]').length) $('form[name=frmChangePassword]').submit(function() {
		if ($(this).find('[name=algemenevoorwaarden]:checked').length) return true;
		else {
			$(this).find('.error').html('Je moet akkoord gaan met de algemene voorwaarden om in te kunnen loggen');
			return false;
		}
	})
	if ($('form[name=frmChangePassword]').length) $('form[name=frmChangePassword]').submit(function() {
		var error = false;
		if ($(this).find('[name=newpassword]').val().length < 5) {
			error = true;
			$(this).find('.error').html('Het wachtwoord moet minimaal 5 tekens zijn');
		} else if ($(this).find('[name=newpassword]').val() != $(this).find('[name=newpasswordrepeat]').val()) {
			error = true;
			$(this).find('.error').html('De wachtwoorden komen niet overeen');
		}
		
		if (!error) return true;
		else return false;
	})
	
});

