/**
 * Checkboxify <http://passingcuriosity.com/code/jquery.checkboxify/>
 * A jQuery plugin for dynamically transforming selects into checkboxes and radio buttons.
 * 
 * Copyright Thomas Sutton and Bouncing Orange.
 *
 * $Id: jquery.checkboxify.js 22 2008-07-15 13:11:01Z root $
 **/ 
 
;(function($){

	// We need hideElement
	if (! $.fn.hideElement) {
		debug("Checkboxify requires hideElement.");
		return;
	}

	/**
	 * Hide a <select> element and replace it with <input type="checkbox"> elements.
	 *
	 * Adds an 'update' event handler on the <select> so that dynamic updates can cascade.
	 */
	$.fn.checkboxify = function(options){
		// Figure out our options
		var opts = $.extend({}, $.fn.checkboxify.defaults, options);
		
		// Checkboxify each of the selected elements
		return this.each(function(){ 
			var $this = $(this);

			// We can only checkboxify select elements.
			if (this.tagName.toLowerCase() != 'select') {
				debug("Can't checkboxify non-<select>: ", $this);
				return $this;
			}
			debug('Checkboxifying: ', $this);
			
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			
			// Wrap the select in another element that we can `$.fn.hide()`
			var newID = $(this).attr('id');
			if (typeof newID != "string" || newID == "") {
				var ran = 'r'+Math.round(Math.random() * Math.pow(10, 10));
				newID = ran;
			}
			newID = o.wrapIdPrefix + newID;
			var addID = ' id="' + newID + '"';
			var addClass = o.wrapClass ? ( ' class="' + o.wrapClass + '"' ) : '';
			var newEl = $('<div'+addID+addClass+'></div>');
			newEl.insertAfter($(this));
			
			// Hide the select
			$(this).hideElement();
            $(this).attr('disabled', 'disabled');
	
			// When the select is updated, we'll need to recreate the checkboxes
			$(this).bind('update', function(){
				updateBoxes('#'+newID, this, opts);
			});
			// Trigger a update
			$(this).trigger('update');
		});
	};	

	/**
	 * Clear the children of `to` and add checkboxes based on `from`.
	 */
	function updateBoxes(to, from){
		// Make sure that `to` is a jQuery object
		to = $(to);
		from = $(from);
		var n = from.attr('name');
		
		debug('#'+ from.attr('id') +'('+ n +') -> #'+ to.attr('id'));
		
		
		to.empty();
		from.children('option').each(function(){
			var id = $(this).attr('value');
			$(this).attr('id', 'nid'+id+'old');
			var t = $(this).text().replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
			t = '<label><input type="checkbox" name="'+n+'" id="nid'+id+'" value="'+id+'" /> '+t+' </label>';
			to.append($(t));
		});
		return false;
	};
	
	/**
	 * Logging debug info.
	 */
	if (window.console && window.console.log) {
		function debug(msg, $obj) {
			if (typeof $obj == 'undefined') {
				$obj = msg;
				msg = "checkboxify: ";
			}
			/*var al = $obj.each(function(){
				var pars = 	$(this).parents().map(function(){ 
					var i = $(this).attr('id');
					var s = (this.tagName).toLowerCase();
					if (i) s = s+"#"+i;
					
					return s;
				});
				var l = pars.get().reverse();
				var i = $(this).attr('id');
				i = i ? '#'+i : '';
				l[l.length] = (this.tagName).toLowerCase() + i;
				l.reverse();
				window.console.log(msg + l.join(" < "));
			});*/
			window.console.log(msg + $obj);
			return $obj;
		};
	} else {
		function debug(msg, $obj) {
			if (typeof $obj == 'undefined') {
				$obj = msg;
				msg = "checkboxify: ";
			}
			return $obj;
		};
	};
	
	$.fn.checkboxify.defaults = {
		type: 'auto',
		wrapTag: 'div',
		wrapClass: 'checkboxified',
		wrapIdPrefix: 'checkboxified'
	};
})(jQuery);
