/**
* @author Ryan Johnson <http://syntacticx.com/>
* @copyright 2008 PersonalGrid Corporation <http://personalgrid.com/>
* @package LivePipe UI
* @license MIT
* @url http://livepipe.net/control/rating
* @require prototype.js, livepipe.js
* 
* Ripped apart and edited by Ken at Norwood Promotional Products (c) 2009
*/
 
if(typeof(Prototype) == "undefined")
  throw "Control.SelectMultiple requires Prototype to be loaded.";
if(typeof(Object.Event) == "undefined")
  throw "Control.SelectMultiple requires Object.Event to be loaded.";
 
Control.CategorySelect = Class.create({
  select: false,
  container: false,
  display_container: false,
  checkboxes: [],
  checkbox_vals: false,
  hasExtraOption: false,
  
  initialize: function(select,container,display_container,options){
    this.options = {
      listid: '',
      checkboxSelector: 'input[type=checkbox]',
      nameSelector: 'span.name',
      labelSeparator: ', ',
      valueSeparator: ',',
      subcatDelim: 'SUB_',
      value: false,
      afterChange: Prototype.emptyFunction,
      overflowString: function(str){
        return str.truncate();
      },
      overflowLength: 30
    };
    Object.extend(this.options,options || {});
    this.select = $(select);
    this.container = $(container);
    this.display_container = $(display_container);
    this.checkboxes = (typeof(this.options.checkboxSelector) == 'function')
      ? this.options.checkboxSelector.bind(this)()
      : this.container.select(this.options.checkboxSelector)
    ;
    
    var value_was_set = false;
    if(this.options.value){
      value_was_set = true;
      this.setValue(this.options.value);
      delete this.options.value;
    }
    this.hasExtraOption = false;
    this.checkboxes.each(function(checkbox){
      checkbox.observe('click',this.checkboxOnClick.bind(this,checkbox));
    }.bind(this));
    if(!value_was_set)
      this.scanCheckBoxes();
    
  },
  setValue: function(value_string){
    var value_collection = $A(value_string.split ? value_string.split(this.options.valueSeparator) : value_string)

	value_collection.each(function(value){
		var checkbox = $('checkbox_'+value);
		
		if(checkbox != null){
			checkbox.checked = true;
			
			// If subbox, check the parent box
			if (checkbox.hasClassName("subbox")) {
				var check_up_id = checkbox.up('ul').id;
				var submenu_arr = check_up_id.split("_");
				var cat = submenu_arr[1];
				
				$('checkbox_'+cat).checked = true;
			}
		}
	}.bind(this));
    
    this.scanCheckBoxes();
  },
  checkboxOnClick: function(checkbox){
	var check_up_id = checkbox.up('ul').id;
	
	// If a main menu box was unchecked, uncheck all submenu boxes
	if (!checkbox.checked && check_up_id == this.options.listid) {
		var submenu = checkbox.up('li').down('ul');
		
		submenu.select('input').each( function (e) {
			e.checked = false;
		});
	}
	// If a submenu box was checked, make sure the parent menu box is checked
	else if (checkbox.checked && check_up_id != this.options.listid) {
		var submenu_arr = check_up_id.split("_");
		var cat = submenu_arr[1];
		
		$('checkbox_'+cat).checked = true;
	}
	// If a submenu box is unchecked, and no other submenu boxes are checked, then uncheck parent
	else if (!checkbox.checked && check_up_id != this.options.listid) {
		var submenu = checkbox.up('ul');
		
		//alert(submenu.getElementsByTagName('input').length);
		
		// Check if all boxes are unchecked
		if (submenu.select('input:checked').length == 0) {
			var submenu_arr = submenu.id.split("_");
			var cat = submenu_arr[1];
			
			$('checkbox_'+cat).checked = false;
		}
	}
	  
    this.scanCheckBoxes();
  },
  scanCheckBoxes: function(){
	  this.checkbox_vals = Array();
	  this.cat_sel = "<table cellpadding=2>";

	  // Get the main checkboxes that are checked
	  this.main_checked = $(this.options.listid).select('.mainbox:checked');
	  
	  this.cat_sel = (this.main_checked.length > 0) ? "<b>Searcing in:</b>" : "";
	  this.cat_sel += "<table cellpadding=2>";
	  
	  // Go through the main checked checkboxes and...
	  //   Add vals for main checkboxes where all submenu boxes are checked
	  //   Add vals for main checkboxes where no submenu boxes are checked
	  //   Add vals for submenu boxes where not all submenu boxes are checked
	  this.main_checked.each( function(mainbox){
		  var submenu = mainbox.up('li').down('ul');
		  
		  var total_boxes = submenu.select('.subbox').length;
		  var checked_boxes = submenu.select('.subbox:checked');
		  
		  if (checked_boxes.length == 0 || checked_boxes.length == total_boxes) {
			  this.checkbox_vals.push(mainbox.value);
			  this.cat_sel += "<tr><td>" + mainbox.up('li').down('.descr').innerHTML + "</td><td><img src='/sites/www/images/arrow_us.png' height='10' /></td><td>All Subcategories</td></tr>";
		  }
		  else if (checked_boxes.length > 0) {
			  checked_boxes.each( function(subbox){
				  this.checkbox_vals.push(subbox.value);
				  this.cat_sel += "<tr><td>" + subbox.up('li',1).down('.descr').innerHTML + "</td><td><img src='/sites/www/images/arrow_us.png' height='10' /></td><td>" + subbox.up('li').down('.descr').innerHTML + "</td></tr>";
			  }.bind(this));
		  }
	  }.bind(this));
	  
	  this.display_container.innerHTML = this.cat_sel + "</table>";
	  
	  this.select.value = this.checkbox_vals.join(this.options.valueSeparator);
  }
});
Object.Event.extend(Control.CategorySelect);