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);
		var state = $e.text();

		switch (state) {
			case '<<':
				$e.text('>>');
				$('#sidebar-right, #wrapper').removeClass('remove-right');
				break;
			case '>>':
			default:
				$e.text('<<');
				$('#sidebar-right, #wrapper').addClass('remove-right');
				break;
		}

		$(window).resize();
		return false;
	});
});
