/*jslint browser: true, devel: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */

(function ( ) {
	"use strict";
	
	var checkbox	=	function ($el) {
		var methods, $box;
		
		methods	=	{
			"init":		function ( ) {
				$el.wrap($("<div/>").addClass("checkbox"));
				$box	=	$el.parent();
				
				$box.bind("click", methods.changeState);
				$el.bind("change, keyup", methods.checkState);
				
				methods.checkState();
				
				return methods;
			},
			"checkState":	function ( ) {
				if ($el.is(":checked")) {
					$box.addClass("checked");
				} else {
					$box.removeClass("checked");
				}
				
				return methods;
			},
			"changeState":	function ( ) {
				if ($el.is(":checked")) {
					$el.removeAttr("checked");
				} else {
					$el.attr({"checked": "checked"});
				}
				
				methods.checkState();
			}
		};
		
		return methods.init();
	};
	
	$(document).ready(function ( ) {
		$("input:checkbox").each(function ( ) {
			checkbox($(this));
		});
	});
}());

