/**
 * Rules for hyperlink behaviour
 */
var hyperlink_rules = {

    'a.popup' : function(el) {

        /**
         * Event:  click
         * Action: open a popup window
         */
        Event.observe(el, 'click', function(e) {
            var me = Event.findElement(e,'A');
            window.open(me.href,
                'PopUp',
                'width=684,height=350,top=200,left=50%,scrollbars=0,status=no,resizable=0,toolbar=0,titlebar=0,menubar=0,location=0');
            return false;
        }, false);
    },

    'a.status' : function(el) {

        /**
         * Event:  mouseover
         * Action: display the hyperlinks title in the status bar
         */
        Event.observe(el, 'mouseover', function(e) {
            var me = Event.findElement(e,'A');
            window.status=me.title;
            return true;
        },false);

        /**
         * Event:  mouseout
         * Action: clear the status bar
         */
        Event.observe(el, 'mouseout', function(e) {
            window.status='';
            return true;
        },false);
    },

    'a.switch' : function(el) {
        /**
         * Event:  click
         * Action: show / hide the element with the same ID minus "_switch"
         */

        Event.observe(el, 'click', function(e) {
            var me = Event.findElement(e,'A');
            var c = $(me.id.replace('_switch',''));
            if (c) {
                Element.toggle(c);
            }
        }, false);
    }
}
Behaviour.register(hyperlink_rules);

/**
 * Behaviour rules for form elements
 */

var form_rules = {
    'input.auto_upper' : function(el) {

        /**
         * Event:  blur
         * Action: convert field value to upper case
         */
        Event.observe(el, 'blur', function(e) {
            me = Event.element(e);
            me.value = me.value.toUpperCase();
        }, false);

        /**
         * Event:  change
         * Action: convert field value to upper case
         */
        Event.observe(el, 'change', function(e) {
            me = Event.element(e);
            me.value = me.value.toUpperCase();
        }, false);
    },

    'input.auto_blur' : function(el) {

        /**
         * Event:  focus
         * Action:
         *   - replace field's classname from "_off" to "_on"
         *   - if labeled, replace label's classname from "_off" to "_on"
         *   - if label is image, replace image with "_hover" version
         */
        Event.observe(el, 'focus', function(e) {
            me = Event.element(e);
            me.className = me.className.replace('_off','_on');
            var fieldLabel = $(me.id + '_label');
            if (fieldLabel) {
                  fieldLabel.className = fieldLabel.className.replace('_off','_on');
                var image = fieldLabel.getElementsByTagName('img')[0];
                if (image) {
                    image.src = image.src.replace('_normal','_hover');
                }
            }
        }, false);

        /**
         * Event:  blur
         * Action:
         *   - replace field's classname from "_on" to "_off"
         *   - if labeled, replace label's classname from "_on" to "_off"
         *   - if label is image, replace image with "_normal" version
         */
        Event.observe(el, 'blur', function(e) {
            me = Event.element(e);
            me.className = me.className.replace('_on','_off');
            var fieldLabel = $(me.id + '_label');
            if (fieldLabel) {
                  fieldLabel.className = fieldLabel.className.replace('_on','_off');
                var image = fieldLabel.getElementsByTagName('img')[0];
                if (image) {
                    image.src = image.src.replace('_hover','_normal');
                }
            }
        }, false);
    },
    'input.auto_clear' : function(el) {

        /**
         * Event:  focus
         * Action:
         *   - clear value
         *   - remove auto_clear class
         */
        Event.observe(el, 'focus', function(e) {
            var me = Event.element(e);
            if (Element.hasClassName(me,'auto_clear')) {
                me.value='';
            }
            Element.removeClassName(me,'auto_clear');
        }, false);
    },
    'input.rollover' : function(el) {

        /**
         * Event:  mouseover
         * Action:
         *   - if not "active", replace classname by "_hover" classname
         *   - if type is "image", replace image by "_hover" version
         */
        Event.observe(el,'mouseover',function(e) {
            var me = Event.element(e);
            if (!Element.hasClassName(me,'active')) {
                me.className = me.className.replace('_normal','_hover');
                if (me.type == 'image') {
                    me.src = me.src.replace('_normal','_hover');
                }
            }
        },false);

        /**
         * Event:  mouseout
         * Action:
         *   - replace classname by "_hover" classname
         *   - if type is "image", replace image by "_hover" version
         */
        Event.observe(el,'mouseout',function(e) {
            me = Event.element(e);
            if (!Element.hasClassName(me,'active')) {
                me.className = me.className.replace('_hover','_normal');
                if (me.type == 'image') {
                    me.src = me.src.replace('_hover','_normal');
                }
            }
        },false);
    }
}
Behaviour.register(form_rules);

var rollover_rules = {
    'img.rollover' : function(el) {

        /**
         * Event:  mouseover
         * Action: show hover version
         */
        Event.observe(el, 'mouseover', function(e) {
            me = Event.element(e);
            me.src = me.src.replace('_normal','_hover');
        }, false);

        /**
         * Event:  mouseout
         * Action: show normal version
         */
        Event.observe(el, 'mouseout', function(e) {
            me = Event.element(e);
            me.src = me.src.replace('_hover','_normal');
        }, false);
    }
}
Behaviour.register(rollover_rules);