/* ===========================================================================
//	lib_themefinder3.js															
// ======================================================================== */

var themefinderObjects = {
	/* here's an example of the object *** 
	TF_1	: {
				CURRENT_BOX		: 1,			// current box number 
				PATH			: ["ROOT"],		// current theme path
				PATH_POINTER	: 0,			// current pointer (path index in first box!)
				CURRENT_OBJKEY	: "",			// current object key 
				EXAMPLE@L_n : {					// key@level_number
						"Arts":"Arts",
						"Artifical_Intelligence":"Artifical Intelligence",
						"Communication":"Communication",
						},
			}
	*/
}

var themefinder = {
	DEBUG			: 0,
	DEBUG_MSG		: 0,
	/* ROOT_OBJKEY		: "ROOT@0", */
	ROOT_OBJKEY		: "@0",
	EOP_MARKER		: "__EOP__",
	NUM_OF_BOXES	: 3,
	LEVEL_DEPTH		: 3,
	CURRENT_TF_ID	: "",
	IS_LOADING		: 0,
	PRESETS			: new Array(),
	
	POPUP_URL		: "_popup_themefinder_needhelp.html",
	POPUP_W			: 450,
	POPUP_H			: 500,
	
	getCurrentObjKey 		: function() { return themefinderObjects[this.CURRENT_TF_ID]['CURRENT_OBJKEY']; },
	getObjKeyName 			: function(objKey) {
								if(!objKey) { objKey=themefinderObjects[this.CURRENT_TF_ID]['CURRENT_OBJKEY']; }
								return objKey.split("@")[0];
							},
	getObjKeyLevel 			: function(objKey) {
								if(!objKey) { objKey=themefinderObjects[this.CURRENT_TF_ID]['CURRENT_OBJKEY']; }
								return objKey.split("@")[1];
							},
	getCurrentBox 			: function() { return themefinderObjects[this.CURRENT_TF_ID]['CURRENT_BOX']; },
	getPath 				: function() { return themefinderObjects[this.CURRENT_TF_ID]['PATH']; },
	getPathLength			: function() { return themefinderObjects[this.CURRENT_TF_ID]['PATH'].length; },
	getPathPointer			: function() { return themefinderObjects[this.CURRENT_TF_ID]['PATH_POINTER']; },
	getPathPointerObject 	: function(ind) {
								if(ind==null) { ind = this.getPathPointer(); }
								if(ind=="") { ind = '0'; }
								return themefinderObjects[this.CURRENT_TF_ID]['PATH'][ind];
							},
	getPathAsString			: function(lastObjKeyName) {
								var a = [];
								var p = themefinderObjects[this.CURRENT_TF_ID]['PATH'];
								for(var i=0; i<p.length; i++) {
									if(p[i] == this.ROOT_OBJKEY) { continue; }
									a.push(this.getObjKeyName(p[i]));
								}
								if(lastObjKeyName && lastObjKeyName != a[a.length-1]) {
									a.push(lastObjKeyName);
								}
								return a.join("|");
							},
	
	createObjKey			: function(objKey) { themefinderObjects[this.CURRENT_TF_ID][objKey]={}; },
	createObjKeyObject		: function(key,value) { themefinderObjects[this.CURRENT_TF_ID][this.getCurrentObjKey()][key] = value; },
	
	setCurrentObjKey		: function(objKey) { themefinderObjects[this.CURRENT_TF_ID]['CURRENT_OBJKEY']=objKey; },
	setCurrentBox			: function(box) { themefinderObjects[this.CURRENT_TF_ID]['CURRENT_BOX']=box; },
	setPathLength			: function(ind) { themefinderObjects[this.CURRENT_TF_ID]['PATH'].length=ind; },
	setPathPointer			: function(ind) { themefinderObjects[this.CURRENT_TF_ID]['PATH_POINTER']=ind; },
	
	getBoxPdId				: function(box) { return this.CURRENT_TF_ID+'#'+box+'S'; },
	getThemeFieldId			: function() { return this.CURRENT_TF_ID+'#Theme'; },
	getThemePathFieldId		: function() { return this.CURRENT_TF_ID+'#ThemePath'; },
	getThemeNameFieldId		: function() { return this.CURRENT_TF_ID+'#ThemeName'; },
	getThemeNamePreFieldId	: function() { return this.CURRENT_TF_ID+'#ThemeNamePrefix'; },
	
	getThemefinderObjects	: function() {
								var msg = "";
								for(var o in themefinderObjects[this.CURRENT_TF_ID]) {
									msg += o+": "+themefinderObjects[this.CURRENT_TF_ID]+"\n";
								}
								alert(msg);
							},
	
	showPath				: function() {
								if(this.DEBUG) {
									var s = "PATH is \n\n";
									var pathArray = this.getPath();
									for(var i=0; i<pathArray.length; i++) {
										if(this.getPathPointer()==i) {
											s += " > *"+pathArray[i]+"*"+"\n";
										} else {
											s += " > "+pathArray[i]+"\n";
										}
									}
									alert(s);
								}
							},
	
	showDebugMsg		: function(s) {
								if(this.DEBUG_MSG) {
									if(typeof(document.getElementById('debug'))=='object') {
										document.getElementById('debug').value += ((s)?s:'')+"\n";
									}
								}
							},
	
	/* load data for the first box */
	init			: 	function(themeFinderId) {
							if(!themeFinderId) { return; }
							themeFinderId = "TF_"+themeFinderId;
							
							/* create new themefinder object */
							this.createThemeFinderObject(themeFinderId);
							
							/* delete values of 'theme display' */
							if(document.getElementById(this.getThemeNamePreFieldId())) {
								document.getElementById(this.getThemeNamePreFieldId()).style.display = 'none';
							}
							if(document.getElementById(this.getThemeNameFieldId())) {
								document.getElementById(this.getThemeNameFieldId()).firstChild.data = ' ';
							}
							
							/* load data */
							this.load(themeFinderId,'','');
							
							/* check if presets available */
							if(this.PRESETS.length > 0) {
								this.checkPresets();
							}
						},
	
	/* init presets */
	checkPresets	:	function() {
							for(var a=0; a<this.PRESETS.length; a++) {
								var id = this.PRESETS[a][0];
								var path = this.PRESETS[a][1] || "";
								var outputObjKey = this.PRESETS[a][2] || "";
								var outputObjName = this.PRESETS[a][3] || "";
								var outputObjNamePrefix = this.PRESETS[a][4] || "";
								this.initPreset(id, path, outputObjKey, outputObjName, outputObjNamePrefix);
							}
						},
	
	/* init the preset */
	initPreset		: 	function(initThemefinderId, initThemefinderPath, outputObjKey, outputObjName, outputObjNamePrefix) {
							var initThemefinderId = initThemefinderId || this.CURRENT_TF_ID;
							var outputObjKey = outputObjKey || this.CURRENT_ELEMENT_ID_THEME_KEY;
							var outputObjName = outputObjName || this.CURRENT_ELEMENT_ID_THEME_NAME;
							var outputObjNamePrefix = outputObjNamePrefix || this.CURRENT_ELEMENT_ID_THEME_NAME_PREFIX;
							
							var pToken = initThemefinderPath.split("|");
							var boxnum = 1;
							var level = 1;
							
							for(var p=0; p<pToken.length; p++) {
								var key = pToken[p]+"@"+level;
								this.highlightOption(boxnum, key);
								this.load(initThemefinderId,boxnum,key, outputObjKey, outputObjName, outputObjNamePrefix);
								if(boxnum < this.NUM_OF_BOXES) {
									boxnum++;
								}
								level++;
							}
						},
	
	/* highlight option in box */
	highlightOption	:	function(box,key) {
							var boxId = this.CURRENT_TF_ID+"#"+box+"S";
							var node = document.getElementById(boxId);
							
							for(var x=0; x<node.options.length; x++) {
								if(node.options[x].value == key) {
									node.options[x].selected = true;
								}
							}
						},
	
	
	/* create a new themefinder object */
	createThemeFinderObject : function(themeFinderId) {
							themefinderObjects[themeFinderId] = {};
							themefinderObjects[themeFinderId]['CURRENT_BOX'] = 1;
							themefinderObjects[themeFinderId]['PATH'] = new Array(); /* this.ROOT_OBJKEY */
							themefinderObjects[themeFinderId]['PATH_POINTER'] = 0;
							themefinderObjects[themeFinderId]['CURRENT_OBJKEY'] = "";
						},
	
	/* set styles of option tags */
	setOptionStyle	: 	function(node, selectedIndex) {
							
							/* let's keep the selected item as it looks like */
							return true;
							
							for(var i=0; i<node.length; i++) {
								if(i==selectedIndex) {
									if(!isSafari) {
										/* we won't deselect the selection... 
										node.selectedIndex = -1;
										*/
										node.options[i].className = "hi";
									} else {
										node.options[i].setAttribute("class", "hi", 0);
									}
								} else {
									node.options[i].className = "fake";
								}
							}
						},
	
	/* load object */
	load			: 	function(themeFinderId,box,objKey, outputObjKey, outputObjName, outputObjNamePrefix) {
							if(!themeFinderId) {
								if(this.DEBUG) { alert("*** ALARM *** \n\n"+"load --> no themeFinderId"); }
								return;
							}
							if(!themefinderObjects[themeFinderId]) {
								if(this.DEBUG) { alert("*** ALARM *** \n\n"+"load --> no object themefinderObjects[themeFinderId] -> "+themefinderObjects[themeFinderId]); }
								return;
							}
							
							/* lock */
							this.IS_LOADING = 1;
							
							this.CURRENT_TF_ID = themeFinderId;
							
							if(!box) { box = 1; }
							if(!objKey) {
								/*	no object key -> start with rootlevel 
								**	set box to 1 and clear all boxes! 
								*/
								box		= 1; 
								objKey	= this.ROOT_OBJKEY;
								for(var n=1; n<=this.NUM_OF_BOXES; n++) {
									this.clearBox(n);
								}
							}
							
							this.setCurrentBox(box);
							this.setCurrentObjKey(objKey);
							var objKeyName = this.getObjKeyName(objKey);
							
							
							/* output path into html element 
							
							sven: noho... we shell do this in updatePath !!! trust me ;-) 
							document.getElementById(this.getThemePathFieldId()).value = this.getPathAsString(objKeyName);
							*/
							
							/* output object key into html element */
							if(outputObjKey) {
								document.getElementById(outputObjKey).value = (objKey==this.ROOT_OBJKEY) ? "" : objKeyName;
							} else {
								document.getElementById(this.getThemeFieldId()).value = (objKey==this.ROOT_OBJKEY) ? "" : objKeyName;
							}
							
							var boxPdId = this.getBoxPdId(box);
							var boxPdNode = document.getElementById(boxPdId);
							var boxPdSelectedIndex = boxPdNode.selectedIndex;
							if(boxPdSelectedIndex >=0) {
								var boxPdSelectedText = boxPdNode.options[boxPdSelectedIndex].text;
								
								/* output object name into html element */
								if(outputObjName) {
									
									/* possibly more than one element-id here! */
									var outputObjNameToken = outputObjName.split(',');
									for(var t=0; t<outputObjNameToken.length; t++) {
										var node = document.getElementById(outputObjNameToken[t]).firstChild;
										node.data = boxPdSelectedText;
									}
									
									if(outputObjNamePrefix) {
										document.getElementById(outputObjNamePrefix).style.display = 'inline';
									}
								} else {
									document.getElementById(this.getThemeNamePreFieldId()).style.display = 'inline';
									var node = document.getElementById(this.getThemeNameFieldId()).firstChild;
									node.data = boxPdSelectedText;
								}
							}
							
							/* set style of box options */
							this.setOptionStyle(boxPdNode, boxPdSelectedIndex);
							
							/* if object does not exists, load it from server */
							if(typeof(this.getObj(objKey))=='object') {
								this.loadAhead();
							} else {
								this.loadFromServer();
							}
							
							return true;
						},
	
	loadAhead			: function() {
							
							/* ignore shifting, if LEVEL_DEPTH reached */
							if(this.getCurrentBox()==this.NUM_OF_BOXES && this.getPathLength()>=this.LEVEL_DEPTH) {
								return true;
							}
							
							/* update the path */
							this.updatePath();
							
							var objHashRef = this.getObj();
							if(typeof(objHashRef)!='object') { return; }
							
							/* clear all boxes right to the current box! */
							for(var i=(this.getCurrentBox()+1); i<=this.NUM_OF_BOXES; i++) {
								this.clearBox(i);
							}
							
							/* is objkey = end of path? exit now. */
							if(objHashRef[this.EOP_MARKER]) {
								return;
							}
							
							/* depending on the CURRENT_BOX... */
							if(this.getCurrentBox()==this.NUM_OF_BOXES) {
								/* last box arrived -> shift to the left */
								this.shiftToLeft();
							}
							
							/* set new box */
							if(this.getCurrentBox() < this.NUM_OF_BOXES && this.getCurrentObjKey() != this.ROOT_OBJKEY) {
								this.setCurrentBox(this.getCurrentBox()+1);
							}
							
							/* go, fill the box now */
							this.fillBox(objHashRef,null);
							
							/* unlock */
							this.IS_LOADING = 0;
						},
						
	/* load data from server */ 
	loadFromServer	:	function() {
							if(!this.getCurrentObjKey()) { 
								if(this.DEBUG) { alert("*** ALARM *** \n\n"+"loadFromServer --> no CURRENT_OBJKEY, start with ROOT KEY ("+this.ROOT_OBJKEY+")"); }
								this.setCurrentObjKey(this.ROOT_OBJKEY);
							}
							
							/* set name of new object (syntax: objectKey@level) */
							var createObject = this.getCurrentObjKey(); 
							var currentLevel = this.getObjKeyLevel(createObject);
							
							/* ask the server */
							/* var themeId = parseInt(this.getObjKeyName()); */
							var themeId = this.getObjKeyName();
							var response = (themeId=="" || themeId==null) ? dkWebService2.getSubclasses() : dkWebService2.getSubclasses(themeId);
							if(!response || response == "") {
								/* empty response, create object, mark as EOP (End Of Path) */
								this.createObjKey(createObject);
								this.createObjKeyObject(this.EOP_MARKER, this.EOP_MARKER);
								
							} else {
								/* create new keyObj, fill in the childs */
								currentLevel++;
								this.createObjKey(createObject);
								
								for(var o in response) {
									if(o=='toXmlRpc') { continue; }
									var cLabel = '';
									var cKey = '';
									for(var x in response[o]) {
										if(x == 'id') { cKey = response[o][x]; }
										if(x == 'label') { cLabel = response[o][x]; /* cLabel = cLabel.replace(/_/g,' '); */ }
									}
									/* alert("key: "+cKey+" --> "+cLabel); */
									cKey = cKey+"@"+currentLevel;
									themefinderObjects[this.CURRENT_TF_ID][createObject][cKey] = cLabel;
								}
							}
							
							/* go ahead */
							this.loadAhead();
						},
						
	/* load data from object */ 
	getObj			: 	function(objKey) {
							if(!objKey) { objKey = this.getCurrentObjKey(); }
							if(themefinderObjects[this.CURRENT_TF_ID][objKey]) {
								return themefinderObjects[this.CURRENT_TF_ID][objKey];
							}
						},
						
	/* check and update path */ 
	updatePath		: 	function() {
							var currentObjKey 	= this.getCurrentObjKey();
							var currentObjLevel	= this.getObjKeyLevel(currentObjKey) || 0;
							if(!currentObjKey) { return; }
							
							/*	check level of current theme / cut the path */
							if(currentObjLevel < this.getPathLength()) {
								this.setPathLength(currentObjLevel);
							}
							
							/* add new item, set pointer */
							themefinderObjects[this.CURRENT_TF_ID]['PATH'].push(currentObjKey);
							
							if(this.LEVEL_DEPTH >= this.getPathLength()) {
								/* make the scroll buttons visible or not. */
								this.showHideNavi();
							}
							
							/* output path into html element */
							var objKeyName = this.getObjKeyName(currentObjKey);
							document.getElementById(this.getThemePathFieldId()).value = this.getPathAsString(objKeyName);
						},
						
	/* show or hide navigation */
	showHideNavi	: 	function() {
							var pathL	= this.getPathLength();
							var pointer	= this.getPathPointer();
							
							/* check scroll-left-button */
							var linkIdL = this.CURRENT_TF_ID+"#NavLinkLeft";
							if(pointer>0) {
								document.getElementById(linkIdL).style.display = 'block';
							} else {
								document.getElementById(linkIdL).style.display = 'none';
							}
							
							/* check scroll-right-button */
							var linkIdR = this.CURRENT_TF_ID+"#NavLinkRight";
							if((pointer+this.NUM_OF_BOXES) < pathL) {
								document.getElementById(linkIdR).style.display = 'block';
							} else {
								document.getElementById(linkIdR).style.display = 'none';
							}
						},
	
	
	/* scroll left/right and fill empty box */
	scroll			: 	function(direction) {
							var pointer	= this.getPathPointer();
							
							if(direction=='left') {
								this.shiftToRight();
								var pointer	= this.getPathPointer();
								var objHashRef	= this.getObj(this.getPathPointerObject(pointer));
								var parentObj	= this.getPathPointerObject(pointer+1);
								this.fillBox(objHashRef,1,parentObj);
							}
							
							if(direction=='right') {
								this.shiftToLeft();
								var pointer	= this.getPathPointer();
								var objHashRef	= this.getObj(this.getPathPointerObject(pointer+this.NUM_OF_BOXES-1));
								var parentObj	= this.getPathPointerObject(pointer+this.NUM_OF_BOXES);
								this.fillBox(objHashRef,this.NUM_OF_BOXES,parentObj);
							}
						},
	
	/* shift boxes... */
	shiftToLeft		: 	function() {
							this.showDebugMsg(" <> shiftTo  LEFT");
							for(var b=1; b<this.NUM_OF_BOXES; b++) {
								var src = b+1;
								this.showDebugMsg(" << shifting "+src+" -> "+b);
								this.shiftBox(src,b);
							}
							this.showDebugMsg(" << shifting -> pointer = "+this.getPathPointer());
							this.setPathPointer(this.getPathPointer()+1);
							this.showDebugMsg(" << shifting -> pointer = "+this.getPathPointer());
							this.showHideNavi();
						},
						
	shiftToRight	: 	function() {
							this.showDebugMsg(" <> shiftTo  RIGHT");
							for(var b=this.NUM_OF_BOXES; b>1; b--) {
								var src = b-1;
								this.showDebugMsg(" >> shifting "+src+" -> "+b);
								this.shiftBox(src,b);
							}
							this.setPathPointer(this.getPathPointer()-1);
							this.showHideNavi();
						},
	
	/* shift box */
	shiftBox		: 	function(srcBox, destBox) {
							
							/* build id of pulldown field */ 
							var boxPdIdSrc	= this.getBoxPdId(srcBox);
							var boxPdIdDest = this.getBoxPdId(destBox);
							
							/* clear destination box */
							this.clearBox(destBox);
							
							/* switch box content */
							var srcNode		= document.getElementById(boxPdIdSrc);
							var destNode	= document.getElementById(boxPdIdDest);
							if(srcNode.hasChildNodes()) {
								var childs = srcNode.getElementsByTagName("option");
								for(var i=0; i<childs.length; i++) {
									var theClone = childs[i].cloneNode(true);
									destNode.appendChild(theClone);
								}
								
								/* copy the selection. be sure the pulldown is not 'multiple' */
								if(srcNode.selectedIndex >= 0) {
									destNode.selectedIndex = srcNode.selectedIndex;
								}
							}
						},
	
	/* fill the themelist into a box */
	fillBox			: 	function(objHashRef,box,sel) {
							if(box==null) { box = this.getCurrentBox(); }
							
							/* clear box first */
							this.clearBox(box);
							
							/* add childs to pulldown list */
							var boxPdId = this.getBoxPdId(box);
							var boxPdNode = document.getElementById(boxPdId);
							var i = 0;
							for(var k in objHashRef) {
								boxPdNode.options[boxPdNode.length] = new Option(objHashRef[k], k, false, false);
								if(sel && sel!='undefined') {
									if(sel==k) {
										if(!isSafari) {
											boxPdNode.options[boxPdNode.length-1].className = "hi";
										} else {
											boxPdNode.selectedIndex=i;
										}
									} else {
										boxPdNode.options[boxPdNode.length-1].className = "fake";
									}
									i++;
								}
							}
						},
	
	/* clear a box */
	clearBox		: 	function(box) {
							if(!box) {
								if(this.DEBUG) { alert("*** ERROR *** \n\n"+"clearBox --> no box to clear"); }
								return;
							}
							var boxPdId = this.getBoxPdId(box);
							document.getElementById(boxPdId).length = 0;
						},
	
	
	/* ----------------------------------------
	**	popup stuff 
	** --------------------------------------- */	
	popUp			: function(themeFinderId, outputObjKey, outputObjName, outputObjNamePrefix) {
						var url = this.POPUP_URL;
						
						/* park some values here, to find back from popup to the right tf */
						this.CURRENT_TF_ID = themeFinderId;
						this.CURRENT_ELEMENT_ID_THEME_KEY = outputObjKey;
						this.CURRENT_ELEMENT_ID_THEME_NAME = outputObjName;
						this.CURRENT_ELEMENT_ID_THEME_NAME_PREFIX = outputObjNamePrefix;
						
						lopo = window.open(url, 'lopo', 'toolbar=no,location=no,directories=no,menubar=no,scrollbars=yes,copyhistory=no, width='+this.POPUP_W+', height='+this.POPUP_H);
						window.lopo.focus();
					}
					
}



/* eof ==================================================================== */


