(function(jq) {

    Accordian = {

        DUR: 200,
        element: null,  // current open element;
        

        init: function() {
            jq('.accordianPanel').css('display', 'none');
        },
        

        update: function(o) {
            var next = this.getNextElement(o.element);
            if (this.isAccordian(next)) {                
                // if new element is same as old, toggle;
                if (this.isMatch(next)) {
                    if (this.isVisible(this.element)) {
                        this.element = jq(next).slideUp(this.DUR);
                    } else {
                        this.element = jq(next).slideDown(this.DUR);
                    };
                }
                // else shut old and open new;
                else {
                    if (this.element) {
                        jq(this.element).slideUp(this.DUR);
                    }
                    this.element = jq(next).slideDown(this.DUR);
                };
                return this.element;              
            };
        },
        
        
        isMatch: function(el) {
            return this.element && this.element.get(0) === el;
        },
        
        
        isVisible: function(el) {
            return jq(el).css('display') === 'block';
        },
        

        isAccordian: function(el) {
            return el !== undefined &&
                el.nodeName.toLowerCase() == 'div' &&
                el.className.indexOf('accordianPanel') !== -1;
        },

        getNextElement: function(el) {
            return jq(el).next().get(0);
        }

    };


    ACTrigger = $.klass({

        callback: null,
        scope: null,

        initialize: function(fn, scope) {
            this.callback = fn;
            this.scope = scope;
        },

        onclick: function(e) {
            var link = jq(e.target).parents('a');
            this.callback.call(this.scope, {
                id: link.attr('id'), element: jq(link).parents('h3')});
            return false;
        },

        onkeydown: function(e) {
            if (e.keyCode === 32 || e.keyCode === 13) {
                this.callback.call(this.scope, {
                    id: e.target.id, element: jq(e.target).parents('h3')});    
            };
        }

    });

})(jQuery);
