/* ClickFix.js - A fix to ensure that click() and focus() events work properly cross browser
 * Created by Brian Crescimanno
 * http://briancrescimanno.com
 * Requires: PrototypeJS 1.6.0 or later
 */

var ClickFix = Class.create({

    initialize: function(){
        /* To prevent IE from throwing an error if this variable is anything other than an extended element we just
         * create an empty on here that will simply be discarded once the application is in use.
         */
        this.focused = new Element("div");
        this.form = $('my-form') /* set this to your form */

        var checkboxes = this.form.getInputs('checkbox');
        var radios = this.form.getInputs('radio');

        var fakeClick = this.fakeClick.bindAsEventListener(this);
        var eventHandler = this.eventHandler.bindAsEventListener(this);

        checkboxes.invoke('observe', 'click', fakeClick);
        checkboxes.invoke('observe', 'focus', eventHandler);
        radios.invoke('observe', 'click', fakeClick);
        radios.invoke('observe', 'click', eventHandler);


    },

    fakeClick: function(e) {
        var el = e.element();
        if(el.identify){
            if(this.focused.identify() != el.identify()) {
                e.element().focus();
            }
        }
    },

    eventHandler: function(e) {
        var el = e.element();
        if(this.focused.identify() != el.identify()) {
            /* Your functionality here */
            this.focused = el;
        }
    }

});