function LayoutManager(Pedido){
    
    var pedido = Pedido;
    
    var uma_borda = true;
    
    this.setPedido = function(p){
         pedido = p;
    }
    
    this.updateToolTips = function(){
       
       /*jQuery('.tooltip').remove();
       
       //var tooltips = new Array();   
       jQuery('.helptext:not(.ritstooltip)').each(function(index){  
           if(jQuery(this).is(':visible')){
               var t = jQuery(this).ritsToolTip();
               //tooltips.push(t);
           }    
                        
       });*/       
    }
    var updateToolTips = this.updateToolTips;
    
    this.addPizzaToList = function(num){
         //var img = '<img src="/fonts.php?text='+num+'&amp;color=FFFFFF&amp;bgcolor=e7cb31&amp;size=13&amp;font=tt0142m_.ttf" alt="'+num+'">';
         jQuery('#list-pizzas').append('<div id="btn-pizza-'+num+'" title="Selecionar pizza '+num+'" class="helptext item-unidade" style="visibility: visible;"><span class="replace" style="color:#fff;">'+num+'</span></div>');
    }
    
    this.showPizza = function(id){
        
        // Se existir o número anterior.
       /* if(jQuery('#list-pizzas .item-unidade-on').length > 0){
            var oldId = parseInt(jQuery('#list-pizzas .item-unidade-on').attr('id').substring(10));
            
            jQuery('#btn-pizza-'+oldId).removeClass('item-unidade-on').addClass('item-unidade').html(oldId);
            //jQuery('#btn-pizza-'+oldId).jitr('tt0142m_.ttf');
        }
        
        jQuery('#btn-pizza-'+id).removeClass('item-unidade').addClass('item-unidade-on').html(id);
        //jQuery('#btn-pizza-'+id).jitr('tt0142m_.ttf', '8E161A');
        
        Cufon.refresh();
        
        animateSelectPizza(id);
        */

        jQuery('#content-pizza').html('<div id="loading">Carrengando...</div>');
        document.loadContent('/pedidos/pizzas/contentpizza/');
        
    }
    
    this.animateSelectPizza = function(currentId){      
         /*
        jQuery('#pizza-name').html(Pedido.getPizzaName().toUpperCase());
        Cufon.replace('#pizza-name');
        
        

        
        
        var total = jQuery('#list-pizzas div').size();
        
        // Captura o index do elemento corrente.
        var index = jQuery('#list-pizzas div').index(jQuery('#btn-pizza-'+currentId));
        
        // Animação de movimento do selecionador de pizza
        var currentLeft = 0;
        if(jQuery('#list-pizzas').css('left') != null);
            currentLeft = parseInt(jQuery('#list-pizzas').css('left').replace('px', ''))*-1;
        
        var leftNumber; 
        if(currentLeft == 0){
            leftNumber = 0;
        } else {
            leftNumber =  parseInt(currentLeft/(32+5));
        }
                
        var rightNumber = leftNumber + 3;
        
        if(index < leftNumber){
            var p = ((index)*(32+5))*-1; 
            jQuery('#list-pizzas').animate({left:(p+'px')});
        } else if(index > rightNumber){
            var p = ((index-3)*(32+5))*-1; 
            jQuery('#list-pizzas').animate({left:(p+'px')});
        }
        
        if(jQuery('#btn-pizza-'+currentId).prevAll(':not(.pizza-off)').size() > 0){ // old: index > 0
            jQuery('#go-back-list').removeClass('pizza-back-off').addClass('pizza-back-on');
        } else {
            jQuery('#go-back-list').removeClass('pizza-back-on').addClass('pizza-back-off');
        }
        
        if(jQuery('#btn-pizza-'+currentId).nextAll(':not(.pizza-off)').size() > 0){ // index < (total-1)
            jQuery('#advance-list').removeClass('pizza-advance-off').addClass('pizza-advance-on');
        } else {
            jQuery('#advance-list').removeClass('pizza-advance-on').addClass('pizza-advance-off');
        }
        
        */ 
    }
    var animateSelectPizza = this.animateSelectPizza;
    
    
    this.updateSize = function updateSize(size){
        
        jQuery('#sabordapizza .item-price').hide();
        jQuery('#sabordapizza .pricesize-'+size).show();
        
        jQuery('#promocoes .item-price').hide();
        jQuery('#promocoes .pricesize-'+size).show();
        

        jQuery('#promocoes .pricesize-'+size + '[rel=false]').parents('li.jcarousel-item').hide();
        jQuery('#promocoes .pricesize-'+size + '[rel=true]').parents('li.jcarousel-item').show();
        
        if( jQuery('#promocoes .pricesize-'+size + '[rel=true]').length == 0 ){
            jQuery('[href=#promocoes]').parent().hide();
        } else {
            jQuery('[href=#promocoes]').parent().show();
        }
        
        jQuery('#bordarecheada .item-price').hide();
        jQuery('#bordarecheada .pricesize-'+size).show();
        
        jQuery('#tamanhopizza .btn-control').removeClass('selected');
        jQuery('#tamanhopizza .btn-control[rel='+size+']').addClass('selected');
        
        jQuery('#tamanhopizza li[rel='+size+'] .item-picture-wrapper').fadeTo(200, 1);
        jQuery('#tamanhopizza li[rel='+size+']').siblings().children('.item-picture-wrapper').fadeTo(200, .5);
        
    }
    
    this.updatePrice = function(price){
            
        jQuery('#box-content-fixed .text').html(price);
    }
    var updatePrice = this.updatePrice;
    
                           
    this.showSizeInfo = function(size, data){
        
        this.updateSize(size);
        
        jQuery('#preco-final').html(data.total);
        updatePrice(data.total_pedido)
        
        jQuery('.btn-size-on').removeClass('btn-size-on').addClass('btn-size-off');
        jQuery('#pizzasize-'+size).removeClass('btn-size-off').addClass('btn-size-on');
        
    }
    
    this.showMassa = function(position, data){ 
        if(position == 'promo'){
            jQuery('.top-half').html(data.image).show();
            jQuery('.bottom-half').html(data.image).show();
            
            jQuery('#pizza-massa-top .type-pizza').html(data.massa_name_top + ' <span style="color: rgb(9, 64, 26);">(Promoção)</span>');
            jQuery('#pizza-massa-bottom .type-pizza').html(data.massa_name_bottom  + ' <span style="color: rgb(9, 64, 26);">(Promoção)</span>');
            
            jQuery('#adicione-pizza-top').hide();
            jQuery('#adicione-pizza-bottom').hide();
            jQuery('#pizza-massa-top').show();
            jQuery('#pizza-massa-bottom').show();
            
            updateIngredientes('top', data.ingredientes_top);
            updateIngredientes('bottom', data.ingredientes_bottom);  
        } else {
            var oposity = (position == 'top') ? 'bottom' : 'top'; 
            
            jQuery('.'+position+'-half').html(data.image).show();
            
            
            
            if(data.massa_name_top)
                jQuery('#pizza-massa-top .type-pizza').html(data.massa_name_top + ((data.is_promo) ? ' <span style="color: rgb(9, 64, 26);">(Promoção)</span>' : ''));
            
            if(data.massa_name_bottom)
                jQuery('#pizza-massa-bottom .type-pizza').html(data.massa_name_bottom  + ((data.is_promo) ? ' <span style="color: rgb(9, 64, 26);">(Promoção)</span>' : ''));
            
            jQuery('#adicione-pizza-'+position).hide();
            jQuery('#pizza-massa-'+position).show();
            
            if(data.ingredientes_top)
                updateIngredientes('top', data.ingredientes_top);
                
            if(data.ingredientes_bottom)
                updateIngredientes('bottom', data.ingredientes_bottom);   
        }                
        
        jQuery('#frase-inicial').hide();
        jQuery('#preco-final').html(data.total);
        updatePrice(data.total_pedido)
        jQuery('#preco-final').show();
        jQuery('.options-botoes').show(); 
        
    } 
    
    this.removeMassa = function(position, data){
        
        jQuery('#pizza-massa-'+position).hide();
        jQuery('#adicione-pizza-'+position).show();
        
        jQuery('#lista-ing-'+position).html('');
        
        var oposity = (position == 'top') ? 'bottom' : 'top';
        
        //jQuery('#pizza-massa-'+oposity+' .numero-metade').html('PIZZA INTEIRA');
        
        //AQUI jQuery('.'+position+'-half').hide();
        jQuery('.'+position+'-half').html('');
        jQuery('#pizza-massa-'+position+' .numero-metade').html((position == 'top') ? '<img src="/_resources/media/img/default/txt-primeira-metade.gif" alt="" />' : '<img src="/_resources/media/img/default/txt-segunda-metade.gif" alt="" />');
        jQuery('#pizza-massa-'+oposity+' .type-pizza span').remove();
        
        // AQUI if(jQuery('#'+oposity+'-half').is(':visible'))
            // AQUI jQuery('#'+oposity+'-half').addClass('full-pizza');
        
        jQuery('#preco-final').html(data.total);     
        updatePrice(data.total_pedido)    
        
        if(!Pedido.hasMassa()){
            jQuery('#frase-inicial').show();
            jQuery('#preco-final').hide();
            if(data.total_pedido == 'R$ 0,00')
                jQuery('.options-botoes').hide(); 
        }       
    }
    
    this.updateIngredientes = function(position, html){

        var number = (position == 'bottom') ? '2' : '';

        jQuery('#lista-ing-'+position).html('<div id="mycustomscroll'+number+'" class="flexcroll"><div class="bg-list-scroll">'+html+'</div></div>');
 
        CSBfleXcroll('mycustomscroll'+number);
 
        updateToolTips();
   
    }
    var updateIngredientes = this.updateIngredientes;
    
    this.removePizza = function(id){
        jQuery('#btn-pizza-'+id).addClass('pizza-off');
    }
    var removePizza = this.removePizza;
    
    this.updateDroppable = function(position){
        
        // Colocar a massa no massa top ou bottom
        jQuery('.'+position+'-half, #pizza-massa-'+position+', #adicione-pizza-'+position).droppable(
            { accept: "#sabordapizza .item-picture-wrapper, #ingredientes .item-picture-wrapper, #bordarecheada .item-picture-wrapper",
             activeClass: 'droppable-active',
             hoverClass: 'droppable-hover',
             tolerance: 'pointer',
             drop: function(ev, ui) {
                 var stringId = jQuery(ui.draggable).attr('id');
                 if(stringId.substr(0,1) == 'p'){
                     var dragId = jQuery(ui.draggable).attr('id').substring('pizza-'.length);
                     Pedido.setMassa(dragId ,position);
                     ui.helper.fadeOut();
                 } else if(stringId.substr(0,1) == 'i'){
                                          
                     if(jQuery(this).attr('id').search('top') != -1 && !Pedido.canAddIngTop()){
                         // Não faz nada, passou do limite de ingredientes.
                     } else if(jQuery(this).attr('id').search('bottom') != -1 && !Pedido.canAddIngBottom()){
                         // Não faz nada, passou do limite de ingredientes.
                     } else {
                         var dragId = jQuery(ui.draggable).attr('id').substring('ingrediente-'.length);
                         Pedido.addIngrediente(dragId ,position);
                         ui.helper.fadeOut();
                     }
                     
                 } else if (stringId.substr(0,1) == 'b'){
                     
                     var dragId = jQuery(ui.draggable).attr('id').substring('borda-'.length);
                     Pedido.setBorda(dragId ,position);
                     ui.helper.fadeOut();
                     
                 }
             }
            }
        );
        
        // Jogar a massa fora
        jQuery('.box-pizza-full').droppable(
        {
            accept: ".top-half, .bottom-half",
            activeClass: 'droppable-active',
            hoverClass: 'droppable-hover',
            tolerance: 'pointer',
            'out': function(event, ui) {
                jQuery(ui.helper).mouseup(function() {
                    ui.helper.children('img').fadeOut(100);
                    if(jQuery(ui.helper).attr('class').search('top-half') != -1)
                        Pedido.clearMassa('top');
                    else 
                        Pedido.clearMassa('bottom');
                    
                });
                
            },
            over: function(event, ui) {
                jQuery(ui.helper).unbind("mouseup");
            }
        });
        
         
        jQuery('.top-half, .bottom-half').draggable(
        {
            revert:true,
            helper:'original',
            appendTo:'body',
            drag: function(ev, ui){
            }
        });
        
    }
    var updateDroppable = this.updateDroppable;
    
    
    
    this.updateListBebidas = function(data){
        
       updatePrice(data.total_pedido)
       jQuery('#list-bebidas-content').html(data.bebidas);
       
       
         
       //CSBfleXcroll('mycustomscroll2');
       updateDropBebidas();
       updateToolTips();
    }
    
    this.updateListMeuPedido = function(data){
       if(data.resultado == 'true'){
           updatePrice(data.total_pedido);
           jQuery('#list-meupedido-content').html(data.meupedido);    
       } 
       
       
       updateToolTips();
    }
    
    var updateListMeuPedido =  this.updateListMeuPedido;
    
    this.updateDropBebidas = function (){
        jQuery('#list-bebidas').droppable(
        { accept: "#carousel-bebidas .item-picture-wrapper",
             activeClass: 'droppable-active',
             hoverClass: 'droppable-hover',
             drop: function(ev, ui) {
                 var stringId = jQuery(ui.draggable).attr('id');
                     var dragId = jQuery(ui.draggable).attr('id').substring('bebida-'.length);
                     jQuery.post('/pedidos/bebidas/add/'+dragId+'/', null, function(data){
                        
                        updateListBebidas(data);
                               
                    }, 'json');
                     ui.helper.fadeOut();
                 }
            }
        );
    }
    var updateDropBebidas = this.updateDropBebidas;
     
};

