$(function() {
    jQuery('div[class=andaccordion]').acc({
        speed: 400,
        active: 'active',
        list: '.children()',
        opener: 'h2 > a',
        slide: '.andslide'
    });
});

jQuery.fn.acc = function(_options) {
    var _options = jQuery.extend({
        speed: 400,
        active: 'active',
        list: '.children()',
        opener: 'a.andopener',
        slide: 'div.andslide'
    }, _options);
    return this.each(function() {
        var _list = eval('jQuery(this)' + _options.list);
        var _active = _options.active;
        var _speed = _options.speed;
        var _a = _list.index(_list.filter('.' + _active + ':eq(0)'));
        if (_a != -1) _list.removeClass(_active).eq(_a).addClass(_active);
        for (var i = 0; i < _list.length; i++) {
            _list.eq(i).data('btn', _list.eq(i).find(_options.opener).eq(0));
            _list.eq(i).data('box', _list.eq(i).children(_options.slide).eq(0));
            if (i == _a) _list.eq(i).data('box').css('display', 'block');
            else _list.eq(i).data('box').css('display', 'none');
            _list.eq(i).data('btn').data('ind', i);
            _list.eq(i).data('btn').click(function() {
                if (_list.eq(jQuery(this).data('ind')).data('box').length != 0) {
                    changeEl(jQuery(this).data('ind'));
                    return false;
                }
            });
        }
        var anim_f = true;
        var a_h, ind_h, _k;
        function changeEl(_ind) {
            if (anim_f) {
                anim_f = false;
                if (_a == _ind) {
                    _list.eq(_a).removeClass(_active).data('box').animate({ height: 0 }, {
                        duration: _speed,
                        complete: function() {
                            jQuery(this).css({ display: 'none', height: 'auto' });
                            _a = -1;
                            anim_f = true;
                        }
                    });
                }
                else {
                    _list.eq(_ind).data('box').css('display', 'block');
                    ind_h = _list.eq(_ind).data('box').outerHeight();
                    _list.eq(_ind).data('box').height(0);
                    if (_a != -1) {
                        a_h = _list.eq(_a).removeClass(_active).data('box').outerHeight();
                        _k = a_h / ind_h;
                    }
                    _list.eq(_ind).addClass(_active).data('box').animate({ height: ind_h }, {
                        duration: _speed,
                        step: function(t_h) {
                            if (_a != -1) _list.eq(_a).data('box').height(a_h - t_h * _k);
                        },
                        complete: function() {
                            _list.eq(_ind).data('box').height('auto');
                            if (_a != -1) _list.eq(_a).data('box').css({ display: 'none', height: 'auto' });
                            _a = _ind;
                            anim_f = true;
                        }
                    });
                }
            }
        }
    });
}
$(document).ready(function() {
    /** Large Clickable **/
    //When the given element is clicked, the first anchor tag within the element is triggered
    function makeLargeClickArea(e) {
        $(e).click(function() {
            document.location = $(this).find('a:first').attr('href');
            return false;
        });
    }
    // Make Large Clickable
    makeLargeClickArea('.largeClickable');
});
