//This is the updated version 1.5 
var CheckSelect = {
	_arr_lists: new Array(),
	_curr_list: null,
	debug: true,

	/**
	 * Registers the list for use as a RadioSelect
	 *
	 * @access public
	 * @param elem_ul - An id or elem to use as the list - must be a UL
	 * @return bool
	 */
	registerList: function(elem_ul)
	{
		
		var tmp_list = $(elem_ul);
		if ( tmp_list != null ) {
			this._arr_lists[tmp_list.id] = tmp_list;
			this._curr_list = tmp_list;
			this._setupList();
			return true;
				
		} else {
			return false;
			
		}
		
	},//end registerList
	
	/**
	 * Sets the RadioSelect object to work with the specified list - list must be registered already
	 *
	 * @access public
	 * @param elem - An id or eleme
	 * @return bool
	 */
	selectList: function(elem)
	{
		var tmp_list = $(elem);
		if ( tmp_list != null ) {
			this._curr_list = this._arr_lists[tmp_list.id];
			return true;
			
		} else {
			return false;
			
		}
		
	},//end selectList
	
	/**
	 * Do all of the initial setup
	 *
	 * @access protected
	 * @return void
	 */
	_setupList: function()
	{
		//First apply styles
		this._setupStyles();	
		
		//Now register click events	
		this._setupEvents();		
		
	},//end _setupList
	
	_setupStyles: function()
	{
		this._setupStylesMainList();
		
	},//_setupStyles
	
	_setupStylesMainList: function()
	{
		//Main ul
		Element.setStyle(this._curr_list, $H({margin: "0px", padding: "0px"}));
		
		//Handle main lis
		this._setupStylesMainListLis();
			
	},//end _setupStylesMainList
	
	_setupStylesMainListLis: function()
	{		
		//Child lis
		var styles = $H({fontWeight: "bold",fontSize: "12px",listStyle: "none"});
		var arr_child_lis = Element.getChildElementsByTagName(this._curr_list, "LI");
		for ( var i=0; i<arr_child_lis.length; i++ ) {
			Element.setStyle(arr_child_lis[i], styles);
			
			//Handle Secondary lists
			this._setupStylesSecondaryList(arr_child_lis[i]);			
			
		}//for
		
	},//end _setupStylesMainListLis
	
	_setupStylesSecondaryList: function(elem_li)
	{
		var styles = $H({margin: "0px 0px 0px 20px", padding: "0px"});
		var arr_child_divs = Element.getChildElementsByTagName(elem_li, "DIV");
		
		if ( arr_child_divs == null || arr_child_divs.length == 0 ) {
			return false;
			
		}
		
		var elem_div = arr_child_divs[0];
		var arr_child_uls = Element.getChildElementsByTagName(elem_div, "UL");
		if ( arr_child_uls == null ) {
			return;
			
		}
		
		for ( var i=0; i<arr_child_uls.length; i++ ) {
			Element.setStyle(arr_child_uls[i], styles);
			
			//Handle secondary lis
			this._setupStylesSecondaryListLis(arr_child_uls[i]);
			
		}//for
		
	},//end _setupStylesSecondaryList
	
	_setupStylesSecondaryListLis: function(elem_ul)
	{
		var styles = $H({fontWeight: "normal", listStyle: "none", fontSize: "12px"});
		var arr_child_lis = Element.getChildElementsByTagName(elem_ul, "LI");
		if ( arr_child_lis == null ) {
			return;
		}
		
		for ( var i=0; i<arr_child_lis.length; i++ ) {
			Element.setStyle(arr_child_lis[i], styles);
			
		}//for
		
	},//end _setupStylesSecondaryListLis
	
	_setupEvents: function()
	{
		//Get all the main "lis"
		var arr_lis = Element.getChildElementsByTagName(this._curr_list, "LI");
		
		//Loop through the main lis and make sure that the input types match, 
		//should be one input - either radio or checkbox in each li
		var input_type = null;
		for ( var i=0; i<arr_lis.length; i++ ) {
			var arr_inputs = Element.getChildElementsByTagName(arr_lis[i], "INPUT");
			if ( arr_inputs.length != 1 ) {
				if ( this.debug ) {
					alert("Invalid number of inputs in main li level on li #" + i);
					return false;
					
				} else {
					Element.hide(this._curr_list);
					return false;
					
				}
				
			}
			
			var elem_input = arr_inputs[0];
			if ( input_type == null && (elem_input.type == "radio" || elem_input.type == "checkbox") ) {
				input_type = elem_input.type;
				
			}
			
			//Handle an invalid input type - just hide it
			if ( elem_input.type != input_type ) {
				if ( this.debug == true ) {
					alert("Invalid input type " + elem_input.type + " on li " + i + " should be " + input_type);
									
				}
				
				Element.hide(arr_lis[i]);
				continue;
				
			}
			
			//Still here?  Then it's all good
			elem_input.onclick = this.hndlClick.bindAsEventListener(this);
			
			//All child ULS are now wrapped in DIVs because of IE7 (and so it begins)
			var arr_child_divs = Element.getChildElementsByTagName(arr_lis[i], "DIV");
			if ( arr_child_divs == null || arr_child_divs.length == 0 ) {
				//None found
				continue;
				
			}
			
			//Only should be one, so that's all I'm pulling up
			var elem_div = arr_child_divs[0];
			
			//Hide him
			if ( elem_input.checked == false ) {
				Element.hide(elem_div);
				
			}
			
		}//for		
		
	},//end _setupEvents
	
	/**
	 * Handles the click of either a radio or checkbox
	 *
	 * @access public
	 * @param event - Javascript event object
	 * @return void
	 */
	hndlClick: function(event)
	{
		var elem = Event.element(event);
		
		switch ( elem.type )
		{
			case "checkbox":
				this._hndlClickCheckBox(elem);
				break;
				
			case "radio":
				this._hndlClickRadio(elem);
				break;
				
			default:
				return false;
				break;
			
		}//end switch
		
	},//end hndlClick
	
	/**
	 * Method to handle clicks when we are dealing with a checkbox
	 *
	 * @access protected
	 * @param elem - HTML Element node
	 * @return void
	 */
	_hndlClickCheckBox: function(elem)
	{
		var elem_parent = elem.parentNode;
		var arr_divs = Element.getChildElementsByTagName(elem_parent, "DIV");
		
		if ( arr_divs == null || arr_divs.length == 0 ) {
			return;
			
		}
		
		var elem_div = arr_divs[0];
		
		if ( elem.checked == true ) {
			this.showList(elem_div);
			
		} else {
			this.hideList(elem_div);
			
		}
		
	},//end _hndlClickCheckBox
	
	/**
	 * Method to handle clicks when we are dealing with a radio button
	 *
	 * @access protected
	 * @param elem - HTML Element node that was clicked
	 * @return void
	 */
	_hndlClickRadio: function(elem)
	{
		//Main UL
		var elem_ul = Element.getAncestorByTagName(elem, "UL");
		
		//Main LIs
		var arr_lis = Element.getChildElementsByTagName(elem_ul, "LI");
		
		//The li that the clicked item is in
		var elem_parent_li = elem.parentNode;
		
		if ( arr_lis == null ) {
			return;
			
		}
		
		//Loop through the main level lis, since these are radio buttons, we have to shut down
		//any open menus that aren't this menu
		for ( var i=0; i<arr_lis.length; i++ ) {
			var arr_divs = Element.getChildElementsByTagName(arr_lis[i], "DIV");
			if ( arr_divs == null || arr_divs.length == 0 ) {
				//No div found, skip
				continue;
				
			}
			var elem_div = arr_divs[0];
			
			//If the li the loop is currently on is NOT the li of the element that was clicked
			if ( arr_lis[i] != elem_parent_li ) {
				//If the li the loop is on is visible, hide it
				if ( Element.visible(elem_div) ) {
					this.hideList(elem_div);
						
				}				
				
			} else {
			//If it IS the li we are working with
				if ( Element.visible(elem_div) == false ) {
					this.showList(elem_div);
					
				} else {
					this.hideList(elem_div);
					elem.checked = false;
					
				}
				
			}
			
		}//for
		
	},//end _hndlClickRadio	
	
	/**
	 * Displays the given list
	 *
	 * @access public
	 * @param elem_div - UL HTML Node
	 * @return void
	 */
	showList: function(elem_div)
	{
		Effect.BlindDown(elem_div);
		
	},//end showList
	
	/**
	 * Hides the given list
	 * 
	 * @access public
	 * @param elem_div - The UL to hide
	 * @param auto_uncheck - Optional, whether to automatically uncheck any items being hidden, true by default
	 * @return void;
	 */
	hideList: function(elem_div)
	{
		//Check to see if we received a second parameter
		if ( arguments.length == 2 ) {
			var auto_uncheck = arguments[1];
			
		} else {
			var auto_uncheck = true;
			
		}
		
		if ( auto_uncheck == true ) {
			var callback = { 
				afterFinish: function(obj) 
				{
					var elem = obj.element;
					//Deep dive the DOM to find any INPUTs that are descendents of the div we are hiding and uncheck them
					var arr_children = elem.getElementsByTagName('INPUT');
					for ( var i=0; i<arr_children.length; i++ ) {
						if ( arr_children[i].type == "radio" || arr_children[i].type == "checkbox" ) {
							arr_children[i].checked = false;
							
						}
						
					}//for
				}
			};
			
		} else {
			var callback = {};
			
		}
		
		Effect.BlindUp(elem_div, callback);
		
	}//end hideList
	
}//end CheckSelect