jQuery(document).ready( function($) {
	var $form = $('#salsa-unsubscribe-form'),
		$fieldsets = $('fieldset', $form),
		$checkboxes = $fieldsets.find(':checkbox'),
		$some = $('#remove-some'),
		$all = $('#remove-forever'),
		clickme = "<div class='clickme'></div>";

	if ($all.length === 0) {
		return;
	}

	$checkboxes.attr('disabled', true); // disable all checkboxes, to reduce clutter

	// bind click handlers (if any)
    $all.click( function(event) {
        if (event.button) return true;

        $checkboxes.attr('checked', 'checked').attr('disabled', true);
    });

    $some.click( function(event) {
        if (event.button) return true;

        $checkboxes.removeAttr('disabled');
    })
    .find(':checkbox').click( function(event) {
        if (event.button) return true;
        
        $checkboxes.removeAttr('disabled');
    });

	$fieldsets.find('div.checkbox')
		.append(clickme)
		.find('div.clickme').each( function(index, elem) {
			var $elem = $(elem);

			$elem.width( $elem.parent().width() )
				.height( $elem.parent().height() );

			$elem.click(checkboxClickHandler);
		});

	function checkboxClickHandler(event) {
        if (event.button) return true;

        if ($some.attr('checked') !== true) {
            $some.attr('checked', true);
            $all.removeAttr('checked');
            $checkboxes.removeAttr('disabled').attr('checked', false);
			$(this).parent().find(':checkbox').attr('checked', true);
        }
		else {
			$(this).parent().find(':checkbox').click();
		}
	}
});

