//********
//* Module 'shopbuilderCore' This is a YUI3 module for the shopbuilder application core.
//* 

//alert('shopbuilder_appcore.js loading');


YUI.add('shopbuilder-core', function(Y) {
    var Lang = Y.Lang,
        NAME = 'appCore';
    Y.namespace('Shopbuilder');
    
    Y.Shopbuilder.AppCore = Y.Base.create(
        NAME, 
        Y.Base, 
        [], 
        {
            initializer : function (config) {
                this.publish("cartModified", {
                    defaultFn: this._defCartModifiedFn
                });
            },
            destructor : function () {
            
            },
            
            _defCartModifiedFn : function (e) {
                Y.log("appcore - cart modified event");
                //alert('appcore - cart modified');
                
                //-----------------
                //directly update the old cartstatus style 1 dom
                //(newer cartstatus styles will subscribe and handle it themselves)
                var cart = e.cartsummary;
                var statustable = Y.one('#table_cart_status');
                if (statustable) {
                    statustable.one('.numitems_value').set('innerHTML',cart.unit_count);
                    statustable.one('.total_value').set('innerHTML',cart.currency_sign + cart.value);
                }
                //-----------------
            },
            
            notifyCartModified : function(cart) {
                this.fire("cartModified",{
                    cartsummary: cart
                });
            }
            
            
            
        }, 
        {
            ATTRS : {
                version : {
                    value: '0.1'
                },
                storename : {
                    value: undefined
                },
                simple_basket_procedure : {
                    value: 0,
                    validator: Lang.isNumber
                }
            }
        }
    );
}, '1.0', {requires: ['base-build']});



/*
//e.g
SHOPBUILDER.Y.use('shopbuilderCore', function(Y) {
    SHOPBUILDER.appCore = new Y.shopbuilder.AppCore();
});
SHOPBUILDER.Y.use('*', function(Y) {
    alert('shopbuilder core version: ' + SHOPBUILDER.appCore.get('version'));
  });
*/
YUI.add('shopbuilder-cartstatus2', function(Y) {
    Y.namespace('Shopbuilder').Cartstatus2 = Y.Base.create(
        'cartstatus2', 
        Y.Widget,
        [], 
        {
            initializer : function (config) {
                
            },
            destructor : function () {
            
            },
            renderUI: function () {
                var cb = this.get("contentBox");
                //var lbl = this.getString("cartstatus_title"); //widget 'strings' attr ?
                //cb.appendChild(new_element);
            },
            bindUI: function () {
            
            
                //state to view
                this.after("lineitem_countChange", Y.bind(this._afterlineitem_countChange, this));
                
                //view to state
                //e.g 'undo' last add
                //this.on("click",Y.bind(this._onUndoClick, this),this.undoButton);
            },
            syncUI: function () {
                this._uiSetCartStatus({
                    lineitem_count: this.get("lineitem_count"),
                    unit_count: this.get("unit_count"),
                    value: this.get("value"),
                    currency_sign: this.get("currency_sign")
                });
            },
            _afterStateChange: function (e) {
                if (e.src !== UI) {
                    this._uiSetCartStatus(e.newVal);
                }
            },
            _uiSetCartStatus: function (objstatus) {
                //manipulate the dom to match the cartstatus
                //cbox.one('.lineitem_count_value').set('innerHTML',objstatus.lineitem_count
            },
            setCartStatus: function (config) {
                this.setAttrs(config);
            }
            
        },
        {
            ATTRS : {
                lineitem_count: {
                    value: 0,
                    validator: Y.Lang.isNumber
                },
                unit_count: {
                    value: 0,
                    validator: Y.Lang.isNumber
                },
                value: {
                    value: 0,
                    validator: Y.Lang.isNumber
                },
                currency_sign: {
                    value: '$'
                }
            }
        }
    );
}, '1.0', {requires: ['widget']});

