$(document).ready(function () {
	contact_init_body();
	contact_setup();
	contact_css();
});

function contact_init_body() {
	$('body').removeClass('nojs').addClass('js');
}

function contact_setup() {
	var $cf = $('#contact-form');
	$('#has_js').val('yes');
	$('label', $cf).css('display', 'none');
	$('#cont-cafe-lbl').css('display', 'inline');

	program_height($cf);
	contact_suffix(true);

	$('.phone input', $cf).mask('(999) 999-9999');

	$('input', $cf).each(function () {
		var ta, ce, tv;

		ta = 'title';
		ce = $(this);
		tv = ce.attr(ta);
		ce.data('dv', tv).removeAttr('title');

		if (ce.val() == '' || ce.val() == tv) {
			ce.val(tv).addClass('default');
		}
	});

	$('form', $cf).submit(function (e) {
		e.preventDefault();

		var f = $(this);

		$('input.default', f).each( function () {
			var ce = $(this);
			if (ce.val() == ce.data('dv')) ce.val('');
		});

		if (contact_validate()) {
			$.post(
				'/asp/contact4/contact.inc.asp',
				f.serialize(),
				function (data) {
					f.fadeOut(
						1000,
						function () {
							contact_update(escape(data));
						}
					);
				}
			);
		}
	});

	$('input.default', $cf).focus(function () {
		$(this).val('').removeClass('default');
	});

	$('input.default', $cf).blur(function () {
		var ce, dv;

		ce = $(this);
		dv = ce.data('dv');

		if ('' == ce.val()) {
			ce.val(ce.data('dv'));
			ce.addClass('default');
		}
	});
}

function contact_suffix() {
	var se = $('#cont-suffix');
	var sv = se.val();

	var init = (true == arguments[0]) ? true : false;

	if (init) {
		se.change(contact_suffix);
	}

	$('li.program').each( function () {
		var t = $(this);

		if (this.id == sv) {
			if (init) {
				t.css('display', 'block')
			}
			else {
				t.fadeIn('slow');
			}
		}
		else {
			t.fadeOut('fast');
		}
	});

	if ('' != sv) {
		$("#suffix option.blank").remove();
	}
}

function contact_validate() {
	return true;
}

function contact_update(str) {
	str = unescape(str);
	$('#contact-form').html(str);

	// setup contact form again.
	contact_setup();
}

function contact_css() {
	var cf, ss, h;

	var cf = $('#contact-form');

	if (ss = $('#contentcontainer div.slideshow')) {
		h = ss.height() - (cf.offset().top - ss.offset().top);
		h -= 3; // fudge
//		console.log(ss.height(), cf.offset().top, ss.offset().top, h);

		cf.height(h);
	}
}

function program_height(cont) {
	var c = 0, t = 0, $olp = $('ol.program', cont);

	$('li', $olp).each(function() {
		++c;
		t += $(this).height();
	});

	c  = (c - 1) / c

	$olp.height(t * c);
}

