﻿
$(document).ready(function() {

	//This function makes the divs on the accordions clickable and changes their background colour immediately.
	$('.clickWrapper').live('click', makeClickable);

	function makeClickable(e) {

		//Check this isn't the controls panel
		$ppc = $(this).find('.pricePanelControls')[0];
		$targ = $(e.target)[0];

		if (e.target.className != 'pricePanelControls' && !jQuery.contains($ppc, $targ)) {


			//This takes care of both the radio buttons and checkboxes
			$check = $(this).find('input:checkbox');
			$radio = $(this).find('input:radio');

			if ($check.length > 0) {
				$check.attr('checked', !$check.attr('checked'));
				$check.triggerHandler('click');

				//change the control's css class to show it's been selected/deselected
				if ($(this).find('.pricePanel').length > 0) {
					$ctrl = $(this).find('.pricePanel');
					$ctrl.addClass('pricePanelActive');
					$ctrl.removeClass('pricePanel');
				}
				else {
					$ctrl = $(this).find('.pricePanelActive');
					$ctrl.addClass('pricePanel');
					$ctrl.removeClass('pricePanelActive');
				}
			}

			//This changes the backgrounds of all radiobutton-containing repeater items so that only one appears selected at a time.
			if ($radio.length > 0) {
				if (!$radio.attr('checked')) {
					$radio.attr('checked', true);
					$radio.triggerHandler('click');
					$ctrl2 = $(this).parent().find('.pricePanelActive');
					$ctrl2.addClass('pricePanel');
					$ctrl2.removeClass('pricePanelActive');

					$ctrl = $(this).find('.pricePanel');
					$ctrl.addClass('pricePanelActive');
					$ctrl.removeClass('pricePanel');



				}
			}
		}
	}

	$('.checkbox').find("input:checkbox").live().change();
});
