/**
 * Rich Cart Trigger Widget
 * This widget provides the 'show rich cart' / 'hide rich cart' icon/link that is used
 * to display the Rich Cart.
 */
dojo.provide("atg.b2cblueprint.widget.RichCartTrigger");

dojo.widget.defineWidget(
  "atg.b2cblueprint.widget.RichCartTrigger", 
  dojo.widget.HtmlWidget,
  {
    // Define all global variables for the widget.
    // templatePath: dojo.uri.dojoUri(contextPath+"/javascript/widget/template/richCartTrigger.html"),
    templateString: '<a href="javascript:void(0);" class="richCartTrigger" dojoAttachEvent="onClick:toggleCart" dojoAttachPoint="triggerLink" title="${this.i18n.showCart}">${this.i18n.showCart}</a>',

    widgetId: "richCartTrigger",
    containerNodeId: null,            // DOM ID of the node to create the widget within
    cartWidget: null,                 // Reference to the rich cart widget
    cartOpenCssClass: "richCartOpen",  // CSS class that is appended to trigger DOM node when cart showing
    
    /**
     * Initialize the widget
     */
    postCreate: function(){     
      var _this=this;
      dojo.addOnLoad(function(){
        _this.attachToContainer();
      });
    },
    
    /**
     * Toggle the display of the rich cart
     */
    toggleCart: function(){
      dojo.debug("Toggling rich cart");         
      this.cartWidget.toggleCart();  
      this.updateTriggerDisplay();    
    },
    
    /**
     * Set the display state of the trigger widget.
     * Toggle trigger image/text "Show Cart" <--> "Hide Cart"
     */
    updateTriggerDisplay: function(){
      if (this.cartWidget===null){
         return;
      }
      
      var openOrOpening=(
        (this.cartWidget.isShowing && !this.cartWidget.cartAnimationInProgress)||
        (!this.cartWidget.isShowing && this.cartWidget.cartAnimationInProgress));

      if (openOrOpening){
        // Cart is open (or opening animation is in progress)
        this.triggerLink.innerHTML=this.i18n.hideCart;
        this.triggerLink.title=this.i18n.hideCart;
        dojo.html.addClass(this.domNode,this.cartOpenCssClass);
      }
      else {
        // Cart is closed (or closing animation is in progress)
        this.triggerLink.innerHTML=this.i18n.showCart;
        this.triggerLink.title=this.i18n.showCart;
        dojo.html.removeClass(this.domNode,this.cartOpenCssClass,false);
      }
    },
    
    /**
     * Attach this widget's domNode to its containing node
     */
    attachToContainer: function(){
      dojo.debug("Appending trigger domNode to "+this.containerNodeId);
      dojo.byId(this.containerNodeId).appendChild(this.domNode);
    }
  }
);

