$(document).ready(function(){

        //Add prefixes to nested options in Opera and IE.
        var nestOptions = function()
        {
                $('select.tree option').each(function(){
                        var match = (new String(this.className)).match(/level\-[0-9]+/g);

                        // below breaks both ie & opera... have to check this out with drew... he's much better at js than me !
                        if (match != null)
                        {
                                if (match.length > 0)
                                {
                                        var level = parseInt(match[0].substr(6), 10);
                                        if (level > 0)
                                        {
                                        	var prefix = '&nbsp;&nbsp;';
                                        }
                                        else
                                        {
                                        	var prefix = '';
                                        }
                                    	
                                        // Strip all the spaces from the name (used by mozilla)    
                                    	var ffsStr = new String($(this).html()).toString().replace(/&nbsp;/g, '');
                                        
                                    	// Add some dashes for nesting
                                    	for(i=1;i<=level;i++)
                                    	{
                                    		prefix += '--';
                                    	}
                                    	
                                    	if (prefix != '')
                                    	{
                                    		prefix += '&nbsp;';
                                    	}
                                    	
                                    	$(this).html(prefix + ffsStr);
                                }
                        }
                });
        };

        $('#folderTree').each(function(){
                var select = $(this);

                //Add class "new" to allow the user to create new folders
                if (!select.hasClass('new'))
                {
                        return;
                }

                var create = function(message, callback)
                {
                        var name = prompt(message ? message : i18n.text.tree.name, '');

                        if (name)
                        {
                                button.attr('disabled', true);

                                var properties = {
                                        'name': name
                                };

                                var complete = function()
                                {
                                        button.attr('disabled', null);
                                }

                                var success = function(data)
                                {
                                        if (parseInt(data.success, 10) > 0)
                                        {
                                                var option = $('<option style="padding-left: 26px; background-position: 5px 50%;" class="level-0" value=""></option>').
                                                        html('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+data.folder.folder_name).
                                                        val(data.folder.hash);

                                                select.append(option);
                                                option.get(0).selected = true;

                                                if (typeof callback == "function")
                                                {
                                                        callback();
                                                }
                                        }

                                        complete();
                                };
                                $.ajax({
                                        type: 'POST',
                                        url: '/newFolder.ajax.php',
                                        data: properties,
                                        success: success,
                                        error: complete,
                                        dataType: 'json'
                                });
                        }
                };

                if (typeof(window.FileFactory) == 'undefined' || window.FileFactory === undefined)
                {
                	window.FileFactory = {};
                }
                window.FileFactory.createFolder = create;
				
                var button = $('<button type="button" class="newFolder"></button>').
                                text(i18n.text.tree.create.toString()).
                                click(function(){
                                        create();
                                });

                select.after(button);
        });

        //Only Mozilla supports padding and BG images on select options
        if (!$.browser.mozilla)
        {
                nestOptions();
                if ($.browser.msie)
                {
                        $('select.tree option[disabled]').each(function(){
                                $(this).replaceWith('<optgroup class="disabled" disabled="" label="' + $(this).text() + '"></optgroup>');
                        });
                        $('select.tree').
                                css('height', '20px');
                }
                else if($.browser.safari)
                {
                    // Safari isn't playing nicely with the folder icon in the select
                	$('select.tree').css({'backgroundImage':'','background':'#fff'});
                }
        }
});