//
// create closure
//
(function($) {
  //
  // plugin definition
  //
	$.fn.imageCheckBox = function(options) {
		// build main options before element iteration
    var opts = $.extend({}, $.fn.imageCheckBox.defaults, options);
		$("<img>").attr("src", opts.checkedImage);
		$("<img>").attr("src", opts.uncheckedImage);
		$(this).each(function () {
			$this = $(this);
			//debug($this);
			if($this.attr("checked")) {
				var imageURL = opts.checkedImage;
			} else {
				var imageURL = opts.uncheckedImage;
			}
			$this.hide().before("<img src='"+imageURL+"' class='"+opts.imageClass+"'>");
			if ($this.attr("id")) {
				$("label[for='" + $this.attr("id") + "']").click(function(e){
					e.stopPropagation();
					$.fn.imageCheckBox.change($(this).prev(),$(this).prev().prev(),opts);
					return false;
				});
			}
			$this.prev().click(function(){
				$.fn.imageCheckBox.change($(this).next(),this,opts);
			});
		});
	};
	//
  // private function for debugging
  //
  function debug($obj) {
    if (window.console && window.console.log)
      window.console.log('hilight selection count: ' + $obj.size());
  };
	//
  // define and expose our change function
  //
  $.fn.imageCheckBox.change = function(inp,img,opt) {
  	$(inp).change();
		if($(inp).attr("checked")) {
			$(img).attr("src", opt.uncheckedImage);
			$(inp).removeAttr("checked");
		} else {
			$(img).attr("src", opt.checkedImage);
			$(inp).attr("checked", "checked");
		}
    return false;
  };
  //
  // plugin defaults
  //
  $.fn.imageCheckBox.defaults = {
    checkedImage: "cb_checked.png",
		uncheckedImage: "cb_unchecked.png",
		imageClass: "checkbox"
  };
//
// end of closure
//
})(jQuery);
