﻿/**

 * jQuery.quickcheck

 * Copyright (c) 2008 Ryan Grauer - ryan(at)modusdev(dot)net : http://www.modusdev.net

 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)

 * Date: 10/1/2008

 *

 * @projectDescription Easy checkbox checking using jQuery.

 * Tested with jQuery 1.2.1. On FF 3.0.3.

 *

 * @author Ryan Grauer

 * @version 1.0

 *

 * @id jQuery.fn.quickcheck

 * @example $('input[type=checkbox]').quickcheck();

 *

 * Notes:

 */

;(function( $ ){

	$.fn.quickcheck = function(callback){
	
		var _this = this, checking = false, check = false, start_checker,
			callback = callback || function(){};
			
		
		
		this.mousedown(function(){
				start_checker = this;
				checking = true;
				if( check = !$(this).is(":checked") ) $(this).attr("checked", "checked");
				else $(this).attr("checked", "");
			})
			.mouseover(function(){
				if(!checking) return;
				if(check) $(this).attr("checked", "checked");
				else $(this).attr("checked", "");
			})
			.click(function(){
				if(this != start_checker ) return;
				if(!$(this).is(":checked")) $(this).attr("checked", "checked");
				else $(this).attr("checked", "");
			});
			
		this.each(function(){
			var cbox = $(this);
			if( !cbox.is("[id]") ) return;
			$("label[for=" + cbox.attr("id") + "]").mousedown(function(){
					cbox.mousedown();
				})
				.mouseover(function(){
					cbox.mouseover();
				})
				.click(function(){
					cbox.click();
				});
		});
		
		$("body").mouseup(function(){
			if(!checking) return;
			callback(_this);
			checking = false;
		});
	
		return this;
	}


})( jQuery );