 
brand.product.ProductMessage = Class.create(generic.popupMessage, 
{ 
    templateString: "",

    baseClass: "",
    widgetsInTemplate: false,
    itemCount: 0,
    
    initialize: function($super, arguments) {
    	//console.log("brand.product.productMessage.init: "+arguments.id); 
    	$super(arguments, true); 
    },

    postCreate: function() {
        //console.log("brand.product.productMessage.postCreate: "); 
        this.popup = this.containerNode;
    },
    
    show: function($super, resp) {
        //console.log("brand.product.productMessage.show "+this.is_open);
        if (this.is_open) { return; }
        this._updateDisplay(resp);
        var t = this._getTop();
        this.position = {top: t};
        $super(resp);
    }, 
    
    _getTop: function() { 
        //console.log("brand.product.productMessage._getTop");
        var t, h = 0;
        h = this.containerNode.offsetHeight;
        t = (h * -1)
        t = t.toString() + "px";  
        return t;
    },
    
    getErrors: function(response) {
        var errors;
        var key;
        if (response && response.getMessages()) {
            errors = {};
            var messages = response.getMessages();
            for (var i = 0; i < messages.length; i++) {
                if (messages[i].text) {
                    key = messages[i].key;
                    errors[key] = messages[i];
                }
            }
        }
        return errors;
    }
    
});


brand.product.CartAdd = Class.create( brand.product.ProductMessage,
{
    //templatePath: "/js/brand/product/templates/CartAdd.html",
    templatePath: "jsTemplates.product.CartAdd",
    
    is_shaded: false,
    _enabled: true,
    
    // sku: Object
    // sku object from page_data
    sku: null,
    
    // template vars
    prodName: "",
    smooshPath: "",
    hex: "",
    price: "",
    product_price_with_tax: "",
    
    initialize: function($super, arguments) {
    	this.templateString = false; //overrides ProductMessage  
          $super(arguments);
    },
    postCreate: function($super) {      
    	$super();
        this.smooshNode = $(this.smooshId);
        var button = $(this.skuFieldId);
        var self = this;
        if (button) {
            // add to cart button
            var prodBtn = new brand.product.productButton({
                node: button,
                callback: function(resp) {
                    if (self.callback) {
                        self.callback(resp);
                    }
                    self.close();
                    self.cartConfirm.show(resp);
                }
            });
        }
 
        generic.events.observe("productmessage:cartadd/show", function(event) {  
          
            if (self.is_open) {
                self.close();
            }
        });  
    },
    
    setConfirmProperties: function(args) {
        // used to set display properties of confirm on CartAdd popover callback (ex: for favorites vs. checkout message)
        this.cartConfirm.setDisplayProperties(args);
    },

    show: function($super, arguments) {
        generic.events.fire({event:"productmessage:cartadd/show",msg:this.sku});
        console.log("brand.product.CartAdd.show: ", this.prodName); 
        $super(arguments); 
    },
    
    _updateDisplay: function() {
        //console.log("CartAdd._updateDisplay: sku = "+this.sku);
        if ( this.smooshNode ) {
            this.smooshNode.style.backgroundColor = this.sku.color[0];
            var smooshImg = this.sku.smoosh;
            this.smooshNode.src = smooshImg.replace(/168x168/g, "56x56");
        }
        this.swatchTitleNode.innerHTML = this.sku.shade_name;
        
        /* inventory status */
        var inventory_status = this.sku.inventory_status;
        var suppressAddToBag = brand.product.inventoryStatus.suppressAddToBag.any(function(n) { return n==inventory_status; });
        if ( suppressAddToBag ) {  //JSTest use css class
        /* suppress the add to bag button */
            this.inventoryStatusNode.innerHTML = this.sku.inventory_status_message;
            this.addToBagNode.style.display = "none";
            this.containerNode.style.paddingBottom = 7 + "px"; 
        } else {
            this.inventoryStatusNode.innerHTML = "";
            this.addToBagNode.style.display = "";
            this.containerNode.style.paddingBottom = 0;
        }
        
        // "finish" name
        this.finishNameNode.innerHTML = (this.sku.finish ? "("+this.sku.finish+")" : "");
    }

});


