/*****************************************************************************
*	Pulldownmenu written in Javascript
*	(c) 2005 Jens Koehler
*	Tested width IE6SP1, Firefox 1.1, Opera 8.0 amd Safari 2.0
*****************************************************************************/

/*
Global Array which contains all Menus
Menu registeres itself to it
Needed for eventhandling
*/
var ARR_POPRIGHTMENUS = new Array;


/*
Class PRMenu (Pulldownmenu)
The Menu itself
Parameters:
 aid: id of element in HTML-Document
 arr_linktexts: array of labels for menuitems
 arr_linkurls: array of urls for menuitems
 ieresize: if not false, onresize of first body-element will be replaced in IE with location.reload
*/
function PRMenu(aid , txt, arr_linktexts, arr_linkurls, ieresize){
	if(!ieresize) {
		ieresize = true;
	}
	
	//minimal check
	if(arr_linktexts.length != arr_linkurls.length) {
		alert("Fehlerhafter Aufruf von pmenu!");
		return;
	}
	
	//properties
	//register self to array
	this.m_id = ARR_POPRIGHTMENUS.length;
	ARR_POPRIGHTMENUS[this.m_id] = this;

	//id of element in HTML-Document
	this.m_aid = aid;
	//array of labels for menuitems
	this.m_linktexts = arr_linktexts;
	//array of urls for menuitems
	this.m_linkurls = arr_linkurls;
	this.m_txt = txt;
	
	//state ("inactive", "active" [expanded, collapsed])
	this.m_state;

	//menu active
	this.m_menuactive;
	//menu inactive
	this.m_menuinactive;
	this.m_ieresize = ieresize;

	//eventhandlers
	var foverstring = "if(ARR_POPRIGHTMENUS[" + this.m_id + "] != null) ARR_POPRIGHTMENUS[" + this.m_id + "].onmouseover(e);";
	var foutstring = "if(ARR_POPRIGHTMENUS[" + this.m_id + "] != null) ARR_POPRIGHTMENUS[" + this.m_id + "].onmouseout(e);";
	this.m_onmouseover = new Function("e", foverstring); 
	this.m_onmouseout = new Function("e", foutstring); 
	this.onmouseover = mover;
	this.onmouseout = mout;

	//methods
	this.initView = initView;
	this.createActive = createActive;
	this.createInactive = createInactive;
	this.isPartOf = isPartOf;
	this.expand = expand;
	this.collapse = collapse;
	this.setX = setX;
	this.setY = setY;
	this.getX = getX;
	this.getY = getY;
	this.rightHoverOver =  rightHoverOver;
	this.rightHoverOut = rightHoverOut;

	//Initialisierung
	this.initView();
}

function initView(){
	this.m_state = "inactive";
	this.createInactive();
	this.createActive();
	//IE workaround
	if(navigator.userAgent.toLowerCase().indexOf("msie") + 1 && this.m_ieresize){
		document.getElementsByTagName("body")[0].style.position="relative";
	}
}

function createInactive(){
	this.m_menuinactive = document.createElement("div");
	this.m_menuinactive.className = "pbr_leftelement";
	this.m_menuinactive.style.cursor= "default";
	this.m_menuinactive.onmouseover = this.m_onmouseover;
	this.m_menuinactive.onmouseout = this.m_onmouseout;
	if(this.m_menuinactive.captureEvents) this.m_menuinactive.captureEvents(Event.MOUSEOVER);

	var nSpan = document.createElement("span");
	nSpan.className = "pbr_leftelement";
	var nTxt = document.createTextNode(this.m_txt);
	
	nSpan.appendChild(nTxt);
	this.m_menuinactive.appendChild(nSpan);

	this.m_menuinactive.style.visibility = "visible";
	document.getElementById(this.m_aid).appendChild(this.m_menuinactive);
}

