// Ad-Hoc Wrappers: Adding meta-options to your content, without a coffee break, since yesterday.

jQuery.adhoc = function() {  }

jQuery.adhoc.defaultSettings = {
    'label': false,
    'klass': 'adhoc-theme-default',
    'top': false,
    'bottom': false
}

jQuery.adhoc.wrapperRecorner = function(self) {
    //console.debug("jQuery.adhoc.wrapperRecorner: Re-styling %r.", self);
    
    // $(self).corner('20px')
    //     .find('.content')
    //     .corner('20px');
};

jQuery.adhoc.onHover = function() {
    var self = $(this);
    
    //console.debug("jQuery.adhoc.onHover: %r", self);
    
    self.addClass('hover').find('.ah-wrapper-head').slideDown(250).siblings('.ah-wrapper-foot').slideDown(250);
    jQuery.adhoc.wrapperRecorner(self);
};

jQuery.adhoc.onLeave = function() {
    var self = $(this);
    
    //console.debug("jQuery.adhoc.onLeave: %r", self);
    
    self.find('.ah-wrapper-head').slideUp(250, function(){
        $(this).parents('.ah-wrapper').removeClass('hover');
        jQuery.adhoc.wrapperRecorner($(this).parents('.ah-wrapper'));
    }).siblings('.ah-wrapper-foot').slideUp(250);;
};

jQuery.fn.adhoc = function(options) {
    var settings = jQuery.extend({}, jQuery.adhoc.defaultSettings, options);
    
    //console.info("jQuery.fn.adhoc: Creating new adhoc from %r with: %r", this, options);
    
    $(this).wrapInner('<div class="ah-wrapper"></div>');
    var target = $(this).find('.ah-wrapper');
    
    if ( settings.klass ) target.addClass(settings.klass);
    jQuery.adhoc.wrapperRecorner(target);
    target.hover(jQuery.adhoc.onHover, $.adhoc.onLeave);
    
    //console.debug("jQuery.fn.adhoc: Adhoc wrapped and events assigned.");
    
    var header = $('<div class="ah-wrapper-head"></div>');

    if ( settings.label )
        header.append('<h3>' + settings.label + '</h3>');
    
    if ( settings.top ) {
        var list = $('<ul></ul>');
        
        for ( i in settings.top )
            list.append('<li><a class="icon ' + settings.top[i].klass + '"' + ( settings.top[i].action ? (' href="' + settings.base + '/action:' + settings.top[i].action + '"') : '' ) + '>' + settings.top[i].label + '</a></li>');
        
        header.append(list);
    }
    
    target.prepend(header);
    
    var footer = $('<div class="ah-wrapper-foot"></div>');
    
    if ( settings.bottom ) {
        var list = $('<ul></ul>');
        
        for ( i in settings.bottom ) {
            var item = $('<li></li>');
            var action = $('<a class="icon ' + settings.bottom[i].klass + '"' + ( settings.bottom[i].view ? (' href="' + settings.base + '/view:' + settings.bottom[i].view + '"') : '' ) + '>' + settings.bottom[i].label + '</a>');
            
            if ( settings.bottom[i].click !== undefined )
                action.click(settings.bottom[i].click);
            
            item.append(action);
            list.append(item);
        }
        
        footer.append(list);
        footer.append('<br />');
    }
    
    target.append(footer);
    
    //console.debug("jQuery.fn.adhoc: Done.");    
    return this;
};