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

    'a.popup' : function(el) {

        /**
         * Event:  click
         * Action: open a popup window
         */
        Event.observe(el, 'click', function(e) {
            Event.stop(e);
            var me = Event.findElement(e,'A');
            window.open(me.href,
                'PopUp',
                'width=575,height=330,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) {
            Event.stop(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) {
            Event.stop(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) {
            Event.stop(e);
            var me = Event.findElement(e,'A');
            var c = $(me.id.replace('_switch',''));
            if (c) {
                Element.toggle(c);
            }
        }, false);
    },

    'a.print' : function(el) {
        /**
         * Event:  click
         * Action: print the window
         */

        Event.observe(el, 'click', function(e) {
            Event.stop(e);
            window.print();
        }, false);
    },

	'a.external' : function(el) {

	    /**
	     * Event:  click
	     * Action: open a new window
	     */
	    Event.observe(el, 'click', function(e) {   
	    	Event.stop(e);      
	        if (!e.ctrlKey && !e.altKey && !e.shiftKey) {
	          window.open(el.href);
	          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.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);
    },
    '#mailingform' : function(el) {
        Event.observe(el,"submit", function(e) {
            var submit = true;
            var me = Event.element(e);
            if (me.email.value == "") {
                setError("mailingform_email","Vul een e-mailadres in.");
                submit = false;
            } else if (/^[\w-.]+@[\w-.]+\.\w{2,}$/.test(me.email.value) == false) {
                setError("mailingform_email","Vul een geldig e-mailadres in.");
                submit = false;
            } else {
                unsetError("mailingform_email");
            }
            if (!submit) {
                Event.stop(e);
            }
        }, 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);
    },
    'img.rollover_grayscale' : function(el) {
        /**
         * Event:  mouseover
         * Action: show hover version with grayscaled thumb
         */
        Event.observe(el, 'mouseover', function(e) {
            me = Event.element(e);
            me.src = me.src.replace('_smallbw','_small');
        }, false);

        /**
         * Event:  mouseout
         * Action: show normal version
         */
        Event.observe(el, 'mouseout', function(e) {
            me = Event.element(e);
            me.src = me.src.replace('_small','_smallbw');
        }, false);
    }
}
Behaviour.register(rollover_rules);
/**
 * UFO Flash Replacer
 */
var flash_rules = {
    'div.portfolio_ticker' : function(el) {
        var FOTicker = {
            movie:"/flash/portfolio_ticker.swf",
            flashvars: "gatewayUrl=http://" + Element.getClassParameter(el,'server') + "/flashservices/gateway.php&portfolio_page=" + Element.getClassParameter(el,'portfolio_page') + "&portfolio_menu=" + Element.getClassParameter(el,'portfolio_menu'),
            width:"199",
            height:"52",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"8",
            build:"0"
        };
        UFO.create(FOTicker,el.id);
    },
    'div.portfolio_case_home' : function(el) {
        var FOCaseHome = {
            movie:"/flash/portfolio_case_home.swf",
            flashvars: "gatewayUrl=http://" + Element.getClassParameter(el,'server') + "/flashservices/gateway.php&amp;portfolio_page=" + Element.getClassParameter(el,'portfolio_page') + "&amp;navigation=" + Element.getClassParameter(el, 'navigation') + "&portfolio_menu=" + Element.getClassParameter(el,'portfolio_menu'),
            width:"575",
            height:"417",
            menu:"false",
            wmode:"transparent",
            xi:"false",
            majorversion:"7",
            build:"0"
        };
        UFO.create(FOCaseHome,el.id);
    },
    'div.flash_video' : function(el) {
        var FOVideo;
        var flashvars;
        var obj = el.getElementsByTagName('OBJECT')[0];
        if (obj && obj.getAttribute('type') == 'application/x-shockwave-flash') {
            /* Build a list of all params */
            var params = $H();
            $A(obj.getElementsByTagName('PARAM')).each(function(p) {
                params[p.name]=p.value;
            });
            FOVideo = {
                movie: obj.getAttribute('data'),
                flashvars: params.flashvars,
                width: obj.getAttribute('width'),
                height: obj.getAttribute('height'),
                menu: params.menu || "false",
                wmode: params.wmode || "transparent",
                xi:"false",
                majorversion: Element.getClassParameter(el,'majorversion') || "7",
                build: Element.getClassParameter(el,'build') || "0"
            };
        } else {
            params = $H(Element.getClassParameters(el));
            FOVideo = {
                movie: params.movie || "/flash/video.swf",
                flashvars: params.toQueryString(),
                width: params.width || "557",
                height: params.Height || "426",
                menu: params.menu || "false",
                wmode: params.wmode || "transparent",
                xi: "false",
                majorversion: params.majorversion || "7",
                build: params.build || "0"
            };
        }
        UFO.create(FOVideo,el.id);
    }
}
Behaviour.register(flash_rules);