loadingDiv = jQuery('<div class="news"></div>')
    .append(loadingImg)
    .append(' Loading News Feed');

jQuery(document).ready(function() {
    jQuery('.accordion')
        .data('status', 'closed')
        .click(function(e) {
            e.preventDefault();
            var el = jQuery(this);
            if(el.data('status') == 'closed') {
                el.data('status', 'open');
                el.children('a').css({
                    'background-position': '3px top'
                });
                if(el.children('a').attr('href') == '#') {
                    el.next().show();
                } else {
                    jQuery.ajax({
                        type: "GET",
                        url: '/includes/newsfetch.php',
                        data: {
                            num: 3,
                            feed: el.children('a').attr('href')
                        },
                        beforeSend: function() {
                            loadingDiv.insertAfter(el);
                        },
                        success: function(data) {
                            el.next().remove();
                            el.after(data);
                        }
                    });
                }
            } else {
                el.data('status', 'closed');
                el.children('a').css({
                    'background-position': '3px bottom'
                });
                if(el.children('a').attr('href') == '#') {
                    el.next().hide();
                } else {
                    el.next().remove();
                }
            }
        });
});

