//Pop-it menu- By Dynamic Drive
//For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
//This credit MUST stay intact for use
page_path = "/"+page_lang+"/admin/";
page_path_real = "/"+page_lang+"/";
page_ext = "";

var tmp=new Array();

if (!typeof(TreeRoot)=="undefined") {
        TreeRoot = "Website";
}
site_id = 1;
domain_id = 1;
////No need to edit beyond here

var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all
var ns4=document.layers

function showmenu(e,which){
if (!document.all&&!document.getElementById&&!document.layers)
return

clearhidemenu()

menuobj=ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj

if (ie4||ns6)
menuobj.innerHTML=which
else{
menuobj.document.write('<layer name=gui bgColor=#E6E6E6 width=165 oncontextmenu="clearhidemenu();return false;" onmouseout="hidemenu()">'+which+'</layer>')
menuobj.document.close()
}

menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height
eventX=ie4? event.clientX : ns6? e.clientX : e.x
eventY=ie4? event.clientY : ns6? e.clientY : e.y

//Find out how close the mouse is to the corner of the window
var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<menuobj.contentwidth)
//move the horizontal position of the menu to the left by it's width
menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth
else
//position the horizontal position of the menu where the mouse was clicked
menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX

//same concept with the vertical position
if (bottomedge<menuobj.contentheight)
menuobj.thestyle.top=ie4? document.body.scrollTop+eventY-menuobj.contentheight : ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight
else
menuobj.thestyle.top=ie4? document.body.scrollTop+event.clientY : ns6? window.pageYOffset+eventY : eventY
menuobj.thestyle.visibility="visible"
return false
}

function contains_ns6(a, b) {
//Determines if 1 element in contained in another- by Brainjar.com
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}

function hidemenu(){
if (window.menuobj)
menuobj.thestyle.visibility=(ie4||ns6)? "hidden" : "hide"
}

function dynamichide(e){
if (ie4&&!menuobj.contains(e.toElement))
hidemenu()
else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
hidemenu()
}

function delayhidemenu(){
if (ie4||ns6||ns4)
delayhide=setTimeout("hidemenu()",500)
}

function clearhidemenu(){
if (window.delayhide)
clearTimeout(delayhide)
}

function highlightmenu(e,state){
if (document.all)
source_el=event.srcElement
else if (document.getElementById)
source_el=e.target
if (source_el.className=="menuitems"){
source_el.id=(state=="on")? "mouseoverstyle" : ""
}
else{
while(source_el.id!="popmenu"){
source_el=document.getElementById? source_el.parentNode : source_el.parentElement
if (source_el.className=="menuitems"){
source_el.id=(state=="on")? "mouseoverstyle" : ""
}
}
}
}

if (ie4||ns6)
document.onclick=hidemenu

/**************************************************************************
        Copyright (c) 2001 Geir Landrö (drop@destroydrop.com)
        JavaScript Tree - www.destroydrop.com/hugi/javascript/tree/
        Version 0.96

        This script can be used freely as long as all copyright messages are
        intact.
**************************************************************************/

// Arrays for nodes and icons
var nodes                = new Array();
var entriesIndex = new Array();
var openNodes        = new Array();
var icons                = new Array(6);
//var ImgFolder = "/portal/html";
var ImgFolder = "";
//var AbsPath = "/portal/html"; 
var AbsPath = ""; 

