function initializeFocus(){
    var element = $$('.focus').first();
    if (element) {
        element.focus();
        return;
    }
    
    $$('form').each(function(form){
        if (!form.hasClassName('skip_focus')) {
            form.focusFirstElement();
            throw $break;
        }
    });
} // initializeFocus

function initializeBoxes(){
    $$('div.box img').each(function(box){		
        box.observe('click', function(e) {
			var url = "/admin/admin/pictures/select/";
            isSelected = this.hasClassName('selected');
            
            removeCurrentSelected();
            
            if (!isSelected) {
                url =  url + this.readAttribute('id');
                this.addClassName("selected");
            }
				
            /* If no picture has been selected, the current picture will be nil */
            new Ajax.Request(url, { method: 'get' });            
        });
    });
} // initializeBoxes

function removeCurrentSelected(){
    $$('.selected').each(function(node){
        node.removeClassName('selected');
    });
} // removeCurrentSelected

document.observe("dom:loaded", function(){
    initializeFocus();
    initializeBoxes();
});

