// Author: Jordy Boezaard
// Bitterzoet Media
var cMenu = {
    // Configuration
    iFps:                   30,     // Frames per second
    iSpeed:                 3,      // Animation speed
    
    // Vars
    iMenuOffsetLeft:        null,
    sSelectedMenu:          null,
    iHighlightStartPos:     null,
    iAnimationTimer:        null,
    iReturnDelay:           null,
    oHighlight:             null,
    aMenus:                 new Array(),
    iTargetPos:             null,

    init:               function() {
    						var oMenuList = document.getElementById('menu');
    
                            // Set iHighlightOffsetLeft
                            cMenu.iMenuOffsetLeft = oMenuList.offsetLeft;

                            // Highlight
                            cMenu.oHighlight = document.getElementById('menuArrow');
                            
                            // All menubuttons
                            var aMenuButtons = oMenuList.getElementsByTagName('li');
                            
                            // Loop through buttons
                            var i = aMenuButtons.length;                        
                            while (i--) {
                                cMenu.aMenus[i] = aMenuButtons[i].id;
                                
                                aMenuButtons[i].onmouseover = cMenu.onMenuMouseOver;
                                aMenuButtons[i].onmouseout = cMenu.onMouseOut;
                                
                                if (aMenuButtons[i].className == 'menu_selected') {
                                    // Save name selected item
                                    cMenu.sSelectedMenu = aMenuButtons[i].id;
                                    
                                    // Set start positie highlight
                                    cMenu.iHighlightStartPos = cMenu.iMenuOffsetLeft + aMenuButtons[i].offsetLeft + Math.round((aMenuButtons[i].offsetWidth - 7) / 2);
                                    cMenu.oHighlight.style.left = cMenu.iHighlightStartPos + "px";
                                }
                            }
                        },
    
    onMenuMouseOver:    function() {
                            clearTimeout(cMenu.iReturnDelay);

                            cMenu.iTargetPos = cMenu.iMenuOffsetLeft + this.offsetLeft + Math.round((this.offsetWidth - 7) / 2);

                            cMenu.startAnimation();
                        },
    
    onMouseOut:         function() {
                            cMenu.iReturnDelay = setTimeout("cMenu.returnHighlight()", 300);
                        },
    
    returnHighlight:    function() {                            
                            cMenu.iTargetPos = cMenu.iHighlightStartPos;

                            cMenu.startAnimation();
                        },
                    
    startAnimation:     function() {
                            cMenu.stopAnimation();
                            
                            cMenu.iAnimationTimer = setInterval("cMenu.animate()", 1000 / cMenu.iFps);
                        },
                    
    stopAnimation:      function() {
                            clearInterval(cMenu.iAnimationTimer);
                        },
                    
    animate:            function() {
                            var iDifference = cMenu.iTargetPos - cMenu.oHighlight.offsetLeft;

                            if (iDifference >= -2 && iDifference <= 2) {
                                var iNewPos = cMenu.iTargetPos;
                                cMenu.stopAnimation();
                            } else {
                                var iDistance = iDifference * (cMenu.iSpeed / 10);                            
                                var iNewPos = cMenu.oHighlight.offsetLeft + iDistance;
                            }
                            
                            cMenu.oHighlight.style.left = iNewPos + "px";
                        }
                    
};
