var filbrowser_urls = new Array();
var initialuid = 0;

function filebrowser_create($div_foldertitle, $div_foldertree, $div_files, $div_folderpath, $div_upbutton) {
    var do_animation = function($container, uid, append_data, callback) {
        var $active;
        var $fieldset = $container.parents("fieldset");
        $container.children("div.filebrowser_item").each(function() {
            if ($(this).css('display') != 'none') 
                $active = $(this);
        });
        if (!$active.hasClass('itemuid-'+uid)) {
            $container.animate({'opacity': 0}, 150, function() {
                $container.css({'height': $container.height()+'px', 'overflow': 'hidden'});
                $active.css('display', 'none');
                append_data();
                var $target = $container.children("div.filebrowser_item.itemuid-"+uid);
                var fnc = function() {
                    $target.show();
                    $container.animate({'opacity': 1}, 150, function() {
                        $container.css({'height': '', 'overflow': ''});
                    });
                }
                if ($target.html() == '') {
                    $target.show();
                    if (!$fieldset.hasClass('hidden')) {
                        $fieldset.animate({'opacity': 0}, 150, function() {
                            $fieldset.hide(250);
                            $fieldset.addClass('hidden');
                        });
                    }
                }
                else {
                    if ($fieldset.hasClass('hidden')) {
                        $target.show();
                        $container.css({'height': '', 'overflow': '', 'opacity': 1});
                        $fieldset.show(250, function() {
                            $fieldset.animate({'opacity': 1}, 150);
                            $fieldset.removeClass('hidden');
                        });
                    }
                    else {
                        if ($container.css('display') == 'block' && $target.height() != $container.height())
                            $container.animate({'height': $target.height()+'px'}, 250, 'linear', fnc);
                        else 
                            fnc();
                    }
                }
            });
        }
        else {
            var $target = $container.children("div.filebrowser_item.itemuid-"+uid);
            if ($target.html() == '') {
                $fieldset.hide();
                $fieldset.addClass('hidden');
            }
        }
    }
    var change_folder = function(uid) {
        var href = filbrowser_urls[uid];
        var fetch_foldertitle=($div_foldertitle.find("div.filebrowser_item.itemuid-"+uid).length==0)+0;
        var fetch_folderpath=($div_folderpath.find("div.filebrowser_item.itemuid-"+uid).length==0)+0;
        var fetch_upbutton=($div_upbutton.find("div.filebrowser_item.itemuid-"+uid).length==0)+0;
        var fetch_foldertree=($div_foldertree.find("div.filebrowser_item.itemuid-"+uid).length==0)+0;  
        var fetch_files=($div_files.find("div.filebrowser_item.itemuid-"+uid).length==0)+0;  
        if (fetch_folderpath || fetch_upbutton || fetch_foldertree || fetch_files) {
            $.getJSON(href+'?type=111&fetch_foldertitle='+fetch_foldertitle+
                                    '&fetch_folderpath='+fetch_folderpath+
                                    '&fetch_upbutton='+fetch_upbutton+
                                    '&fetch_foldertree='+fetch_foldertree+
                                    '&fetch_files='+fetch_files, function(data) {
                do_animation($div_foldertitle, uid, function() {
                    if (fetch_foldertitle) $div_foldertitle.append(data.foldertitle); 
                });
                do_animation($div_folderpath, uid, function() {
                    if (fetch_folderpath) $div_folderpath.append(data.folderpath); 
                });
                do_animation($div_upbutton, uid, function() {
                    if (fetch_upbutton) $div_upbutton.append(data.upbutton); 
                });
                do_animation($div_files, uid, function() {
                    if (fetch_files) $div_files.append(data.files); 
                });
                do_animation($div_foldertree, uid, function() {
                    if (fetch_foldertree) {
                        $div_foldertree.append(data.foldertree); 
                        filebrowser_create($div_foldertitle, $div_foldertree, $div_files, $div_folderpath, $div_upbutton);
                    }
                });
            });
        }
        else {
            do_animation($div_foldertitle, uid, function() {});
            do_animation($div_folderpath, uid, function() {});
            do_animation($div_upbutton, uid, function() {});
            do_animation($div_foldertree, uid, function() {});
            do_animation($div_files, uid, function() {});
        }
    }
    var hload = function(hash) {
        var fnc = function() {
            if (initialuid != 0) 
     	          change_folder(initialuid);
        }
        if(hash && hash.substr(0, 'tmp:'.length) == 'tmp:') {
            var uid = hash.substr('tmp:'.length);
            if (typeof filbrowser_urls[uid] != 'undefined')
    	          change_folder(uid);
            else 
                fnc();
        }
        else {
                // Add History Entry:
            fnc();
        }
    }
    initialuid = $div_foldertree.find("div.filebrowser_item").attr('class').match(/itemuid-\d+/) + '';
    initialuid = initialuid.substr(8);
    $.historyInit(hload);
    $div_foldertree.add($div_folderpath.selector).add($div_upbutton.selector).find("a.sitetree_itemtitle[href]").each(function() {
        var href = $(this).attr('href');
        var uid = $(this).parent().parent().attr('class').match(/itemuid-\d+/) + '';
        uid = uid.substr(8);
        filbrowser_urls[uid] = href;
        $(this).removeAttr('href');
        $(this).parent().click(function() {
            $.historyLoad('tmp:' + uid)
        });
    });
}


