// Title: Tigra Tree
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
// Version: 1.1
// Date: 11-12-2002 (mm-dd-yyyy)
// Notes: This script is free. Visit official site for further details.

function tree (a_items, a_template) {
	tree2(a_items, a_template, true, null);
}
function treeajax(a_items, a_template, o_elt) {
	tree2(a_items, a_template, false, o_elt);
}

function menupop(doc,ele,item){
//	alert('doc '+doc+' ele='+ele+' item='+item);
    if(doc && ele){
        Position.absolutize(ele);
        var posy = ((ele.style.top.replace("px",""))-100) + 'px';
        var posx = (((ele.style.left.replace("px",""))-1)+80) + 'px';
        Position.relativize(ele);
		
        if(!$('treeMenu')){
            var objBody1 = document.getElementsByTagName("body").item(0);
            var objMenu1 = document.createElement("div");
			objMenu1.setAttribute('id','treeMenu');
                objMenu1.style.top = posy;
                objMenu1.style.left = posx;
                objBody1.appendChild(objMenu1);
                var url = 'optionsmenu.php?doc=' + doc + '&item=' + item;
                new Ajax.Updater('treeMenu',url,{
                    method: 'get',
                    onComplete: function(){
                        $('treeMenu').show();
                        initLightbox();
                    }            
                });
                objMenu1.onmouseout = function(e) {
                    //this.hide();
                }
        }else{
            $('treeMenu').style.top = posy;
            $('treeMenu').style.left = posx;
            var url = 'optionsmenu.php?doc=' + doc + '&item=' + item;
            new Ajax.Updater('treeMenu',url,{
                method: 'get',
                onComplete: function(){
                    $('treeMenu').show();
                    initLightbox();
                }            
            });
                        
        }
    }
}

function tree2 (a_items, a_template, b_write, o_elt) {

	this.a_tpl      = a_template;
	this.a_config   = a_items;
	this.o_root     = this;
	this.a_index    = [];
	this.o_selected = null;
	this.n_depth    = -1;
	//var ans		= [];
	
	var o_icone = new Image(),
		o_iconl = new Image();
	o_icone.src = a_template['icon_e'];
	o_iconl.src = a_template['icon_l'];
	a_template['im_e'] = o_icone;
	a_template['im_l'] = o_iconl;
	for (var i = 0; i < 64; i++)
		if (a_template['icon_' + i]) {
			var o_icon = new Image();
			a_template['im_' + i] = o_icon;
			o_icon.src = a_template['icon_' + i];
		}
	
	this.toggle = function (n_id) {	var o_item = this.a_index[n_id]; o_item.open(o_item.b_opened) };
	this.select = function (n_id) { return this.a_index[n_id].select(); };
	this.mout   = function (n_id) { this.a_index[n_id].upstatus(true) };
	this.mover  = function (n_id) { this.a_index[n_id].upstatus() };
	this.check_open = item_check_open;

	this.a_children = [];
//alert(a_items.length);	
	for (var i = 0; i < a_items.length; i++)
		new tree_item(this, i);

	this.n_id = trees.length;
	trees[this.n_id] = this;
	
	if(o_elt) {
		o_elt.innerHTML = '';
	}
	
	for (var i = 0; i < this.a_children.length; i++) {
		if(b_write) {
			document.write(this.a_children[i].init());
		} else {
			o_elt.innerHTML = o_elt.innerHTML + this.a_children[i].init();
			//ans.push(this.a_children[i].init());
		}
		this.a_children[i].open();
		this.check_open();
	}
	
	//return ans;
}
function item_check_open() {
//alert(this.tobeopen);	
	if(this.tobeopen) {
		this.open();
	}
	for (var i = 0; i < this.a_children.length; i++) {
		this.a_children[i].check_open();
	}
}

function tree_item (o_parent, n_order) {

	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 2 : 0)];
	if (!this.a_config) return;

	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	this.b_opened  = !this.n_depth;

	this.n_id = this.o_root.a_index.length;
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++)
		new tree_item(this, i);

	this.get_icon = item_get_icon;
	this.open     = item_open;
	this.select   = item_select;
	this.init     = item_init;
	this.upstatus = item_upstatus;
	this.is_last  = function () { return this.n_order == this.o_parent.a_children.length - 1 };
	this.check_open = item_check_open;
}

function item_open (b_close) {
	var o_idiv = get_element('i_div' + this.o_root.n_id + '_' + this.n_id);
	if (!o_idiv) return;
	
	if (!o_idiv.innerHTML) {
		var a_children = [];
		for (var i = 0; i < this.a_children.length; i++)
			a_children[i]= this.a_children[i].init();
		o_idiv.innerHTML = a_children.join('');
	}
	o_idiv.style.display = (b_close ? 'none' : 'block');
	
	this.b_opened = !b_close;
	var o_jicon = document.images['j_img' + this.o_root.n_id + '_' + this.n_id],
		o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	
	if (o_jicon) o_jicon.src = this.get_icon(true);
	if (o_iicon) o_iicon.src = this.get_icon();
	this.upstatus();
}

