
function node_item(){
	this.id = "";
    this.title = "";
    this.long_title = "";
    this.content;
    this.html_obj;
    this.reserved;
}

Viewer = function(){

    var dict_nodes = new Array();
    var tree_parent = null;
    var flat_parent = null;
    var entries_changed = false;
    var viewer_mode = "tree";
    
    this.clear = function(){
        dict_nodes.length = 0;
        tree_parent = null;
        flat_parent = null;
    }
    
    this.insert_item = function (dict_id, flag_text, dict_longname, dict_shortname, entry){
    
        log_info(1, "insert_item", dict_id + dict_longname + dict_shortname);
        var r = contains_element_ex(dict_nodes, dict_id, "id");
        var where_to_insert = r[1];
        var new_node = r[0];
        entries_changed = true;
        
        if (viewer_mode == "tree"){
            var ret_item = insert_item_into_tree(new_node, where_to_insert, dict_id, flag_text, dict_shortname, entry);
        }else{
            var ret_item = insert_item_into_flat(where_to_insert, dict_id, flag_text, dict_shortname, entry);
        }
    
        //dump it into
        var tmp_node = new node_item();
        tmp_node.id = dict_id;
        tmp_node.title = flag_text + dict_shortname;
        tmp_node.long_title = flag_text + dict_longname;
        tmp_node.content = entry;
        tmp_node.html_obj = ret_item;
        if (where_to_insert == -1){
            dict_nodes.push(tmp_node); //new Array(dict_id, ret_item, 0, entry, flag_text, dict_longname, dict_shortname)
        }else
            dict_nodes = dict_nodes.insert(where_to_insert, tmp_node);
        //-1 means: last
        if (where_to_insert == -1)
            where_to_insert = dict_nodes.length - 1;
        //if (!new_node)
            //where_to_insert += 1;
        return where_to_insert;
    }
    
    function insert_item_into_tree(new_node, where_to_insert, dict_id, flag_text, dict_longname, entry){
        if(!new_node){ //where_to_insert != -1
            var entry_node = CreateTreeItem( dict_nodes[where_to_insert].html_obj, images_path+"/tree/folder_closed.gif", images_path+"/tree/folder_open.gif", entry, null, null, true, -1); //fixme -1
            return entry_node.parentNode;
        }else{
            var title = flag_text + dict_longname;
            var node = CreateTreeItem( rootCell, images_path+"/tree/folder_closed.gif", images_path+"/tree/folder_open.gif", title, null, null, false, where_to_insert );//images/tree/project.gif
            var entry_node = CreateTreeItem( node, images_path+"/tree/UseCase.gif", images_path+"/tree/UseCase.gif", entry, null, null, true, -1 );
            return node;
        } 
    }
    
    function insert_item_into_flat(where_to_insert, dict_id, flag_text, dict_shortname, entry){
    
        if (flat_parent == null){
            build_flat(false);
        }
        var row=document.createElement("TR");
        if (where_to_insert == -1){
            flat_parent.appendChild( row );
        }else
            //flat_parent.insertBefore( row, dict_nodes[where_to_insert].html_obj );
            //flat_parent.insertBefore( row, dict_nodes[where_to_insert][1] );
            dict_nodes[where_to_insert].html_obj.parentNode.appendChild( row );
    
        var cell=document.createElement("TD");
        row.appendChild(cell);
        var item = make_flat_entry(flag_text, dict_shortname, entry);
        cell.appendChild(item);
        return row;
    
    }
    
    function make_flat_entry(flag_text, dict_shortname, entry){
        var flags = document.createElement("div");
        flags.innerHTML = flag_text;
        var dict_name = document.createTextNode(dict_shortname);
        
        var table = document.createElement("TABLE");
        var tablebody = document.createElement("TBODY");
        table.appendChild(tablebody);
        var row=document.createElement("TR");
        tablebody.appendChild( row );
    
        var cell=document.createElement("TD");
        cell.setAttribute( "width", 45 );
        cell.setAttribute( "valign", "top");
        if (font_size != "")
            row.style.fontSize = font_size;

        row.appendChild(cell);            
        cell.appendChild(flags);

        cell=document.createElement("TD");
        cell.setAttribute( "align", "left");
        row.appendChild(cell);            
        cell.appendChild(dict_name);

        row=document.createElement("TR");
        tablebody.appendChild( row );
    
//        cell=document.createElement("TD");
//        cell.setAttribute( "width", 3 );
//        row.appendChild(cell);            
        
        cell=document.createElement("TD");
        cell.setAttribute( "colSpan", 2);
        if (font_size != "")
            cell.style.fontSize = font_size;

        //cell.colspan = 2;
        //cell.setAttribute( "margin", "2");
        row.appendChild(cell);            
        cell.appendChild(entry);
        
        return table;

    }
    
    function build_tree(){
        for (var i=0; i< rootCell.childNodes.length; i++){
            rootCell.removeChild(rootCell.childNodes[i]);
        }

        var arr = new Array();
        for (var i=0; i< dict_nodes.length; i++){
            var new_node = true;
            var node_id = -1;
            for(var k=0; k<arr.length; k++){
                if (String(arr[k]) == String(dict_nodes[i].id)){
                    new_node = false;
                    node_id = k;
                    break;
                }
            }
            var item = insert_item_into_tree(new_node, node_id, dict_nodes[i].id, "", dict_nodes[i].title, dict_nodes[i].content);
            dict_nodes[i].html_obj = item;
            dict_nodes[i].reserved = document.createTextNode("");
            item.parentNode.insertBefore(dict_nodes[i].reserved, item);
            arr.push(dict_nodes[i].id);
        }
    }
    
    function build_flat(from_data){
    
       // if (flat_parent  != null){
            var mytable = document.getElementById("root_entry_table");
            if (mytable){
                mytable.parentNode.removeChild(mytable);
            }
    /*        for (i=0; i< flat_parent.childNodes.length; i++){
                flat_parent.removeChild(flat_parent.childNodes[i]);
            }
      //      flat_parent.parentNode.removeChild(flat_parent);
          */  
        //}
        var table = document.createElement("TABLE");
        tree_parent.appendChild( table ); 
    
        table.setAttribute( "border", 0 );
        table.setAttribute( "cellpadding", 1 );
        table.setAttribute( "cellspacing", 1 );
        table.setAttribute( "id", "root_entry_table" );
        
            
        var tablebody = document.createElement("TBODY");
        table.appendChild(tablebody);
        flat_parent = tablebody;
        
        if (!from_data)
            return;
        for (i=0; i< dict_nodes.length; i++){
            var row=document.createElement("TR");
            tablebody.appendChild( row );
    
            var cell=document.createElement("TD");
            row.appendChild(cell);            
            var item = make_flat_entry(dict_nodes[i].title, "", dict_nodes[i].content);

            cell.appendChild(item);
        }
    }
    
/*    
    function insert_into_tree(dict_id, title, text){
        
        var r = contains_element_ex(dict_nodes, dict_id, 0);
        var index = r[1];
        var new_node = r[0];
        entries_changed = true;
        
        if(!new_node){ //index != -1
            var entry_node = CreateTreeItem( dict_nodes[index][1], "images/tree/folder_closed.gif", "images/tree/folder_open.gif", text, null, null, true, -1); //fixme -1
            dict_nodes = dict_nodes.insert(index, new Array(dict_id, entry_node.parentNode, 0, text));
        }else{
            var node = CreateTreeItem( rootCell, "images/tree/folder_closed.gif", "images/tree/folder_open.gif", title, null, null, false, index );//images/tree/project.gif
            var entry_node = CreateTreeItem( node, "images/tree/UseCase.gif", "images/tree/UseCase.gif", text, null, null, true, -1 );
            if (index == -1)
                dict_nodes.push(new Array(dict_id, node, 0, text));
            else
                dict_nodes = dict_nodes.insert(index, new Array(dict_id, node, 0, text));
        }
    }
*/    

/*    this.abc = function (){
        var mytable = document.getElementById("root_entry_table");
        if (mytable){
            mytable.parentNode.removeChild(mytable);
        }
        var table = document.createElement("TABLE");
        tree_parent.appendChild( table ); 

        table.setAttribute( "border", 0 );
        table.setAttribute( "cellpadding", 1 );
        table.setAttribute( "cellspacing", 1 );
        table.setAttribute( "id", "root_entry_table" );
        
            
        var tablebody = document.createElement("TBODY");
        table.appendChild(tablebody);
        flat_parent = tablebody;

    }
*/    
    this.change_view = function (view_mode){
        viewer_mode = view_mode;
        if (tree_parent == null){
            return; //uninitialised object
        }
    
        if (view_mode == "tree"){
            setvisible(rootCell, true);
            setvisible(document.getElementById("root_entry_table"), false);
    
            if (entries_changed){
                build_tree();
                entries_changed = false;
                return;
            }
            
            var mytable = document.getElementById("root_entry_table");
    //        setvisible(mytable, false);
    
    
            for (i=0; i< dict_nodes.length; i++){
                dict_nodes[i].reserved.parentNode.replaceChild(dict_nodes[i].content, dict_nodes[i].reserved); //instead of 1 => 6
            }
        }else if (view_mode == "flat"){
            setvisible(rootCell, false);
    /*
            if (entries_changed){
                build_flat(true);
                entries_changed = false;
                viewer_mode = view_mode;
                return;
            }
    */
            //build_flat(false);
            //this.abc();
            var mytable = document.getElementById("root_entry_table");
            if (mytable){
                mytable.parentNode.removeChild(mytable);
            }
            var table = document.createElement("TABLE");
            tree_parent.appendChild( table ); 
    
            table.setAttribute( "border", 0 );
            table.setAttribute( "cellpadding", 1 );
            table.setAttribute( "cellspacing", 1 );
            table.setAttribute( "id", "root_entry_table" );
            
                
            var tablebody = document.createElement("TBODY");
            table.appendChild(tablebody);
            flat_parent = tablebody;
                
              
            for (i=0; i< dict_nodes.length; i++){
                var row=document.createElement("TR");
                tablebody.appendChild( row );
    
                var cell=document.createElement("TD");
                row.appendChild(cell);            
                
                dict_nodes[i].reserved = document.createTextNode("");
                dict_nodes[i].content.parentNode.insertBefore(dict_nodes[i].reserved, dict_nodes[i].content);

                var item = make_flat_entry(dict_nodes[i].title, "", dict_nodes[i].content);
                cell.appendChild(item);//dict_nodes[i][3]);
            }
        }
        entries_changed = false;
    }
    
    //return value an array
    // [0] new_node or not
    // [1] where to insert position or -1
    function contains_element_ex(arr, value, index){
        var result = new Array();
        var new_node = true;
        var equal_id = -1;
        var less_id = -1;
        for(var i=0; i<arr.length; i++){
            if (String(arr[i].id) == String(value)){
                new_node = false;
                equal_id = i;
                break;
            }
            if (less_id == -1 && dict_man.greater(value, arr[i].id)){
                less_id = i;
            }
        }
        result[0] = new_node;
        if (!new_node)
            result[1] = equal_id;
        else
            result[1] = less_id;
        
        return result;
    }
    
    
    // JavaScript Document
    // <script language="JavaScript1.2" type="text/JavaScript">
    // Copyright (c)2005 Rewritten Software.  http://www.rewrittensoftware.com
    // This script is supplied "as is" witrhout any form of warranty. Rewritten Software 
    // shall not be liable for any loss or damage to person or property as a result of using this script.
    // Use this script at your own risk!
    // You are licensed to use this script free of charge for commercial or non-commercial use providing you do not remove 
    // the copyright notice or disclaimer.
    
    // Define the array that will contain the mapping table for ids to images.
    var iconMap = new Array();
    var iconList = new Array( iconMap );
    
    this.Toggle = function(item)
    {
        var idx = -1;
        for(var i = 0; i < iconList.length; i++ )
        {
            if( iconList[i][0] == item )
            {
                idx = i;
                break;
            }
        }
        
        if( idx < 0 )
            alert( "Could not find key in Icon List." );
               
        var div=document.getElementById("D"+item);
        var visible=(div.style.display!="none");
        var key=document.getElementById("P"+item);
        
        
        // Check if the item clicked has any children. If it does not then remove the plus/minus icon
        // and replace it with a transaparent gif.
        var removeIcon = div.hasChildNodes() == false;
        
        if( key != null )
        {
            if( !removeIcon )
            {
                if (visible)
                {
                    div.style.display="none";
                    key.innerHTML="<img src='"+images_path+"/tree/plus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
                }
                else
                {
                    div.style.display="block";
                    key.innerHTML="<img src='"+images_path+"/tree/minus.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
                }
            }
            else
                key.innerHTML="<img src='"+images_path+"/tree/transparent.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
        }
    
        // Toggle the icon for the tree item
        key=document.getElementById("I"+item);
        if( key != null )
        {
            if (visible)
            {
                div.style.display="none";
                key.innerHTML="<img src='"+iconList[idx][1]+"' width='16' height='16' hspace='0' vspace='0' border='0'>";
            }
            else
            {
                div.style.display="block";
                key.innerHTML="<img src='"+iconList[idx][2]+"' width='16' height='16' hspace='0' vspace='0' border='0'>";
            }
        }	
    }
    
    function Expand() {
       divs=document.getElementsByTagName("DIV");
       for (var i=0;i<divs.length;i++) {
         divs[i].style.display="block";
         key=document.getElementById("x" + divs[i].id);
         key.innerHTML="<img src='"+images_path+"/tree/textfolder.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
       }
    }
    
    function Collapse() {
       divs=document.getElementsByTagName("DIV");
       for (var i=0;i<divs.length;i++) {
         divs[i].style.display="none";
         key=document.getElementById("x" + divs[i].id);
         key.innerHTML="<img src='"+images_path+"/tree/folder.gif' width='16' height='16' hspace='0' vspace='0' border='0'>";
       }
    }
    
    function AddImage( parent, imgFileName )
    {
        img=document.createElement("IMG");
        img.setAttribute( "src", imgFileName );
        img.setAttribute( "width", 16 );
        img.setAttribute( "height", 16 );
        img.setAttribute( "hspace", 0 );
        img.setAttribute( "vspace", 0 );
        img.setAttribute( "border", 0 );
        parent.appendChild(img);
    }
    
    function CreateUniqueTagName( seed )
    {
        return "node_" + Math.random ( );
        var tagName = seed;
        var attempt = 0;
        
        if( tagName == "" || tagName == null )
            tagName = "x";
    
        while( document.getElementById(tagName) != null )
        {
            tagName = "x" + tagName;
            if( attempt++ > 50 )
            {
                alert( "Cannot create unique tag name. Giving up. \nTag = " + tagName );
                break;
            }
        }
        
        return tagName;
    }
    
    // Creates a new package under a parent. 
    // Returns a TABLE tag to place child elements under.
    function CreateTreeItem( parent, img1FileName, img2FileName, nodeName, url, target, leaf_node, insert_pos )
    {
        var uniqueId = CreateUniqueTagName( nodeName );
        for(var i=0; i < iconList.length; i++ )
            if( iconList[i][0] == uniqueId )
            {
                alert( "Non unique ID in Element Map. '" + uniqueId + "'" );
                // return;
            }
        iconList[iconList.length] = new Array( uniqueId, img1FileName, img2FileName );
    
        table = document.createElement("TABLE");
        if( parent != null ){
            if (insert_pos == -1)
                parent.appendChild( table );
            else
                parent.insertBefore(table, dict_nodes[insert_pos].html_obj.previousSibling); //1 => 6
        }
    
        table.setAttribute( "border", 0 );
        //table.setAttribute( "cellpadding", 2 );
        //table.setAttribute( "cellspacing", 2 );
        //table.setAttribute( "width", "100%" );
            
        tablebody = document.createElement("TBODY");
        table.appendChild(tablebody);
            
        row=document.createElement("TR");
        tablebody.appendChild( row );
    
        if (!leaf_node){
            // Create the cell for the plus and minus.
            cell=document.createElement("TD");
            cell.setAttribute( "width", 16 );
            row.appendChild(cell);
            
                // Create the hyperlink for plus/minus the cell
            a=document.createElement("A");
            cell.appendChild( a );
            a.setAttribute( "id", "P"+uniqueId );
            a.setAttribute( "href", "javascript:entries_module.viewer.Toggle(\""+uniqueId+"\");" );
            if (toggle_by_default)
                AddImage( a, images_path+"/tree/minus.gif" );        
            else
                AddImage( a, images_path+"/tree/plus.gif" );
            
            // Create the cell for the image.
            cell=document.createElement("TD");
            cell.setAttribute( "width", 16 );
            row.appendChild(cell);
                
/*          commented out: folder icon
            // all the event to call when the icon is clicked.
            a=document.createElement("A");
            a.setAttribute( "id", "I"+uniqueId );
            a.setAttribute( "href", "javascript:Toggle(\""+uniqueId+"\");" );
            cell.appendChild(a);
        
            // Add the image to the cell
            if (toggle_by_default)        
                AddImage( a, img2FileName );
            else
                AddImage( a, img1FileName );
*/        }
        // Create the cell for the text
        cell=document.createElement("TD");
        cell.setAttribute( "width", "100%" );
        //cell.noWrap = true;
        a=document.createElement("A");
        a.setAttribute( "id", uniqueId );
        if( url != null )
        {
            cell.appendChild( a );
            a.setAttribute( "href", url );
            if( target != null )
                a.setAttribute( "target", target );
            else
                a.setAttribute( "target", "_blank" );
            
            //text=document.createTextNode( nodeName );
            text=nodeName;
            a.appendChild(text);
        }
        else
        {
            if (isString(nodeName)){
                var div_text = document.createElement("div");
                div_text.innerHTML = nodeName;
                cell.appendChild(div_text);
            }else
                cell.appendChild(nodeName);
            if (font_size != "")
                cell.style.fontSize = font_size;
        }
        //log_info(4, "insert_tree", div_text);
        row.appendChild(cell);
        
        return CreateDiv( parent, uniqueId, insert_pos );
    }
    
    // Creates a new DIV tag and appends it to parent if parent is not null.
    // Returns the new DIV tag.
    function CreateDiv( parent, id, insert_pos )
    {
        div=document.createElement("DIV");
        if( parent != null ){
            if (insert_pos == -1)
                parent.appendChild( div );
            else
                parent.insertBefore(div, dict_nodes[insert_pos].html_obj.previousSibling); //.previousSibling
        }
            
        div.setAttribute( "id", "D"+id );
        if (toggle_by_default)        
            div.style.display  = "block";
        else
            div.style.display  = "none";
        div.style.marginLeft = "2em";
    
        return div;
    }
    
    // This is the root of the tree. It must be supplied as the parent for anything at the top level of the tree.
    var rootCell = null;
    var toggle_by_default = true;
    
    // This is the entry method into the Tree View. It builds an initial single row, single cell table tat will 
    // contain the tree. It initialises a global object "rootCell". This object must be used as the parent for all 
    // top-level tree elements.
    // There are two methods for creating tree elements: CreatePackage() and CreateNode(). The images for the 
    // package are hard coded. CreateNode() allows you to supply your own image for each node element.
    this.init = function (obj)
    {
    /*	body = document.getElementsByTagName("body").item(0);
        body.setAttribute( "leftmargin", 2 );
        body.setAttribute( "topmargin", 0 );
        body.setAttribute( "marginwidth", 0 );
        body.setAttribute( "marginheight", 0 );
    */	
        var table = document.createElement("TABLE");
        obj.appendChild( table );
    
        table.setAttribute( "border", 0 );
        table.setAttribute( "cellpadding", 1 );
        table.setAttribute( "cellspacing", 1 );
            
        var tablebody = document.createElement("TBODY");
        table.appendChild(tablebody);
            
        var row = document.createElement("TR");
        tablebody.appendChild(row);
            
        var cell = document.createElement("TD");
        row.appendChild(cell); 	
        
        rootCell = cell;	// Initialise the root of the tree view.
        this.clear();
    
        tree_parent = obj;
        toggle_by_default = true;
    }
    

}