brand.product.CartAddFromFavorites = Class.create( brand.product.CartAdd, {

    // isRemovable: Boolean
    // Use/show remove button
    isRemovable: true,
    
    initialize: function($super,args) {
	//console.log("brand.product.cartAddFromFavorites.init");
	$super(args);
    },
    postCreate: function($super, arguments) {    
        $super(arguments); 
        //console.log("brand.product.cartAddFromFavorites.postCreate");
           
        // set up remove button
        if (this.isRemovable) {     
            var self = this;
            var removeBtn = new brand.product.productButton({
                node: self.removeNode,
                
                valueField: $(self.skuFieldId),
                cartType: "favorites",
                cartAction: "remove",
                method: "alterCollection",
                action: "delete",
                callback: function(resp) { 
                    if (self.callbackRemoveButton) {
                        self.callbackRemoveButton({removeNodeId:self.removeNode.id,skuFieldValue:$(self.skuFieldId).value});
                    }
                    self.close();
                    //self.cartConfirm.show(resp);
                }
            });       
        
            this.removeNode.removeClassName("hidden");
        }
    }

});

brand.product.CartConfirm = Class.create( brand.product.ProductMessage,
{
    //templatePath: "/js/brand/product/templates/CartConfirm.html",
    templatePath: "jsTemplates.product.CartConfirm",
     
    is_shaded: false,
    //displayDuration: 5000,
    cartHandler: null,
    // sku: Object
    // sku object from page_data
    sku: null,
    
    prodName: "",
    
    // default text
    text_addedMessageCheckout: generic.rbKeys.get("was_added_to_your_shopping_bag"),
    text_addedMessageFavorites: generic.rbKeys.get("was_added_to_your_favourites"),
    text_itemCount: generic.rbKeys.get("items_in_bag"),
    text_itemCountSingular: generic.rbKeys.get("item_in_bag"),
    text_add_to_bag: generic.rbKeys.get("add_to_bag"),
    text_thank_you: generic.rbKeys.get("thank_you"),
    text_favorites: generic.rbKeys.get("favorites"),
    text_checkout: generic.rbKeys.get("checkout"),
    text_sorry: generic.rbKeys.get("sorry"),
    text_continue_shopping: generic.rbKeys.get("continue_shopping"),
	
    // type: String
    // item type for count in cookie (optional) ex: "favorites"
    type: "order", 
    
    initialize: function($super, arguments) { 
	$super(arguments); 
    },
    postCreate: function($super, arguments) {         
        $super(arguments); 
        this.cartHandler = brand.checkout.cartHandler;
    },
    setDisplayProperties: function(args) {
        // used to set display properties of confirm (ex: for favorites vs. checkout message) 
       generic.updateProperties.apply(this, [args]);
    },
    
   /** show: function($super, resp) {
        if (this.is_open) { return; }
        this._updateDisplay(resp);
        $super(resp);
    },**/
    
    _updateDisplay: function(resp) {
        //console.log("brand.product.cartConfirm._updateDisplay "+resp.getError()); 
        
        // check for error
        var hasErrors = resp ? resp.getError() : false;
                 
        // show error
        if (hasErrors) {  
            this.errorMessageNode.innerHTML = resp.getMessages() ? resp.getMessages()[0].text : ""; 
            this.cartConfirmErrorNode.removeClassName("hidden");
            this.cartConfirmDisplayNode.addClassName("hidden");  
        } 
        
        // show confirm message 
        else {
          try { 
            //this.itemCount = this.cartHandler.getItemCount(this.type);
            //this.itemCount = this.cartHandler.getTotalItems();   
            //this.itemCountNode.innerHTML = this.itemCount.toString();
            this.prodNameNode.innerHTML = this.prodName;
            if (this.is_shaded) {
                this.shadeNameNode.innerHTML = " - " + this.sku.shade_name;
            }
             
            if (this.type === "favorites") {
                this.buttonNodeCheckout.addClassName("hidden");
                this.buttonNodeFavorites.removeClassName("hidden"); 
                this.addedMessageNode.innerHTML = this.text_addedMessageFavorites;       
            } else {
                this.buttonNodeCheckout.removeClassName("hidden");
                this.buttonNodeFavorites.addClassName("hidden");      
                this.addedMessageNode.innerHTML = this.text_addedMessageCheckout;  
            }
          } catch(e) {
          	   console.log("brand.product.cartCOnfirm._updateDisplay e: "+e.description);
          }
        }
    },
    
    close: function($super) {
    	//console.log("brand.product.ProductMessage.close: " + this.closeCallback);
    	if (this.closeCallback) this.closeCallback();
    	$super();
    }

});
