/**
 * @description Javascript menu for SIL 2010
 */

var menuSil = Class.create();

menuSil.prototype = {
    initialize : function(config) {
        this.config = config;

        this.beingShown = new Array();
        this.shown = new Array();
        this.hideDelayTimer = new Array();

        this.loadApplication();
    },

    loadApplication : function() {

        $$('.' + this.config.bubbleClass).each(function(elt, index) {
            var popup = elt.select('.' + this.config.popupClass).first();
            popup.setOpacity(0);
            var trigger = elt.select('.' + this.config.triggerClass).first();
            Event.observe(trigger, 'mouseover', this.over.bind(this, popup, index));
            Event.observe(trigger, 'mouseout', this.out.bind(this, popup, index));
            Event.observe(popup, 'mouseover', this.over.bind(this, popup, index));
            Event.observe(popup, 'mouseout', this.out.bind(this, popup, index));
        }.bind(this));

        this.getActive();

    },

    over : function(popup, index, event) {
        if (this.hideDelayTimer[index]) {
            clearTimeout(this.hideDelayTimer[index]);
        }

        if (this.beingShown[index] == true || this.shown[index] == true) {
            return null;
        } else {
            this.beingShown[index] = true;
			this.shown[index] = true;
			popup.setStyle({
				'top': 35,
				'left': 0,
				'display': 'block'
			});
            new Effect.Opacity(popup, {
                from: 0,
                to: 1,
                duration: 1,
                afterFinish : function() {
                    this.beingShown[index] = false;
                }.bind(this)
            });
        }
    },

    out : function(popup, index, event) {
        if (this.hideDelayTimer[index]) {
            clearTimeout(this.hideDelayTimer[index]);
        }

		this.beingShown[index] = false;
        this.hideDelayTimer[index] = setTimeout(function () {
            new Effect.Opacity(popup, {
                from: 1,
                to: 0,
                duration: 0.1,
                afterFinish : function() {
					this.shown[index] = false;
                    popup.setStyle({
                        'display': 'none'
                    });
                }.bind(this)
            });
        }.bind(this), 100);
    },

    getActive : function() {

        if (this.config.active) {
            var id = this.config.active;
            var active = $('menu-item-' + id);
            active = active.select('dl').first();
            var copy = active.cloneNode(true);
            //$(this.config.activeContainer).appendChild(copy);

            var img = $('imgMenu' + id);
            var imgSrc = img.src;
            //console.log(imgSrc.replace(/_a./, '_b.'));
            img.src = imgSrc.replace(/_a./, '_b.');
        }
    }
};