/**
 * AdminMenu()
 * Erzeugt ein dynamisches Menue, in dem ein DIV mit den 
 * Menueinhalt (Liste mit Links) angelegt und ins Dokument 
 * eingehangen wird.
 *
 * version 0.2 (last mofified: 22.01.2008)
 *
 * - bestimme optimale Groesse des Menues via offsetWidth|Height
 * - schliesse Menu bei onmousout-Event
**/
AdminMenu = function() {

	//CSS
	this.finalOpacity = 1;
	this.finalWidth  = 180; // Wert hier setzen oder in setVisible() setzen lassen
	this.finalHeight = 0;

	//Der Hauptcontainer
	this.mainDIV = document.createElement("div");
	this.mainDIV.id = "javBoardAdminMenu";
	this.mainDIV.style.position = "absolute";

	this.mainDIV.Instanz = this;
	this.mainDIV.onmouseout = function(e) {
		// Event-Bubble, da das ueberfahren von Child-Elementen den
		// mouseout-Event feuert. 
		// http://forum.de.selfhtml.org/archiv/2006/11/t140567/#m913521
		if(!this.parentNode) 
			return;
		if(!e) 
			e = window.event;
			
        var related = e.relatedTarget || e.toElement;
        while (related && related != this) {
			related = related.parentNode;
            if (related == this)
				return;
		}
		if (related != this) //Modifiziert: Scheint trotzdem noetig zu sein
			this.Instanz.setVisible(false);
	};
	
	
/*
> - Thema löschen --> TopciEdit(ID, 'DeleteRestore') 
> - Thema aktivieren --> TopciEdit(ID, 'AktivateReActivate')
> - Thema schließen --> TopciEdit(ID, 'DeleteRestore')
> - Thema oben festhalten --> TopciEdit(ID, 'Sticky')
> - Präfix bearbeiten --> TopciEdit(ID, 'EditPrefix')
> - Titel umbenennen --> TopciEdit(ID, 'ThreadTopic')
*/	
	
	//Das Menue
	this.threadOptions = new Object();	
	this.threadOptions.comDelete = document.createElement("a");
	this.threadOptions.Delete    = document.createElement("a");
	this.threadOptions.Activ     = document.createElement("a");
	this.threadOptions.Close     = document.createElement("a");	
	this.threadOptions.Prefix    = document.createElement("a");
	this.threadOptions.Topic     = document.createElement("a");
	this.threadOptions.Pinned    = document.createElement("a");
	this.threadOptions.Dones     = document.createElement("a");
	
	/*
	this.threadOptions.comDelete.icon = "./images/menuIcons/isDelete.png";
	this.threadOptions.Delete.icon    = "./images/menuIcons/isDelete.png";
	this.threadOptions.Activ.icon     = "./images/menuIcons/isActiv.png";
	this.threadOptions.Close.icon     = "./images/menuIcons/isClose.png";	
	this.threadOptions.Prefix.icon    = "./images/menuIcons/isClose.png";
	this.threadOptions.Topic.icon     = "./images/menuIcons/isClose.png";	
	this.threadOptions.Pinned.icon    = "./images/menuIcons/isSticky.gif";
	this.threadOptions.Dones.icon     = "./images/menuIcons/isDone.gif";
	*/
	this.threadOptions.comDelete.msg = [" ", "Endgültig Löschen"];
	this.threadOptions.Delete.msg    = ["Löschen", "Wiederherstellen"];
	this.threadOptions.Activ.msg     = ["Aktivieren", "Deaktivieren"];
	this.threadOptions.Close.msg     = ["Schliessen", "Öffnen"];	
	this.threadOptions.Prefix.msg    = ["Präfix bearbeiten", "Präfix bearbeiten"];
	this.threadOptions.Topic.msg     = ["Titel umbenennen", "Titel umbenennen"]; 
	this.threadOptions.Pinned.msg    = ["Oben anheften", "Lösen"]; 
	this.threadOptions.Dones.msg     = ["als Erledigt markieren", "nicht Erledigt"]; 
    
	this.threadOptions.comDelete.key = "isDelete";
	this.threadOptions.Delete.key    = "isDelete";
	this.threadOptions.Activ.key     = "isActiv";
	this.threadOptions.Close.key     = "isClose";	
	this.threadOptions.Prefix.key    = "isPrefix";
	this.threadOptions.Topic.key     = "isTopic"; 
	this.threadOptions.Pinned.key    = "isPinned"; 
	this.threadOptions.Dones.key     = "isDone"; 
	
	this.threadOptions.comDelete.value = "CompleteDelete";
	this.threadOptions.Delete.value    = "DeleteRestore";
	this.threadOptions.Activ.value     = "ActivateReActivate";
	this.threadOptions.Close.value     = "OpenClose";	
	this.threadOptions.Prefix.value    = "EditPrefix";
	this.threadOptions.Topic.value     = "ThreadTopic";
	this.threadOptions.Pinned.value    = "Sticky";
	this.threadOptions.Dones.value     = "MarkAsDone";
  
	this.threadOptions.comDelete.permissions = "candelcompleteThread";
	this.threadOptions.Delete.permissions    = "candelThread";
	this.threadOptions.Activ.permissions     = "canactivateThread";
	this.threadOptions.Close.permissions     = "canopencloseThread";	
	this.threadOptions.Prefix.permissions    = "caneditprefixThread";
	this.threadOptions.Topic.permissions     = "canrenameThread";	
	this.threadOptions.Pinned.permissions    = "canstickyThread";	
	this.threadOptions.Dones.permissions     = "candoneThread";	
    
	this.threadOptions.comDelete.appendChild(document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Delete.appendChild(document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Activ.appendChild( document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Close.appendChild( document.createTextNode( String.fromCharCode(160) ));	
	this.threadOptions.Prefix.appendChild( document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Topic.appendChild( document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Pinned.appendChild( document.createTextNode( String.fromCharCode(160) ));
	this.threadOptions.Dones.appendChild( document.createTextNode( String.fromCharCode(160) ));
    
	//keine Mehrfach Initialisierung
	this.isInitialized = false;	
	
	/**
	* init()
	* @return: void
	* Adminmenue erzeugen und mit Grundfunktionen ausstatten 
	* Menue ist unsichtbar!
	**/
	
	this.init = function() {
		if (this.isInitialized)
			return;

		// Erzeugt Liste, die Links ausnimmt
		var uList = document.createElement("ul");
		var ii=1;
		for (var el in this.threadOptions){
			if (window.perm[ this.threadOptions[el].permissions ] == 1) {
				var listEl = document.createElement("li");
				listEl.style.listStyleImage = "url("+this.threadOptions[el].icon+")";
				listEl.appendChild(this.threadOptions[el]);
				uList.appendChild(listEl);
			}
		}		
		
		this.mainDIV.appendChild( uList );   

		// Suche alle SPANs im Dokument mit num. ID	
		var docSpans = document.getElementsByTagName("span");    		
		for (var i=0; i<docSpans.length; i++){      
			if (/\d+/.test(docSpans[i].id)) {            
				docSpans[i].topic = topic[docSpans[i].id];
				docSpans[i].Instanz = this;
      
				// Fuege Event-Handler hinzu
				// Zeige beim Klick das Menue 
				// an Klick- bzw. Mausposition	
				docSpans[i].onclick = function(e)
				{	
					if (this.topic != undefined && this.topic['isEdit'] == 1)
					{
						var m_pos = getMousepos(e);
						this.Instanz.mainDIV.style.left = m_pos.scrollLeft+5+"px";
						this.Instanz.mainDIV.style.top  = m_pos.scrollTop-15 +"px";						
						this.Instanz.setOptions(this.topic); 					
						this.Instanz.setVisible(true);
					}
				};     
			}			
		}		

		//this.setVisible(false);	
		document.getElementsByTagName("body")[0].appendChild(this.mainDIV);	
		this.isInitialized = true;
	}
	
	/**
	* setVisible()
	* @param: <Boolean>setVisible
	* @return: void
	* Legt Sichtbarkeit des Menues fest
	* 
	**/
	this.setVisible = function(setVisible){
		this.mainDIV.style.display = setVisible?"block":"none";
		this.finalHeight = this.finalHeight==0?this.mainDIV.offsetHeight:this.finalHeight;
		this.finalWidth  = this.finalWidth==0?this.mainDIV.offsetWidth+10:this.finalWidth;
		this.fadeIn();
	}
	
	/**
	* setOptions()
	* @param: <Thread>t
	* @return: void
	* aendert die Eintraege im Menue
	*	+ Linktitel
	*	+ Linkziel
	* 
	**/	
	this.setOptions = function(t){
		for (var el in this.threadOptions){
			for (var tOptions in t) {        
        if (this.threadOptions[el].key == tOptions && window.perm[ this.threadOptions[el].permissions ] == 1) {				
        //Ermittle den Status aus Thread t, um Linktext anzupassen.
					//zulässiges Intervall [0,1]
					var status = parseInt(t[this.threadOptions[el].key]);
					status = (isNaN(status) || status<0 || status>1) ? 0 : status;					

					//Linktext anpassen
					this.threadOptions[el].firstChild.replaceData(0, this.threadOptions[el].firstChild.nodeValue.length, this.threadOptions[el].msg[ status ]); 
					//Title-Attribut setzen
					this.threadOptions[el].title = this.threadOptions[el].msg[ status ]; 
					//HREF-hinzufuegen
					this.threadOptions[el].href = "javascript:TopicEdit("+ t['isFolder'].replace(/\D+/, '') +", '"+this.threadOptions[el].value+"','thread')";
					//automatisches Schliessen nach Klick
					this.threadOptions[el].Instanz = this;
					this.threadOptions[el].onclick = function(e) { this.Instanz.setVisible(false); };
				}
			}
		}
	}
	
	/**
	* fadeIn()
	* @param: <Double>opac
	* @return: void
	* @uri: http://www.dustindiaz.com/sweet-titles/ 
	* @modified: Michael Loesler
	* laesst das Menue einfaden
	* 
	**/
	this.fadeIn = function (opac) {
		opac = opac || 0;
		var newOpac   = opac + 0.1;
		var newWidth  = parseInt(this.finalWidth*newOpac);
		var newHeight = parseInt(this.finalHeight*newOpac);
		if (newOpac < this.finalOpacity) {
			this.mainDIV.style.opacity = newOpac;
			this.mainDIV.style.filter = "alpha(opacity:" + (newOpac * 100) + ")";
			this.mainDIV.style.height = newHeight+"px";
			this.mainDIV.style.width  = newWidth+"px";
			window.setTimeout(function () {
				adminMenu.fadeIn(newOpac);
			}, 30);

		} else {
			this.mainDIV.style.opacity = this.finalOpacity;
			this.mainDIV.style.filter = "alpha(opacity:" + (this.finalOpacity * 100) + ")";
			this.mainDIV.style.height = this.finalHeight+"px";
			this.mainDIV.style.width  = this.finalWidth+"px";
		}
	}
	
}


/**
* getMousepos()
* @param: <Event>evt
* @return: <Objekt>pos
* @url: http://javascript.jstruebig.de/javascript/54/
* Ermittelt die Mauskkordinaten im Dokument
* .top / .left             - Position im Fenster
* .scrollTop / .scrollLeft - Position im Dokument
**/	
getMousepos = function (evt){
    if(!evt) evt = window.event;
    var pos = new Object();
    pos.left = evt.clientX;
    pos.top = evt.clientY;
    var b = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ? window.document.documentElement : window.document.body || null;
    if (b) {
        pos.scrollLeft= pos.left + b.scrollLeft;
        pos.scrollTop = pos.top + b.scrollTop + 10;
    }
    else if(document.layers) {
        // Netscape 4.
        pos.scrollLeft = evt.pageX;
        pos.scrollTop = evt.pageY;
        pos.left = evt.pageX - window.pageXOffset;
        pos.top = evt.pageY - window.pageYOffset;
    }
    return pos;
}

/**
  * Funktion starten beim Laden des Dokumentes
  *
  */
	
var adminMenu = new AdminMenu();

var isDOMContentLoaded = false;
function addContentLoadListener () {
	if (document.addEventListener) {
	var DOMContentLoadFunction = function () {
		isDOMContentLoaded = true;
		adminMenu.init();
	};
	document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
}
var oldonload = (window.onload || new Function());
	window.onload = function () {
		if (!isDOMContentLoaded) {
			oldonload();
			adminMenu.init();
		}
	};
}
addContentLoadListener();
