// Title: Tigra Tree Menu
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
// Version: customized
// Date: 08-25-2004 (mm-dd-yyyy)
// Technical Support: support@softcomplex.com (specify product title and order ID)
// Notes: Registration needed to use this script legally.
// Visit official site for details.

// ------------------------------------------------------------------------------------------
// tree constructor
// ------------------------------------------------------------------------------------------

function tree (a_items, a_template, s_name) {
    var i;
    // update global variables
    B_DOM = Boolean(document.body && document.body.innerHTML);

    // preload all images
    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 (i = 0; i < 128; i++)
        if (a_template['icon_' + i]) {
            var o_icon = new Image();
            a_template['im_' + i] = o_icon;
            o_icon.src = a_template['icon_' + i];
        }

    // item toggle routine
    this.toggle = function (n_id) {
        var o_item = this.a_index[n_id];
        o_item.open(o_item.n_state & 8);
    };
    // on item select handler
    this.select = function (n_id) {
        return this.a_index[n_id].select();
    };
    // on mouse over event handler
    this.rollover = function (n_id, b_out) {
        var o_item = this.a_index[n_id];
        o_item.rollover(b_out);
    };

    // find item by caption/link method
    this.find_item = function (s_value) {
        var a_items = [];
        for (var i in this.a_index)
            if (this.a_index[i]&&this.a_index[i].a_config[0] == s_value) {
                a_items[a_items.length] = this.a_index[i];
            }
        return a_items;
    };
    this.find_item_by_id = function (s_value) {
        var a_items = [];
        for (var i in this.a_index)
            if (this.a_index[i]&&this.a_index[i].a_config[2] == s_value) {
                a_items[a_items.length] = this.a_index[i];
            }
        return a_items;
    };



    this.n_id = s_name?s_name:TREES.length;
    TREES[this.n_id] = this;

    // index all tree items
    this.a_tpl      = a_template;
    this.o_root     = this;

    this.a_config = a_items;
    this.a_index    = [];
    this.a_nodes    = [];
    this.o_selected = 0;
    this.n_depth    = -1;

    this.a_children = [];

    for (i = 0; i < a_items.length; i++) {
        this.a_children[this.a_children.length] = new tree_item(this, i, obj2obj(a_items[i]));
    }


    this.a_states = [];

    for (i = 0; i < this.a_children.length; i++) {
        this.a_children[i].init = item_init;
        document.write(this.a_children[i].init());
    }
}

// ------------------------------------------------------------------------------------------
// item constructor
//  minimal actions required to index the item, minimal memory allocation
//  this constructor is run for all items of the tree
// ------------------------------------------------------------------------------------------
function tree_item (o_parent, n_order, a_config) {
    this.o_root   = o_parent.o_root;
    this.n_id = this.o_root.a_index.length;
    this.o_root.a_index[this.n_id] = this;
    this.n_depth  = o_parent.n_depth + 1;
    this.a_config = [a_config[0],a_config[1],a_config[2],a_config[3]];
    this.o_parent = o_parent;
    this.n_order = n_order;
    this.o_parent.b_chaged = true;

    if (typeof TREE_ITEMS_SEL != "undefined" && typeof TREE_ITEMS_SEL[this.n_id] != "undefined") {
        this.n_state = TREE_ITEMS_SEL[this.n_id];
    } else {
        this.n_state = 0;
    }

    // ignore commas after the last item of the list
    while (!a_config[a_config.length - 1])
        a_config.length = a_config.length - 1;


    this.select = item_select;
    this.remove = item_remove;
    this.edit = item_edit;
    this.state_lookup = item_state_lookup;

    this.a_children = [];
    this.add = item_add;

    // register in nodes index
    this.n_node_id = this.o_root.a_nodes.length;
    this.o_root.a_nodes[this.n_node_id] = this;

    if (a_config.length < 5)return;
    // init items recursively
    var i = 4;
    for (; i < a_config.length; i++) {
        var a_itemconfig = obj2obj(a_config[i - 4 + (this.n_depth+1 ? 4 : 0)]);
        new tree_item(this, i - 4, a_itemconfig);
    }
}

