/**
 * 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 3/13/2009
 * @projectDescription	Replaces the standard HTML form checkbox or radio buttons. Allows for disable, and very customizable.
 * @version 1.0.3
 * 
 * @requires jquery.js (tested with 1.3.2)
 * 
 * @param disable_all:	false,
 * @param wrapperclass:	"group"
 */

(function($) {	
	$.fn.custCheckBox = function(options){
		
		var defaults = {
				disable_all:	false,				//disables all the elements
				wrapperclass:	"group"
			};
		//override defaults
		var opts = $.extend(defaults, options);
		
		return this.each(function() { 
			var obj = $(this);
	 		
			this.setEnabled = function(enable, state) {
	 			var input = $(this);
	 			var type = $(this).attr("type");
	 			if (state === undefined)
	 				state = $(this).is(":checked") ? "on" : "off";
	 			
				input.prev().removeClass("cust_" + type + "_disabled_on");
				input.prev().removeClass("cust_" + type + "_disabled_off");
				input.prev().removeClass("cust_" + type + "_on");
				input.prev().removeClass("cust_" + type + "_off");

				if (state == "on")
					input.attr("checked", "checked");
				else
					input.removeAttr("checked");
				
				if (enable){
					input.removeAttr("readonly");
					input.prev().addClass("cust_" + type + "_" + state);
				} else {
					input.attr("readonly", "readonly");
					input.prev().addClass("cust_" + type + "_disabled_" + state);
				}
			}
	 		 
	 		 $.fn.buildbox = function(thisElm){

	 			 var currElm = $(thisElm);
			
	 			 $(currElm).css({display:"none"}).before("<span class=\"cust_checkbox\">&nbsp;&nbsp;&nbsp;&nbsp;</span>");
			
	 			 var isChecked = $(currElm).attr("checked");
	 			 var boxtype = $(currElm).attr("type");
	 			 var readonly = $(currElm).attr("readonly");
			
	 			 $(currElm).prev("span").addClass($(currElm).attr("id"));
	 			 if(boxtype === "checkbox")
	 			 {
	 				 $(currElm).prev("span").addClass("checkbox");
	 				 if(readonly || opts.disable_all){boxtype = "checkbox_disabled";}
	 			 }
	 			 else
	 			 {
	 				 $(currElm).prev("span").addClass("radio");
	 				 if(readonly || opts.disable_all){boxtype = "radio_disabled";}
	 			 }
			
	 			 if(isChecked)
	 				 $(currElm).prev("span").addClass("cust_"+boxtype+"_on");
	 			 else
	 				 $(currElm).prev("span").addClass("cust_"+boxtype+"_off");
			
	 			 if(opts.disable_all)
	 				 $(currElm).attr("readonly","readonly");
	 		 };
		
	 		 $.fn.buildbox($(obj));
		
	 		 $("." + opts.wrapperclass+" label").unbind().mousedown(function(){
		  
	 			 if(!opts.disable_all)
	 			 {
	 				 var id = $(this).attr("for");
	 				 var custbox = $("span." + id);
	 				 var boxtype = $(custbox).next("input").attr("type");
	 				 var readonly = $(custbox).next("input").attr("readonly");
	 				 if($(custbox).hasClass("checkbox"))
	 				 {
	 					 if($(custbox).hasClass("cust_"+boxtype+"_off") && !readonly){
	 						 $(custbox).removeClass("cust_"+boxtype+"_off").addClass("cust_"+boxtype+"_on").next("input").attr("checked","checked").trigger("change", true).trigger("onChange"); //turn on
	 					 }else if(!readonly){
	 						 $(custbox).removeClass("cust_"+boxtype+"_on").addClass("cust_"+boxtype+"_off").next("input").removeAttr("checked").trigger("change", true).trigger("onChange"); //turn off
	 					 }
	 				 }
	 				 else if(!readonly)
	 				 {
	 					 $(custbox).parent().find(".cust_checkbox").removeClass("cust_"+boxtype+"_on").addClass("cust_"+boxtype+"_off").next("input").removeAttr("checked"); // turn off
	 					 $(custbox).removeClass("cust_"+boxtype+"_off").addClass("cust_"+boxtype+"_on").next("input").attr("checked","checked").trigger("change", true).trigger("onChange"); //turn on
	 				 }
	 			 }
			
	 		 });
		
	 		 $(this).change(function (e, triggered){
		  
	 			 if ($.browser.opera) return; // crappy opera
		  
	 			 var ref = $(this);
	 			 if (!triggered) {
	 				 if (ref.is(":checked"))
	 					 ref.removeAttr("checked");
	 				 else
	 					 ref.attr("checked", "checked");
	 			 }
	 		 });
		
	 		 $(".cust_checkbox").unbind().mousedown(function(){
			
	 			 if(!opts.disable_all)
	 			 {
	 				 var boxtype = $(this).next("input").attr("type");
	 				 var readonly = $(this).next("input").attr("readonly");
					
	 				 if($(this).hasClass("checkbox"))
	 				 {
	 					 if($(this).hasClass("cust_"+boxtype+"_off") && !readonly)
	 						 $(this).removeClass("cust_"+boxtype+"_off").addClass("cust_"+boxtype+"_on").next("input").attr("checked","checked").trigger("change", true).trigger("onChange"); //turn on
	 					 else if(!readonly)
	 						 $(this).removeClass("cust_"+boxtype+"_on").addClass("cust_"+boxtype+"_off").next("input").removeAttr("checked").trigger("change", true).trigger("onChange"); //turn off
	 				 }
	 				 else if(!readonly)
	 				 {
	 					 $(this).parent().find(".cust_checkbox").removeClass("cust_"+boxtype+"_on").addClass("cust_"+boxtype+"_off").next("input").removeAttr("checked"); // turn off
	 					 $(this).removeClass("cust_"+boxtype+"_off").addClass("cust_"+boxtype+"_on").next("input").attr("checked","checked").trigger("change", true).trigger("onChange"); //turn on
	 				 }
	 			 }
	 		 });
		}); 	
	};
	
})(jQuery);
