$(document).ready(function(){
// start DOM

	// Calendar control section
	//-------------------------
	
	// collapse months
	var toggleMinus = 'com/img/toggle_minus.png';
	var togglePlus = 'com/img/toggle_plus.png';
	var $subHead = $('th.month');
	$subHead.prepend('<img src="' + toggleMinus + '" alt="Collapse this section" />');
	$('img', $subHead).addClass('clickable')
	.click(function(){
		var toggleSrc = $(this).attr('src');
		if (toggleSrc == toggleMinus){
			$(this).attr('src', togglePlus)
			.parents('tr').siblings().fadeOut('fast');
		}else{
			$(this).attr('src', toggleMinus)
			.parents('tr').siblings().fadeIn('fast');
		};
	});
	
	// Filter
	$('#caltable').each(function(){
		var $table = $(this);
		$table.find('th').each(function (column){
			if ($(this).is('.filter-column')){
				var $filters = $('<div><b>Filter By ' + $(this).text() + ':</b></div>');
				var keywords = {};
				$table.find('tbody tr td').filter(':nth-child(' + (column) + ')').each(function(){
					keywords[$(this).text()]=$(this).text();
				});
				$('<div>All</div>').click(function(){
					$table.find('tbody tr').show();
					$(this).addClass('active').siblings().removeClass('active');
				}).addClass('clickable active').appendTo($filters);
				$.each(keywords, function(index, keyword){
					$('<div></div>').text(keyword).bind('click', {'keyword':keyword}, function(event){
						$table.find('tbody tr').each(function(){
							if ($('td', this).filter(':nth-child(' + (column) + ')').text()==event.data['keyword']){
								$(this).show();
							}
							else if ($('th',this).length==0){
								$(this).hide();
							}
						});
						$(this).addClass('active').siblings().removeClass('active');
					}).addClass('clickable').appendTo($filters);
				});
				$filters.insertAfter('#caltable');
			};
		});
	});
	
	// display email @ signs
	$('.at').replaceWith('@');
	
	// Add field hint for phones
	phoneHint = '###-###-####';
	$('#phone').addClass('placeholder').val(phoneHint).focus(function(){
		if (this.value== phoneHint){
			$(this).removeClass('placeholder').val('');
		};
	}).blur(function(){
		if (this.value==''){
			$(this).addClass('placeholder').val(phoneHint);
		};
	});
	
	// Add field hint for pobox
	poboxHint = 'po box ###';
	$('#pobox').addClass('placeholder').val(poboxHint).focus(function(){
		if (this.value== poboxHint){
			$(this).removeClass('placeholder').val('');
		};
	}).blur(function(){
		if (this.value==''){
			$(this).addClass('placeholder').val(poboxHint);
		};
	});
	
// end DOM
});