// 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"));
		}
	}); 
	
});

$(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();
	});

	// Show/Hide menus for navigation dropdowns
	/*
	$("#navMain .dir").hoverIntent(showNavigation, hideNavigation);
	function showNavigation() {
		$(this).find("ul:first").slideDown("fast");
	}
	function hideNavigation() {
		$(this).parent().find("ul").slideUp("fast");
	}
	*/
	//flashembed("flash", {src: "flash/home.swf", w3c: true, wmode: "opaque"});
	$('#slider').bxSlider({
		mode: 'horizontal',                 // 'horizontal', 'vertical', 'fade'
		infiniteLoop: true,                 // true, false - display first slide after last
		hideControlOnEnd: true,            // true, false - if true, will hide 'next' control on last slide and 'prev' control on first
		controls: true,                     // true, false - previous and next controls
		speed: 1000,                         // integer - in ms, duration of time slide transitions will occupy
		easing: 'swing',                    // used with jquery.easing.1.3.js - see http://gsgd.co.uk/sandbox/jquery/easing/ for available options
		pager: false,                        // true / false - display a pager
		pagerSelector: null,                // jQuery selector - element to contain the pager. ex: '#pager'
		pagerType: 'full',                  // 'full', 'short' - if 'full' pager displays 1,2,3... if 'short' pager displays 1 / 4
		pagerLocation: 'bottom',            // 'bottom', 'top' - location of pager
		pagerShortSeparator: '/',           // string - ex: 'of' pager would display 1 of 4
		pagerActiveClass: 'pager-active',   // string - classname attached to the active pager link
		nextText: 'next',                   // string - text displayed for 'next' control
		nextImage: '',                      // string - filepath of image used for 'next' control. ex: 'images/next.jpg'
		nextSelector: null,                 // jQuery selector - element to contain the next control. ex: '#next'
		prevText: 'prev',                   // string - text displayed for 'previous' control
		prevImage: '',                      // string - filepath of image used for 'previous' control. ex: 'images/prev.jpg'
		prevSelector: null,                 // jQuery selector - element to contain the previous control. ex: '#next'
		captions: false,                    // true, false - display image captions (reads the image 'title' tag)
		captionsSelector: null,             // jQuery selector - element to contain the captions. ex: '#captions'
		auto: true,                        // true, false - make slideshow change automatically
		autoDirection: 'next',              // 'next', 'prev' - direction in which auto show will traverse
		autoControls: false,                // true, false - show 'start' and 'stop' controls for auto show
		autoControlsSelector: null,         // jQuery selector - element to contain the auto controls. ex: '#auto-controls'
		autoStart: true,                    // true, false - if false show will wait for 'start' control to activate
		autoHover: false,                   // true, false - if true show will pause on mouseover
		autoDelay: 0,                       // integer - in ms, the amount of time before starting the auto show
		pause: 8000,                        // integer - in ms, the duration between each slide transition
		startText: 'start',                 // string - text displayed for 'start' control
		startImage: '',                     // string - filepath of image used for 'start' control. ex: 'images/start.jpg'
		stopText: 'stop',                   // string - text displayed for 'stop' control
		stopImage: '',                      // string - filepath of image used for 'stop' control. ex: 'images/stop.jpg'
		ticker: false,                      // true, false - continuous motion ticker mode (think news ticker)
		                                    // note: autoControls and autoControlsSelector apply to ticker!
		tickerSpeed: 5000,                  // integer - has an inverse effect on speed. therefore, a value of 10000 will 
		                                    // scroll very slowly while a value of 50 will scroll very quickly.
		tickerDirection: 'next',            // 'next', 'prev' - direction in which ticker show will traverse
		tickerHover: false,                 // true, false - if true ticker will pause on mouseover
		wrapperClass: 'bx-wrapper',         // string - classname attached to the slider wraper
		startingSlide: 0,                   // integer - show will start on specified slide. note: slides are zero based!
		displaySlideQty: 1,                 // integer - number of slides to display at once
		moveSlideQty: 1,                    // integer - number of slides to move at once
		randomStart: true                 // true, false - if true show will start on a random slide					  
	});

});


// 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');
	});
}
