$(function() {
	if ($('.further-qualification').length) {
		handleEducation();
	}
	
	if ($('.further-membership').length) {
		handleMembership();
	}
	
	if ($('#declaration-check').length) {
		observeDeclaration();
	}
	
	if ($('#error-messages').length) {
		niceErrorScroll();
	}
});

function handleEducation() {
	$('.further-qualification').hide();
	$('.further-qualification .form-summary p').append(' <a href="#" class="remove-qualification">Remove this qualification</a>');
	$('#qualifications').append('<div id="add-qualification-button"><a href="#">Click to add another qualification</a></div>');
	
	if ($('further-qualification-expanded').length == $('further-qualification').length) {
		$('#add-membership-button').hide();
	}	
	$('.further-qualification').hide();
	$('.further-qualification-expanded').show();
	
	
	$('#add-qualification-button').click(
		function() {
			$('.further-qualification:hidden:first').slideDown('slow');
			if ($('.further-qualification:hidden').length == 0) {
				$('#add-qualification-button').hide();
			}
			return false;
		}
	);
	
	$('.remove-qualification').click(
		function() {
			var removeFields = $(this).parents('.further-qualification').eq(0);
			$(removeFields).slideUp('slow',
				function() {
					$(removeFields).find('input').val('');
				}
			);
			$('#add-qualification-button').show();
			return false;
		}
	);
	
	
	
}

function handleMembership() {
	$('.further-membership .form-summary p').append(' <a href="#" class="remove-membership">Remove</a>');
	$('#organisation-membership').append('<div id="add-membership-button"><a href="#">Click to add another membership</a></div>');
	
	if ($('.further-membership-expanded').length == $('.further-membership').length) {
		$('#add-membership-button').hide();
	}	
	$('.further-membership').hide();
	$('.further-membership-expanded').show();
	
	$('#add-membership-button').click(
		function() {
			$('.further-membership:hidden:first').slideDown('slow');
			if ($('.further-membership:hidden').length == 0) {
				$('#add-membership-button').hide();
			}
			return false;
		}
	);
	
	$('.remove-membership').click(
		function() {
			var removeFields = $(this).parents('.further-membership').eq(0);
			$(removeFields).slideUp('slow',
				function() {
					$(removeFields).find('input').val('');
				}
			);
			$('#add-membership-button').show();
			return false;
		}
	);
}

function observeDeclaration() {
	$('#step1-submit').attr('disabled','disabled');
	$('#step1-submit').addClass('disabled-button');
	checkDeclaration();
	
	$('#declaration-check').click(
		function() {
			checkDeclaration();
		}
	);
	
}

function checkDeclaration() {
	if ($('#declaration-check').attr('checked')) {
		$('#step1-submit').removeAttr('disabled');
		$('#step1-submit').removeClass('disabled-button');
	}
	else {
		$('#step1-submit').attr('disabled', 'disabled');
		$('#step1-submit').addClass('disabled-button');
	}
}

function niceErrorScroll() {
	$('#error-messages a').click(
		function() {
			var linkVal = $(this).attr('href');
			$.scrollTo(linkVal,800);
			$(linkVal).focus();
			return false;
		}
	);
}

/*
function handleProgress() {
	var index = $('#application-progress .selected').getIndex();
	$('#application-progress li:lt('+index+'):last').css('color', '#c00');	
}

$.fn.getIndex = function(){
  var parentSet=$(this).parent().children();
  return $(parentSet).index(this);
}
*/

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

