$(document).ready(function(){
	var hash = window.location.hash;
	$('.register_prompt_text,.for_employers').hide();
	var myDate = new Date();
	var prettyDate = (myDate.getMonth()+1) + '/' + myDate.getDate() + '/' + myDate.getFullYear();
	$('.datepicker').datepicker({minDate: prettyDate});
	$('.datepicker_no_date_block').datepicker();
	$('input[type=text],input[type=password],input[type=select],textarea').css('border','1px solid #aaa').css('padding','4px');
	$('.twitter').load('ajax/ajax.load_twitter.php');
	$('.lisjobs').load('ajax/ajax.load_lisjobs.php');
	$('.hot_jobs').load('ajax/ajax.load_hot_jobs.php');
	$('#profile_tabs').tabs({ fxFade: true, fxSpeed: 'fast' });
	/**
	 *	Forward / back
	 */
	$.address.change(function(event){
		$('#profile_tabs').tabs( 'select' , window.location.hash )
	})
	/**
	 *	Append hash to URL
	 */
	$('#profile_tabs').bind('tabsselect', function(event, ui) { 
		window.location.hash = ui.tab.hash;
	});
	$('.register_prompt_link').click(function() {
		$('.register_prompt_text').slideToggle('slow');
		$('.short_register').parent('div').css('outline','1px solid #ff7800').effect('bounce', { times: 2, distance: 15 }, 500);
		return false;
	});
	if ( $('#register input[name=account_type]:checked').val() == 'employer' ) {
		$('.for_employers').show('slow');
		$('#organization,#city,#street,#location,#zipcode').addClass('required').rules('add', {
			required: true
		});
	}
	$('#register').live('change', function() {
		if ( $('#register input[name=account_type]:checked').val() == 'employer' ) {
			$('.for_employers').show('slow');
			$('#organization,#city,#street,#location,#zipcode').addClass('required').rules('add', {
				required: true
			});
		} else {
			$('.for_employers').hide('slow');
			$('#organization,#city,#street,#location,#zipcode').removeClass('required').rules('remove');	
		}
	});
	$('#register').validate({
	   errorContainer: '.jquery_form_feedback',
	   errorLabelContainer: '.jquery_form_feedback',
	   wrapper: 'p',
	   rules: {
		    email: {
				email: true	
			}
	   }
	})
	$('#login_form').validate({
	   errorContainer: '.jquery_form_feedback',
	   errorLabelContainer: '.jquery_form_feedback',
	   wrapper: 'p'
	})
	$('form input[type=submit]').click(function() {
		$('input[type=submit]', $(this).parents('form')).removeAttr('clicked');
		$(this).attr('clicked', 'true');
	});
	$('#post_job').validate();
	$('#post_job').live('submit', function() {
		var submit_type = $('input[type=submit][clicked=true]').val()
		
		if ( submit_type == 'Post Job' ) {
			$('#job_employer, #job_zip').addClass('required').rules('add', {
				required: true
			});	
			if ( $('#job_date_no_deadline').attr('checked') === false ) {
				var current_date = new Date();
				current_date = Date.parse(current_date);
				var app_deadline = $('#job_date_deadline').val();
				app_deadline = Date.parse(app_deadline);
				if ( app_deadline <= current_date ) {
					$('.application_deadline_text').after('<label class="error" for="job_date_deadline" generated="true">Application deadline must be greater than today</label>');
					window.scroll(0,0);
					return false;
				}
				var zip = $('#job_zip').val();
				if ( zip == 0 ) {
					$('#job_zip').after('<label class="error" for="job_zip" generated="true">Zipcode cannot be zero</label>');
					window.scroll(0,0);
					return false;	
				}
			}
		} else {
			$('#job_employer, #job_zip').removeClass('required').rules('remove');
		}
		
	});
	$('#job_date_deadline').change(function() {
		if ( $(this).val() != '' ) {
			$('#job_date_no_deadline').attr('checked', false);	
		} else {
			$('#job_date_no_deadline').attr('checked', true);
		}
	});
	$('#job_date_no_deadline').change(function() {
		if ( $(this).attr('checked') ) {
			$('#job_date_deadline').val('');
		}
	});
	$('#edit_user').validate({
		rules: {
			edit_user_email: {
				email: true	
			}
		}
	});
	$('#job_ben_none').change(function() {
		if ( $(this).attr('checked') ) {
			$('#job_ben_retire, #job_ben_medical, #job_ben_dental, #job_ben_sick, #job_ben_vacation').attr('checked', false);
		}	
	});
	$('#job_ben_retire, #job_ben_medical, #job_ben_dental, #job_ben_sick, #job_ben_vacation').change(function() {
		if ( $(this).attr('checked') ) {
			$('#job_ben_none').attr('checked', false);
		}	
	});
	/**
	 *	Manage "Type of Library" selections
	 */
	if ( $('input[name="jobseeker_library_type_all"]').attr('checked') ) {
		$('input[name="jobseeker_library_type[]"]').each(function() {
			$(this).attr('checked', true);
			$(this).attr('disabled', true);
		});	
	}
	$('input[name="jobseeker_library_type_all"]').click(function() {
		this_checked = $(this).attr('checked');
		$('input[name="jobseeker_library_type[]"]').each(function() {
			$(this).attr('checked', true);
			if ( this_checked ) {
				$(this).attr('disabled', true);
			} else {
				$(this).removeAttr('disabled');
			}
		});
	});
	$('input[name="jobseeker_library_type[]"]').click(function() {
		this_is_checked = $(this).attr('checked');
		if ( !this_is_checked ) {
			$('input[name="jobseeker_library_type_all"]').removeAttr('checked');
		}
	});
	/**
	 *	Manage "Hours per week" selections
	 */
	if ( $('input[name="jobseeker_hours_per_week_all"]').attr('checked') ) {
		$('input[name="jobseeker_hours_per_week[]"]').each(function() {
			$(this).attr('checked', true);
			$(this).attr('disabled', true);
		});	
	}
	$('input[name="jobseeker_hours_per_week_all"]').click(function() {
		this_checked = $(this).attr('checked');
		$('input[name="jobseeker_hours_per_week[]"]').each(function() {
			$(this).attr('checked', true);
			if ( this_checked ) {
				$(this).attr('disabled', true);
			} else {
				$(this).removeAttr('disabled');
			}
		});
	});
	$('input[name="jobseeker_hours_per_week[]"]').click(function() {
		this_is_checked = $(this).attr('checked');
		if ( !this_is_checked ) {
			$('input[name="jobseeker_hours_per_week_all"]').removeAttr('checked');
		}
	});
	/**
	 *	Manage "Salary" selections
	 */
	if ( $('input[name="jobseeker_salary_range_all"]').attr('checked') ) {
		$('input[name="jobseeker_salary_range[]"]').each(function() {
			$(this).attr('checked', true);
			$(this).attr('disabled', true);
		});	
	}
	$('input[name="jobseeker_salary_range_all"]').click(function() {
		this_checked = $(this).attr('checked');
		$('input[name="jobseeker_salary_range[]"]').each(function() {
			$(this).attr('checked', true);
			if ( this_checked ) {
				$(this).attr('disabled', true);
			} else {
				$(this).removeAttr('disabled');
			}
		});
	});
	$('input[name="jobseeker_salary_range[]"]').click(function() {
		this_is_checked = $(this).attr('checked');
		if ( !this_is_checked ) {
			$('input[name="jobseeker_salary_range_all"]').removeAttr('checked');
		}
	});
});
