$(document).ready(function() {
	if($("#noticeform").html() == '') {
		$('#buttonbox').hide();
	} else {
		sameAddress();
  	  	usePBS();
  	  	datePicker();
  	  	$("input[type=text],textarea").labelify({labelledClass: "labelHighlight"});
  	  	initHelp();
  	  	// poor old IE gets it wrong, again
		if($.browser.msie) {
			setTimeout(function() {
				$("textarea:first").focus().blur();
			}, 200);
		}
	}
	$('input[name=notice_type]').change(function() {
		$.post("/partywall/loadForm", {form: $(this).val() }, 
            function(data) { 
          	  	$("#noticeform").html(data);
          	  	sameAddress();
          	  	usePBS();
          	  	datePicker();
          	  	$("input[type=text],textarea").labelify({labelledClass: "labelHighlight"});
          	  	initHelp();
          	  	trig_bind();
            });
		$('#buttonbox').show();
		// poor old IE gets it wrong, again
		if($.browser.msie) {
			setTimeout(function() {
				$("textarea:first").focus().blur();
			}, 200);
		}
	});
	trig_bind();	
});

function datePicker() {
	$('#date').datepicker({
		dateFormat: 'dd-mm-yy'
	});
}

function sameAddress() {
	$('#same_address').change(function() {
		if($(this).is(':checked')) {
			$('#postal_address').slideUp('slow', function() {
				$('textarea[name=postal_address]').val($('textarea[name=property_address]').val());
			});
		} else {
			$('#postal_address').slideDown('slow');
		}
	});
	$('#unknown_adjoining_owner_address').change(function() {
		if($(this).is(':checked')) {
			$('#adjoining_owner_address_wrapper').slideUp('slow', function() {
				$('textarea[name=adjoining_owner_address]').val($('textarea[name=adjoining_address]').val());
			});
		} else {
			$('#adjoining_owner_address_wrapper').slideDown('slow');
		}
	});
}

function usePBS() {
	$('#use_pbs').change(function() {
		if($(this).is(':checked')) {
			$('#surveyor').slideUp('slow');
		} else {
			$('#surveyor').slideDown('slow');
		}
	});
}

function initHelp() {
	// create the help links
	$('span.help').html('[&nbsp;<a title="Click to view help for this item" href="javascript:void(0);" class="help">?</a>&nbsp;]');
	// hide the help sections
	$('div.answer').hide();
	// attach behaviours
	$('span.help a').click(function() {
		var target = $(this).parents('div.helpwrapper').nextAll('div.answer').eq(0);
		if(target.css('display') == 'block') {
			target.slideUp('slow');
		} else {
			$('div.answer').hide('fast');
			target.slideToggle('slow');
		}
	});
}

//IE waits until another event to send the 'change' events on radios and checkboxes
//This bind a trigger for those events on click, not accessible but that's not a requirement here :(
function trig_bind() {
	if($.browser.msie) {
		//unbind old events
		$("input[type='checkbox']").unbind('click');
		$("input[type='radio']").unbind('click');
		//bind the events
		$("input[type='checkbox']").bind('click', function() {
			$(this).trigger('change');
		});
		$("input[type='radio']").bind('click', function() {
			$(this).trigger('change');
		});
	}
}