
$(document).ready(function() {

	/* Initialize graphical checkboxes */
	
	$('.checkbox').each(function() {
		if ($('INPUT[type=checkbox]:checked', this).length > 0)
			$(this).addClass('checkbox_checked');
	}).click(checkbox_clicked);


	/* Initialize graphical radiobuttons */

	$('.radiogroup').each(function() {
		// Each group must have one checked radiobutton
		if ($('INPUT[type=radio]:checked', this).length == 0) {
			$('INPUT[type=radio]:first', this).attr('checked', 'checked');
		}
	});

	$('.radiobutton').each(function() {
		if ($('INPUT[type=radio]:checked', this).length > 0)
			$(this).addClass('radio_checked');
	}).click(radiobutton_clicked);


	/* Initialize flash previews */

	$('.flash IMG').addClass('clickable').click(function() { insert_flash(this.parentNode, $(this).width(), $(this).height()) });
	$('.autoplay').each(function() { insert_flash(this) });
	
	/* Initialize text inputs with default value */

	$('INPUT[type=text].default_value').each(function() {
		$(this).attr('default_value', $(this).val());
	}).focus(function() {
		if ($(this).val() == $(this).attr('default_value')) {
			$(this).val('').removeClass('default_value');
		}
	}).blur(function() {
		if ($(this).val() == $(this).attr('default_value') || $(this).val() == '') {
			$(this).val($(this).attr('default_value')).addClass('default_value');
		}
	});
});

function checkbox_clicked() {
	var checkbox = $('INPUT[type=checkbox]', this).get(0);
	checkbox.checked = ! checkbox.checked;
	if (checkbox.checked) {
		$(this).addClass('checkbox_checked')
	} else {
		$(this).removeClass('checkbox_checked');
	}
}

function radiobutton_clicked() {
	var radio = $('INPUT[type=radio]', this).get(0);
	radio.checked = true;
	var siblings = $(this).parents('.radiogroup').find('.radiobutton');
	siblings.removeClass('radio_checked');
	$(this).addClass('radio_checked');
}

function insert_flash(parent, width, height) {
	width = width || $(parent).width();
	height = height || $(parent).height();
	var flash_props = { src: $(parent).attr('title'), width: width, height: height, wmode: 'transparent' };
	//$(parent).empty().flash(flash_props).attr('title', '');
	$(parent).empty().flash(flash_props);
}

function show_shadow() {
	$('.shadow').appendTo('body').
		css('height', $('body').height() + 'px', 'zIndex', '500').show();
}


/* jQuery extensions */

jQuery.fn.shake = function(speed, offset) {
	speed = speed || 100;
	offset = offset || 5;
	var fx = [ offset, offset * -1, 0 ];
	return this.each(function() {
		var orig = parseInt($(this).css('marginLeft')) || 0;
		for (var i = 0; i < fx.length; i++) {
			$(this).animate({ marginLeft: orig + fx[i] + 'px' }, speed, 'swing');
		}
	});
};

