/*
'*********************************************************************************
' Aurigma Deep Tree 2.0 (c), Fedor Skvortsov, Aurigma Inc. 2001-2003
' Mailto: support@aurigma.com
' WWW: http://www.aurigma.com
'
' This script and his components are free, but must be
' maintained this text and the copyright to use it.
'
'*********************************************************************************
*/
function ADTree_ctor(RootTree, Tree, IFrame, ImagesPath, ObjectName ) {
	// RootTree - url to data for tree's first level
	// Tree - id of tree container
	// SourceFrame - id of inner frame, where data will be loaded
	// ObjectName MUST be equal to the name of created object,
	// ImagesPath - path to state images 
	// eg:
	// var mytree = new ADTree_ctor('/tree/source.htm', 'tree_id', 'frame_id', '/images/', 'mytree')
	this.sObjectName = ObjectName;
	
	this.sRootTree = RootTree;
	this.sIFrame = IFrame;
	this.sTree = Tree; 	
	
	//Images
	this.sImagesPath = ImagesPath;
	this.sImageNone = this.sImagesPath + "none.gif";
	this.sImagePlus = this.sImagesPath + "plus.gif";
	this.sImageMinus = this.sImagesPath + "minus.gif";

	//Styles
	this.sNodeLoadingClass = "BeingLoadedNode";
	this.sNodeClass = "TreeNode";
	this.sNodeInActive = "#ECF0F4";
	this.sNodeActive = "orange";
	
	//Text
	this.sCancelLoading = "Wczytywanie...";
	
	//Timeout during which node will be loaded
	this.timeOut = 60000;
	
	//Selected node
	this.oSelNode;
	
	//Curent loading node. Only one node can be being loading at one moment!*/
	this.beingLoadedNode;
	
	//Current node counter. We use it for generation unique ID
	this.nodeId = 0;
	
	// id of loading branch
	this.sBranchId;
	
	// methods
	this.initPage = m_initPage;
	this.processLoading = m_processLoading;
	this.showBeingLoadedNode = m_showBeingLoadedNode;
	this.hideBeingLoadedNode = m_hideBeingLoadedNode;
	this.cancelLoadByTimeout = m_cancelLoadByTimeout;
	this.cancelLoad = m_cancelLoad;
	this.actionOnClick = m_actionOnClick;
	this.buildTree = m_buildTree;
	this.refreshNode = m_refreshNode;
	this.actionOnHref = m_actionOnHref;
	this.afterHref = m_afterHref;	
}

//Init data and start loading root tree
function m_initPage(){
	oSelNode = null;
	// if root url is known, begin loading nodes
	if( this.sRootTree != '' ) {
		//set beingLoadedNode to the tree container
		this.beingLoadedNode = document.getElementById(this.sTree);
		this.processLoading(this.sRootTree);		
	} else {
		this.beingLoadedNode = null;
	}
}

//Load subtree into hidden frame
function m_processLoading(sTreeURL){	
	window.frames[this.sIFrame].location = sTreeURL;
	//We will cancel loading of tree if it willn't have success during timeout
	window.setTimeout(new Function(this.sObjectName + ".cancelLoadByTimeout('" + this.beingLoadedNode.id + "')"), this.timeOut);
}

//Show hint [Loading...]
function m_showBeingLoadedNode(){
	var oDIV = this.beingLoadedNode.appendChild(document.createElement("div"));
	oDIV.className = this.sNodeLoadingClass;
	var oA = oDIV.appendChild(document.createElement("a"));
	oA.href = "#";
	oA.title = "Kliknij aby anulować..."
	oA.onclick = this.cancelLoad;	
	var	oText = oA.appendChild(document.createTextNode(this.sCancelLoading));
}

//Hide hint [Loading...]
function m_hideBeingLoadedNode(){	
	if (this.beingLoadedNode.childNodes.length>1){
		var oDIV = this.beingLoadedNode.childNodes[1];			
		this.beingLoadedNode.removeChild(oDIV); 
	}
}

//Cancel loading of node
function m_cancelLoadByTimeout(id){
	//Check if node is still  loaded
	if (this.beingLoadedNode!=null && this.beingLoadedNode.id==id){
		this.hideBeingLoadedNode();
		//Check whether its top container node
		if (this.beingLoadedNode.childNodes.length>0){
			this.beingLoadedNode.childNodes[0].childNodes[0].src = this.sImageNone;
			this.beingLoadedNode.childNodes[0].childNodes[0].onclick = null;
		}
		this.beingLoadedNode = null;	
		alert("Nie można załadować gałęzi.");			
	}
}

//This function handles OnClick event on hint [Loading...]
function m_cancelLoad(){
	this.hideBeingLoadedNode();
	//Check whether its top container node
	if (this.beingLoadedNode.childNodes.length>0){
		this.beingLoadedNode.childNodes[0].childNodes[0].src = this.sImagePlus;
	}
	this.beingLoadedNode = null;	
	return false;
}

//This function handle OnClick event on action image ([+] or [-]). 
//As parameter we pass id of associated node
function m_actionOnClick(id){
	
	var oNode = document.getElementById(id);

	//If node is already expanded we collapse it
	if (oNode.getAttribute("Expanded")=="true"){
		oNode.childNodes[1].style.display = "none";
		oNode.childNodes[0].childNodes[0].src = this.sImagePlus;
		oNode.setAttribute ("Expanded", "false")
	}
	//If node is collapsed we expand it
	else{
		//Show minus icon		
		oNode.childNodes[0].childNodes[0].src = this.sImageMinus;				
		
		//If children nodes are already loaded, just expand it
		if (oNode.getAttribute("LoadedChildren")=="true"){
			oNode.childNodes[1].style.display = "block";
			oNode.setAttribute ("Expanded", "true")
		}
		 //Children nodes are not loaded yet, we need to load it
		else{
			//If the other nodes are loaded at the current time, we should stop it
			if (this.beingLoadedNode!=null){
				this.hideBeingLoadedNode();
			}
			//Set new current being loaded node
			this.beingLoadedNode = oNode;
			//Show loading node
			this.showBeingLoadedNode();		
			//loading subtree

			this.processLoading(this.beingLoadedNode.getAttribute("src"));
		}		
	} 
	// KD - teraz wywolujemy specjalnie spreparowaną pauzę, 
	// która pozwoli odetchnąć interpreterowi JS i dokończyć mu 
	// odświeżać zmiany dokonywane dynamicznie.
	// gdyby nie to, że zaraz po onClick() wykonuje się przekierowanie 
	// dzięki href="..." - pewnie by zdążył, a tak... (kod funkcji poniżej, 
	// źródło w adt_komentarz.txt)
	pause(150);
}

function pause(numberMillis) {
        var dialogScript = 'window.setTimeout(' +
           ' function () { window.close(); }, ' + numberMillis + ');';
           
        // For IE5. KD: pod Firefox i Mozilla_1.6+ i tak działa...
        if(document.all)
        {
	        if (typeof document.body.style.maxHeight == "undefined")
	        {
		        // IE < 7
        		var result = window.showModalDialog(
           		'javascript:document.writeln(' +
            	'\'<script language=\"JavaScript\">' + dialogScript + '<' + '/script>\')');
        	}
		}
          
		/* For NN6, but it requires a trusted script.
         openDialog(
           'javascript:document.writeln(' +
            '"<script>' + dialogScript + '<' + '/script>"',
           'pauseDialog', 'modal=1,width=10,height=10');
 		*/
}

// function refereshes contents of active node
function m_refreshNode() {
	if( this.oSelNode != null ) {
		
		var oNode = document.getElementById(this.sBranchId);
		
		if( oNode.getAttribute("Expanded")=="true" ) {
			// node is expanded, we need to hide it	
			oNode.childNodes[1].style.display = "none";
			oNode.childNodes[0].childNodes[0].src = this.sImagePlus;
			oNode.setAttribute ("Expanded", "false");
		}
		
		if( oNode.getAttribute("LoadedChildren") == "true" ) {
			// if children were	loaded, then we must delete them
			oNode.removeChild(oNode.childNodes[1]);
			oNode.setAttribute ("LoadedChildren", "false");
		}
		
		// ok, node is collapsed and children were removed.
		// now we must read children from data source
		// If the other nodes are loaded at the current time, we should stop it
		if (this.beingLoadedNode!=null){
			this.hideBeingLoadedNode();
			return;
		}
		
		oNode.childNodes[0].childNodes[0].src = this.sImageMinus;
		
		//Set new current being loaded node
		this.beingLoadedNode = oNode;
		//Show loading node
		this.showBeingLoadedNode();		
		//loading subtree
		this.processLoading(this.beingLoadedNode.getAttribute("src"));
	} else {
		alert('There is no current node');	
	}
}

function m_afterHref() {
	// oznaczamy element jako wybrany
	var oTmp = document.getElementById(this.sBranchId);

	if( this.oSelNode != null ) {
		// remove selection from previously selected node
		this.oSelNode.childNodes[0].childNodes[1].style.color = this.sNodeInActive;
	}
	
	// remember current selected node
	this.oSelNode = oTmp;
	this.oSelNode.childNodes[0].childNodes[1].style.color = this.sNodeActive;	
}

function m_actionOnHref(id, url) {
	// generate new function	
	var f = new Function(url);	
	//alert( id);
	this.sBranchId = id;		
	// execute function
	f();
}

