﻿; (function($) {
    //Archive List plugin, copyright 2009 Blix Interactive
    $.fn.archivelist = function(opt) {
        var o = $.extend({}, $.fn.archivelist.defaults, opt || {})

        function loadArchive($a, id) {
            $('#article-archive').block({ message: null, fadeIn: 0, fadeOut: 0, 
                css: { width: 'auto' },
                overlayCSS: { backgroundColor: '#CCC', opacity: 0.1 }
            });
            var ld = $('<div id="d_' + id + '"></div>').html('<div class="load-dialog"><img src="/common/img/ajax-loader.gif" /></div>');
            $a.after(ld);
            var u = $a.attr('href');
            $.ajax({
                type: "GET",
                url: u,
                cache: false,
                dataType: "html",
                success: function(html) {
                    $('#article-archive').unblock();
                    ld.html(html);
                    openArchive($a, id);
                    return;
                },
                error: function(xhr, textStatus) {
                    $('#article-archive').unblock();
                    ld.html('<div class="load-dialog">An error occurred getting the requested archive. Please refresh this page and try again.</div>');
                    return;
                }
            });
        };

        function openArchive($a, id) {
            $a.removeClass('monthClose');
            $a.addClass('monthOpen');
            $('div.arch-list', $('#d_' + id)).slideDown('fast');
        };

        function closeArchive($a, id) {
            $a.removeClass('monthOpen');
            $a.addClass('monthClose');
            $('div.arch-list', $('#d_' + id)).slideUp('fast');
        };

        return this.each(function() {
            var $this = $(this);

            $this.click(function() {
                if ($this.hasClass('monthOpen')) {
                    closeArchive($this, this.id);
                }
                else if ($this.hasClass('monthClose')) {
                    openArchive($this, this.id);
                }
                else
                    loadArchive($this, this.id);

                return false;
            });

            //            $this.t = $this.text();

            //            $this.bind('poststart', function() {
            //                poststart($this);
            //            });
            //            $this.bind('postend', function() {
            //                postend($this);
            //            });

            return $this;
        });

    };

    // public defaults
    $.fn.archivelist.defaults = {
        setting: null
    };

})(jQuery);


