var chkImgFalse = '/static/img/search/checkbox.gif';
var chkImgFalseDisabled = '/static/img/search/checkbox_disabled.gif';
var chkImgTrue = '/static/img/search/checkbox_checked.gif';
var chkImgTrueDisabled = '/static/img/search/checkbox_disabled.gif';

function replaceChecks(container) {
	var getCheckImg = function($chkBox) {
		return ($chkBox.attr('checked')?
			($chkBox.attr('disabled')?chkImgTrueDisabled:chkImgTrue):
			($chkBox.attr('disabled')?chkImgFalseDisabled:chkImgFalse));
	}

	$("input:checkbox",$(container)).each( function(i) {
		var	$chkBox = $(this);

		/* replace checkboxes with images */
		$chkBox.before("<img src='"+getCheckImg($chkBox)+"'/>").hide();

		/* bind click event to all image cheks that are enabled */
		$chkBox.prev().click( function() {
			var $img = $(this), $checkbox = $img.next();

			if (!$checkbox.attr('disabled')) { 

				if($checkbox.attr('checked')) {
					$img.attr('src',chkImgFalse);
					$checkbox.removeAttr('checked');

				} else {
					$img.attr('src',chkImgTrue);
					$checkbox.attr('checked','checked');
				};
			};
		});

		/* bind checkbox change event back to images */
		$chkBox.change( function() {
			var $checkbox = $(this), $img = $checkbox.prev();

			if (!$checkbox.attr('disabled')) { 

				if($checkbox.attr('checked')) {
					chkImg = chkImgTrue;

				} else {
					chkImg = chkImgFalse;
				};

				$img.attr('src',chkImg);
			};
		});

		/* allow to enable-disable */
		$chkBox.bind('disable', function(e) {
			var $checkbox = $(this), $img = $checkbox.prev();
			$checkbox.attr('disabled','disabled');
			$img.attr('src',getCheckImg($checkbox));
		});	

		$chkBox.bind('enable', function(e) {
			var $checkbox = $(this), $img = $checkbox.prev();
			$checkbox.removeAttr('disabled');
			$img.attr('src',getCheckImg($checkbox));
		});	
	});
}

/* If changing the value of a checkbox using javascript, call this function instead of setting it directly 
	-- ensures that the change event is triggered (it won't be otherwise) */
function setCheckbox(checkbox, value) {
	if(checkbox) {
		checkbox.checked = value;
		$(checkbox).trigger("change");
	}
}

var radioImgFalse = '/static/img/search/ss_radio.gif';
var radioImgFalseDisabled = '/static/img/search/ss_radio_disabled.gif';
var radioImgTrue = '/static/img/search/ss_radio_checked.gif';
var radioImgTrueDisabled = '/static/img/search/ss_radio_disabled.gif';

function replaceRadios(container) {

	$("input:radio",$(container)).each( function(i) {
		var radioImg = $(this).attr("checked")?
			($(this).attr("disabled")?radioImgTrueDisabled:radioImgTrue):
				($(this).attr("disabled")?radioImgFalseDisabled:radioImgFalse);

		/* replace radios with images */
		$(this).before("<img src='"+radioImg+"'/>").hide();

		/* bind click event to all image radios that are enabled */
		if (!$(this).attr("disabled")) { 

			$(this).prev().click( function() {

				/* if unchecked, toggle; otherwise do nothing */
				if(!$(this).next().attr("checked")) {
					/* 'uncheck' all other radios */ 
					$("input[name='"+$(this).next().attr('name')+"']",

						$(this).closest("form")).each( function(){

							if (!$(this).attr("disabled")) 
								$(this).prev().attr("src",radioImgFalse);
						});

					/* 're-check' this image */
					$(this).attr("src",radioImgTrue);
					$(this).next().attr("checked","checked");
				}
			});
		}
	});
}
