var currentRoot   = null; //'<?=$current_root?>';
var currentFolder = null; //'<?=$current_folder?>';

var controllerUrl = ''; //'<?=$html->url('/images/')?>';

var onImageClicked = null;
var onClose        = null;

//------------------------------------------------------------------------------

/**
 * 
 */
function initChooser(baseUrl, root) {
    
    var options = Object.extend({
        
        show  :         false,
        draggable:      true,
        cacheChooser:   true,
        onInit:         null,
        onImageClicked: null,
        onClose:        null
        
    }, arguments[2] || {})
    
    if(options.cacheChooser || !$('image_chooser')) {
	    if(root) {
	        currentRoot   = root;
	    } else {
	        currentRoot   = '*';
	    }
	    controllerUrl = baseUrl;
	    
	    onImageClicked = options.onImageClicked;
	    onClose        = options.onClose;
	    
	    var url = controllerUrl + 'chooser/' + currentRoot + '/*/1';
	    new Ajax.Request(url, {
	        onSuccess: function(t, meta) {
	            currentFolder = meta.currentFolder;
	            currentRoot   = meta.currentRoot;
	            
	            if($('image_chooser'))
	            $('image_chooser').replace(t.responseText);
	            
	            else
	            new Insertion.Bottom($(document.body), t.responseText);
	            
	            if(options.draggable)
	            new Draggable('image_chooser', {
	            	handle:       'image_chooser_handle',
	            	starteffect:  null,
	            	reverteffect: null,
	            	endeffect:    null
	            })
	            
	            if(options.show)
	            $('image_chooser').show();
	            
	            if(options.onInit) 
	            options.onInit($('image_chooser'));
	        }
	    });
    }
}

//------------------------------------------------------------------------------

function closeChooser() {
    $('image_chooser').hide();
    
    if(onClose)
    onClose();
}

//------------------------------------------------------------------------------

/**
 * 
 */
function folderClicked(name, isAbsolute, limit, page) {

	$('image_chooser').block();

    var url = controllerUrl + 'chooser/' + currentRoot + '/';
    
    if(name == '')
    name = '*';
    
    if(currentFolder == '' || isAbsolute) {
    	url += name + '/0';
    } else {
    	url += currentFolder + '::' + name + '/0';
    }
    
    url += '/' + limit + '/' + page;
    
    new Ajax.Request(url, {
        onSuccess: function(t, meta) {
            currentFolder = meta.currentFolder;
            currentRoot   = meta.currentRoot;
            
            $('image_chooser').update(t.responseText);
            $('image_chooser').unblock();
        }
    });
}

//------------------------------------------------------------------------------

/**
 * 
 */
function fileClicked(imageName) {

    if(onImageClicked) {
    
        if(currentFolder && currentFolder != '') 
        imageName = currentFolder + '::' + imageName;
    
        onImageClicked(imageName, $('image_chooser'));
    }
}

