/*
	Written by Bborie Park
	Copyright (c) 2008 - 2010 California Vectorborne Disease Surveillance System
	Licensed under the GNU GPL
*/
initFocusBackground = function(context) {
	var filter = [':radio', ':checkbox', ':submit', ':image', ':reset', ':button', '.nofocushighlight'];
	if ($.support.opacity === false) filter.push('select'); // select has issues in IE
	// highlight fields of all input types with specific exceptions with focus
	$(':input', context).not(filter.join(','))
		.focus(function() {
			$(this).addClass('focus');
			$(':input').not(this).removeClass('focus');
		})
		.blur(function() {
			$(this).removeClass('focus');
		});
}

initStrictDTDTarget = function(context) {
	// Strict DTD does not support targets of _blank
	// so substitute one
	$('.targetBlank', context).click(function() {
		var href = $(this).attr('href');
		openWindow(href);
		return false;
	});
}

initReadOnlyInputs = function(context) {
	$(':input[readonly]', context).each(function() {
		// set tooltip
		$.Util.setElementTitle(this, 'Readonly field.', 'prepend', '<br>');
	});
}

initRequiredFields = function(context) {
	$('.required', context).not(':input').each(function() {
		$(this).prepend('<span class="requiredFlag">*</span>');
		
		// set tooltip
		$.Util.setElementTitle(this, 'Required field.', 'prepend', '<br>');
	});

	// mark required fields' labels
	$(':input.required', context).each(function() {
		// set tooltip
		$.Util.setElementTitle(this, 'Required field.', 'prepend', '<br>');

		var $lbl = $("label[for='" + $.Util.id(this) + "']", context);
		if ($lbl.length) {
			$lbl
				.addClass('required')
				.prepend('<span class="requiredFlag">*</span>');
			$.Util.setElementTitle($lbl, 'Required field.', 'prepend', '<br>');
		}
	});
}

startInitByTheme = function(context) {
	context = context || $(document);
	if (!(context instanceof jQuery)) context = jQuery(context);

	initStrictDTDTarget(context);
	initFocusBackground(context);
	initReadOnlyInputs(context);
	initRequiredFields(context);
}

jQuery(document).ready(function() {
	$('#sidebar-right-toggle a').click(function() {
		var $e = $(this).blur(), state = $e.text(), count, hidden;
		hidden = $.Util.getCookie('toggle-right-hidden') || 0;
		count = $.Util.getCookie('toggle-right') * 1;
		count = isNaN(count) ? 0 : count;

		switch (state) {
			case '<<':
				$e.text('>>');
				$('#sidebar-right, #wrapper').removeClass('remove-right');

				if (hidden) count--;
				if (count < -5) {
					if (confirm('You have selected to expand the right sidebar more than five times this session.  Would you like to keep the right sidebar expanded on this browser?')) {
						$.Util.setCookie('toggle-right-hidden', 0, -1, '.' + location.hostname);
					}
					count = 0;
				}
				break;
			case '>>':
			default:
				$e.text('<<');
				$('#sidebar-right, #wrapper').addClass('remove-right');
				if (hidden) return false;

				count++;

				// see about permanently keeping the sidebar-right minimized
				if (count > 2) {
					if (confirm('You have selected to collapse the right sidebar more than twice this session.  Would you like to keep the right sidebar collapsed on this browser?')) {
						$.Util.setCookie('toggle-right-hidden', 1, 60, '.' + location.hostname);
					}

					count = 0;
				}
				break;
		}

		$.Util.setCookie('toggle-right', count, null, '.' + location.hostname);
		return false;
	});

	if ($.Util.getCookie('toggle-right-hidden')) $('#sidebar-right-toggle a').click();
});

