/**
 * 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');
            var params = Element.getClassParameters(me);
            var width = params.width || 684;
            var height = params.height || 350;
            var top = params.top || 200;
            var left = params.left || '50%';
            Event.stop(e);
            window.open(me.href,
                'PopUp',
                'width=' + width + ',height=' + height + ',top=' + top + ',left=' + left + ',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);
    },
    'a.submit' : function(el) {
    	/**
    	* Event: click
    	* Action: submit the form given as a param. (used by captcha)
    	*/
    	Event.observe(el, 'click', function(e) {
            var me = Event.findElement(e,'A');
            var params = Element.getClassParameters(me);
            var form = $(params.form);
            if (form){
            	form.submit();
            }
			Event.stop(e);
            return false;
        }, 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_blur_multiple' : function(el) {

        /**
     * This is the multiple variant of auto_blur. It will highlight the
     * label of replace the image. The id of the label must be the same
     * as the id of the field, minus the last underscore and digits.
     * Now you can do those fancy multiple fields with one label :)
         */
        Event.observe(el, 'focus', function(e) {
            me = Event.element(e);
            me.className = me.className.replace('_off','_on');
            var fieldLabel = $(me.id.sub(/_\d+$/, '') + '_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.observe(el, 'blur', function(e) {
            me = Event.element(e);
            me.className = me.className.replace('_on','_off');
            var fieldLabel = $(me.id.sub(/_\d+$/, '') + '_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);
    },
    '#display_changepw' : function(el) {

        /**
         * Event:  onclick
         * Action:
         *   - check if div is displayed
         *   - display or hide div
         */
        Event.observe(el, 'click', function(e) {
            if($('changepw').style.display == 'block'){
              $('changepw').style.display = 'none';
            }else{
              $('changepw').style.display = 'block';
            }
        }, 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);
    },
    'select.change_submit' : function (el){
    /**
    * Event: change
    * Action:
    *  - submit the form
    */
        Event.observe(el,'change',function() {
      this.form.submit();
        },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);

var focus_rules = {
	    '.auto_focus' : function(el) {
					el.focus();
    }
}
Behaviour.register(focus_rules);