// ------------------------------------------------------------------------------------------
// item open/close routine
// node will be closed if b_close == true
// ------------------------------------------------------------------------------------------
/*
function item_open (b_close, b_noreload) {

    // skip if node is already in the requested state
    if (Boolean(this.n_state & 8) != Boolean(b_close)) return;


    // update state indicator
    this.n_state ^= 8;
    this.o_root.b_needreload = true;

    doRequest('tree_items_save.php4?id=' + this.n_id + '&state=' + this.n_state);

    // update styles and images
    this.state_lookup();

    // update status line
    this.upstatus();
    if (B_DOM) {
        if(this.a_children.length) {
            var o_idiv = this.redraw();
            o_idiv.style.display = (b_close ? 'none' : 'block');
        }
    }
    else if (!b_noreload && this.a_children.length)
        window.location = window.location;
    // reload page if ancient browser
}
*/

function item_redraw(){
    var o_idiv = get_element('c' + this.o_root.n_id + '_' + this.n_id);
    if (!o_idiv.innerHTML || this.b_chaged)
        o_idiv.innerHTML = this.subhtml();
    o_idiv.style.display = (this.a_children.length&&this.n_state&8?'block': 'none');
    this.state_lookup();
    return o_idiv;
}

function item_edit(s_text, b_link){
    b_link=b_link?1:0;
    this.a_config[b_link] = s_text;
    if(this.b_inited) {
        var o_idiv = get_element('a' + this.o_root.n_id + '_' + this.n_id);
        if(b_link==1) o_idiv.href = unescape(s_text);
        else o_idiv.innerHTML= s_text;
    }
    var test = 1;
}

function item_add(a_config)
{
        if (!B_DOM) {
            return;
        }

        a_config = obj2obj(a_config);
        var i = this.a_children.length;

        if(this.b_inited) {
            var o_item = new tree_item(this, i, a_config);
            this.a_children[i] = o_item;
            this.a_children[i].init = item_init;
            this.a_children[i].init();
            if (this.n_state & 8) {
                this.redraw();
            }
            var e = this.a_children[i];
        } else {
            var e = new tree_item(this, ++i, a_config);
        }

        if (this.a_children.length > 1) {
            this.o_parent.b_chaged = true;
            if(this.o_parent.n_state & 8) {
                if (typeof(this.o_parent.redraw) != "function") {
	            	this.o_parent.redraw = item_redraw;
	            }
				this.o_parent.redraw();
            }
            if (typeof(this.redraw) != "function") {
            	this.redraw = item_redraw;
            }
			this.redraw();
            this.open();
        }

        return e;
}

function item_remove(){
        if (!B_DOM) return;
        if(!this.o_root.a_index[this.n_id]) return;
        if(this.a_children){
            while(this.a_children.length){
                this.a_children[this.a_children.length-1].remove();
            }
        }
        var o_parent = this.o_parent;
        var a_items = o_parent.a_children;
        var b_found = 0;
        for(var i=0; i<a_items.length; i++){
            if(a_items[i]==this) b_found = 1;
            if(b_found) a_items[i] = a_items[i+1];
        }
        if(b_found) a_items.length = a_items.length - 1;
        for(var i=0; i<a_items.length; i++){
            a_items[i].n_order = i;
        }
        this.o_root.a_index[this.n_id] = 0; //
        o_parent.b_chaged = true;
        if(o_parent.n_state&8) o_parent.redraw();
}

// ------------------------------------------------------------------------------------------
// item select/deselect routine
// node will be deselected if b_deselect == true
// ------------------------------------------------------------------------------------------
function item_select (b_deselect) {

    if (b_deselect) {
        this.n_state &= ~4;
    }
    else {
        var o_olditem = this.o_root.o_selected;
        if (o_olditem) o_olditem.select(true);
        this.o_root.o_selected = this;
        this.n_state |= 4;
    }

    if(this.b_inited) {
        var e = get_element('a' + this.o_root.n_id + '_' + this.n_id );
        if (e) {
            e.style.fontWeight = b_deselect ? 'normal' : 'bold';
        }
        // update status line message
        this.upstatus();
        this.state_lookup();
    }
    return Boolean(this.a_config[1]);
}

// ------------------------------------------------------------------------------------------
// item rollover routine
// ------------------------------------------------------------------------------------------
function item_rollover(b_mout) {

    this.upstatus(b_mout);
}

