var eShops = {
    store: {
        fills: Class.create({
            current: null,
            initialize: function() {
				var groups = $$('.store .fills ul > li > a');
                groups.each(function(group) {
					group.observe('click', function(e) {
                        Event.stop(e);
                        this.toggle(group);
                    }.bindAsEventListener(this));
                }.bind(this));				
            },
            toggle: function(element) {
                if (this.current === element) {
                    element.removeClassName('active');
                    element.next().hide();
                    this.current = null;
                } else {
                    element.addClassName('active');
                    element.next().show();
                    if (this.current !== null) {
                        this.current.removeClassName('active');
                        this.current.next().hide();
                    }
                    this.current = element;
                }
            }
        }),
        services: Class.create({
            services: {},
            orders: {},
            credits: 0,
            total: 0,
            initialize: function(credits, data) {
                this.credits = credits;
                data.each(function(service) {
                    this.services[service.name] = service;
                }.bind(this));
                $('order').disabled = true;
            },
            calculate: function() {
                this.total = 0;
                for (var service in this.orders) {
                    this.total = this.total + this.orders[service];
                }
                if (this.total > this.credits) {
                    $('status').removeClassName('info');
                    $('status').addClassName('error');
                    $('order').disabled = true;
                } else if (this.total === 0) {
                    $('status').removeClassName('error');
                    $('status').addClassName('info');
                    $('order').disabled = true;
                } else {
                    $('status').removeClassName('error');
                    $('status').addClassName('info');
                    $('order').disabled = false;
                }
                $('credits').update(this.credits - this.total);
                return false;
            },
            order: function(service, amount) {
                this.orders[service] = this.services[service].price * amount;
                return this.calculate();
            }
        })
    }
};

Event.observe(window, 'load', function() {
    new eShops.store.fills();
    
    $('big-red-button').observe('mouseover', function() {
        $('big-red').setStyle({
            left: ($('big-red-button').cumulativeOffset().left-10)+ 'px'
        }).show();
    });
    
    $('big-red-button').observe('click', function(e) {
        Event.stop(e);
    });
    
    Event.observe(document.body, 'click', function() {
        $('big-red').hide();
    });
});
