﻿
if (typeof Pagen == "undefined" || !Pagen) {
    var Pagen = {};
}
var breadmachine;

FutureLab.addNamespace(Pagen,'Pagen.Web.UI');

Pagen.Web.UI.BreadMachine = Class.create({

    initialize: function(breadMachineId,productContainerId) {
        this.machine = $(breadMachineId);
        this.productresults = $('breadmachine-results');        
        this.productcontainer = $('breadmachine-products');        
                
        if (this.machine && this.productcontainer) {
            this.products = this.setupProducts(this.productcontainer);
            this.listSelectCallback = this.listClickCallback.bind(this);
            
            this.list = new FutureLab.selectableList('ul.select_list','li.select_item','selected','mouseover',this.listSelectCallback);
        
            var rows, height;    
            rows = Math.ceil(this.products.length/4);
            if (rows < 1) { rows = 1 };
            height = rows * 187; //182;
            this.productcontainer.style.height=height+'px';
            this.productresults.style.height=height+'px';         
        }        
    },
    
    setupProducts : function(container) {
        var me = this;
        var products = new Array();
        var count=0;
       
        if (container) {
            container.select('a').each( function(item) { 
                products[count]= item;
                count=count+1;
            });            
        }
        return products;
    },
    
    listClickCallback : function() {
        this.refresh();
    },
        
    refresh : function() {
        var me = this;
        var filter = new Array();      
     
        var start = function() {
            var effect = new fx.Opacity(me.productcontainer, { duration: 600, onComplete: afterHide });
            effect.custom(1,0);
        }        
        var afterHide = function(effect) {
            me.products.each( function(item) {
                item.processed = false;
                item.removeClassName('last');
                item.removeClassName('display');
                item.hide();
            });
            API.Web.UI.Client.Service.BreadMachine.FilterBreadList(filter, succeeded, failure);
         }        
        var isAllProcessed = function() {
            var result = true;
            me.products.each( function(item) {
                if (!item.processed) { result = false; }
            });
            return result;
        }        
        var succeeded = function(filter) {
            me.applyFilter(filter);
        }    
        var failure = function(result) { alert('BreadMachine.refresh : Failure : ?'); }
        
        this.list.getSelected().each( function(item) {
            filter.push(item.id);
        })
        
        start();
        
    },
    

    applyFilter : function(filter) {
        var me = this;
        var item;   
        
        for (var i=0; i < filter.length; i++) {
            item = $(filter[i]);
            if ((i+1) % 4==0) {
                item.addClassName('last');
            }
            item.addClassName('display');
            item.show();
        }
        // calc height   
        var rows,height;
        rows = Math.ceil(filter.length/4);
        if (rows < 1) { rows = 1 };
        
        height = rows * 187;
                 
        me.productcontainer.style.height=height+'px';
        
        var to = this.productcontainer.getHeight();
        var from = this.productresults.getHeight();
            
        var fadeInProducts = function() {
            var effect = new fx.Opacity(me.productcontainer, { duration: 600});
            effect.custom(0,1);
        }        
        var effect = new fx.Height(this.productresults, { duration: 400, onComplete: fadeInProducts });
        effect.custom(from,to);
    }
    
});


Event.observe(window, 'load', function() {  
    breadmachine = new Pagen.Web.UI.BreadMachine('breadmachine','breadmachine-products');
}); 