// ------------------------------------------------------------------------------------------
// updates status bar message of the browser
// ------------------------------------------------------------------------------------------
function item_upstatus (b_clear) {
    window.setTimeout("window.status=unescape('" + (b_clear
        ? ''
        : escape(this.a_config[0]) + (this.a_config[1]
                ? ' ('+ escape(this.a_config[1]) + ')'
                : '')) + "')", 10);
}

// ------------------------------------------------------------------------------------------
// full item initialization routine
// returns HTML for item and opened subitems
// ------------------------------------------------------------------------------------------
function item_init () {
    if(!this.b_inited) {
        // calculate missing parameters of the item from those available
        var a_index = this.o_root.a_index;
        // assign methods
        this.rollover     =    item_rollover;
        this.load        =  item_load;
        this.upstatus = item_upstatus;
        this.draw = item_draw;
        this.open  = item_open;
        this.redraw = item_redraw;
        this.subhtml = item_subhtml;

        if (this.a_children) {
            // node related methods

            // find children
            for(var n_id in a_index) {
                if (a_index[n_id].o_parent == this) {
                    a_index[n_id].init = item_init;
                    this.a_children[this.a_children.length] = a_index[n_id];
                }
            }

			//this.open  = item_open;
            //this.redraw = item_redraw;
//            this.save  = item_save;
            //this.subhtml = item_subhtml;
        }
        else {
            // leaf specific methods
            //this.open = function () {};
        }
        this.b_inited = 1;
    }
    // store initial state
    if (this.n_state & 8) {
        this.n_state |= 8;
    } else {
        this.n_state =
             // bit indicating root item
            (this.n_depth ? 0 : 32) +
             // bit indicating node
            (this.a_children? 16 : 0) +
             // bit indicating selected
            (this.n_state&4 ? 4:0) +
             // bit indicating last item
            (this.n_order == this.o_parent.a_children.length - 1 ? 1 : 0);
    }
    this.load();
    return this.draw();
}

function item_draw () {

    // prepare for visualization
    var s_iconstyle = (this.o_root.a_tpl['style_icons']    ? ' class="' + this.o_root.a_tpl['style_icons'] + '"' : ''),
        a_offset = [],
        o_current_item = this.o_parent,
        a_opt = this.a_config[2];
    for (i = this.n_depth; i > 1; i--) {
        a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.n_state & 33 ? 'icon_e' : 'icon_l'] + '"' + s_iconstyle + ' border="0">';
        o_current_item = o_current_item.o_parent;
    }


    // get icons and styles for current state
    var a_params = this.state_lookup(true);
    return '<table cellpadding="0" cellspacing="0" border="0"><tr id="r' + this.o_root.n_id + '_' + this.n_id + '" onmouseover="TREES[\'' + this.o_root.n_id + '\'].rollover(' + this.n_id
                + ')" onmouseout="TREES[\'' + this.o_root.n_id + '\'].rollover(' + this.n_id
                + ',1)" '
                +'><td nowrap>' + a_offset.join('')
         + (a_params[1]
            ? (this.a_children
                ? '<a href="javascript: TREES[\'' + this.o_root.n_id + '\'].toggle('
                    + this.n_id + ')" onclick = "TREES[\'' + this.o_root.n_id + '\'].toggle('
                    + this.n_id + ');return false;" '
                    //+a_prop[0]
                    +' ><img src="' + a_params[1]
                    + '" border="0" name="j' + this.o_root.n_id + '_' + this.n_id
                    + '"' + s_iconstyle + '></a>'
                : '<img src="' + a_params[1] + '" border="0"' + s_iconstyle + '>')
            : '')
        + (a_params[0]
            ? '<a href="'+ this.a_config[1] + '" target="'
                + (a_opt && a_opt['tw']
                    ? a_opt['tw']
                    : this.o_root.a_tpl['target'])
                + '" title="' + (a_opt && a_opt['tt']
                    ? a_opt['tt']
                    : '')
                +'" onclick="return TREES[\'' + this.o_root.n_id    + '\'].select(' + this.n_id
                + ')" ondblclick="TREES[\'' + this.o_root.n_id  + '\'].'
                + (this.a_children
                    ? 'toggle('
                    : 'select(')
                + this.n_id
                + ')" '
                +'><img src="' + a_params[0]
                + '" border="0" name="i' + this.o_root.n_id + '_' + this.n_id
                + '"' + s_iconstyle    +'></a>'

            : '')
        + '</td><td nowrap' + (a_params[2]
            ? ' class="' + a_params[2]    + '"'
            : '')
        + ' id="t' + this.o_root.n_id + '_' + this.n_id
        + '"><a href="' + this.a_config[1]
        + '" target="' + (a_opt && a_opt['tw']
            ? a_opt['tw']
            : this.o_root.a_tpl['target'])
        + '" title="' + (a_opt && a_opt['tt']
            ? a_opt['tt']
            : '')
        + '" id="a' + this.o_root.n_id + '_' + this.n_id +'" onclick="if(this.blur)this.blur();return TREES[\'' + this.o_root.n_id + '\'].select(' + this.n_id
        + ')" ondblclick="TREES[\'' + this.o_root.n_id + '\'].'
        + (this.a_children
            ? 'toggle('
            : 'select(')
        + this.n_id
        + ')" onmouseover="TREES[\'' + this.o_root.n_id + '\'].rollover(' + this.n_id
                + ')" onmouseout="TREES[\'' + this.o_root.n_id + '\'].rollover(' + this.n_id
                + ',1)" '
                +'>' + this.a_config[0]
        + '</a></td></tr></table>' + (this.a_children
            ? '<div id="c' + this.o_root.n_id + '_' + this.n_id + '" style="display:' + (this.n_state & 8 && this.a_children.length
                ? 'block">' + this.subhtml()
                : 'none">' + (this.o_root.solid?this.subhtml():'')) + '</div>'
            : '');
}

