/**
 * @package: Admin Classes
 * @subpackage: Dynamic tree creater
 * @version: v.1.2
 * @author: Mezahir Efendiyev <cengmezo@yahoo.com>
 */

function kiwi_dTree(varName) {
	
	this.variableName				= varName;
	
	this.imagePath 					= '_templates/library/dTree/';
	
	this.imageItemParent 			= '_parent.gif';
	this.imageItemParentExp 		= '_parentExp.gif';
	this.imageItemChild				= '_child.gif';
	
	this.imageExpand_01				= 'expand_01.gif';	
	this.imageExpand_02				= 'expand_02.gif';	
	this.imageExpand_03				= 'expand_03.gif';	
	this.imageExpand_04				= 'expand_04.gif';		
	
	this.imageCollapse_01			= 'collapse_01.gif';	
	this.imageCollapse_02			= 'collapse_02.gif';
	this.imageCollapse_03			= 'collapse_03.gif';
	this.imageCollapse_04			= 'collapse_04.gif';
	
	this.imageLine_01				= 'line_01.gif';
	this.imageLine_02				= 'line_02.gif';
	this.imageLine_03				= 'line_03.gif';
	this.imageLine_04				= 'line_04.gif';
	this.imageLine_05				= 'line_05.gif';
	
	this.browsedItemImages			= new Array ();
	
	this.hierarchyTree 				= new Array();
	this.hierarchyKeys				= new Array();
	this.hierarchyOpened 			= new Array();
	this.hierarchyLength			= 0;
	
	this.iconChildToParent			= 0;
	this.iconParentToChild			= 0;
	
	this.emptyMessage				= 'No record!';
	
	this.fn_par_singleClick			= '';
	this.fn_par_singleClick_Ext		= '';

	this.fn_itm_singleClick			= '';	
	this.fn_itm_singleClick_Ext		= '';
	
	this.fn_par_doubleClick			= '';
	this.fn_par_doubleClick_Ext		= '';
	
	this.fn_itm_doubleClick			= '';	
	this.fn_itm_doubleClick_Ext		= '';
	
	this.contentDivID				= 'dtree';
}


/**
 * Dynamic tree prototype definition
 *
 */


