var Ajax = function( func, cont, data, callback, layout ){
    this.data = data || [];
    
    this.layout = layout || 'ajax';
        
    this.callback = callback || function(data){};
    this.cont = cont || false;

    if( typeof this.data == 'function' ){
        this.data = this.data();
    }
    
    if( typeof func == 'function' ){
        this.params = func.apply( this, this.data );
    }else{
        this.params = eval(func).apply( this, this.data );
    }
    if( typeof this.params == 'undefined' || ! this.params )
        return false;
   
    if( this.params ){
        if( this.cont ){
            $(this.cont).blockUI();
        }
        jQuery.ajax( this.prepareParams( this.params, layout ) );
    } 
};

Ajax.prototype.prepareCallback = function(){
    var self = this;
    
    return function( data ){
        
        var $data = $('<div>' + data + '</div>');
        
        if( self.cont ){
            $(self.cont).html( $data.html() ).unblockUI();
        }else{
            $( 'script', $data ).each(function(){
    			eval( $(this).html() );
    			$(this).remove();
    		});
        }
        
        self.callback( data );
    };
};

Ajax.prototype.getDataType = function(){
    
    if( this.layout == 'json' )
        return 'json';
    
    return 'html';  
};

Ajax.prototype.prepareParams = function( data ){
    return {
        cache: false,
        data: { 'mode': 'portlet', 'layout': this.layout, 'data[]': data },
        dataType: this.getDataType(),
        success: this.prepareCallback(),
        timeout: 20000,
        type: 'POST'
    };
};
