/* Copyright (c) 2008, Experion Inc.  All rights reserved. */

// HighlightManager
(function() {

    var Dom = YAHOO.util.Dom;

    HighlightManager = function(el, hoverColor, noHoverColor, duration) {
        this.el = Dom.get(el);
        this.duration = duration || 0.3;
        this.hoverColor = hoverColor;
        this.noHoverColor = noHoverColor;
    };

    HighlightManager.prototype = {
    
        anim: null,
        toColor: null,
        mouseOutTimer: null,
        
        onMouseOver: function()
        {
        	this.stopTimer();
        	this.animate(this.hoverColor);
        },
        
        onMouseOut: function()
        {
        	this.stopTimer();
        	var _this = this;
        	
        	this.mouseOutTimer = setTimeout(function() {
	        	_this.animate(_this.noHoverColor);
	        	}, 10);
        },
    
        animate: function(color) 
        {
        	if (color == this.toColor) { return; }
        	
        	if (this.toColor)
        	{
        		this.stop();
        	}
        	this.toColor = color;

            this.anim = new YAHOO.util.ColorAnim(
                this.el,
                { color: {to: color} },
                this.duration,
                YAHOO.util.Easing.easeNone);
            this.anim.onComplete.subscribe(function() {
                this.anim = null;
            });
            this.anim.animate();
        },
        
        stop: function()
        {
        	if (this.anim)
        	{
        		this.anim.stop();
	       		this.el.style.color = this.toColor;
	       		this.anim = null;
	       	}
        },
        
        stopTimer: function()
        {
        	if (this.mouseOutTimer)
        	{
        		clearTimeout(this.mouseOutTimer);
        		this.mouseOutTimer = null;
        	}
        }
    };
    
}());

function initMenu()
{
	var elem, items, item, hm, i, clr, ref;
		Event = YAHOO.util.Event;
	
	elem = document.getElementById('menu-items');
	if (!elem) { return; }
	items = elem.getElementsByTagName('a');
	for(i = 0; i < items.length; ++i)
	{
		ref = item = items[i];
		do {
			clr = YAHOO.util.Dom.getStyle(ref, 'color');
			ref = ref.parentNode;
		} while (!clr  ||  clr == 'transparent');
		hm = item.highlightManager = new HighlightManager(item, '#7C7D74', clr);
		Event.addListener(item, 'mouseover', hm.onMouseOver, hm, true);
		Event.addListener(item, 'mouseout',  hm.onMouseOut,  hm, true);
	}
}

function init()
{
	initMenu();
}

//YAHOO.util.Event.onDOMReady(init);
init();
