// jQuery File Tree Plugin
//
// Version 1.01
//
// Cory S.N. LaViska
// A Beautiful Site (http://abeautifulsite.net/)
// 24 March 2008
//
// Visit http://abeautifulsite.net/notebook.php?article=58 for more information
//
// Usage: $('.fileTreeDemo').fileTree( options, callback )
//
// Options:  root           - root folder to display; default = /
//           script         - location of the serverside AJAX file to use; default = jqueryFileTree.php
//           folderEvent    - event to trigger expand/collapse; default = click
//           expandSpeed    - default = 500 (ms); use -1 for no animation
//           collapseSpeed  - default = 500 (ms); use -1 for no animation
//           expandEasing   - easing function to use on expand (optional)
//           collapseEasing - easing function to use on collapse (optional)
//           multiFolder    - whether or not to limit the browser to one subfolder at a time
//           loadMessage    - Message to display while initial tree loads (can be HTML)
//
// History:
//
// 1.01 - updated to work with foreign characters in directory/file names (12 April 2008)
// 1.00 - released (24 March 2008)
//
// TERMS OF USE
// 
// jQuery File Tree is licensed under a Creative Commons License and is copyrighted (C)2008 by Cory S.N. LaViska.
// For details, visit http://creativecommons.org/licenses/by/3.0/us/
//
if (jQuery) (function($) {

    $.extend($.fn, {
        fileTree: function(o, h) {
            // Defaults
            if (!o) var o = {};
            if (o.root == undefined) o.root = '/';
            if (o.script == undefined) o.script = 'jqueryFileTree.php';
            if (o.folderEvent == undefined) o.folderEvent = 'dblclick';
            if (o.selectEvent == undefined) o.selectEvent = 'click';
            if (o.expandSpeed == undefined) o.expandSpeed = 500;
            if (o.collapseSpeed == undefined) o.collapseSpeed = 500;
            if (o.expandEasing == undefined) o.expandEasing = null;
            if (o.collapseEasing == undefined) o.collapseEasing = null;
            if (o.multiFolder == undefined) o.multiFolder = true;
            if (o.loadMessage == undefined) o.loadMessage = 'Loading...';


            $(this).each(function() {

                function showTree(c, t) {
                    $(c).addClass('wait');
                    $(".jqueryFileTree.start").remove();
                    $.post(o.script, { dir: t }, function(data) {
                        $(c).find('.start').html('');
                        $(c).removeClass('wait').append(data);
                        if (o.root == t) {
                            $(c).find('UL:hidden').show();
                        } else {
                            $(c).find('UL:hidden').slideDown({ duration: o.expandSpeed, easing: o.expandEasing });
                        }
                        if (c.children().size() < 2);
                        else c.removeClass('selected');
                        bindTree(c);
                    });
                }

                function bindTree(t) {
                    $(t).find('LI').bind(o.selectEvent, function() {
                        if ($(this).hasClass('directory')) {
                            $('.jqueryFileTree').find('LI').each(function() {
                                $(this).removeClass('selected');
                            });
                            $(this).addClass('selected');
                            currentFolder = $(this).children(':first').attr('rel');
                            $('#curfolder').html(currentFolder.split('client1/')[1]);
                        } else {
                            $('.jqueryFileTree').find('LI').each(function() {
                                $(this).removeClass('selected');
                            });
                            $(this).addClass('selected');
                            var value2 = $(this).parent().parent().children(':first').attr('rel');
                            if (!value2) value2 = clientRoot;
                            currentFolder = value2;
                            $('#curfolder').html(currentFolder.split('client1/')[1]);
                        }
                    });

                    $(t).find('LI').bind(o.folderEvent, function() {
                        if ($(this).hasClass('directory')) {
                            if ($(this).hasClass('collapsed')) {
                                // Expand
                                if (!o.multiFolder) {
                                    $(this).parent().find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                    $(this).parent().find('LI.directory').removeClass('expanded').addClass('collapsed');
                                }
                                $(this).find('UL').remove(); // cleanup
                                showTree($(this), escape($(this).children(':first').attr('rel').match(/.*\//)));
                                currentFolder = $(this).children(':first').attr('rel');
                                $('#curfolder').html(currentFolder.split('client1/')[1]);
                                $(this).removeClass('collapsed').addClass('expanded');
                            } else {
                                var value = $(this).parent().parent().children(':first').attr('rel');
                                if (!value) value = clientRoot;
                                currentFolder = value;
                                $('#curfolder').html(currentFolder.split('client1/')[1]);
                                $(this).find('UL').slideUp({ duration: o.collapseSpeed, easing: o.collapseEasing });
                                $(this).removeClass('expanded').addClass('collapsed');
                                $(this).removeClass('selected');
                            }
                        } else {
                            h($(this).children(':first').attr('rel'));
                        }
                        return false;
                    });
                    // Prevent A from triggering the # on non-click events 
                    if (o.folderEvent.toLowerCase != 'click') $(t).find('LI').bind('click', function() { return false; });
                }
                // Loading message
                $(this).html('<ul class="jqueryFileTree start"><li class="wait">' + o.loadMessage + '<li></ul>');
                // Get the initial file list
                showTree($(this), escape(o.root));
            });
        }
    });

})(jQuery);