// Loads all icons that are used in the tree
function preloadIcons() {
        icons[0] = new Image();
        icons[0].src = ImgFolder+"/i/tree/plus.gif";
        icons[1] = new Image();
        icons[1].src = ImgFolder+"/i/tree/plusbottom.gif";
        icons[2] = new Image();
        icons[2].src = ImgFolder+"/i/tree/minus.gif";
        icons[3] = new Image();
        icons[3].src = ImgFolder+"/i/tree/minusbottom.gif";
        icons[4] = new Image();
        icons[4].src = ImgFolder+"/i/tree/folder.gif";
        icons[5] = new Image();
        icons[5].src = ImgFolder+"/i/tree/folderopen.gif";
}
// Create the tree
function createTree(arrName, _entriesIndex, startNode, openNode) {
        nodes = arrName;
        entriesIndex=_entriesIndex;
        if (nodes.length > 0) {
                preloadIcons();
                if (startNode == null) startNode = 0;

                if (openNode != 0 || openNode != null) setOpenNodes(openNode);

                if (startNode !=0) {
                        var nodeValues = nodes[getArrayId(startNode)];
                                                
                        linkset[0] = "";
                        linkset[0]+='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'">'+locale[STR_VIEW_IN_NEW_WIN]+'</a></div>'
                        linkset[0]+='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'">'+locale[STR_EDIT_CONTENT]+'</a></div>'
                        linkset[0]+='<div class="menuitems"><a href="javascript:ask_before_delete(\''+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=del_all&site_id='+site_id+'\');">'+locale[STR_DEL_ALL]+'</a></div>';
                        document.write("<a href=\"" + nodeValues[3] + "\" oncontextmenu=\"showmenu(event,linkset[0]);return false;\" onMouseout=\"delayhidemenu()\"><img src=\""+ImgFolder+"/i/tree/folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
                        
                } else {
                        tmp_root='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=add&site_id='+site_id+'">'+locale[STR_ADD_PAGE]+'</a></div>';
                        if(isDisplayDomain){
	                        tmp_root+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=copy_site&site_id='+site_id+'">'+locale[STR_COPY_SITE]+'</a></div>';
                        }
                        tmp_root+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=edit&site_id='+site_id+'">'+locale[STR_PROPERITES]+'</a></div>';
                        tmp_root+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=domain&_c=edit&site_id='+site_id+'">'+locale[STR_DOMAIN_PROPERITES]+'</a></div>';						
                        tmp_root+='<div class="menuitems"><a href="javascript:void(0);" onclick="window.open(\''+page_path_real+'rights/setPrivileges.html'+page_ext+'?s=print&object=site&element='+site_id+'\',\'access_open_window\',\'menubar=no,location=no,scrollbars=yes,resizable=yes,width=500,height=600\');">'+locale[STR_SITE_RIGHTS]+'</a></div>';
						tmp_root+='<div class="menuitems"><a href="javascript:void(0);" onclick="window.open(\''+page_path_real+'rights/setPrivileges.html'+page_ext+'?s=print&object=domain&element='+domain_id+'\',\'access_open_window\',\'menubar=no,location=no,scrollbars=yes,resizable=yes,width=500,height=600\');">'+locale[STR_DOMAIN_RIGHTS]+'</a></div>';
                        
//                        tmp_root+='<div class="menuitems"><a href="javascript:ask_before_delete(\''+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=del_all&site_id='+site_id+'\');">'+locale[STR_DEL_ALL]+'</a></div>';
                        document.write("<a href=\"javascript:void(0);\" oncontextmenu=\"showmenu(event,tmp_root);return false;\" onMouseout=\"delayhidemenu()\">");
                        document.write("<img src=\""+ImgFolder+"/i/tree/base.gif\" align=\"absbottom\" alt=\"\" />"+TreeRoot+"<br />");
                        document.write("</a>");
                }
                var recursedNodes = new Array();
                addNode(startNode, recursedNodes);
        } else {
                tmp_root='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=add&site_id='+site_id+'">'+locale[STR_ADD_PAGE]+'</a></div>';
                tmp_root+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=edit&site_id='+site_id+'">'+locale[STR_PROPERITES]+'</a></div>';
                tmp_root+='<div class="menuitems"><a href="javascript:ask_before_delete(\''+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=sites&_c=del&site_id='+site_id+'\');">'+locale[STR_DELETE]+'</a></div>';
				tmp_root+='<div class="menuitems"><a href="javascript:void(0);" onclick="window.open(\''+page_path_real+'rights/setPrivileges.html'+page_ext+'?s=print&object=site&element='+site_id+'\',\'access_open_window\',\'menubar=no,location=no,scrollbars=yes,resizable=yes,width=500,height=600\');">'+locale[STR_RIGHTS]+'</a></div>';                
                document.write("<a href=\"javascript:void(0);\" oncontextmenu=\"showmenu(event,tmp_root);return false;\" onMouseout=\"delayhidemenu()\">");
                document.write("<img src=\""+ImgFolder+"/i/tree/base.gif\" align=\"absbottom\" alt=\"\" />"+TreeRoot+"<br />");
                document.write("</a>");
        }
}
// Returns the position of a node in the array
function getArrayId(node) {
        for (i=0; i<nodes.length; i++)
                if (nodes[i][0]==node) return i;
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
        for (i=0; i<nodes.length; i++) {
                if (nodes[i][0]==openNode) {
                        openNodes.push(nodes[i][0]);
                        setOpenNodes(nodes[i][1]);
                }
        }
}
// Checks if a node is open
function isNodeOpen(node) {
        for (i=0; i<openNodes.length; i++)
                if (openNodes[i]==node) return true;
        return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
        return entriesIndex[parentNode]?true:false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
        if(entriesIndex[parentNode] && (nodes[entriesIndex[parentNode][1]][0]==node))return true;
        return false;
}
// Adds a new node in the tree
function addNode(parentNode, recursedNodes) {
        var start=0;
        var stop=-1;
//        alert(imgFolder);
        //if(parentNode!=0){
                if(entriesIndex[parentNode]){
                        start=entriesIndex[parentNode][0];
                        stop=entriesIndex[parentNode][1];
                }
        //}else{
//                start=0;
//                stop=nodes.length-1;
//        }
//        alert(start+":"+stop);
//        start=0;
//        stop=nodes.length-1;
        for (var i = start; i <= stop; i++) {
                var nodeValues = nodes[i]/*.split("|")*/;
                        var ls  = (i==stop);
                        var hcn = entriesIndex[nodeValues[0]]?true:false;
                        var ino = isNodeOpen(nodeValues[0]);

                        // Write out line & empty icons
                        for (g=0; g<recursedNodes.length; g++) {
                                if (recursedNodes[g] == 1) document.write("<img src=\""+ImgFolder+"/i/tree/line.gif\" align=\"absbottom\" alt=\"\" />");
                                else  document.write("<img src=\""+ImgFolder+"/i/tree/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) {
                                if (ls) {
                                        document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\""+ImgFolder+"/i/tree/");
                                                if (ino) document.write("minus");
                                                else document.write("plus");
                                        document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
                                } else {
                                        document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\""+ImgFolder+"/i/tree/");
                                                if (ino) document.write("minus");
                                                else document.write("plus");
                                        document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
                                }
                        } else {
                                if (ls) document.write("<img src=\""+ImgFolder+"/i/tree/join.gif\" align=\"absbottom\" alt=\"\" />");
                                else document.write("<img src=\""+ImgFolder+"/i/tree/joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
                        }
                        // Write out folder & page icons
                        if (hcn) {
                                document.write("<img id=\"icon" + nodeValues[0] + "\" src=\""+ImgFolder+"/i/tree/folder")
                                        if (ino) document.write("open");
                                document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
                        } else {
                                if (nodeValues[4]==1) {
                                        document.write("<img id=\"icon" + nodeValues[0] + "\" src=\""+ImgFolder+"/i/tree/page.gif\" align=\"absbottom\" alt=\"Page\" />");
                                        is_folder=0;
                                } else {
                                        is_folder=1;
                                        document.write("<img id=\"icon" + nodeValues[0] + "\" src=\""+ImgFolder+"/i/tree/folder.gif\" align=\"absbottom\" alt=\"Page\" />");
                                }
                        }
                        // Start link
                        var mod_info = "";
                        if (nodeValues[5]) {
                                var mod_info = "<hr><div class=mod_info>"+locale[STR_MOD_USER]+":&nbsp;"+nodeValues[5]+"<br>"+locale[STR_MOD_DATE]+":&nbsp;"+nodeValues[6]+"<br>&nbsp;</div>";
                        }
                        if (hcn||is_folder) {
                                if (show_menu) {
                                        tmp[nodeValues[0]]='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=add&cat_id='+nodeValues[0]+'&site_id='+site_id+'">'+locale[STR_ADD_PAGE]+'</a></div>';
                                        //tmp[nodeValues[0]]+='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'">'+locale[STR_VIEW]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="#" onClick="javascript:window.open(\''+nodeValues[3]+'\');">'+locale[STR_VIEW_IN_NEW_WIN]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'?c=edit&_c=bo">'+locale[STR_EDIT_CONTENT]+'</a></div>';
                   						tmp[nodeValues[0]]+='<div class="menuitems"><a href="javascript:void(0);" onclick="window.open(\''+page_path_real+'rights/setPrivileges.html'+page_ext+'?s=print&object=page&element='+nodeValues[0]+'\',\'access_open_window\',\'menubar=no,location=no,scrollbars=yes,resizable=yes,width=500,height=600\');">'+locale[STR_RIGHTS]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="javascript:ask_before_delete(\''+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=del&page_id='+nodeValues[0]+'&site_id='+site_id+'\');">'+locale[STR_DELETE]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=edit&page_id='+nodeValues[0]+'&site_id='+site_id+'">'+locale[STR_PROPERITES]+'</a></div>';
                                        tmp[nodeValues[0]]+=mod_info;
//                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="structure.html'+page_ext+'?c=new_step_2&choise=cat&id='+nodeValues[0]+'">'+locale[STR_ADD_CAT]+'</a></div>';
//                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="structure.html'+page_ext+'?c=rights&id='+nodeValues[0]+'&cat=1">'+locale[STR_RIGHTS]+'</a></div>';
//                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="javascript:ask_before_delete(\'structure.html'+page_ext+'?c=delete&id='+nodeValues[0]+'\');">'+locale[STR_DELETE]+'</a></div>';
//                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="structure.html'+page_ext+'?c=edit&id='+nodeValues[0]+'">'+locale[STR_PROPERITES]+'</a></div>';
//                                        document.write("<a href=\"#\" oncontextmenu=\"showmenu(event,tmp["+nodeValues[0]+"])\" onMouseout=\"delayhidemenu()\">");
                                        document.write("<a href=\"" + nodeValues[3] + "\" oncontextmenu=\"showmenu(event,tmp["+nodeValues[0]+"]);return false;\" onMouseout=\"delayhidemenu()\">");
                                } else {
                                        document.write("<a href=\"" + nodeValues[3] + "\">");
//                                        document.write("<a href=\"#\">");
                                }
                        } else {
                                if (show_menu) {
                                        tmp[nodeValues[0]] = "";
                                        //tmp[nodeValues[0]]='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'">'+locale[STR_VIEW]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="#" onClick="javascript:window.open(\''+AbsPath+nodeValues[3]+'\');">'+locale[STR_VIEW_IN_NEW_WIN]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="'+AbsPath+nodeValues[3]+'?c=edit&_c=bo">'+locale[STR_EDIT_CONTENT]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="javascript:void(0);" onclick="window.open(\''+page_path_real+'rights/setPrivileges.html'+page_ext+'?s=print&object=page&element='+nodeValues[0]+'\',\'access_open_window\',\'menubar=no,location=no,scrollbars=yes,resizable=yes,width=500,height=600\');">'+locale[STR_RIGHTS]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="javascript:ask_before_delete(\''+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=del&page_id='+nodeValues[0]+'&site_id='+site_id+'\');">'+locale[STR_DELETE]+'</a></div>';
                                        tmp[nodeValues[0]]+='<div class="menuitems"><a href="'+AbsPath+page_path+'structure.html'+page_ext+'?_m=structure&_t=pages&_c=edit&page_id='+nodeValues[0]+'&site_id='+site_id+'">'+locale[STR_PROPERITES]+'</a></div>';
                                        tmp[nodeValues[0]]+=mod_info;
                                        document.write("<a href=\"" + nodeValues[3] + "\" oncontextmenu=\"showmenu(event,tmp["+nodeValues[0]+"]);return false;\" onMouseout=\"delayhidemenu()\">");
                                } else {
                                        document.write("<a href=\"" + nodeValues[3] + "\">");
                                }
                        }


                        // Write out node name
                        document.write(nodeValues[2]);

                        // End link
                        document.write("</a><br />");

                        // If node has children write out divs and go deeper
                        if (hcn) {
                                document.write("<div id=\"div" + nodeValues[0] + "\"");
                                        if (!ino) document.write(" style=\"display: none;\"");
                                document.write(">");
                                addNode(nodeValues[0], recursedNodes);
                                document.write("</div>");
                        }

                        // remove last line or empty icon
                        recursedNodes.pop();
        }
}
// Opens or closes a node
function oc(node, bottom) {
        var theDiv = document.getElementById("div" + node);
        var theJoin        = document.getElementById("join" + node);
        var theIcon = document.getElementById("icon" + node);

        if (theDiv.style.display == 'none') {
                if (bottom==1) theJoin.src = icons[3].src;
                else theJoin.src = icons[2].src;
                theIcon.src = icons[5].src;
                theDiv.style.display = '';
        } else {
                if (bottom==1) theJoin.src = icons[1].src;
                else theJoin.src = icons[0].src;
                theIcon.src = icons[4].src;
                theDiv.style.display = 'none';
        }
}
// Push and pop not implemented in IE(crap!    don´t know about NS though)
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;
}


