$().ready(function() {
    moreLess.init();
    $(".colorbox > a, a.colorbox").colorbox({width:"600px", height:"500px", iframe:true});
    $("a.colorboximage").colorbox({width:"600px", height:"500px"});

    $(".update").live("click", function() {
        var container = $(this).parents(".update-container");
        var index = $(".update-container").index(container);

        $.ajax({
            url: this.href,
            success: function(data) {
                container.replaceWith($(".update-container", data).eq(index));
            }
        });
        return false;
    });
});

/*
Handle switching between text summary and full text. If summary is equal to full text, nothing is done.
 */
var moreLess = {
    createMoreLink: function(i) {
        $("a:last", this).click(function() {
            moreLess.show(this);
            return false;
        });
    },

    createLessLink: function() {
        $("a:last", this).click(function() {
            moreLess.hide(this);
            return false;
        });
    },

    show: function(element) {
        $(element).parents(".summarized").addClass('show');
    },

    hide: function(element) {
        $(element).parents(".summarized").removeClass('show');
    },
    
    init: function() {
        $(".summarized").each(function() {
            var fullText = $(".fulltext", this);
            var summary = $(".summary", this);
            if(fullText.html() != summary.html()) {
                summary.each(moreLess.createMoreLink);
                fullText.each(moreLess.createLessLink);
            }
        });
    },

    summarized: $('.summarized')
}
