$(window).load(function () {
    /* Replace all 'textarea.rich' with FCKEditors */
    var i = 0;
    $('textarea.rich').each(function () {
        var id = $(this).attr('id');
        if (id == null || id == '') {
            i++;
            id = 'FCKEditor'+i;
        }
        var oFCKeditor = new FCKeditor(id) ;
        oFCKeditor.BasePath = "/fckeditor/" ;
        oFCKeditor.Config["CustomConfigurationsPath"] = "/fckeditor/customconfig.js"  ;
        oFCKeditor.ReplaceTextarea() ;
        $(this).parents('dl').eq(0).width(530);
    });
    
    /* Support for image selection previews */
    $('select.nested_images').each(function () {
        var $select = $(this);
        $select.find('option').each(function () {
            var $option = $(this);
            $option
                .css({
                    height: 75,
                    background: "url('/images/"+$option.attr('value')+"/small') no-repeat 50% 90%"
                })
            ;
        });
    });
});

$(function () {
    /* Collapsible sidebarAdmin */
    $('ul.site-structure').each(function () {
        var $ul = $(this);
        $ul.find('li').each(function () {
            var $li = $(this);
            if ($li.find('ul').size() > 0) {
                $li
                    .prepend(
                        $('<div class="expander"></div>')
                            .click(function () {
                                $li
                                    .toggleClass('collapsed')
                                    .toggleClass('expanded')
                                ;
                            })
                    )
                    .addClass($li.hasClass('selected') ? 'expanded' : 'collapsed')
                ;
            }
        });
    });
});
