var KMLanguageSwitcher = Class.create();
KMLanguageSwitcher.prototype = {

    initialize: function(buttons, switchField, toElementName) {
        this.switcher = $(switchField);
        this.switcherId = switchField;
        this.doHide = false;
        this.hidden = true;

        this.setSwitcherPosition($(toElementName));
        
        for ( var i = 0; i < buttons.length; i++ )
        {
            Event.observe(buttons[i], 'mouseover', this.showSwitcher.bind(this));
        }
        
        Event.observe(switchField, 'mouseout', this.hideSwitcher.bind(this));
        Event.observe(switchField, 'mouseover', this.cancelHide.bind(this));    
        
        window.setInterval( this.checkHide.bind(this), 50);    
    },

    setSwitcherPosition: function(toElement) {
        
        var btnOffset = Position.cumulativeOffset(toElement);
        var x = (btnOffset[0] + toElement.getWidth()) - this.switcher.getWidth();
        var y = btnOffset[1] + toElement.getHeight();

        this.switcher.style.position = 'absolute';
        this.switcher.style.top  = y + 'px';
        this.switcher.style.left = x + 'px';
        
        return;
    },
    
    showSwitcher: function(event) {
        Effect.Appear(this.switcherId, {duration: 0.8});
        this.hidden = false;
    },
    
    hideSwitcher: function(event) {
        this.doHide = true;
    },
    
    cancelHide: function(event) {
        this.doHide = false;
    },
    
    checkHide: function() {
        if ( !this.doHide || this.hidden )
            return;
        
        this.hidden = true;
        this.doHide = false;
        Effect.Fade(this.switcherId, {duration: 0.8});
    }
}