/*global $, document, confirm, alert */
$(function() {
	var highlighted = [];
	var fields = $('li.field');
	var inputs = fields.find('input, select, textarea');

	fields.click(function() {
		var current = highlighted.pop();
		if (current != null) {
			current.removeClass('focused');
		}
		var li = $(this);
		highlighted.push(li);
		li.addClass('focused');
	});

	inputs.focus(function() {
		var li = $(this).parents('li.field');
		if (li.length > 0) {
			var current = highlighted.pop();
			if (current != null) {
				current.removeClass('focused');
			}
			highlighted.push(li);
			li.addClass('focused');
		}
	});

	if (inputs.length > 0) {
		try { inputs.get(0).focus(); }
		catch (e) { }
	}

	$(document).keypress(function(e) {
		var buttons = $('input.start,input.next,input.finish,input.submit');
		if (e.target.type == "textarea") {
			return true;
		}
		if (e.keyCode == 13) {
			buttons.trigger('click');
			return false;
		}
	});

	// auto-check the other radio when user focuses the other text box
	$('input.selectquestion.radiobuttonlist.other, input.selectfield.radiobuttonlist.other').keydown(function(e) {
		if (e.keyCode == 9) {
			return;
		}
		
		$(this).closest('div.survey-answer, div.field-value').find('.radiolist input').each(function() {
			$(this).attr("checked", "");
		});
	});

	// clear the other text field when user selects an option that's not other
	$('div.selectquestion .radiolist input, div.radiolist input').click(function() {
		$(this).closest('div.survey-answer, div.field-value').find('input.other:first').val('');
	});

	// auto-check the blank droplist item when user types in the other text box
	$('input.selectquestion.dropdownlist.other, input.selectfield.dropdownlist.other').keydown(function(e) {
		if (e.keyCode == 9) {
			return;
		}
		
		$(this).closest('div.survey-answer, div.field-value').find('select:first').val('');
	});

	// clear the other text field when user selects an option that's not blank
	$('select.selectquestion.dropdownlist, select.selectfield.dropdownlist').change(function() {
		$(this).closest('div.survey-answer, div.field-value').find('input.other:first').val('');
	});

	// can only select one of each item for ranking group questions
	$('.rankinggroup input[type="radio"]').click(function() {
		var clicked = $(this);
		var inputs = $(this).parents('.rankinggroup').find('input[type="radio"]');
		$(inputs).each(function(i, e) {
			if (e.checked && e.value == clicked.val() && e.name != clicked.attr('name')) {
				alert('Please select each option once only per column');
				clicked.attr('checked', 'checked');
				e.checked = false;
			}
		});
	});

	$('.rankinggroup').find('select').change(function() {
		var changed = $(this), inputs = $(this).parents('.rankinggroup').find('select');
		$(inputs).each(function(i, e) {
			var selected = $(e).val();
			if (selected && selected == changed.val() && e.name != changed.attr('name')) {
				alert('Please select each option once only per column');
				e.selectedIndex = 0;
			}
		});
	});

	try {
		if (formItemsConfig) {
			for (var i = 0; i < formItemsConfig.length; i++) {
				if (formItemsConfig[i].Config) {
					var divWrapId = '_' + formItemsConfig[i].ID + '_wrap';
					if ($('#question' + divWrapId).length > 0) { $('#question' + divWrapId).find('input').data('config', formItemsConfig[i].Config); }
					else if ($('#field' + divWrapId).length > 0) { $('#field' + divWrapId).find('input').data('config', formItemsConfig[i].Config); }
				}
			}
		}
	} catch (e) { }

	try {
		var dateConfig = { duration: '', showTime: false, constrainInput: false, dateFormat: 'd M yy', firstDay: 1, changeMonth: true, changeYear: true, yearRange: "1950:2030", hideIfNoPrevNext: true, showOn: 'button', buttonImage: '/Assets/Images/calendar.png', buttonImageOnly: true };
		var dateTimeConfig = { duration: '', constrainInput: false, dateFormat: 'd M yy', firstDay: 1, changeMonth: true, changeYear: true, yearRange: "1950:2030", hideIfNoPrevNext: true, showOn: 'button', buttonImage: '/Assets/Images/calendar.png', buttonImageOnly: true, showTime: true, stepMinutes: 5, stepHours: 1, time24h: true };
		$('input.datetime, input.date').each(function() {
			var input = $(this);
			if (input.hasClass('hasDatepicker')) {
				return;
			}
			var datepickerConfig = input.hasClass('datetime') ? dateTimeConfig : dateConfig;
			input.datepicker(datepickerConfig);
			var config = input.data("config");
			if (config) {
				if (config.minDate) { input.datepicker('option', 'minDate', config.minDate); }
				if (config.maxDate) { input.datepicker('option', 'maxDate', config.maxDate); }
			}
		});
	} catch (e) { }
});