/*
non-selectable
http://www.tek-tips.com/viewthread.cfm?qid=1184318&page=1
http://p2p.wrox.com/javascript/30726-non-selectable-html.html
*/


/* I could if I wanted to make this call work more like
 *  SelectPicker in that it could find the right control
 *  from the event.  But I have so much to do I'll leave it
 *  inconsistent
 */
/*
   second click if open and selected, just close
 */

function TogglePickerChild(event)
{

   if (!event)
   {  e = window.event;
   }
   else e = event;
   mods = get_modifiers(e);

   tgt = e.target;
   if (!tgt)
   {  tgt = e.srcElement;
   }

   ctl = tgt.parent_picker;
   which_row = tgt.which_row;

   /*dBug(e);
   dBug(ctl);
   dBug(tgt);
   dBug(which_row);
 */
   if (!ctl)return;
   cz = ctl.child_zones[tgt.which_cz];

   // probably could make this a member element vs. doing this lookup...
   el = tgt.cz;
//   dBug(el);
//   el = document.getElementById("child_zone_" + which_picker + "_" + which_row);
   alt_mode = false;
   if (mods.meta || mods.ctrl)
   {  alt_mode = true;
   }

   if (!cz.closed && !alt_mode)// || force_close)
   {  
      cz.arrow.src = "/img/arrow_closed_" + 
         (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
      el.style.display = "none";
      cz.closed = true;
   }
   else
   {  
      cz.arrow.src = "/img/arrow_open_" + 
         (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
      el.style.display = "block";
      cz.closed = false;
   }

   // arrows should expand, but not pick stuff
   if (!tgt.skip_pick)
   {
      if (mods.meta || mods.ctrl)
      {  ctl.addSelection(which_row);
      }
      else
      {  ctl.setSelection(which_row);
      }
   }
   
}

function TogglePickerChild_OLD(which_picker, which_row, force_close, my_row)
{
   ctl = group_picker_ctls[which_picker];

   cz = ctl.child_zones[which_row];

   // probably could make this a member element vs. doing this lookup...
   el = document.getElementById("child_zone_" + which_picker + "_" + which_row);
   if (!cz.closed || force_close)
   {  
      cz.arrow.src = "/img/arrow_closed_" + 
         (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
      el.style.display = "none";
      cz.closed = true;
      ctl.removeSelection(my_row);
   }
   else
   {  
      cz.arrow.src = "/img/arrow_open_" + 
         (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
      el.style.display = "block";
      cz.closed = false;
      ctl.setSelection(my_row);
   }
   
}

function SelectPicker(event)
{  if (!event)
   {  e = window.event;
   }
   else e = event;
   mods = get_modifiers(e);

   tgt = e.target;
   if (!tgt)
   {  tgt = e.srcElement;
   }

   ctl = tgt.parent_picker;

   if (!ctl)
   {  tgt = tgt.firstChild;   
      ctl = tgt.parent_picker;
   }

   if (mods.meta || mods.ctrl)
   {  ctl.addSelection(tgt.row_data_idx);
   }
   else
   {  ctl.setSelection(tgt.row_data_idx);
   }
} 


function Picker(my_name, my_table_id_in, expander) {
   this.my_table_id = my_table_id_in; 
   this.my_name = my_name;
   if (!my_table_id_in)
   {  return this;
   }

   this.child_zones = new Array();

   this.my_el = document.getElementById(this.my_table_id);

   this.picked  = new Array();

   // data, mainly what the value is for that row
   this.row_data = new Array();

   this.draw_count = 0;

   this.removeSelection = function(row) {
      for (i = 0; i < this.picked.length; i++)
      {  if (this.picked[i] == row)
         { 
            this.picked.splice(i, 1);
            this.highlight_row(row, false);     
            handle_form_changed();   
            return;
         }
      }
   }

   this.addSelection = function(row) {
      for (i = 0; i < this.picked.length; i++)
      {  if (this.picked[i] == row)
         { 
            this.picked.splice(i, 1);
            this.highlight_row(row, false);     
            handle_form_changed();   
            return;
         }
      }
      this.picked.push(row);
      this.highlight_row(row, true);  
      handle_form_changed();   
   }

   this.clear_picks = function() { 
      for (i = 0; i < this.picked.length; i++)
      {  this.highlight_row(this.picked[i], false);     
      }

      for (i = 0; i < this.child_zones.length; i++)
      {  if (!this.child_zones[i].closed)
         {  TogglePickerChild_OLD(this.my_name, i, true, 0);
         }

      }

      this.picked = new Array();
   }

   this.highlight_row = function(idx, show)
   {
      cz = this.row_data[idx];
      el = cz.cell;
  
      if (!show)
      {  el.className = "pickerUnpicked";
      }
      else
      {
         el.className = "pickerPicked";
      }
   }

   this.setSelection = function(row) {
      if (row == null) return;
      
      for (i = 0; i < this.picked.length; i++) {
         this.highlight_row(this.picked[i], false);
      }
      this.picked = new Array();
      this.picked.push(row);
      this.highlight_row(row, true);
      handle_form_changed();   
   }

   this.getSelection = function() {
      return this.picked;
   }

   this.clear = function() {
      this.setSelection(null);
      this.draw_count = 0;

      // clear ui
      for(var i = this.my_el.rows.length; i > 0;i--)
      {
         this.my_el.deleteRow(i -1);
      }
   }

   // not all parents have children
   this.addData = function(parent_txt, parent_id, child_txt, child_val) {

   row = this.my_el.insertRow(this.draw_count);
   cell_now = 0;

   rd = new Object();
   this.row_data.push(rd);
   rd.val = parent_id;
   rd.pretty = parent_txt;

   if (expander)
   {
      cell = row.insertCell(cell_now++);
      cell.width = 17;

   // draw the arrow
      arrow_spn = null;
      if (child_txt) {
         cz = new Object();
         this.child_zones.push(cz);

         arrow_spn = document.createElement('IMG');
         arrow_spn.parent_picker = this;
         arrow_spn.which_row = this.row_data.length - 1;
         arrow_spn.which_cz = this.child_zones.length - 1;
         arrow_spn.skip_pick = true;
         cz.arrow = arrow_spn;
         cz.closed = true;
         
         arrow_spn.src = "/img/arrow_closed_" + 
            (domain_info.white_on_black_results_bin ? "light" : "dark") +".gif";
         cell.onclick= TogglePickerChild;
         cell.appendChild(arrow_spn);
      }
   }
   // draw the text of the cell (top level)
      cell = row.insertCell(cell_now++);
      rd.cell = cell;
      cell.className = "pickerUnpicked"; 
      cell.style.cursor = 'default';

      if (child_txt)
      {  
         cell.onclick= TogglePickerChild;
      }

      parent_txt_spn = document.createElement('SPAN');
      parent_txt_spn.width = "100%";
      parent_txt_spn.parent_picker = this;
      parent_txt_spn.which_row = this.row_data.length - 1;
      parent_txt_spn.which_cz = this.child_zones.length - 1;
      parent_txt_spn.skip_pick = false;
      cell.appendChild(parent_txt_spn);
      if (!child_txt)
      {
         parent_txt_spn.parent_picker = this;
         cell.className = "pickerUnpicked";
         cell.onclick = SelectPicker;
         rd = new Object();
         this.row_data.push(rd);
         rd.val = parent_id;
         rd.pretty = parent_txt;
         rd.cell = cell;
         rd.picked = false;
         parent_txt_spn.row_data_idx = this.row_data.length - 1;
      }

      tn = document.createTextNode(parent_txt);
      parent_txt_spn.appendChild(tn);

      this.draw_count++; 

   // draw children
      if (child_txt) {

   // make table to hold children.  Makes it easy to hide all at once
         c_table = document.createElement('TABLE');
         parent_txt_spn.cz = c_table;
         arrow_spn.cz = c_table; 
         c_table.id = "child_zone_" + this.my_name + "_" + 
            (this.child_zones.length - 1);
         c_table.style.margin = "0px";
         c_table.style.display = "none";
         c_table.border = "0";
         c_table.cellSpacing = "0";
         c_table.cellPadding = "0";

         for (i = 0; i < child_txt.length; i++) {
            row = c_table.insertRow(i);
            cell = row.insertCell(0);
            cell.width= "1100";  // ugly hack

   //  like before make a span to old our stuff
            spn = document.createElement('SPAN');
            spn.width = "100%";
            cell.appendChild(spn);
            cell.style.cursor = 'default';
            cell.className = "pickerUnpicked"; 
            spn.parent_picker = this;
            cell.onclick = SelectPicker;
            rd = new Object();
            this.row_data.push(rd);

            rd.val = child_val[i];
            rd.pretty = child_txt[i];
            rd.cell = cell;
            rd.picked = false;
            spn.row_data_idx = this.row_data.length - 1;

            tn = document.createTextNode("- " +child_txt[i]);
            spn.appendChild(tn);
// draw count?
         }

         row = this.my_el.insertRow(this.draw_count++);
         row.toggle_showing = false;
         cell = row.insertCell(0);
         cell = row.insertCell(1);
         cell.width = "100%";
         cell.appendChild(c_table);
      }
   }

   return this;
}

