var cc_Tree_Image_Src              = './';
var cc_Tree_SessionActionOpenNode  = '';
var cc_Tree_SessionParamsOpenNode  = '';
var cc_Tree_SessionActionCloseNode = '';
var cc_Tree_SessionParamsCloseNode = '';
var cc_Tree_Icons			       = new Array(6);

// Loads all icons that are used in the tree
function cc_Tree_preloadIcons()
{
	cc_Tree_Icons[0] = new Image();
	cc_Tree_Icons[0].src = cc_Tree_Image_Src + 'images/organisation/plus.gif';
	cc_Tree_Icons[1] = new Image();
	cc_Tree_Icons[1].src = cc_Tree_Image_Src + 'images/organisation/plusbottom.gif';
	cc_Tree_Icons[2] = new Image();
	cc_Tree_Icons[2].src = cc_Tree_Image_Src + 'images/organisation/minus.gif';
	cc_Tree_Icons[3] = new Image();
	cc_Tree_Icons[3].src = cc_Tree_Image_Src + 'images/organisation/minusbottom.gif';
	cc_Tree_Icons[4] = new Image();
	cc_Tree_Icons[4].src = cc_Tree_Image_Src + 'images/organisation/folder.gif';
	cc_Tree_Icons[5] = new Image();
	cc_Tree_Icons[5].src = cc_Tree_Image_Src + 'images/organisation/folderopen.gif';
}

function cc_Tree_Lnk()
{
    this.nom       = cc_Tree_Lnk.arguments[0];
    this.lien      = cc_Tree_Lnk.arguments[1];
    this._class     = cc_Tree_Lnk.arguments[2];
    this._style     = cc_Tree_Lnk.arguments[3];
    this.functions = new Array();
    this.linkposition = 'after';

    if( this.lien == '' || typeof this.lien == 'undefined') this.lien = 'javascript:void(0);';

    this.setClass = function(c) { this._class=c; };
    this.setStyle = function(s) { this._style=s; };

    // Defines if additional links will be displayed before or after main link
    this.setLinkPosition = function(position)
    {
        if( position != 'before' )
            position = 'after';
        this.linkposition = position;
    }

    this.setAction    = cc_Tree_setAction;
    this.getActionKey = cc_Tree_getActionKey;
}

function cc_Tree_Node()
{
    this.id        = cc_Tree_Node.arguments[0];
    this.parent    = cc_Tree_Node.arguments[1];
    this.nom       = cc_Tree_Node.arguments[2];
    this.lien      = cc_Tree_Node.arguments[3];
    this.isOpen    = cc_Tree_Node.arguments[4];
    this._class     = cc_Tree_Node.arguments[5];
    this._style     = cc_Tree_Node.arguments[6];
    this._diese     = cc_Tree_Node.arguments[7];
    this.functions = new Array();
    this.links     = new Array();

    //if( this.lien == '' || typeof this.lien == 'undefined') this.lien = 'javascript:void(0);';
    if( this.lien == '' || typeof this.lien == 'undefined') this.lien = '';
    if( this._diese )
        this.lien += '#cc_Tree_'+this.id;
        
    this.setClass = function(c) { this._class=c; };
    this.setStyle = function(s) { this._style=s; };

    this.addLnk       = cc_Tree_addLnk;    
    this.setAction    = cc_Tree_setAction;
    this.getActionKey = cc_Tree_getActionKey;
}

function cc_Tree_addLnk(n,l,c,s)
{
    var l = new cc_Tree_Lnk(n,l,c,s);
    this.links[ this.links.length ] = l;
    return l;
}
    
function cc_Tree_setAction(a,v)
{
    var k = this.getActionKey(a);
    if( k )
        this.functions[k] = new Array(a,v);
    else
        this.functions[this.functions.length] = new Array(a,v);
}

function cc_Tree_getActionKey(a)
{
    for(var i=0; i < this.functions.length; i++)
    {
        var f = this.functions[i];
        if( f[0] == a ) return i;
    }
    return 0;
}

function cc_Tree_buildLinks(links, linkposition)
{
    var buf = '';
	for(var i=0; i < links.length; i++)
	{
	    var l = links[i];

        if( l.linkposition == linkposition )
        {
    	    if(linkposition == 'after' )
    	        buf += '&nbsp;';

    	    buf += '<a href="'+l.lien+'"';
    	    if( l._class ) buf += ' class="'+l._class+'"';
    	    if( l._style ) buf += ' style="'+l._style+'"';
    	    buf += cc_Tree_buildFunctions(l.functions);
    	    buf += '>'+l.nom+'</a>';

    	    if(linkposition == 'before' )
    	        buf += '&nbsp;';

        }
	}
	return buf;
}

