




/*
     FILE ARCHIVED ON 0:01:11 1 29, 2011 AND RETRIEVED FROM THE
     INTERNET ARCHIVE ON 16:50:34 10 14, 2011.
     JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.

     ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
     SECTION 108(a)(3)).
*/
/*
 * for IE , the coordinates are calculated differently
 * @param co the original cooridnates from mootools getCoordinates() function
 */
function rectifyCoordinates(co){
	return co
    if(Browser.Engine.name=='trident'){
        co.left += 10;
        co.bottom += 15;
    }
    
    return co
}

/**
 * run through the element and get all the A tags
 * 
 * @param el the LI element
 * @return array of A
 */
function parseMenu(el){
    var result = [];
    $each(el.childNodes, function(child, index){
        if (child.nodeName.toString().toUpperCase() == "A"){
            result.extend([child]);
        } else {
            result.extend( parseMenu(child) );
        }
    });
                
    return result;
}
            
/*
 * each item in this array must have 2 properties
 * 
 * {el:theSubMenuDivElement, status:trueToHideTheMenuDiv, head:headerElement}
 */
var hideMenuFlag = new Array();

            
/*
 * you need to add a div tag wiz id spiralmenu-log
 * @param msg the message to be shown
 */            
function log(msg){
    return;
    $("spiralmenu-log").appendText(msg);
    $("spiralmenu-log").appendChild(new Element("br"));
}
            

/*
 * Build the menu from an Element
 * based the parseMenu, all the A tags will be extracted from the el
 * and 1st A is the main menu item
 * and other A tags are then in the submenu
 * @param el the Element
 * @return void
 */
function buildMenu(el){
    // build up sub menu
    log("building menu");
    var menuList = parseMenu($(el));
    var co = $(menuList[0]).getCoordinates();
                
    // small offset for IE
    if(Browser.Engine.name=='trident'){
        co.left += 10;
        co.bottom += 15;
    }
            
    /* sub menu place holder
     * this will be append to the end of BODY tag
     * starts wiz invisible and absolute position 
     */
    var div = new Element("div", {
        "class": "sub_menu_container",
        "styles":{
            "display":"block",
            "visibility": "hidden",
            "position": "absolute",
            "left": co.left,
            "top": co.bottom
        }
    });
    
    div.fade("hide");

    var menuIndex = hideMenuFlag.length;
    hideMenuFlag[menuIndex] = {el:div, status: true, head:null};

    /*
     * see the hideMenuFlag for the item spec
     * according to the status, either fade in or fade out
     */
    function setDivVisible(index){  
        
        var obj = hideMenuFlag[index];
        var op = obj.status ? "out": 1;
        
        var co = rectifyCoordinates($(obj.head).getCoordinates());
        
        obj.el.setStyle("left", co.left);
        obj.el.setStyle("top", co.bottom);
        obj.el.fade(op);
    }

    /* 
     * wrap the A tag wiz div , possibly set 
     * a style to the div tag 
     */
    $each (menuList, function(m, i){
        if(i != 0){
            e = new Element("div" ,{"class":"sub_menu_item"});
            e.appendChild($(m));
            div.appendChild(e);

            // for each item setup the same events
            $(m).addEvent("mouseout", function(){
                hideMenuFlag[menuIndex].status = true;
                setDivVisible.delay(500, null, menuIndex);
                log('mouseout')
            });
            
            $(m).addEvent("mouseover", function(){
                hideMenuFlag[menuIndex].status = false;
                log("mouseover")
            })   
        } else {
            hideMenuFlag[menuIndex].head = m;
        }
    });
                
                
    /* add event , on mouse over, show sub menu */
    $(menuList[0]).addEvent("mouseover", function(){ 
        hideMenuFlag[menuIndex].status = false;
        setDivVisible(menuIndex);
    });
                
    $(menuList[0]).addEvent("mouseout", function(){
        hideMenuFlag[menuIndex].status = true;
        setDivVisible.delay(500,null, menuIndex);
    });
                
    $(document.body).appendChild(div);
}