// ------------------------------------------------------------------------------------------
// returns HTML for children items
// ------------------------------------------------------------------------------------------
function item_subhtml() {
    this.b_chaged = false;
    var a_subhtml = [];
    for (var i = 0; i < this.a_children.length; i++)
        a_subhtml[i] = this.a_children[i].init();
    return a_subhtml.join('');
}

function item_load() {


    // set default states if no cookies found
    if (!this.n_depth) {
        this.n_state |= 8;
        return;
    }

}

// ------------------------------------------------------------------------------------------
// returns or adjusts item icons and styles accordingly to the item's state
// ------------------------------------------------------------------------------------------
function item_state_lookup (b_return) {

    var n_iindex = this.n_state & ~3;
    var n_jindex = this.n_state & ~68 | 2;
    if(this.a_children&&!this.a_children.length) n_jindex &= ~24;

    var s_image = typeof(this.a_config[3]) == 'number'
            ? this.o_root.a_tpl[this.a_config[3]]
                    ?this.o_root.a_tpl[this.a_config[3]]['i' + (n_iindex & ~48)] : 0
            :0;
    if (!s_image) s_image = this.o_root.a_tpl['icon_' + n_iindex];
    if (!s_image) s_image = this.o_root.a_tpl['icon_' + (n_iindex & ~64)];
    var s_junct = this.o_root.a_tpl['icon_' + n_jindex];
    var s_style = this.o_root.a_tpl['style_' + n_iindex];
    if (!s_style) s_style = this.o_root.a_tpl['style_' + (n_iindex & ~64)];

    if (b_return)
        return [s_image, s_junct, s_style];

    var o_obj = document.images['j' + this.o_root.n_id + '_' + this.n_id];
    if (o_obj) o_obj.src = s_junct;
        o_obj = document.images['i' + this.o_root.n_id + '_' + this.n_id];
    if (o_obj) o_obj.src = s_image;
        o_obj = get_element('t' + this.o_root.n_id + '_' + this.n_id);
    if (o_obj) o_obj.className =  s_style;
}


function obj2obj(o_source){
    var o_destination;
    if(typeof(o_source)=='object'){
        o_destination=[];
        for(var i in o_source)
            o_destination[i]=obj2obj(o_source[i])
    }
    else o_destination=o_source;
    return o_destination;
}
// ------------------------------------------------------------------------------------------
// global variables and functions
var TREES = [], B_DOM,MOVEREDITEM=0;
get_element = document.all ?
    function (s_id) { return document.all[s_id] } :
    (document.getElementById ?
        function (s_id) { return document.getElementById(s_id) } :
        function (s_id) { return null });

// ------------------------------------------------------------------------------------------
// fin.
