$(function(){
	/*
	 * Form stuff
	 */
	/* Field Clearing */
	$('.input-clear').live('focus', function () {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	}).live('blur', function () {
		if (this.value === '') {
			this.value = this.defaultValue;
		}
	});

	/* Checkboxer */
	var $chk = $('.checkbox-group .checkbox'),
		$chkgroup = $('.checkbox-group'),
		$chklabel = $('.checkbox-group label');
	// open and close
	$chkgroup.height(20);
	$('h3', $chkgroup).click(function(){
		if($(this).hasClass('open')) {
			$(this).parent($chkgroup).height(20);
			$(this).removeClass('open');
		}else{
			$(this).parent($chkgroup).height('auto');
			$(this).addClass('open');
		}
	});

	
	// set any currently checked checkboxes with the checked class (for error returns)
	$('.checkbox:checked', $chkgroup).each(function(){
		$(this).parent($chklabel).addClass('checked');
	});
	// toggle on input change
	$chklabel.live('click', function(){
		if(!$(this).hasClass('checked')){
			$(this).addClass('checked');
			$('.checkbox', $(this)).attr('checked','checked');
			return false;
		}else if($(this).hasClass('checked')){
			$(this).removeClass('checked');
			$('.checkbox', $(this)).removeAttr('checked');
			return false;
		}
	});

	/* UI - slider */
	// staff
	$( "#staff-slider" ).slider({
		range: "min",
		value: 1,
		min: 1,
		step: 1,
		max: 51,
		slide: function(event, ui) {
			if(ui.value !== 51){
				$("#staff_amount").val(ui.value);
			}else{
				$("#staff_amount").val('50+');
			}
		}
	});
	$("#staff_amount").val($("#staff-slider").slider("value"));
	// sites
	$( "#sites-slider" ).slider({
		range: "min",
		value: 0,
		min: 1,
		step: 1,
		max: 11,
		slide: function(event, ui) {
			if(ui.value !== 11){
				$("#sites_amount").val(ui.value);
			}else{
				$("#sites_amount").val('10+');
			}
		}
	});
	$("#sites_amount").val($("#sites-slider").slider("value"));

	/* Validation */
	var $form = $("#waste-audit");
	// custom mehtods
	$.validator.addMethod("fullname_check", function(value, element) {
		return this.optional(element) || $("#fullname").val() != 'Your Name'; 
	}, "Required Field");

	// go validate, baby
	$form.validate({
		rules: {
			fullname: {
				required: true,
				fullname_check: true
			},
			email: {
				required: true,
				email: true
			}
		}
	});
});
