var i1 = new Image();
i1.src = "images/multibook_tab_bar_selected.png";

function hide_children(element_id) {
  $(element_id).childElements().each(function(x) {
    x.hide();
  });
  
  /* maybe shouldn't be here but
     there shouldn't be a reason
     we don't show this */
  $('multibook_content_top').show();

	if ($('status_messages')) {
		$('status_messages').show();
	}
}

function resize_multibook_sidebar() {
  var h = 0;
  
  if (current_view != null && current_view != "null") h = $(current_view).getHeight();
  if (h < sidebar_height) h = "100%";
  $('multibook_sidebar').setStyle({ height: h });
}

function set_selected_element(id) {	
  // remove highlight from other
  $$('div.selected_element').each(function(s) {
    s.removeClassName('selected_element');
    s.setStyle({ background: 'transparent'});
    s.descendants().each(function(t) {
      t.setStyle({ color: '#1e3c73'});
    });
  });

  if ($(id) == null) return;
  
  // highlight new
  $(id).setStyle({ background: '#6a99c1'});
  $(id).descendants().each(function(s) {
    s.setStyle({ color: 'white'});
  });
  $(id).addClassName('selected_element');
}

function hide_current_view(obj) {
  if ($(obj) != null) $(obj).hide();
  $$('div.results_bar').invoke('hide');
  $$('div.item_bar').invoke('hide');
}


function show_last_book_search(book_id) {
  hide_current_view($(current_view));
  $('record_id').innerHTML = '';
  
  $("book_search_" + book_id).show();
  $("results_bar_" + book_id).show();
  hide_children('casecheck_books');
  hide_children('casecheck_pager');
  hide_children('casecheck_count');
  hide_children('casecheck_sidebar');
  
  current_view = "book_search_" + book_id;

  toggle_caseknowledge("full_caseknowledge", true);
  toggle_casecheck(false);

  set_selected_element('book_wrapper_' + book_id);
  $('book_name_' + book_id).childElements().first().writeAttribute("onclick", "show_last_book_search(" + book_id +"); return false;");
  resize_multibook_sidebar();
  window.location.hash = 'top';
}

function show_last_book_item(book_id) {
  hide_children('casecheck_books');
  hide_children('casecheck_pager');
  hide_children('casecheck_count');
  hide_children('casecheck_sidebar');
  hide_current_view($(current_view));
  $('record_id').innerHTML = '';
  
  $("book_item_" + book_id).show();
  $("item_bar_" + book_id).show();
  $("casecheck_count_" + book_id).show();
  $("casecheck_sidebar_" + book_id).show();

  current_view = "book_item_" + book_id;

  toggle_caseknowledge("book_item_" + book_id + "_caseknowledge", true, true);
  set_selected_element('book_wrapper_' + book_id);
  resize_multibook_sidebar();
  window.location.hash = 'top';
}

function show_casecheck_search(book_id, court) {
  hide_children('messagebar');
  $('multibook_label').show();
  $('casecheck_pager').show();
  
  hide_children('multibook_content');
  $('casecheck_books').show();
  
  hide_children('casecheck_pager');
  hide_children('casecheck_books');
  
  
  $('book_casecheck_' + book_id + '_' + court).show();
  $('casecheck_pager_' + book_id + '_' + court).show();
  $('book_casecheck_' + book_id + '_' + court + '_results').show();
  
  current_view = 'book_casecheck_' + book_id + '_' + court + '_results';
  set_selected_element('book_casecheck_' + book_id + '_' + court + '_wrapper');
  resize_multibook_sidebar();
  window.location.hash = 'top';
}

function toggle_casecheck(visable) {
  /*
  if (!$('casecheck_sidebar').visible()) {
        $('casecheck_sidebar').hide();
    $('casecheck_tab').setStyle({ background: "url('images/multibook_tab_bar.png')"});
  } else {
    $('casecheck_sidebar').show();
    $('casecheck_tab').setStyle({ background: "url('images/multibook_tab_bar_selected.png')"});
  }
  */
  var strDivId = 'casecheck_sidebar'
  if((document.getElementById(strDivId).style.display == '' || document.getElementById(strDivId).style.visibility == 'visible') && (document.getElementById(strDivId).style.display != 'none'))
    {
        document.getElementById(strDivId).style.display = 'none';
        //document.getElementById(strImgId).src = 'images/searchCollapseLeft.png';
        $('casecheck_tab').setStyle({ background: "url('images/multibook_tab_bar.png')"});
    }
    else    
    {
        document.getElementById(strDivId).style.display = '';
        //document.getElementById(strImgId).src = 'images/searchCollapseDown.png';
        $('casecheck_tab').setStyle({ background: "url('images/multibook_tab_bar_selected.png')"});
    }
}

