$( function() {
	$("#nav li").hover( function() {
		if ($(this).children("ul:hidden").length > 0) {
			$(this).addClass("active").children("ul").slideDown("fast");
		}
	}, function() {
		if ($(this).children("ul:visible").length > 0) {
			$(this).children("ul").fadeOut("fast", function() {
			}).parent("li").removeClass("active");
		}
	});


	$("#nav ul ul").each(function(){
		$(this).parent().addClass('has_sub_nav');
		$('<span class="top-border"></span>').insertBefore(this);
	});

	$("ul#hero li").hide();
	var active_index = $("ul#hero-nav li").index($("ul#hero-nav li.active"));
	$("ul#hero li").eq(active_index).show();

	function set_hero(index)
	{
		if (index != active_index) {
			$("ul#hero-nav li").removeClass("active").eq(index).addClass("active");
			$("ul#hero li").eq(active_index).fadeOut("slow");
			$("ul#hero li").eq(index).fadeIn("slow");
			active_index = index;
		}
	}

	var timer = setInterval( function() {
		var index = active_index + 1;

		if (index > $("ul#hero-nav li").length-1) index = 0;

		set_hero(index);
	}, 5000);

	$("ul#hero-nav li").click( function() {
		clearInterval(timer);
		var index = $("ul#hero-nav li").index(this);

		set_hero(index);

		return false;
	});

/*	$("ul li.first fieldset ul li select").val(1);
	$("ul li.first fieldset ul li select").change( function() {
		var applicants = $("ul li.first fieldset ul li select").val();
		if (applicants < 1) applicants = 1;
		$("ul li.applicants").hide();
		for (var i = 0; i < applicants; i++) $("ul li.applicants").eq(i).show();
	});
	*/

	$('.other-country-of-primary-interest, .country-of-primary-interest').hide();
	$('.primary-interest select').change(function(){
		if( this.value == 'overseas-mortgages' )
			$('.country-of-primary-interest').show();
		else {
			$('.other-country-of-primary-interest, .country-of-primary-interest').hide();
			$('.country-of-primary-interest select').val('');
		}
	});


	$('.country-of-primary-interest select').change(function(){
		if( this.value == 'other' )
			$('.other-country-of-primary-interest').show();
		else
			$('.other-country-of-primary-interest').hide();
	})


	$("ul li.has-child-fields").each( function() {
		var elements = $(this).children("ul.child-fields").children("li");

		elements.hide();
		if ( ! elements.hasClass("empty")) {
			elements.eq(0).show();
		}
	});


	$("ul li.has-child-fields select").change( function() {
		var index = this.selectedIndex;
		var elements = $(this).next("ul.child-fields").children("li");

		elements.hide();
		if ( ! elements.eq(index).hasClass("empty")) {
			elements.eq(index).show();
		}
	});
});
