// Подсветка элемента меню путем изменения класса
// Делает ссылкой область которая окружает ссылку, например ячейку, в которой размещена ссылка

Array.prototype.in_array = function(str){
    for(var i = 0; i < this.length; i++)
    if (this[i] == str)
        return true;
    return false
}

var menuItem = Class.create();
menuItem.prototype = {
    initialize: function(params) {
        this.defaultClass = params.defaultClass;
        this.activeClass = params.activeClass;
        this.items = document.getElementsByClassName(this.defaultClass);
        this.eObserve(this.items);
    },

    eObserve: function(elements) {
        var that = this;
        elements.each(function(item) {
            Event.observe(item, 'mouseover', that.changeClass.bindAsEventListener(that, that.activeClass));
            Event.observe(item, 'mouseout', that.changeClass.bindAsEventListener(that, that.defaultClass));
            Event.observe(item, 'click', that.findHref.bindAsEventListener(that));
            var a = item.descendants();
            that.eObserve(a);
        });
    },

    changeClass: function(event, className) {
        var target = Event.element(event);
        while (!this.items.in_array(target))
            target = target.parentNode;
        target.className = className;
    },

    findHref: function(event) {
        var target = Event.element(event);
        var a = target.descendants();
        a.each(function(item) {
            if (item.href && ((item.tagName == 'A') || (item.tagName == 'a')))
                document.location = item.href;
            return;
        });
    }
};

// переключатель
// переключает картинку или содержимое блока
var switcher = Class.create();
switcher.prototype = {
    // type - тип переключателя
    // image переключает src в картинке (при необходмости title, alt, width, height)
    // html - заменяет содержимое блока
    type: 'image', // по умолчанию тип image
    switchEvent: 'over', // over или click, по умолчанию включает по mouseover, выключает по mouseout
    status: 'off', // состояние переключателя on - включен, off - выключен

    initialize: function(params) {
        if (params.type)
            this.type = params.type;
        if (params.imgAttr) // imgAttr - объект, структура такая же как и у defaultImgAttr
            this.imgAttr = $H(params.imgAttr);
        if (params.html)
            this.html = params.html;

        // функция, выполняемая при изменении статуса на 'on'
        this.functionOn = (params.functionOn) ? params.functionOn : false;
        // функция, выполняемая при изменении статуса на 'off'
        this.functionOff = (params.functionOff) ? params.functionOff : false;

        this.switchElement = (params.switchElement) ? params.switchElement : false;
        if (!this.switchElement)
            return;
        if (this.switchEvent)
            this.switchEvent = params.switchEvent;
        var el = this.switchElement;
        switch (this.type)
        {
            case 'image':
                var img = new Image();
                img.src = this.imgAttr.src;
                this.defaultImgAttr = {
                    src: el.src,
                    alt: el.alt,
                    title: el.title,
                    width: el.width,
                    height: el.height
                };
                this.defaultImgAttr = $H(this.defaultImgAttr);
                if (this.switchEvent == 'click')
                    Event.observe(el, 'click', this.switchImage.bindAsEventListener(this));
                if (this.switchEvent == 'over') {
                    Event.observe(el, 'mouseover', this.switchImage.bindAsEventListener(this, 'on'));
                    Event.observe(el, 'mouseout', this.switchImage.bindAsEventListener(this, 'off'));
                }
                break;
            case 'html':
                this.defaultHTML = el.innerHTML;
                if (this.switchEvent == 'click')
                    Event.observe(el, 'click', this.switchHTML.bindAsEventListener(this));
                if (this.switchEvent == 'over') {
                    Event.observe(el, 'mouseover', this.switchHTML.bindAsEventListener(this, 'on'));
                    Event.observe(el, 'mouseout', this.switchHTML.bindAsEventListener(this, 'off'));
                }
                break;
        };
    },

    switchImage: function(sw) {
        if (sw && sw == 'on')
            this.status = 'off';
        if (sw && sw == 'off')
            this.status = 'on';
        var imgAttr = (this.status == 'on') ? this.defaultImgAttr : this.imgAttr;
        var el = this.switchElement;
        imgAttr.each(function(attr, v) {
            el[attr.key] = attr.value;
        });
        this.status = (this.status == 'on') ? 'off' : 'on';
        // выполнить функцию
        eval((this.status == 'on') ? this.functionOn : this.functionOff);
    },

    switchHTML: function(sw) {
        if (sw && sw == 'on')
            this.status = 'off';
        if (sw && sw == 'off')
            this.status = 'on';
        var html = (this.status == 'on') ? this.defaultHTML : this.html;
        var el = this.switchElement;
        el.innerHTML = html;
        this.status = (this.status == 'on') ? 'off' : 'on';
        // выполнить функцию
        eval((this.status == 'on') ? this.functionOn : this.functionOff);
    }
};