function toggle_caseknowledge(div, retain_state, hide) {
	if ($(div + "_count") == null) return; // nothing to toggle
	
  // if we are changing between different divs then hide all
  // the _count divs and show the one we want
  if (!$(div + "_count").visible()) {
    $$("span.caseknowledge_count").invoke('hide');
    $(div + "_count").show();
  }

  // if we are changing between different divs then hide all
  // the content divs
  var was_one_visible = $(div).visible();
  // was_one_visible = $$("div.caseknowledge_contents").find(function(s) { return s.visible(); }) != null;
  if (!$(div).visible() || hide) {
    $$("div.caseknowledge_contents").each(function(s) {
      if (s.visible()) {
        was_one_visible = true;
        s.hide();
      }
    });
  }
  
  // if retain_state is true then we keep the state of the previous
  // div and apply it to the div we are about to show
  var image_tag = '<image src="images/caseknowledge_logo.png" style="border:none">';
  if (retain_state) {
    if (was_one_visible) {
      $(div).show();
      $('caseknowledge_tab').setStyle({ background: "url('images/multibook_tab_bar_selected.png')"});
      $('caseknowledge_label').update('<a href="#" onclick="toggle_caseknowledge(\'' + div + '\', false, true); return false;">' + image_tag + '</a>');
      pos = $(div).cumulativeOffset();
      // $('contents').scrollTo(pos[0], pos[1]);
    } else {
      $('caseknowledge_tab').setStyle({ background: "url('images/multibook_tab_bar.png')"});
      $('caseknowledge_label').update('<a href="#" onclick="toggle_caseknowledge(\'' + div + '\', false, false); return false;">' + image_tag + '</a>');
    }
  } else {
    if (hide) {
      $('caseknowledge_tab').setStyle({ background: "url('images/multibook_tab_bar.png')"});
      $('caseknowledge_label').update('<a href="#" onclick="toggle_caseknowledge(\'' + div + '\', false, false); return false;">' + image_tag + '</a>');
    } else {
      $(div).show();
      $('caseknowledge_tab').setStyle({ background: "url('images/multibook_tab_bar_selected.png')"});
      $('caseknowledge_label').update('<a href="#" onclick="toggle_caseknowledge(\'' + div + '\', false, true); return false;">' + image_tag + '</a>');
      pos = $(div).cumulativeOffset();
      // $('contents').scrollTo(pos[0], pos[1]);
    }
  }  
}

function multibook_hit_basics(book_id) {
  if ((window['currenthit_' + book_id]) == undefined) { // create the variable
    window['currenthit_' + book_id] = 0;                // if doesn't exist
  }
  
  if ($('hit' + window['currenthit_' + book_id] + '_' + book_id) != null)   // unbold previous hit
    $('hit' + window['currenthit_' + book_id] + '_' + book_id).setStyle({ fontWeight: 'normal' });
}

function multibook_show_next_hit(book_id, hit_count) {
  multibook_hit_basics(book_id);
    
  if (window['currenthit_' + book_id] >= hit_count)
    window['currenthit_' + book_id] = 0; // alert("Moving to first hit");
  
  window.location.hash = "hit" + ++window['currenthit_' + book_id];
  $('current_hit_' + book_id).update(window['currenthit_' + book_id]);
  $('hit' + window['currenthit_' + book_id] + '_' + book_id).setStyle({ fontWeight: 'bold'});
}

function multibook_show_prev_hit(book_id, hit_count) {
  multibook_hit_basics(book_id);
  
  if (--window['currenthit_' + book_id] <= 0)
    window['currenthit_' + book_id] = hit_count;
  
  window.location.hash = "hit" + window['currenthit_' + book_id];
  $('current_hit_' + book_id).update(window['currenthit_' + book_id])
  $('hit' + window['currenthit_' + book_id] + '_' + book_id).setStyle({ fontWeight: 'bold'});
}

function multibook_show_all_hits(book_id) {
  var obj = $('show_all_' + book_id);
  var found = obj.classNames().find(function (i) { return i == 'one'})
  
  if (found != undefined) {
    $$("a.hits_" + book_id).each(function(s) {
      // skip the one that is the current hit
      if (s.id == 'hit' + window['currenthit_' + book_id] + '_' + book_id) return;
      s.setStyle({ fontWeight: 'normal' });
    });
    obj.update('Show All');
    obj.removeClassName('one');
  } else {
    $$("a.hits_" + book_id).each(function(s) {
      s.setStyle({ fontWeight: 'bold' });
    });
    obj.update('Show One');
    obj.addClassName('one');
  } 
}

// toggling method for master checkbox
function toggleMasterStateCheckBox() {
	checked = true;
	$$('.state_checkbox').each(function(id, index) {
		obj = $(id);
		if (!obj.checked) {
			checked = false;
		}
	});
	$('master_checkbox').checked = checked;
}

// toggling method for individual checkbox
function toggleStateCheckBoxes(){
	obj = $('master_checkbox');
	$$('.state_checkbox').each(function(id, index) {
		$(id).checked = obj.checked;
	});
}

// observing the toggling behaviors and responses
Event.observe(window, 'load', function() {
	['click', 'keypress'].each(function(e) {
		$$('.state_checkbox').each(function(id, index) {
			Event.observe($(id), e, function(evt) {
				toggleMasterStateCheckBox();
			});
		});
		Event.observe($('master_checkbox'), e, function(evt) {
			toggleStateCheckBoxes()
		});
	});
});