//Build tree. This function is called from hidden frame with new downloaded data,
//which are passed in oData parameter
function m_buildTree(oData){
	//Check whether loading was canceled
	if (this.beingLoadedNode!=null){
		this.hideBeingLoadedNode();
		//Get list of nodes
		var oNodes = oData.nodes;
		if (oNodes.length==0){
			if (this.beingLoadedNode.childNodes.length>0){
				this.beingLoadedNode.childNodes[0].childNodes[0].src = this.sImageNone;
				this.beingLoadedNode.childNodes[0].childNodes[0].onclick = null;
			}
		}
		else{
			var oLI, oUL, oIMG, oA, oText, oNOBR, i, t, re;
			var sIcon, sName, sHref, sTarget, sSrc;
			//Create container for child nodes
			oUL = this.beingLoadedNode.appendChild(document.createElement("ul"));	
			oUL.style.display="block";	
			//Run over nodes
			for (i=0;i<oNodes.length;i++){
				//Set obligatory values
				sIcon = oNodes[i].icon;
				sName =	oNodes[i].name;		
				//Set optional values
				if (oNodes[i].href!= null){
					sHref=oNodes[i].href;			
				}
				else{
					sHref="";					
				}
				if (oNodes[i].target!=null){
					sTarget=oNodes[i].target;
				}
				else{
					sTarget=""
				}
				//Create node
				oLI = oUL.appendChild(document.createElement("li"));	
				//Incerement unique node ID
				this.nodeId++;
				oLI.id = this.sObjectName + '_tn' + this.nodeId;
				//If src attribute is not empty, add custom attribute to the result node
				if (oNodes[i].src!=null){
					sSrc = oNodes[i].src;
				}
				else{
					sSrc = "";
				}
				oLI.setAttribute("src", sSrc);
				oNOBR = oLI.appendChild(document.createElement("nobr"));
				//Create action image [+], [-] [ ]
				oIMG = document.createElement("img");
				oIMG.border = 0;
				//If src attribute is not empty or amount of the child nodes is not equals zero
				if (sSrc!=""){
					//Sub nodes was not loaded and so not expanded
					oLI.setAttribute("LoadedChildren", "false");
					oLI.setAttribute("Expanded", "false");
					oIMG.src = this.sImagePlus;
					//Set action image event handler - dynamicaly assembly handler for
					//It should be like actionOnClick('tnXXXX')		
					oIMG.onclick = new Function(this.sObjectName+".actionOnClick('" + this.sObjectName+ "_tn" + this.nodeId + "')");	
				}		
				else{
					oIMG.src = this.sImageNone;
				}
				//Create icon image
				oIMG.width = 13;
				oIMG.height = 16;
				if (sIcon != null ) {
					oIMG = oNOBR.appendChild(oIMG);		
					//Creation of icon image
					oIMG = document.createElement("img");
					oIMG.border = 0;
					oIMG.src = this.sImagesPath + sIcon + ".gif";	
					oIMG.width = 16;
					oIMG.height = 16;		
				}
				oIMG = oNOBR.appendChild(oIMG);
				//Create link
				oA = document.createElement("a");
				if (sTarget!=""){
					oA.target = sTarget;		
				}
				
				// remove possible escaping
				re = /\+/gi;
				t = unescape(sName);
				sName = t.replace(re, " ");

				oA.title = unescape(sName);		
				oA.className = "treenode";
				oA = oNOBR.appendChild(oA);
				oText = oA.appendChild(document.createTextNode(sName));
				if (sHref!=""){
					//Combine full path from baseHref and relative path
					//If you want to use full pathes (not relative) just
					//assing to baseHref empty string
					
					// href został zmodyfikowany tak, aby wywoływał własną funkcję, której jednym 
					// argumentem jest funkcja/link przekazana przez oData a drugim id naszego node'a
					//oA.href = oData.baseHref + sHref;
															
					oA.href = "javascript:void(" + this.sObjectName + ".actionOnHref('" + this.sObjectName + "_tn" + this.nodeId + "', \"" + oData.baseHref + sHref + "\"))";
					
					
					// KD begin - rozwinięcie gałęzi po kliknięciu na nazwę tematu:
					// IF there is data source for another level, add code for auto 
					// expanding/collapsing branch.
					if (oNodes[i].src!=null)
					{
						oA.onclick = new Function(this.sObjectName+".actionOnClick('" + this.sObjectName + "_tn" + this.nodeId + "')");
					}
					// KD end
				}
			}
			//Add custom attributes to resultNode which specifies whether node loaded children or not and 
			//whether is was expanded or not
			this.beingLoadedNode.setAttribute("Expanded", "true");	
		}
		this.beingLoadedNode.setAttribute("LoadedChildren", "true");
		this.beingLoadedNode = null;
	}
}		
