﻿Ext.ns("OXX.Accordion");
/*
Dynamic ajax accordion (OXX 2009)

2Do: 
-Get dynamic height of expansion for elements
-Set width of accordion according to parent(content, leftbar or rightbar)
-?
*/
OXX.Accordion = Ext.extend(Object, {

    constructor: function() {

    },

    futureHeight: {},

    init: function() {

        var rootEl = Ext.get('accordion');
        var bodyEls = rootEl.select('div.inner-body');
        bodyEls.each(function(item, i) {
            var el = item.load({
                url: 'DocumentJSON.aspx?did=' + item.dom.id.replace('document_', ''),
                method: 'get',
                callback: function() {
                    // console.log('here we go again....');
                }
            });

        }, this);

        Ext.select('.body').setHeight(1);

        rootEl.on('click', function(evt, el) {
            Ext.select('.expand').each(function(el, th, index) {
                Ext.fly(el).toggleClass('expand');
                Ext.fly(el).down('.body').setHeight(1, {
                    duration: 1.4, callback: function() {
                        OXX.Application.resizeLayout();
                    }
                });
            });


            //La til 10 for å kompansere for padding
            var targetHeight = (Ext.fly(el).parent('.expand') == null) ? Ext.fly(el).parent('.container').down('.body .inner-body').getHeight() + 5 : 1;

            Ext.fly(el).parent('.container').toggleClass('expand');

            Ext.fly(el).parent('.container').down('.body').setHeight(targetHeight, {
                duration: 1.4, callback: function() {
                    OXX.Application.resizeLayout();
                }
            });



        }, this, { delegate: '.icon' });
    }
});

Ext.onReady(function() {

    if (Ext.fly('accordion') != null) {

        var accordion = new OXX.Accordion();
        accordion.init();
    }

});