function item_select (b_deselect) {
	if (!b_deselect) {
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
	}
	var o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_iicon) {
		//var ext = o_iicon.className.match(/ext([a-z]+)/);
		var reg = o_iicon.className.match(/ext([a-z]+)/);
		var ext = reg ? reg[1] : '';
		o_iicon.src = this.get_icon(false,ext,ext);
	}
	get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	
	this.upstatus();
	return Boolean(this.a_config[1]);
}

function item_upstatus (b_clear) {
	window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);
}

function item_init () {
	var a_offset = [],
		o_current_item = this.o_parent;
	for (var i = this.n_depth; i > 1; i--) {
		a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.is_last() ? 'icon_e' : 'icon_l'] + '" border="0" align="absbottom">';
		o_current_item = o_current_item.o_parent;
	}
    
	var itemValues, itemOptions, onClick, onedit, ondelete;
	
    if(this.a_config[1]){ 
        itemValues = this.a_config[1].split("|");
    }else{
        itemValues = new Array();
        itemValues[0] = '';
        itemValues[1] = ''; // FOLDER
		itemValues[2] = '';	// DOC
		itemValues[3] = '';	// OPEN?
		itemValues[4] = ''; // EXT
    }
	
	onClick = 'return menupop(\'' + itemValues[2] + '\',this,\'' + this.a_config[0] + '\'); return trees[' + this.o_root.n_id + '].select(' + this.n_id + ')';
	
//alert(this.a_config[1]);    
    if(this.n_id>0){
		if(itemValues[2]>0) {
			onedit = 'bpEditDoc(' + itemValues[2] + ',\'' + this.a_config[0] + '\')';
			ondelete = 'bpDeleteDocument(' + itemValues[2] + ')';
		} else if(itemValues[4]=='vf') { // CLIENT
			onClick = "return bpShowvf(\'"+itemValues[1]+"\');";
		} else {
			onedit = 'bpEditContact(' + itemValues[1] + ',\'' + this.a_config[0] + '\')';
			ondelete = 'bpDeleteContact(' + itemValues[1] + ')';
		}
		
		if(itemValues[3]=='y') {
			this.tobeopen = true;
//alert(itemValues[3]);			
			//this.open();
			//this.b_opened = true;
		} else {
			this.tobeopen = false;
		}
		if(eeiuser==true){
            if(itemValues[4]){ // If its a Doc
                itemOptions = '';
            }else{
                itemOptions = ' <a class="editIcon" href="javascript:void(0)" onclick="return '+onedit+';"><img src="icons2/edit.gif" alt="Edit" title="Edit" /></a><a class="deleteIcon" href="javascript:void(0)" onclick="return '+ondelete+';"><img src="icons2/delete.gif" alt="Delete" title="Delete" /></a>';
            }
        }else{
            //itemOptions = ' (<a class="editIcon" href="javascript:void(0)" onclick="return '+onedit+';">edit</a> | <a class="deleteIcon" href="javascript:void(0)" onclick="return '+ondelete+';">delete</a>)';
            itemOptions = '';
        }
    }else{
        itemOptions = '';
    }
    
	return '<table cellpadding="0" cellspacing="0" border="0"><tr><td nowrap>' + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><img src="' + this.get_icon(true) + '" border="0" align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		: '<img src="' + this.get_icon(true,itemValues[2],itemValues[4]) + '" border="0" align="absbottom">') : '') 
		+ '<a href="' + itemValues[0] + '" target="' + this.o_root.a_tpl['target'] + '" onclick="'+onClick+'" ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" class="t' + this.o_root.n_id + 'i" id="i_txt' + this.o_root.n_id + '_' + this.n_id + '"><img src="' + this.get_icon(false,itemValues[2],itemValues[4]) + '" border="0" align="absbottom" name="i_img' + this.o_root.n_id + '_' + this.n_id + '" class="t' + this.o_root.n_id + 'im'+(itemValues[4]?' ext'+itemValues[4]:'')+'"><span class="treename">&nbsp;' + this.a_config[0] + '</span></a>' + itemOptions + '</td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
}

function item_get_icon (b_junction,doc,ext) {
	var icon = this.o_root.a_tpl['icon_' + ((this.n_depth ? 0 : 32) + (this.a_children.length ? 16 : (!b_junction ? 16:0)) + (this.a_children.length && this.b_opened ? 8 : 0) + (!b_junction && this.o_root.o_selected == this ? 4 : 0) + (b_junction ? 2 : 0) + (b_junction && this.is_last() ? 1 : 0))];
	
	if(!b_junction) {
		if(ext && ext!='vf') {
			icon = 'icons2/'+ext+'.gif'; // doctype icon
			
		} else if(doc) {
			icon = this.o_root.a_tpl['icon_16']; // folder icon
		}
	}
	
	return icon;
}

var trees = [];
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };
