var defaultMenuWidth="150px"; //set default menu width.
var defaultMenuWidth2="300px"; //set default menu width.

////No need to edit beyond here

var ie5 = document.all && !window.opera
var ns6 = document.getElementById

if (ie5||ns6) {
	document.write('<div id="popitmenu" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>');
	document.write('<div id="popitmenu2" onMouseover="clearhidemenu();" onMouseout="dynamichide(event)"></div>')
}

function iecompattest(){
	return (document.compatMode && document.compatMode.indexOf("CSS")!=-1)? document.documentElement : document.body
}

function showmenu(e, which, optWidth) {
	if (!document.all&&!document.getElementById) {
		return
	}
	
	clearhidemenu();
	
	menuobj = ie5 ? document.all.popitmenu : document.getElementById("popitmenu");
	menuobj.innerHTML = which;
	menuobj.style.width = (typeof optWidth != "undefined") ? optWidth : defaultMenuWidth;
	menuobj.contentwidth = menuobj.offsetWidth;
	menuobj.contentheight = menuobj.offsetHeight;
	eventX = ie5 ? event.clientX : e.clientX
	eventY = ie5 ? event.clientY : e.clientY
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px"
//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
else
menuobj.style.top=ie5? iecompattest().scrollTop+event.clientY+"px" : window.pageYOffset+eventY+"px"
menuobj.style.visibility="visible"
return false
}


function showmenu2(e, which, optWidth){
if (!document.all&&!document.getElementById)
return
clearhidemenu()
menuobj=ie5? document.all.popitmenu2 : document.getElementById("popitmenu2")
menuobj.innerHTML=which
menuobj.style.width=(typeof optWidth!="undefined")? optWidth : defaultMenuWidth2
menuobj.contentwidth=menuobj.offsetWidth
menuobj.contentheight=menuobj.offsetHeight
eventX=ie5? event.clientX : e.clientX
eventY=ie5? event.clientY : e.clientY
//Find out how close the mouse is to the corner of the window
var rightedge=ie5? iecompattest().clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie5? iecompattest().clientHeight-eventY : window.innerHeight-eventY
//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px"
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.style.left=ie5? iecompattest().scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px"
//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.style.top=ie5? iecompattest().scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px"
else
menuobj.style.top=ie5? iecompattest().scrollTop+event.clientY+"px" : window.pageYOffset+eventY+"px"
menuobj.style.visibility="visible"
return false
}


function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
  while (b.parentNode)
    if ((b = b.parentNode) == a)
      return true;
  return false;
}

function hidemenu(){
  if (window.menuobj) {
    menuobj.style.visibility="hidden"
  }
}

function dynamichide(e){
  if (ie5&&!menuobj.contains(e.toElement)) {
    hidemenu();
  } else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget)) {
    hidemenu();
  }
}

function delayhidemenu(){
  delayhide = setTimeout("hidemenu()",500)
}

function clearhidemenu(){
  if (window.delayhide) {
    clearTimeout(delayhide)
  }
}

function toggleVis(obj) {
  obj.toggle();
}

//if (ie5||ns6)
//document.onclick=hidemenu

// drop that phpbb shit, here is mine
function toggleProsa() {
  $$(".prosa").each(function(o) { 
    o.toggleClassName("reading"); 
    o.toggleClassName("analysis") 
  });
}

function gotoProsaSection(e) {
  location.hash = this;
}

var Prosa = {
  sections: [],
  sectionButtons: [],
  lastSectionCheck: null,
  
  checkSection: function() {
    if (this.lastSectionCheck != null && ((new Date()).getTime() - this.lastSectionCheck < 250)) {
      return;
    }
    this.lastSectionCheck = (new Date()).getTime();
    // check scroll position
    var min = 10000;
    var mins = null;
    var sections = this.sections;
    for (var i = 0; i < sections.length; i++) {
      var s = sections[i];
      var t = document.body.scrollTop - s.cumulativeOffset().top;
      if (mins == null || t < min && t > 0) {
        min = t;
        mins = s;
      }
    }
    
    if (!mins) { 
      mins = sections[0];
    }

    this.sectionButtons.each(function(b) { 
      b[(b.prosaSection == mins ? 'add' : 'remove') + 'ClassName']('button_active');
    });
  },
  
  toggleDetails: function() {
    $$('.prosa_analysis').each(function(pa) {
      pa['slide' + (pa.visible() ? 'Up' : 'Down')]({ duration: 0.3 });
    });
    $$('.cloudoption').each(function(e) {e.toggle()});
  },
  
  toggleWordGroup: function(name) {
    var wg = $$('.prosa_details .wt_' + name);
    if (wg.length < 1) {
      return;
    }
    wg.each(function(e) {
      e[wg[0].visible() ? 'shrink' : 'grow']({
        direction: 'upper-left',
        duration:  0.6
      });
    });
  }
};

document.observe('dom:loaded', function() {
  if ($$(".prosa_section").length > 0) {
    var pp = $('prosa_panel');
    var ps = $('prosa_sections');

    pp.show();
    
    var secid = 0;
    Prosa.sections = $$('.prosa_section');
    Prosa.sections.each(function(sec) {
      var h = sec.select('.prosa_section_header')[0];
      var a = sec.select('a')[0];
      var mid = 'gtps_' + (secid++);
      ps.insert(
        '<div id="' + mid + '" class="button section_button">' + 
          '<div class="text">' + h.innerHTML + '</div>'+
        '</div><br />'
      );
      var b = $(mid);
      b.prosaSection = sec;
      b.observe('click', gotoProsaSection.bindAsEventListener(a.name));
    });
    Prosa.sectionButtons = $$('.section_button');
  }
  
  $$('.prosa .button').each(function(b) {
    b.observe('mouseenter', function(e) {
      this.addClassName('button_hover');
    }.bindAsEventListener(b));
    b.observe('mouseleave', function(e) {
      this.removeClassName('button_hover');
    }.bindAsEventListener(b));
  });
  
  Event.observe(document, 'scroll', Prosa.checkSection.bindAsEventListener(Prosa));
});

