/*
	Expandable Div Menu Script
	Author: Olaf Maa� an base of a script from Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/


// #######################   initMenus   ########################
function initSelector(fromBoxId, toBoxId, toSelectFirstLevelObjId, toSelectNestedObjId) {
    if (!document.getElementsByTagName) return;

    var fromBox = document.getElementById(fromBoxId);
    if (!eval(fromBox))
        return;

    var aMenus = fromBox.getElementsByTagName("DIV");
    for (var i = 0; i < aMenus.length; i++) {
        var mclass = aMenus[i].className;
        if (mclass.indexOf("treenode") > -1) {
                       
            var submenu = getSubMenu(aMenus[i], aMenus);

            var mLevel = 0;
            if (aMenus[i].attributes["level"] != null) {
                mLevel = parseInt(aMenus[i].attributes["level"].value);
                if (isNaN(mLevel))
                    mLevel = 0;
            }

            if (submenu.length > 0) {
                mclass = (mclass.indexOf("selected") > -1) ? "treenodeselectedopen" : ((mclass.indexOf("open") > -1) ? "treenodeopen" : "treenodeclosed");
                aMenus[i].className = mclass;
                aMenus[i].onclick = function() {
                    opencloseNode(this, aMenus);
                    // ** check if we have to execute also the anchor link **
                    var anchorTag = this.getElementsByTagName("A")[0];
                    if (anchorTag != null) {
                        var ref = anchorTag.getAttribute("href");
                        if (ref != null && ref != "") {
                            /* 	unfortunately if you have a <a href=""></a> link in your code, in IExplorer
									the ref is not "" but the current URL. 
									So this check does not work for InternetExplorer. It has to be rendered as <a></a>.
								*/
                            return true;
                        }
                    }
                    return false;
                }
				if(aMenus[i].lastChild.className == "rightArrow") {
					aMenus[i].lastChild.onclick = function() {
	                	var toSelectObjId = null; 
	                	if(this.parentNode.getAttribute("isGroupItem")!=null) {
	                		toSelectObjId = (this.parentNode.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	} else {
	                		toSelectObjId = (this.parentNode.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	}

	                    opencloseNode(this.parentNode, aMenus);
	                	chooseItem(this.parentNode, fromBoxId, toBoxId, toSelectObjId);
	                    clearSelection();
	                    return false;
					}
					aMenus[i].childNodes[aMenus[i].childNodes.length-2].onclick = function() {
	                	var toSelectObjId = null; 
	                	if(this.parentNode.getAttribute("isGroupItem")!=null) {
	                		toSelectObjId = (this.parentNode.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	} else {
	                		toSelectObjId = (this.parentNode.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	}

	                    opencloseNode(this.parentNode, aMenus);
	                	chooseItem(this.parentNode, fromBoxId, toBoxId, toSelectObjId);
	                    clearSelection();
	                    return false;
					}
				}
                aMenus[i].ondblclick = function() {
                	var toSelectObjId = null; 
                	if(this.getAttribute("isGroupItem")!=null) {
                		toSelectObjId = (this.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
                	} else {
                		toSelectObjId = (this.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
                	}

                    chooseItem(this, fromBoxId, toBoxId, toSelectObjId);
                    clearSelection();
                    return false;
                }
                aMenus[i].onmouseover = function() {
                    this.className = this.className + "Over";
                    return true;
                }
                aMenus[i].onmouseout = function() {
                    var pos = this.className.indexOf("Over");
                    if (pos > 0) {
                        this.className = this.className.substring(0, pos);
                    }
                    return true;
                }
            } else {
                if (mclass.indexOf("selected") == -1) {
                    mclass = "treenode";
                }
                /*
                if (mLevel == 1) {
                    mclass = "level1" + mclass;
                } */
                aMenus[i].className = mclass;
                // ** add a function to switch class to selected **
                aMenus[i].onclick = function() {
                    // ** unselect all **
                    for (var z = 0; z < aMenus.length; z++) {
                        if (aMenus[z].className == "treenodeselected")
                            aMenus[z].className = "treenode";
                    }
                                        
                    this.className = "treenodeselectedOver";
                    return true;
                }
				if(aMenus[i].lastChild.className == "rightArrow") {
					aMenus[i].lastChild.onclick = function() {
	                	var toSelectObjId = null; 
	                	if(this.parentNode.getAttribute("isGroupItem")!=null) {
	                		toSelectObjId = (this.parentNode.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	} else {
	                		toSelectObjId = (this.parentNode.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	}
	                    chooseItem(this.parentNode, fromBoxId, toBoxId, toSelectObjId);
	                    clearSelection();
	                    return false;
					}
					aMenus[i].childNodes[aMenus[i].childNodes.length-2].onclick = function() {
	                	var toSelectObjId = null; 
	                	if(this.parentNode.getAttribute("isGroupItem")!=null) {
	                		toSelectObjId = (this.parentNode.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	} else {
	                		toSelectObjId = (this.parentNode.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
	                	}
	                    chooseItem(this.parentNode, fromBoxId, toBoxId, toSelectObjId);
	                    clearSelection();
	                    return false;
					}
				}
                aMenus[i].ondblclick = function() {
                	var toSelectObjId = null; 
                	if(this.getAttribute("isGroupItem")!=null) {
                		toSelectObjId = (this.getAttribute("isGroupItem")=="true") ? toSelectFirstLevelObjId : toSelectNestedObjId;
                	} else {
                		toSelectObjId = (this.getAttribute("level")=="1") ? toSelectFirstLevelObjId : toSelectNestedObjId;
                	}
                    chooseItem(this, fromBoxId, toBoxId, toSelectObjId);
                    clearSelection();
                    return false;
                }
                aMenus[i].onmouseover = function() {
                    this.className = this.className + "Over";
                    return true;
                }
                aMenus[i].onmouseout = function() {
                    var pos = this.className.indexOf("Over");
                    if (pos > 0) {
                        this.className = this.className.substring(0, pos);
                    }
                    return true;
                }

            }
            // ** show children of open nodes, hide children of closed nodes **
            for (var j = 0; j < submenu.length; j++) {
                if (submenu[j].className.indexOf("selected") == -1) {
                    submenu[j].style.display = (mclass.indexOf("open") > -1) ? "block" : "none";
                }
            }

            // ** set the padding in the enclosed A tag **
            var anchorTag = aMenus[i]; //.getElementsByTagName("TD");
			
            if (anchorTag != null) {
                //anchorTag.style.paddingLeft = (((mLevel * 6) + 0) + "px");  // ### padding inset
                //anchorTag.style.marginLeft = (((mLevel - 1) * 12) + 0 + "px"); // ### margin inset for normal menu items
                anchorTag.style.paddingLeft = (((mLevel - 1) * 12) + 24 + "px"); // ### padding inset for normal menu items
            }
        }
    }
}

function getSubMenu(node, allNodesList) {
    var submenu = new Array();
    if (node.attributes["mid"] != null) {
        for (var k = 0; k < allNodesList.length; k++) {
            if (allNodesList[k].attributes["parentmid"] != null) {
                if (allNodesList[k].attributes["parentmid"].value == node.attributes["mid"].value) {
                    submenu[submenu.length] = allNodesList[k];
                }
            }
        }
    }
        
    return submenu;
}

function showhideNode(node, showIt, aMenus) {
    node.style.display = (showIt) ? "block" : "none";

    // ** if hide and node is a folder, set class to closed **
    if (!showIt && node.className == "treenodeopen") {
        node.className = "treenodeclosed";
    }
    // ** also hide my children **
    if (!showIt) {
        var mySubmenu = getSubMenu(node, aMenus);
        for (var b = 0; b < mySubmenu.length; b++) {
            var mnode = mySubmenu[b];
            showhideNode(mnode, showIt, aMenus);
        }
    }
}

function opencloseNode(node, aMenus) {
    var showIt = (node.className.indexOf("closed") > -1);
    var mySubmenu = getSubMenu(node, aMenus);
					
    for (var z = 0; z < mySubmenu.length; z++) {
        var mnode = mySubmenu[z];
        showhideNode(mnode, showIt, aMenus);
    }
    node.className = (showIt) ? "treenodeopenOver" : "treenodeclosedOver";
}

function chooseItem(whichItem, fromBoxId, toBoxId, toSelectObjId) {
    // alert("Chosen " + whichItem.getElementsByTagName("span")[0].innerHTML + " (" + whichItem.attributes["mid"].value + ")");
    var fromBox = document.getElementById(fromBoxId);
    var toBox = document.getElementById(toBoxId);
    var selectObj = document.getElementById(toSelectObjId);
    if (!eval(fromBox) || !eval(toBox) || !eval(selectObj)) {
        return false;
    }
    // ** create new option in selectObj **
    var itemId = whichItem.getAttribute("mid");
    var existsInSelectObj = false;
    for (var i = 0; i < selectObj.length; i++) {
        if (selectObj.options[i].value == itemId) {
            existsInSelectObj = true;
            break;
        }
    }
    if (!existsInSelectObj) {
        selectObj.options[selectObj.length] = new Option(whichItem.getElementsByTagName("span")[0].innerHTML, itemId, false, true); // ** selected **
    }
    // ** create new DIV in toBox **
    if (!existsInSelectObj) {
        var myDiv = document.createElement("div");
        myDiv.setAttribute("id", itemId);
        myDiv.className = "chosenNode";
        myDiv.onmouseover = function() {
            this.className = this.className + "Over";
            return true;
        }
        myDiv.onmouseout = function() {
            var pos = this.className.indexOf("Over");
            if (pos > 0) {
                this.className = this.className.substring(0, pos);
            }
            return true;
        }
        myDiv.onclick = function() {
            removeChosen(this, toBoxId, toSelectObjId);
        }
        myDiv.innerHTML = "<span>" + whichItem.getElementsByTagName("span")[0].innerHTML + "</span>";
        toBox.appendChild(myDiv);
    }
    return true;
}

function removeChosen(whichItem, fromBoxId, fromSelectObjId) {
    var fromBox = document.getElementById(fromBoxId);
    var selectObj = document.getElementById(fromSelectObjId);
    if (!eval(fromBox) || !eval(selectObj)) {
        return false;
    }
    // ** create new option in selectObj **
    var itemId = whichItem.getAttribute("id");
    var existsInSelectObj = false;
    for (var i = 0; i < selectObj.length; i++) {
        if (selectObj.options[i].value == itemId) {
            existsInSelectObj = true;
            selectObj.options[i] = null;
            break;
        }
    }
    if (existsInSelectObj) {
        // ** also remove the DIV **
        var divList = fromBox.getElementsByTagName("div");
        for (var i = 0; i < divList.length; i++) {
            if (divList[i].getAttribute("id") == itemId) {
                fromBox.removeChild(divList[i]);
            }
        }
    }
}

function chooseAllFirstLevel(fromBoxId, toBoxId, toSelectObjId) {
    var fromBox = document.getElementById(fromBoxId);
    var toBox = document.getElementById(toBoxId);
    var selectObj = document.getElementById(toSelectObjId);
    if (!eval(fromBox) || !eval(toBox) || !eval(selectObj)) {
        return false;
    }
    var divList = fromBox.getElementsByTagName("div");
    for (var i = 0; i < divList.length; i++) {
    	if(divList[i].getAttribute("isGroupItem")!=null && divList[i].getAttribute("isGroupItem")=="true"  || divList[i].getAttribute("isGroupItem")==null && divList[i].getAttribute("level")=="1" ) {
			chooseItem(divList[i], fromBoxId, toBoxId, toSelectObjId);
    	}
    }
}

function chooseAllSecendLevelAndBelow(fromBoxId, toBoxId, toSelectObjId) {
    var fromBox = document.getElementById(fromBoxId);
    var toBox = document.getElementById(toBoxId);
    var selectObj = document.getElementById(toSelectObjId);
    if (!eval(fromBox) || !eval(toBox) || !eval(selectObj)) {
        return false;
    }
    var divList = fromBox.getElementsByTagName("div");
    for (var i = 0; i < divList.length; i++) {
    	if(divList[i].getAttribute("isGroupItem")!=null && divList[i].getAttribute("isGroupItem")=="false"  || divList[i].getAttribute("isGroupItem")==null &&  divList[i].getAttribute("level")!="1" ) {
			chooseItem(divList[i], fromBoxId, toBoxId, toSelectObjId);
    	}
    }
}

function removeAll(fromBoxId, fromSelectObjId) {
    var fromBox = document.getElementById(fromBoxId);
    var selectObj = document.getElementById(fromSelectObjId);
    if (!eval(fromBox) || !eval(selectObj)) {
        return false;
    }
    var divList = fromBox.getElementsByTagName("div");
    for (var i = divList.length - 1; i >= 0; i--) {
        removeChosen(divList[i], fromBoxId, fromSelectObjId);
    }
}

function clearSelection() {
  var sel;
  if(document.selection && document.selection.empty){
    document.selection.empty() ;
  } else if(window.getSelection) {
    sel=window.getSelection();
    if(sel && sel.removeAllRanges)
      sel.removeAllRanges() ;
  }
}

//window.onload = initSelectors;
