// JavaScript Document
$(function(){
	// Clear search box onclick
	$("#q").focus(function () {
		if ($(this).val() === $(this).attr("title")) {
			$(this).val("");
		}
		}).blur(function () {
		if ($(this).val() === "") {
			$(this).val($(this).attr("title"));
		}
	}); 
	
	// Home Page Slide Show
	$("#banner").crossSlide({
	sleep: 2,
	fade: 1
	}, [
  		{ src: '/images/headers/header-1.png' },
  		{ src: '/images/headers/header-2.png' },
  		{ src: '/images/headers/header-3.png' },
  		{ src: '/images/headers/header-4.png' }
	]);
	
});

$(document).ready(function(){
	
	// submit calendar event search form.
	if($('#calendar_search_button').length != 0){
		$('#calendar_search_button').click(function(){
			var category              = $('#calendar_category').val();
			var year                  = $('#calendar_year').val();
			var month                 = $('#calendar_month').val();
			var calendar_search_input = $('#calendar_search_input').val();
			document.location = '/events' + (category ? '/' + urlencode(category) : '') + (year ? '/' + parseInt(year) : '') + (month ? '/' + parseInt(month) : '') + (calendar_search_input ? '/search/' + urlencode(calendar_search_input.replace(/\//g, '')) : '');
		});
	}
	// submit event list search form.
	if($('#events_search_button').length != 0){
		$('#events_search_button').click(function(){
			var category              = $('#events_category').val();
			var year                  = $('#events_year').val();
			var month                 = $('#events_month').val();
			var calendar_search_input = $('#events_search_input').val();
			document.location = '/events' + (category ? '/' + urlencode(category) : '') + (year ? '/' + parseInt(year) : '') + (month ? '/' + parseInt(month) : '') + (calendar_search_input ? '/search/' + urlencode(calendar_search_input.replace(/\//g, '')) : '');
		});
	}
	// return key release - submit event calendar search form.
	$('#calendar_search_input').bind('keyup', function(e){
		switch(e.which){
			case 13:
			{
				if($('#calendar_search_button').length != 0){
					$('#calendar_search_button').click();
				}
				break;
			}
		}
	});
	// return key release - submit event list search form.
	$('#events_search_input').bind('keyup', function(e){
		switch(e.which){
			case 13:
			{
				if($('#events_search_button').length != 0){
					$('#events_search_button').click();
				}
				break;
			}
		}
	});
	// event calendar category change event.
	$('#calendar_category').change(function(){
		var category              = $('#calendar_category').val();
		var year                  = $('#calendar_year').val();
		var month                 = $('#calendar_month').val();
		var calendar_search_input = $('#calendar_search_input').val();
		document.location = '/calendar' + (category ? '/' + urlencode(category) : '') + (year ? '/' + parseInt(year) : '') + (month ? '/' + parseInt(month) : '') + (calendar_search_input ? '/search/' + urlencode(calendar_search_input) : '');
	});
	// event list category change event.
	$('#events_category').change(function(){
		var category              = $('#events_category').val();
		var year                  = $('#events_year').val();
		var month                 = $('#events_month').val();
		var calendar_search_input = $('#events_search_input').val();
		document.location = '/events' + (category ? '/' + urlencode(category) : '') + (year ? '/' + parseInt(year) : '') + (month ? '/' + parseInt(month) : '') + (calendar_search_input ? '/search/' + urlencode(calendar_search_input) : '');
	});

	// datepickers
	$(".date-input").datepicker({dateFormat:"yy-mm-dd"});
	$(".date-button").click(function(){
		$(this).prev('input').focus();
	});

});


// encode a URL!
function urlencode(str){
	var ret = str;
	ret = ret.toString();
	ret = encodeURIComponent(ret);
	ret = ret.replace(/%20/g, '+');
	return ret;
}


// job application

var appCurrentStep = 1;
var appNumberSteps = 6;

function AppInit(pass_required){
	$("#appForm").validate({
		rules: {
			"first_name" : "required",
			"last_name"  : "required",
			"address"    : "required",
			"city"       : "required",
			"state"      : "required",
			"zip_code"   : {
				required  : true,
				minlength : 5
			},
			"job_applicant_types[]" : "required",
			"job_applicant_shifts[]" : "required",
			"work_authorized" : "required",
			previous_employment_date_begin : {
				required : function(element){
					return ($("#previous_employment:checked").val() == "1");
				}
			},
			previous_employment_date_end : {
				required : function(element){
					return ($("#previous_employment:checked").val() == "1");
				}
			},
			current_employer : {
				required : function(element){
					return ($("#currently_employed:checked").val() == "1");
				}
			},
			crime_guilty : "required",
			crime_explanation : {
				required : function(element){
					return ($("#crime_guilty:checked").val() == "1");
				}
			},
			email : {
				required : true,
				email    : true
			},
			password : {
				required  : pass_required,
				minlength : 6
			},
			password2 : {
				required : pass_required,
				equalTo  : "#password"
			},
			certify_read : "required"
		},
		messages: {
			first_name : "Your first name is required.",
			last_name  : "Your last name is required.",
			address    : "Your street address is required.",
			city       : "Your city is required.",
			state      : "Your state is required",
			zip_code   : {
				required  : "Your zip code is required",
				minlength : "Your zip code must be at least 5 digits long"
			},
			current_employer : {
				required : "Please provide your current employer's information."
			},
			crime_explanation : {
				required : "Please explain the crime you were convicted of."
			},
			email : {
				required : "<br />Please provide your email.",
				email    : "<br />Please ensure that your email is in a valid format."
			},
			password  : "<br />Please provide a password that is at least 6 characters in length.",
			password2 : {
				required : "<br />Please confirm your password.",
				equalTo  : "<br />Your password confirmation does not match. Please re-type your password and confirmation."
			},
			certify_read : "Please confirm that you have read the above statement."
		}
	});
}

function AppShowStep(n){
	if(n < appCurrentStep || AppVerifyStep(appCurrentStep)){
		$('.appLink').removeClass('appLinkActive');
		$('#appLink' + n).addClass('appLinkActive');
		$('.appDiv').hide();
		$('#appDiv' + n).show();
		appCurrentStep = n;
	}
	if(appCurrentStep < 2){
		$('#appBtnPrev').hide();
	}else{
		$('#appBtnPrev').show();
	}
	if(appCurrentStep < appNumberSteps){
		$('#appBtnNext').show();
	}else{
		$('#appBtnNext').hide();
	}
}

function AppNextStep(){
	if(appCurrentStep < appNumberSteps){
		if(AppVerifyStep(appCurrentStep)){
			AppScrollTop();
			AppShowStep(appCurrentStep+1);
		}
	}
}

function AppPrevStep(){
	if(appCurrentStep > 1){
		AppScrollTop();
		AppShowStep(appCurrentStep-1);
	}
}

function AppLastStep(){
	if(AppVerifyStep(5)){
		$('#appForm').submit();
	}
}

function AppScrollTop(){
	window.scroll(0, 0);
}

function AppVerifyStep(n){
	var valid = true;
	if(n == 1){
		$.each($("input", "#appDiv1"), function(){
			valid = $("#appForm").validate().element($(this)) == false ? false : valid;
		});
		$.each($("textarea", "#appDiv1"), function(){
			valid = $("#appForm").validate().element($(this)) == false ? false : valid;
		});
		$("input.error:first").focus();
		return valid;
	}else if(n == 2){
		// nothing to validate on this step.
	}else if(n == 3){
		// nothing to validate on this step.
	}else if(n == 4){
		// nothing to validate on this step.
	}else if(n == 5){
		$.each($("input", "#appDiv5"), function(){
			valid = $("#appForm").validate().element($(this)) == false ? false : valid;
		});
		$("input.error:first").focus();
		return valid;
	}
	return true;
}

function SurveyInit(){
	$("#surveyForm").validate({
		rules: {
			"gender" : "required",
			"race"   : "required"
		},
		messages: {
			"gender" : "Please select a gender.",
			"race"   : "Please select a race."
		}
	});
}

function DM_ToggleNode(image, id){
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display == 'none'){
			image.src = '/admin/images/16folderopen.gif';
			$('#' + id).fadeIn('fast');
		}else{
			image.src = '/admin/images/16folder.gif';
			$('#' + id).fadeOut('fast');
		}
	}
}

function DM_OpenAll(){
	$('.dm_class_ul:not([id=dm_node_0])').each(function(){
		$(this).fadeIn('fast').prev('li').find('img').attr('src', '/admin/images/16folderopen.gif');
	});
}

function DM_CloseAll(){
	$('.dm_class_ul:not([id=dm_node_0])').each(function(){
		$(this).fadeOut('fast').prev('li').find('img').attr('src', '/admin/images/16folder.gif');
	});
}