function createActive(){
	this.m_menuactive = document.createElement("div");
	this.m_menuactive.className = "pframer_active";
	this.m_menuactive.onmouseout = this.m_onmouseout;
	this.m_menuactive.style.position = "absolute";
	//IE workaround...
	if(navigator.userAgent.toLowerCase().indexOf("msie") + 1) this.m_menuactive.style.backgroundImage = "url(1pxtrans.gif)";
	if(this.m_menuactive.captureEvents) this.m_menuactive.captureEvents(Event.MOUSEOUT);
		this.m_rightElements = new Array();

	for(var i=0; i<this.m_linktexts.length; i++){
		var nDiv = document.createElement("div");
		this.m_rightElements[i] = nDiv;
		
		if(this.m_linktexts.length-1 > i) {
			nDiv.className="pbr_active";
		}else{
			nDiv.className="pbr_lastactive";
		}
		
		var nA = document.createElement("a");	
		nA.className = "pbr_active";
		nA.href = this.m_linkurls[i];

		var fhoverstring = "if(ARR_POPRIGHTMENUS[" + this.m_id + "] != null) ARR_POPRIGHTMENUS[" + this.m_id + "].rightHoverOver(e);";
		var fhoutstring = "if(ARR_POPRIGHTMENUS[" + this.m_id + "] != null) ARR_POPRIGHTMENUS[" + this.m_id + "].rightHoverOut(e);";
		nA.onmouseover = new Function("e", fhoverstring); 
		nA.onmouseout = new Function("e", fhoutstring); 

		var nSpan = document.createElement("span");
		nSpan.className = "pbr_leftelement";
		nSpan.style.height = "100%";
		nSpan.style.width= "100%";
		nSpan.style.display= "block";
		nSpan.style.cursor= "pointer";
		var nTxt = document.createTextNode(this.m_linktexts[i]);
		nSpan.appendChild(nTxt);
		nA.appendChild(nSpan);
		nDiv.appendChild(nA);
		this.m_menuactive.appendChild(nDiv);
	}
	
	this.m_menuactive.style.visibility = "hidden";
	document.getElementById(this.m_aid).appendChild(this.m_menuactive);
}

function isPartOf(obj){
	if(this.m_menuactive == obj){
		return true;
	}
	if(isChild(this.m_menuactive, obj)){
		return true;
	}

	if(this.m_menuinactive == obj){
		return true;
	}
	if(isChild(this.m_menuinactive, obj)){
		return true;
	}
	
	return false;
}

function expand(){
	this.m_menuinactive.className="pbr_leftelement_hover";
	this.m_menuactive.style.visibility = "visible";
	this.m_state = "active";
	var mwidth = this.m_menuactive.firstChild.offsetWidth;
	this.m_menuactive.style.width = mwidth + "px";
}

function collapse(){
	this.m_menuinactive.className="pbr_leftelement";
	this.m_menuactive.style.visibility = "hidden";
	this.m_state = "inactive";
}

function setX(x){
	this.m_menuactive.style.top = x;
	this.m_menuinactive.style.top = x;
}

function setY(y){
	this.m_menuactive.style.left = y;
	this.m_menuinactive.style.left = y;
}

function getX(){
	return findPosX(this.m_menuactive);
}

function getY(){
	return findPosY(this.m_menuactive);
}

function mover(e){
	if (!e) var e = window.event;
	
	var esource = (e.target) ? e.target : e.srcElement;
	var relTarg;
	if (e.relatedTarget) relTarg = e.relatedTarget;
	else if (e.fromElement) relTarg = e.fromElement;

	for(var i = 0; i < ARR_POPRIGHTMENUS.length ; i++) {
		if(ARR_POPRIGHTMENUS[i] != null
			&& ARR_POPRIGHTMENUS[i].m_id != this.m_id) ARR_POPRIGHTMENUS[i].collapse();
	}

	if(this.m_state == "inactive") {
		this.expand();
	}
}

function mout(e){
	if (!e) var e = window.event;

	var esource = (e.target) ? e.target : e.srcElement;
	var relTarg;
	if (e.relatedTarget) relTarg = e.relatedTarget;
	else if (e.toElement) relTarg = e.toElement;
	
	if(this.m_state == "active") {
		if(!this.isPartOf(relTarg)){
			this.collapse();
		}
	}
}

function rightHoverOver(e)
{
	if(!e) var e = window.event;
	var esource = (e.target) ? e.target : e.srcElement;

	for(var i = 0;  i < this.m_rightElements.length; i++){
		if(isChild(this.m_rightElements[i], esource)){
			if(i == this.m_rightElements.length-1){
				this.m_rightElements[i].className = "pbr_lastactive_hover";
			}else{
				this.m_rightElements[i].className = "pbr_active_hover";
			}
		}else{
			if(i == this.m_rightElements.length-1){
				this.m_rightElements[i].className = "pbr_lastactive";
			}else{
				this.m_rightElements[i].className = "pbr_active";
			}
		}
	}
}

function rightHoverOut(e)
{
	if(!e) var e = window.event;
	var esource = (e.target) ? e.target : e.srcElement;

	for(var i = 0;  i < this.m_rightElements.length; i++){
		if(isChild(this.m_rightElements[i], esource)){
			if(i == this.m_rightElements.length-1){
				this.m_rightElements[i].className = "pbr_lastactive";
			}else{
				this.m_rightElements[i].className = "pbr_active";
			}
		}
	}
}

//returns true if child is child of parent
function isChild(parent, child){
	var iterator = parent.firstChild;
	while(iterator != null){
		if(iterator == child) return true;
		if(parent.firstChild!=null){
			if(isChild(iterator, child)){
				return true;
			}
		}
		iterator = iterator.nextSibling;
	}
	return false;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
