/*

 List Expander
 written by Alen Grakalic, provided by Css Globe (cssglobe.com). modified by Pierre Meyer to use jQuery

 */
var _listExpander = function() {

    // edit
    var expandTo = 1; // level up to which you want your lists to be initially expanded. 1 is minimum
    var expandText = "Expand All"; // text for expand all button
    var collapseText = "Collapse All"; // text for collapse all button
    var listClass = "listexpander"; // class name that you want to assign to list(s). If you wish to change it make sure to update the css file as well
    // end edit (do not edit below this line)
    $('ul.' + listClass).each(function() {
        ($(this)).find('li').each(function() {
            listItem($(this));
        });
        var a_expand_all = $('<a></a>').append(expandText);
        var a_collapse_all = $('<a></a>').append(collapseText);
        var p = $('<p></p>').addClass(listClass).append(a_expand_all).append(a_collapse_all);
        $(this).before(p);
        $(this).prev('p.' + listClass).children('a').unbind('click').bind('click', function() {
            if ($(this).text() == expandText) {
                $(this).parent().next('ul.' + listClass).find('li.collapsed, li.expanded').removeClass('collapsed').addClass('expanded');
                $(this).parent().next('ul.' + listClass).find('ul').slideDown('fast', function() {
                    IFrameInterface.resizeIframe();
                });
            } else if ($(this).text() == collapseText) {
                $(this).parent().next('ul.' + listClass).find('li.collapsed, li.expanded').removeClass('expanded').addClass('collapsed');
                $(this).parent().next('ul.' + listClass).find('ul').slideUp('fast', function() {
                    IFrameInterface.resizeIframe();
                });
            }
        });
    });
    // this.listItem = function(li){
    function listItem(li) {
        if (li.children('ul:first').length > 0) {
            var ul = li.children('ul:first');
            if (depth(ul) <= expandTo) {
                ul.show();
                li.addClass('expanded');
            } else {
                if (!li.hasClass('expanded')) {
                    ul.hide();
                    li.addClass('collapsed');
                } else {
                    ul.show();
                }
            }
            li.unbind('click').bind('click', function(event) {
                if ($(this).hasClass('expanded')) {
                    $(this).removeClass("expanded").addClass("collapsed").children('ul:first').slideUp('normal', function() {
                        IFrameInterface.resizeIframe();
                    });
                } else if ($(this).hasClass('collapsed')) {
                    $(this).removeClass("collapsed").addClass("expanded").children('ul:first').slideDown('normal', function() {
                        IFrameInterface.resizeIframe();
                    });
                }
                event.stopPropagation();
            });
        } else {
            li.addClass('nochildren');
            li.unbind('click').bind('click', function(event) {
                event.stopPropagation();
            });
        }
        ;
    }

    ;
    //	this.depth = function(obj){
    function depth(obj) {
        var level = 1;
        while ((obj.parent().attr('class') != listClass) && level < 30) {
            if (obj[0].tagName.toLowerCase() == "ul") level++;
            obj = obj.parent();
        }
        ;
        return level;
    }

    ;
};
/*Start the listExpander script...*/
/*
 $(document).ready(function() {
 _listExpander();
 });*/