function cc_Tree_buildFunctions(functions)
{
    var buf = '';
	for(var i=0; i < functions.length; i++)
	{
	    var f = functions[i];
	    buf += ' '+f[0]+'="'+f[1]+'"';
	}
	return buf;
}

function cc_Tree()
{
    cc_Tree_preloadIcons();
    this.nodes              = new Array();
    this.openNodes          = new Array();
    this.treeFirstNodeTitle = cc_Tree.arguments[0];
    this.lien               = cc_Tree.arguments[1];
    this._class             = cc_Tree.arguments[2];
    this._style             = cc_Tree.arguments[3];
    this.functions          = new Array();
    this.links              = new Array();

    if( this.treeFirstNodeTitle == '' || typeof this.treeFirstNodeTitle == 'undefined') this.treeFirstNodeTitle = 'Racine';
    if( this.lien == '' || typeof this.lien == 'undefined') this.lien = 'javascript:void(0);';

    this.setClass = function(c) { this._class=c; };
    this.setStyle = function(s) { this._style=s; };
    
    this.addLnk       = cc_Tree_addLnk;    
    this.setAction    = cc_Tree_setAction;
    this.getActionKey = cc_Tree_getActionKey;

    this.setAjaxActionOpenNode = function(a,p)
    {
        cc_Tree_SessionActionOpenNode = a;
        cc_Tree_SessionParamsOpenNode = p;
    }

    this.setAjaxActionCloseNode = function(a,p)
    {
        cc_Tree_SessionActionCloseNode = a;
        cc_Tree_SessionParamsCloseNode = p;
    }

    this.addLine = function(i,p,n,l,o,c,s,d)
    {        
        var n = new cc_Tree_Node(i,p,n,l,o,c,s,d);
        this.nodes[ this.nodes.length ] = n;
        return n;
    }

    this.display = function()
    {
        // affichage racine
		var racine = '';

        var openlink   =   '';
		openlink += '<a id="cc_Tree_Lnk_0" href="' + this.lien + '"';
	    if( this._class ) openlink += ' class="'+this._class+'"';
	    if( this._style ) openlink += ' style="'+this._style+'"';
	    openlink += cc_Tree_buildFunctions(this.functions);
	    openlink += '>';
		racine += openlink + '<img src="'+ cc_Tree_Image_Src + 'images/organisation/base.gif" align="absbottom" alt="" /></a>'
        racine += cc_Tree_buildLinks(this.links,'before');
		racine += openlink + this.treeFirstNodeTitle+'</a>';
        racine += cc_Tree_buildLinks(this.links,'after');
        racine += '<br />';
        document.write(racine);
        var recursedNodes = new Array();
        this.affNodes(0,recursedNodes);
    }

    // Checks if a node has any children
    this.hasChildNode = function(parentNode)
    {
    	for(i=0; i < this.nodes.length; i++)
    	{
    		var n = this.nodes[i];
    		if(n.parent == parentNode) return true;
    	}
    	return false;
    }

    // Checks if a node is the last sibling
    this.lastSibling = function(node, parentNode)
    {
    	var lastChild = 0;
    	for(i=0; i < this.nodes.length; i++)
    	{
    		var n = this.nodes[i];
    		if (n.parent == parentNode)
    			lastChild = n.id;
    	}
    	if (lastChild==node) return true;
    	return false;
    }

    // Checks if a node is open
    this.isNodeOpen = function(node)
    {
    	for(i=0; i < this.openNodes.length; i++)
    		if (this.openNodes[i]==node) return true;
    	return false;
    }

    // Puts in array nodes that will be open
    this.setOpenNodes = function(openNode)
    {
    	for (i=0; i < this.nodes.length; i++)
    	{
    		var n = this.nodes[i];
    		if (n.id==openNode)
    		{
    			this.openNodes.push(n.id);
    			this.setOpenNodes(n.parent);
    		}
    	} 
    }

    this.affNodes = function(p,recursedNodes)
    {
        for (var i=0; i < this.nodes.length; i++)
        {
            var n = this.nodes[i];
            if( n.parent != p ) continue;
            
            //Ouverture du noeud initialisé en ouvert
            if( n.isOpen )
                this.setOpenNodes(n.id);
            
			var ls	= this.lastSibling(n.id, n.parent);
			var hcn	= this.hasChildNode(n.id);
			var ino = this.isNodeOpen(n.id);

			// Write out line & empty icons
			for (g=0; g < recursedNodes.length; g++)
			{
				if (recursedNodes[g] == 1)
				    document.write('<img src="'+cc_Tree_Image_Src+'images/organisation/line.gif" align="absbottom" alt="" />');
				else
				    document.write('<img src="'+cc_Tree_Image_Src+'images/organisation/empty.gif" align="absbottom" alt="" />');
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else    recursedNodes.push(1);
            
			// Write out join icons
			if (hcn)
			{
				document.write('<a href="javascript: cc_Tree_OpenClose(' + n.id + ', '+(ls?1:0)+');"><img id="cc_Treee_Join_' + n.id + '" src="'+cc_Tree_Image_Src+'images/organisation/');
				document.write(ino?'minus':'plus');
                document.write('bottom.gif" align="absbottom" alt="Open/Close node" /></a>');
			}
			else
			{
			    document.write('<img src="'+cc_Tree_Image_Src+'images/organisation/'+(ls?'joinbottom':'join')+'.gif" align="absbottom" alt="" />');
			}

			// construction du lien
			var openlien = '<a name="cc_Tree_'+n.id+'" id="cc_Tree_Lnk_'+n.id+'" href="' + n.lien + '"';
		    if( n._class ) openlien += ' class="'+n._class+'"';
		    if( n._style ) openlien += ' style="'+n._style+'"';
		    openlien += cc_Tree_buildFunctions(n.functions);
			openlien += '>';

            var lien = openlien;

			// Write out folder & page icons
			if (hcn)
			{
			    lien += '<img id="cc_Treee_Icon_' + n.id + '" src="'+cc_Tree_Image_Src+'images/organisation/folder';
				if (ino) lien += 'open';
				lien += '.gif" align="absbottom" alt="Folder" />';
			}
			else
			    lien += '<img id="cc_Treee_Icon_' + n.id + '" src="'+cc_Tree_Image_Src+'images/organisation/page.gif" align="absbottom" alt="Page" />';
			
			lien += '</a>';
			lien += cc_Tree_buildLinks(n.links,'before');
			lien += openlien + n.nom + '</a>';
			lien += cc_Tree_buildLinks(n.links,'after');
			lien += '<br />';
			document.write(lien);

			if(hcn)
			{
				document.write('<div id="cc_Treee_Div_' + n.id + '"');
					if (!ino) document.write(' style="display: none;"');
				document.write('>');
				this.affNodes(n.id,recursedNodes);
				document.write('</div>');
			}
			// remove last line or empty icon 
			recursedNodes.pop();
        }
    }
}

// Opens or closes a node
function cc_Tree_OpenClose(node, bottom)
{
	var theDiv  = $('cc_Treee_Div_'  + node);
	var theJoin	= $('cc_Treee_Join_' + node);
	var theIcon = $('cc_Treee_Icon_' + node);

	if( theDiv.style.display == 'none' )
    {
		if (bottom==1) theJoin.src = cc_Tree_Icons[3].src;
		else theJoin.src = cc_Tree_Icons[2].src;
		theIcon.src = cc_Tree_Icons[5].src;
		theDiv.style.display = '';

        if( cc_Tree_SessionActionOpenNode )
        {
            data = 'do='+cc_Tree_SessionActionOpenNode+'&node_id=' + node;
            if( cc_Tree_SessionParamsOpenNode ) data += '&'+cc_Tree_SessionParamsOpenNode;
            sendData('null',data, ajaxTree, 'GET');
        }
	}
	else
    {
		if (bottom==1) theJoin.src = cc_Tree_Icons[1].src;
		else theJoin.src = cc_Tree_Icons[0].src;
		theIcon.src = cc_Tree_Icons[4].src;
		theDiv.style.display = 'none';

        if( cc_Tree_SessionActionCloseNode )
        {
            data = 'do='+cc_Tree_SessionActionCloseNode+'&node_id=' + node;
            if( cc_Tree_SessionParamsCloseNode ) data += '&'+cc_Tree_SessionParamsCloseNode;
            sendData('null',data, ajaxTree, 'GET');
        }
	}
}
// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}
