/** \file
 * Klasse für den Objekt-Baum.
 *
 * @author "$Author: rr@nci.ch $"
 * @version "$Id: Konstanten.php 741 2010-06-01 07:51:55Z rr@nci.ch $"
 */



// Diese Funktion wird aufgerufen, wenn ein Element ausgewaehlt wird
function ot_nodeSelected(id)
{
	//alert('You selected: ' + id);

	if( id == 'NO_SELECTION' )
	{
		//var sURL='index.php?page=katalog/plain_artikelsuche&q=NO_SELECTION';
		var sURL='index.php?page=katalog/plain_noselection';
		cdatatable.reload(sURL);
		return;
	}

	if(this.selectImplicit)
	{
		var sURL='index.php?page=katalog/plain_artikelliste&id=' + id;
		cdatatable.reload(sURL);
	} else
	{
		top.location='index.php?page=katalog/home';
	}
}


function ObjectTree(rootelemid, treename, cssprefix) {

	this.eRoot=document.getElementById(rootelemid);
	this.selectedNode=null;
	this.selectImplicit=true;
	this.treename=treename?treename:this.eRoot.id;
	this.cssprefix=cssprefix?cssprefix:this.treename;

	this.urlprefix='index.php/REST/objecttree/';

	this.class_tree=this.cssprefix + '_tree';
	this.class_itemtext=this.cssprefix + '_itemtext';
	this.class_selecteditemtext=this.cssprefix + '_selecteditemtext';
	this.class_disableditemtext=this.cssprefix + '_disableditemtext';

	this.contenttag=null;

	this.img_closed='design/img/plus.gif';
	this.img_opened='design/img/minus.gif';
	this.img_leaf='design/img/square.gif';

	this.showDirectChildren=ot_showDirectChildren;
	this.addDirectChildren=ot_addDirectChildren;
	this.removeAllChildren=ot_removeAllChildren;
	this.selectNode=ot_selectNode;
	this.nodeSelected=ot_nodeSelected;
	this.setNodeStatus=ot_setNodeStatus;
	this.start=ot_start;
	this.EH=ot_EH;



	// init
	this.eRoot.id=this.treename;
	//this.showDirectChildren(this.eRoot);
	this.eRoot.onmousedown=this.EH;
	this.eRoot.ondblclick=this.EH;
	this.eRoot.onfocus=this.EH;
	this.eRoot.className=this.class_tree;


	/**
	 * Startet den Objekt-Baum.
	 * @param implicit_select bool   Auswahl auswerten und Inhalt der Kategorie anzeigen
	 */
	function ot_start(implicit_select)
	{
		this.selectImplicit=implicit_select;
		this.showDirectChildren(this.eRoot);
	}


	function ot_showDirectChildren(e, nid) {
		if(!e) return;
		if(nid == null) nid=e.getAttribute('id');
		var http=new HTTPRequest("GET", this.urlprefix + nid + "/directchildren");
		http.open();
		http.httpobj.setRequestHeader('Content-Type', 'text/xml; charset=UTF-8')
		http.httpobj.setRequestHeader('Accept', 'text/xml')
		http.cb4=this.addDirectChildren;
		http.caller=this;
		http.container=e;
		http.send();
	}

	function ot_addDirectChildren(http) {
		//alert(http.httpobj.responseText);

		var wait=0;

		dc=http.httpobj.responseXML.getElementsByTagName('directchildren')[0];

		if(!dc) alert('Error in response:\n' + http.httpobj.responseText);
		var items=dc.getElementsByTagName('node');
		for(var i=0; i < items.length; i++) {
			var item=items[i];

			var nid=item.getAttribute('myid');

			// LI-Element (root f�r Tree-Item)
			var newLI=document.createElement("li");
			newLI.id=nid;
			if(item.getAttribute('title')) newLI.title=item.getAttribute('title');
			http.container.appendChild(newLI);

			// Bild
			var newIMG=document.createElement("img");
			newLI.appendChild(newIMG);

			// A-Element (umschliesst den Text)
			var newA=document.createElement("a");
			newA.className=this.class_itemtext;
			if(item.getAttribute('href')) {
				newA.href=item.getAttribute('href');
			} else {
				newA.href='javascript:void(0)';
			}
			newLI.appendChild(newA);

			// Text
			var txt=item.firstChild.nodeValue;
			var newLItext=document.createTextNode(txt);
			newA.appendChild(newLItext);

			if( this.selectImplicit && (item.getAttribute('isselected') == 1) ) {
				this.selectNode(newLI, nid);
			}

			if( item.getAttribute('hassub') == 1 ) {
				// UL-Element (Container f�r Kind-Objekte)
				var newUL=document.createElement("ul");
				newLI.appendChild(newUL);
				
				if( item.getAttribute('isdisabled') == 1 ) {
					item.setAttribute('isopen', 0);
				}

				if( item.getAttribute('isopen') == 1 ) {
					newUL.style.display='list-item';
					newIMG.src=this.img_opened;
					this.showDirectChildren(newUL, nid);
					wait++;
				} else {
					newUL.style.display='none';
					newIMG.src=this.img_closed;
					this.removeAllChildren(newUL);
				}
			} else {
				newIMG.src=this.img_leaf;
			}
				
			if( item.getAttribute('isdisabled') == 1 ) {
				newLI.disabled=true;
				newA.className=this.class_disableditemtext;
			}
		}

		if( this.selectImplicit && (wait == 0) && (this.selectedNode == null) ) {
			this.nodeSelected('NO_SELECTION');
		}
	}

	function ot_removeAllChildren(e) {
		while(e.hasChildNodes()) {
			var child=e.firstChild;
			e.removeChild(child);
		}
	}

	function ot_selectNode(myLI, nid) {
		var myA=null;
		if(this.selectedNode != null) {
			// A-Element finden
			myA=this.selectedNode.getElementsByTagName("a");
			if(myA) {
				myA=myA.item(0);
			}
			myA.className=this.class_itemtext;
		}
		this.selectedNode=myLI;
		// A-Element finden
		myA=this.selectedNode.getElementsByTagName("a");
		if(myA) {
			myA=myA.item(0);
		}
		myA.className=this.class_selecteditemtext;
		this.nodeSelected(nid)
	}
	
	function ot_setNodeStatus(nid, opt, val) {
		//alert(this.urlprefix + nid + "/" + opt + "/" + val);
		var http=new HTTPRequest("POST", this.urlprefix + nid + "/" + opt + "/" + val);
		http.open();
		http.httpobj.setRequestHeader('Content-Type', 'text/xml; charset=UTF-8')
		http.httpobj.setRequestHeader('Accept', 'text/xml')
		/*http.cb4=function(http) {
			alert(http.httpobj.responseText);
		}*/
		//http.caller=this;
		http.send();
	}

	function ot_EH(e) {
		if(this != arguments.callee._oScope){
			return arguments.callee.apply(arguments.callee._oScope, arguments);
		};


		// event-Object holen
		if(!e) var e=window.event || arguments.callee.caller.arguments[0];
		// target-Element holen
		var target;
		if (e.target) target = e.target;
		else if (e.srcElement) target = e.srcElement;
		if (target.nodeType == 3) // defeat Safari bug
			target = target.parentNode;

		// Node-ID und LI-Element finden
		var nid='';
		var elem=target;
		var myLI=null;
		while( (elem.tagName) && (nid == '') ) {
			if(elem.tagName == 'LI') {
				nid=elem.id;
				myLI=elem;
			}
			elem=elem.parentNode;
		}

		// UL-Element finden
		var myUL=myLI.getElementsByTagName("ul");
		if(myUL) {
			myUL=myUL.item(0);
		}

		// IMG-Element finden
		var myIMG=myLI.getElementsByTagName("img");
		if(myIMG) {
			myIMG=myIMG.item(0);
		}

		var command=e.type + '_' + target.tagName;
		//alert('event\n' + 'command: ' + command + '\nNode-ID: ' + nid);

		if(myLI.disabled) return;

		switch(command) {
			case 'dblclick_A':
			case 'mousedown_IMG': // Oeffnen / Schliessen
				if(myUL) {
					if(myUL.style.display == 'none') {
						myUL.style.display='list-item';
						myIMG.src=this.img_opened;
						this.showDirectChildren(myUL, nid);
						this.setNodeStatus(nid, 'open', null);
					} else {
						myUL.style.display='none';
						myIMG.src=this.img_closed;
						this.removeAllChildren(myUL);
						this.setNodeStatus(nid, 'close', null);
					}
				}
				break;
			case 'focus_A': // Select
			case 'mousedown_A': // Select
				this.selectNode(myLI, nid);
				this.setNodeStatus(nid, 'select', null);
				break;
			default:
				return true;
				break;
		}
	}
	// setting reference
	this.EH._oScope=this;

}