kiwi_dTree.prototype = {
	
	_init : function  () {
		
		// Get Hierarchy Indexes		
		var k, j = 0, i = this.hierarchyTree.length;
		
		for ( k = 0; k < i ; k ++ ) {		
			if ( this.hierarchyTree[k] ) {
				this.hierarchyKeys[this.hierarchyLength++] = k;
			}
		}
		
		if (!this.hierarchyLength) {
			// No data
			j_common_getElement(this.contentDivID).innerHTML = this.emptyMessage;
		} else {
			// construct
			j_common_getElement(this.contentDivID).innerHTML = this._expandParent (0 , Array() );
		}
	},
	
	// Expand parent
	_expandParent : function  ( node , deepness ) {		
		
		// Get childs
		var j, k, i=0, c = new Array ();
		for ( j = 0 ; j < this.hierarchyLength ; j ++ ) {			
			if (  this.hierarchyTree[this.hierarchyKeys[j]][0] == node ) {
				c [i++] = [this.hierarchyKeys[j]];
			}
		}
		
		// Show childs
		i = '';
		j = c.length;				
		for ( k=0; k < j; k ++ ) {
			i += this._createNode ( c[k] , k , j , node , deepness );
		}
		return i;
	},
	
	// Create node content
	_createNode : function  ( node , current , total , nodeParent , deepness  ) {
		
		var itemIcon, i, margin='', onSnClick, onDbClick, newDeepnessString, newDeepness = new Array();
		
		if ( ! total ) {
			return '';
		}		
		
		if ( ! this.browsedItemImages[node] ) {
			this.browsedItemImages[node] = new Array();
		}
		
		// Icon
		
		if ( this.hierarchyTree[node][1] || this.hierarchyTree[node][2] ) {
			if ( this.iconParentToChild ) {
				itemIcon = this.imagePath+this.imageItemChild;
			} else {
				if (this.hierarchyTree[node][4]) {
					itemIcon = this.hierarchyTree[node][4];
				} else {
					itemIcon = this.imagePath+this.imageItemParent;
				}			
			}
		} else {
			if ( this.iconChildToParent ) {
				itemIcon = this.imagePath+this.imageItemParent;
			} else {
				if (this.hierarchyTree[node][4]) {
					itemIcon = this.hierarchyTree[node][4];
				} else {
					itemIcon = this.imagePath+this.imageItemChild;
				}
			}
		}
		
		
		if ( this.hierarchyTree[node][2] ) {		
			
			if ( (total == 1) && (!nodeParent) ) {
				this.browsedItemImages[node][0] = this.imageExpand_01;
				this.browsedItemImages[node][1] = this.imageCollapse_01;
			} else if (total == 1) {
				this.browsedItemImages[node][0] = this.imageExpand_02;
				this.browsedItemImages[node][1] = this.imageCollapse_02;
			} else if ( ( current == 0 ) && (!nodeParent) ) {
				this.browsedItemImages[node][0] = this.imageExpand_03;
				this.browsedItemImages[node][1] = this.imageCollapse_03;
			} else if ( current == 0  ) {
				this.browsedItemImages[node][0] = this.imageExpand_04;
				this.browsedItemImages[node][1] = this.imageCollapse_04;
			} else if ( ( current + 1 ) == total ) {
				this.browsedItemImages[node][0] = this.imageExpand_02;
				this.browsedItemImages[node][1] = this.imageCollapse_02;
			} else {
				this.browsedItemImages[node][0] = this.imageExpand_04;
				this.browsedItemImages[node][1] = this.imageCollapse_04;
			}
			
		} else {		
			
			if ( (total == 1) && (!nodeParent) ) {
				this.browsedItemImages[node][0] = this.imageLine_01;
				this.browsedItemImages[node][1] = this.imageLine_01;
			} else if (total == 1) {
				this.browsedItemImages[node][0] = this.imageLine_02;
				this.browsedItemImages[node][1] = this.imageLine_02;
			} else if ( ( current == 0 ) && (!nodeParent) ) {
				this.browsedItemImages[node][0] = this.imageLine_03;
				this.browsedItemImages[node][1] = this.imageLine_03;
			} else if ( current == 0  ) {
				this.browsedItemImages[node][0] = this.imageLine_04;
				this.browsedItemImages[node][1] = this.imageLine_04;
			} else if ( ( current + 1 ) == total ) {
				this.browsedItemImages[node][0] = this.imageLine_02;
				this.browsedItemImages[node][1] = this.imageLine_02;
			} else {
				this.browsedItemImages[node][0] = this.imageLine_04;
				this.browsedItemImages[node][1] = this.imageLine_04;
			}
		}
		
		for ( i = 0; i < deepness.length ; i ++ ) {
		
			newDeepness[i] = deepness[i];
			
			if ( deepness[i] ) {
				margin += '<img src="'+this.imagePath+this.imageLine_05+'" align="absmiddle" border="0" />&nbsp;';
			} else {
				margin += '<img src="'+this.imagePath+this.imageLine_01+'" align="absmiddle" border="0" />&nbsp;';
			}
		}
		
		
		if ( (total==1) || ( ( current + 1 ) == total ) ) {		
			newDeepness[i] = 'false';
		} else {
			newDeepness[i] = 'true';
		}
		
		newDeepnessString = newDeepness.toString();
		
		
		// Double clicks
		
		onSnClick = '';
		onDbClick = '';
		
		// PARENT
		if (this.hierarchyTree[node][1] || this.hierarchyTree[node][2]) {
			
			// Single click function
			if (this.fn_par_singleClick !='' ) {
				onSnClick = this.fn_par_singleClick + '(\''+this.hierarchyTree[node][5]+'\'); ';				
			}
			
			// Single click Extra
			if (this.fn_par_singleClick_Ext != '') {
				onSnClick += this.fn_par_singleClick_Ext + ' ';
			}
			
			// Double click function
			if (this.fn_par_doubleClick != '') {
				onDbClick = this.fn_par_doubleClick + '(\''+this.hierarchyTree[node][5]+'\'); ';				
			}
			
			// Double click Extra
			if (this.fn_par_doubleClick_Ext != '') {
				onDbClick += this.fn_par_doubleClick_Ext + ' ';
			}
			
		} else {
			
			// Single click function
			if (this.fn_itm_singleClick != '') {
				onSnClick = this.fn_itm_singleClick + '(\''+this.hierarchyTree[node][5]+'\'); ';			
			}	
			
			// Single click Extra
			if (this.fn_itm_singleClick_Ext != '') {
				onSnClick += this.fn_itm_singleClick_Ext + ' ';
			}				
			
			// Double click function
			if (this.fn_itm_doubleClick != '') {
				onDbClick = this.fn_itm_doubleClick + '(\''+this.hierarchyTree[node][5]+'\'); ';
			}		

			// Double click Extra
			if (this.fn_itm_doubleClick_Ext != '') {
				onDbClick += this.fn_itm_doubleClick_Ext + ' ';
			}
			

		}
		
		if (onSnClick != ''){
			onSnClick = 'onclick="'+onSnClick+'" ';			
		} else {
			onSnClick = '';
		}
		
		if (onDbClick != ''){
			onDbClick = 'ondblclick="'+onDbClick+'" ';
		} else {
			onDbClick = '';
		}
		
		/*
		this.		= 'alert';
		this.		= 'alert';
		this.	= false;
		this.	= false;
	
		this.fn_par_doubleClick		= 'alert';
		this.		= 'alert';
		this.fn_par_doubleClick_Ext	= false;
		this.	= false;
		*/
	
		
		
		var content =	'<div>'+margin+					
							'<a href="#" onclick="'+this.variableName+'._showNodeChilds('+node+', Array('+(newDeepnessString)+')); return false;">'+
								'<img id="nodeExpandImage_'+node+'" src="'+this.imagePath+this.browsedItemImages[node][0]+'" align="absmiddle" border="0" />'+
							'</a> '+						
							'<img id="nodeItemImage_'+node+'" src="'+itemIcon+'" align="absmiddle" border="0" /> '+
							'<a href="'+this.hierarchyTree[node][5]+'" '+
								onSnClick + onDbClick +  
								'>'+
							this.hierarchyTree[node][3] +
							'</a>'+
							'<div style="visibility:hidden;position:absolute;" id="nodeChilds_'+node+'"></div>'+
						'</div>';
						
		return content;				

	},
	
	// Show node childs
	_showNodeChilds : function  ( node , deepness ) {
		
		var obj = j_common_getElement('nodeChilds_'+node);
		
		if ( obj.style.visibility == 'hidden' ) {

			if ( ! this.hierarchyOpened[node] ) {
				this.hierarchyOpened[node] = this._expandParent ( node , deepness );
			}
			
			j_common_getElement('nodeExpandImage_'+node).src = this.imagePath + this.browsedItemImages[node][1];
			
			if ( this.iconParentToChild ) {
				j_common_getElement('nodeItemImage_'+node).src = this.imagePath + this.imageItemChild;
			} else {
				j_common_getElement('nodeItemImage_'+node).src = this.imagePath + this.imageItemParentExp;
			}
			
			obj.innerHTML = this.hierarchyOpened[node];
			obj.style.visibility = 'visible';
			obj.style.position = ''
			
		} else {
			
			j_common_getElement('nodeExpandImage_'+node).src = this.imagePath + this.browsedItemImages[node][0];
			
			if ( this.iconParentToChild ) {
				j_common_getElement('nodeItemImage_'+node).src = this.imagePath + this.imageItemChild;
			} else {
				j_common_getElement('nodeItemImage_'+node).src = this.imagePath + this.imageItemParent;
			}
			
			this.hierarchyOpened[node] = obj.innerHTML;
			obj.style.visibility = 'hidden'
			obj.style.position = 'absolute'
			obj.innerHTML = '';
			
		}

	},
	
	_setHierarchyTree : function (h) {
		this.hierarchyTree = h;
	},
	
	_setImagesPath : function (p) {
		this.imagePath = p;
	},
	
	_setContentDiv : function (dv) {
		this.contentDivID = dv;
	},
	
	_setSingleClickPar : function (fn, ex ) {
		this.fn_par_singleClick = fn;
		this.fn_par_singleClick_Ext = ex;
	},
	
	_setSingleClickItm : function (fn, ex ) {
		this.fn_itm_singleClick = fn;
		this.fn_itm_singleClick_Ext = ex;
	},
	
	_setDoubleClickPar : function (fn, ex ) {
		this.fn_par_doubleClick = fn;
		this.fn_par_doubleClick_Ext = ex;
	},
	
	_setDoubleClickItm : function (fn, ex ) {
		this.fn_itm_doubleClick = fn;
		this.fn_itm_doubleClick_Ext = ex;
	},
	
	_setIconTypes : function ( allPar, allChild ) {
		this.iconChildToParent = allPar;
		this.iconParentToChild  = allChild;
	},
	
	_setEmptyMessage : function ( mess ) {
		this.emptyMessage = mess;
	}
	
};
