/**
 * cust_checkbox_plugin.js
 * Copyright (c) 2009 myPocket technologies (www.mypocket-technologies.com)
 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * View the GNU General Public License <http://www.gnu.org/licenses/>.

 * @author Darren Mason (djmason9@gmail.com)
 * @date 7/3/2009
 * @projectDescription	Replaces the standard HTML form checkbox or radio buttons. Allows for disable, and very customizable.
 * @version 1.0.6
 * 
 * @requires jquery.js (tested with 1.3.2)
 * 
 * @param disable_all:	false,
 * @param hover:	true,
 * @param wrapperclass:	"group"
 * @param callback:	function(){ * your code here * }
 */

(function($) 
{	
	$.fn.CHKBOX_EX = function(options)
    {

		var defaults = 
        {
            idx:0,
			disable_all:	false,				//disables all the elements
			hover:	true,						//adds a hover state to the tag
			wrapperclass:	"group",			//the class name of the wrapper tag
			callback:	function(){},			//a click event call back
            excl:""
		};
		//override defaults
		var opts = $.extend(defaults, options);
		
		return this.each(function() 
        { 
	 		var obj = $(this);
		    $.fn.buildbox = function(thisElm)
            {
				$(thisElm).css({display:"none"}).before("<span style=\"display:inline-block;padding-left:10px;padding-right:11px;line-height:19px;\" class=\"CHKBOX\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>");
				
               		    var isChecked = $(thisElm).attr("checked");
			    var disabled = $(thisElm).attr("disabled");
			    var boxtype = "CHKBOX";
                
			    if(isChecked)     $(thisElm).prev("span").addClass(boxtype+"_on");
			    else			  $(thisElm).prev("span").addClass(boxtype+"_off");
				
			    $(thisElm).prev("span").prev("label").unbind().click(function()
                {
					var custbox  = $(this).next("span");

					$(custbox).parent().find(".CHKBOX").each(function()
                    {
                        var boxtype = "CHKBOX" 
                        $(this).removeClass(boxtype+"_on").addClass(boxtype+"_off").next("input").removeAttr("checked");
                    });
                    
					$(custbox).removeClass(boxtype+"_off").addClass(boxtype+"_on").next("input").attr("checked","checked"); //turn on

					opts.callback.call(this);					 				
			    });
		
                //attach a click event for each checkbox.
                $(thisElm).prev("span").unbind().click(function()
                {
			
                    var boxtype = "CHKBOX" 
                    var disabled = $(this).next("input").attr("disabled");
			
		     $(this).parent().parent().find(".CHKBOX").each(function()
                    {
                        var boxtype = "CHKBOX"
                        
                        
                        $(this).removeClass(boxtype+"_on").addClass(boxtype+"_off").next("input").removeAttr("checked");
                    });
                    
                    $(this).removeClass(boxtype+"_off").addClass(boxtype+"_on").next("input").attr("checked","checked"); //turn on

					opts.callback.call(this);
                });	
            }

		//build the boxes
		$.fn.buildbox($(obj));            
		});

    };
	
})(jQuery);

