//$Id: notes.js 62 2010-02-04 21:20:23Z Папа $

//Have to put <DIV ID="sdfootnote*" style="display: none"> manually because of limitation described at http://prototypejs.org/api/element/show

//Default values to be overridden from the more specific script
var heightUnit = '%';
//Height of Content div when notes are hidden
var contentHeight = '100';
//Height of Footer div when
var footerHeight = '10';

var IsAllNotesHidden = true;

function nshow(a) {
  var hash = a.hash.substring(1);

	//if note is in other document, go to it via <a>
  if (!document.URL.replace(/#.*/, '').replace(/\\/g, '/').endsWith(a.pathname.replace(/\\/g, '/')))
    return true;

  //locate note
  var divFooter = $$('div#Footer')[0];
  var note = divFooter.select('a[name="' + hash + '"]')[0];
	
  var isInFooter = ($(a).ancestors().detect(function(n) { return n.tagName == 'DIV' && n.id == 'Footer' }))
  //if anchor is in divFooter, hide only this note on back click, otherwise hide all notes.
	note.observe('click', (isInFooter) ? hide_note : hide_notes);
	note.writeAttribute('href', '#' + a.name);

	show_note(note, isInFooter);
}

//The function runs after div#Footer is loaded
function ifNote() {
  //If opened with hash pointing to a note,
  if (!window.location.hash) 
		return;

	var note = $$('div#Footer')[0].select('a[name="' + window.location.hash.substring(1) + '"]')[0];
	if (!note)
		return;

	//scroll the note anchor into view
	$$('div#Content')[0].select('a[href="' + window.location.hash + '"]')[0].scrollIntoView();
	
	note.observe('click', (window.opener) ? function() { window.close(); } : hide_notes);
	note.writeAttribute('href', window.location.hash);
	show_note(note);
}

function show_note(note, add) {
  var divFooter = $$('div#Footer')[0];
  var divContent = $$('div#Content')[0];
	if (!add && !IsAllNotesHidden) {
    divFooter.select('div').each(Element.hide);
//    IsAllNoteHidden = true;
//    divFooter.setStyle({ height: 0 });
//    divContent.setStyle({ height: contentHeight + heightUnit });
  }

  note.ancestors()[1].show();
  IsAllNotesHidden = false;

  //show divFooter
  divFooter.setStyle({ height: footerHeight + heightUnit });
  divContent.setStyle({ height: contentHeight - footerHeight + heightUnit });
}

function hide_note(note) {
	$(this).ancestors()[1].hide();
	if (window.event)
		event.returnValue = false;
	return false;
}
function hide_notes() {
var divFooter = $$('div#Footer')[0];
divFooter.select('div').each(Element.hide);
IsAllNoteHidden = true;

divFooter.setStyle({ height: 0 });
$$('div#Content')[0].setStyle({ height: contentHeight + heightUnit });
if (window.event)
	event.returnValue = false;
return false;
}
