with (classes) {
//
with (OCP) {
//
with (Book) {
//
Book.TextGuide = function (The_Book, Version_Element)
{
 with (Guide) call(this, The_Book, Version_Element);

 with (TextGuide)
 {
  this[XHTML.Form.Button.events.click] = buttonClicked;
  this[XHTML.Form.Select.events.change] = selectChanged;
  this[XHTML.Form.CheckboxInput.events.change] = checkboxChanged;

  this.updateDivisionSelections = updateDivisionSelections;
  this.checkDivisionSelections = checkDivisionSelections;

  with (this)
  {
   with (elements) try // These classes are used to allow the cursor to change appropriately ... (!)
   {
    this.ToggleReading_Button = namedItem("ToggleReading_Button");

    this.FromDivision_Selects = namedItem("FromDivision_Selects");
    this.To_CheckboxInput = namedItem("To_CheckboxInput");
    this.ToDivision_Selects = namedItem("ToDivision_Selects");

    this.Show_Button = namedItem("Show_Button");
   }
   catch (e) {}

   if (ToggleReading_Button) /* Opera workaround */ with (XHTML.Form)
   {
    Button.call(ToggleReading_Button).addObserver(this);
    Button.call(Show_Button).addObserver(this);

    var select_count = FromDivision_Selects.length;

    for (var select_index = 0; select_index != select_count; ++ select_index)
    {
     var The_Select = FromDivision_Selects[select_index]; // Safari workaround

     Select.call(The_Select).addObserver(this);

     The_Select.disabled = false;
    }

    select_count = ToDivision_Selects.length;

    for (var select_index = 0; select_index != select_count; ++ select_index)
     Select.call(ToDivision_Selects[select_index]).addObserver(this); // item(select_index) // Safari workaround

    CheckboxInput.call(To_CheckboxInput).addObserver(this);

    updateDivisionSelections(FromDivision_Selects, 0);
    updateDivisionSelections(ToDivision_Selects, 0);
   }
  }
 }

 return this;
}
//
TextGuide.Events =
{
 readingRequest : "OCP_Book_TextGuide_readingRequest"
}
//
TextGuide.buttonClicked = function (Clicked_Button) { with (TextGuide) with (this)
{
 switch (Clicked_Button)
 {
  case ToggleReading_Button: toggleFieldset(Clicked_Button, Clicked_Button.parentNode.parentNode);
   break;
  case Show_Button:
  {
   disable();

   var from_positions = new Array,
       to_positions;

   var division_index, division_count;

   division_count = FromDivision_Selects.length;

   for (division_index = 0; division_index != division_count; ++ division_index)
    from_positions.push(FromDivision_Selects[division_index].selectedIndex + 1); // item(division_index) // Safari workaround

   if (To_CheckboxInput.checked)
   {
    to_positions = new Array;

    division_count = ToDivision_Selects.length;

    for (division_index = 0; division_index != division_count; ++ division_index)
     to_positions.push(ToDivision_Selects[division_index].selectedIndex + 1); // item(division_index) // Safari workaround

   }

   broadcastAsync(Events.readingRequest, from_positions, to_positions);
  }
   break;
  default: Guide.buttonClicked.call(this, Clicked_Button);
 }
}}
//
TextGuide.updateDivisionSelections = function (Division_Selects, updated_index, update_count) { with (TextGuide) with (this)
{
 var select_index = 0,
     select_count = Division_Selects.length;

 if (updated_index + 1 >= select_count) return;

 var expression_string = "/book/version/text";

 do
 {
  expression_string += "/div[@number = '" + Division_Selects[select_index].value + "']"; // item(select_index) // Safari workaround

 } while (select_index ++ != updated_index);

 if (! (select_index < select_count)) return;

 if (update_count) select_count = select_index + update_count;

 do
 {
  var This_Select = Division_Selects[select_index]; // item(select_index) // Safari workaround

  with (This_Select) while (lastChild) removeChild(lastChild);

  expression_string += "/div";

  Divisions_XPathResult = The_XPathEvaluator.evaluate(expression_string, Version_Element, null, dom.xpath.XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);

  var This_Element,
      This_HTMLOptionElement;

  // Firefox workaround - this.ownerDocument isn't set properly until this is added to the doc hierarchy
  while (This_Element = Divisions_XPathResult.iterateNext())
  {
   with (This_HTMLOptionElement = Application.contentDocument.createElement("option"))
    appendChild(ownerDocument.createTextNode(value = This_Element.getAttribute("number")));

   This_Select.appendChild(This_HTMLOptionElement);
  }

  expression_string += "[position() = 1]";

 } while (++ select_index != select_count);
}}
//
TextGuide.checkDivisionSelections = function (Check_Selects, Changed_Selects, lessThan) { with (TextGuide) with (this)
{
 var select_index = 0, select_count = Changed_Selects.length;

 var test = lessThan? function (a, b) { return a < b; } : function (a, b) { return a > b; };

 while (select_index != select_count)
 {
  var Check_Select = Check_Selects[select_index], // .item(select_index), // Safari workaround
      Changed_Select = Changed_Selects[select_index]; // .item(select_index); // Safari workaround

  if (test(Check_Select.selectedIndex, Changed_Select.selectedIndex)) return;

  if (Check_Select.selectedIndex != Changed_Select.selectedIndex)
  {
   Check_Select.selectedIndex = Changed_Select.selectedIndex;

   updateDivisionSelections(Check_Selects, select_index, 1);
  }

  ++ select_index;
 }
}}
//
TextGuide.selectChanged = function (Changed_Select) { with (Guide) with (this)
{
 var select_index = 0,
     select_count = FromDivision_Selects.length;

 while (select_index != select_count && FromDivision_Selects[select_index] != Changed_Select) ++ select_index; // .item(select_index) // Safari workaround

 if (select_index != select_count) // From division changed
 {
  updateDivisionSelections(FromDivision_Selects, select_index);

  var To_Select = ToDivision_Selects[select_index] // ToDivision_Selects.item(select_index); // Safari workaround

  if (Changed_Select.selectedIndex > To_Select.selectedIndex) checkDivisionSelections(ToDivision_Selects, FromDivision_Selects, false);
 }
 else // To division changed
 {
  select_index = 0;
  select_count = ToDivision_Selects.length;

  while (select_index != select_count && ToDivision_Selects[select_index] != Changed_Select) ++ select_index; // .item(select_index) // Safari workaround

  if (select_index == select_count) return; // !!

  updateDivisionSelections(ToDivision_Selects, select_index);

  var From_Select = FromDivision_Selects[select_index]; // FromDivision_Selects.item(select_index); // Safari workaround

  if (Changed_Select.selectedIndex < From_Select.selectedIndex) checkDivisionSelections(FromDivision_Selects, ToDivision_Selects, true);
 }
}}
//
TextGuide.checkboxChanged = function (Changed_CheckboxInput) { with (TextGuide) with (this)
{
 switch (Changed_CheckboxInput)
 {
  case To_CheckboxInput:
  {
   var select_count = ToDivision_Selects.length;

   for (var select_index = 0; select_index != select_count; ++ select_index)
    with (ToDivision_Selects[select_index]) disabled = ! disabled; // .item(select_index) // Safari workaround

  }
   break;
  default: Guide.checkboxChanged.call(this, Changed_CheckboxInput);
 }
}}
//
}
//
}
//
}
