/**
 * 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.RichCalculateShippingTrigger");

dojo.widget.defineWidget(
  "atg.b2cblueprint.widget.RichCalculateShippingTrigger",
  dojo.widget.HtmlWidget,
  {
    // Define all global variables for the widget.
    // templatePath: dojo.uri.dojoUri(contextPath+"/javascript/widget/template/richCalculateShippingTrigger.html"),
    templateString: '<a href="javascript:void(0);" class="richCartTrigger" onclick="atg.b2cblueprint.shipping.openCalculateProductShippingPopup(\'product\'); return false;" dojoAttachPoint="calculateShippingTriggerLink" title="${this.i18n.calculate}">${this.i18n.calculate}</a>',
    widgetId: "richCalculateShippingTrigger",
    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
    availableLink: true, //Flag fo "Calculate Shipping" link availability
    /**
     * Initialize the widget
     */
    postCreate: function(){     
      var _this=this;
      dojo.addOnLoad(function(){
      var selectedSku = dojo.byId("hSelectedSku");
          if ('' == selectedSku.value || 'unselectedSku' == selectedSku.value) {
            _this.setLinkUnavailable();
          }
      _this.attachToContainer();
      });
    },
    
    /**
     * Toggle the display of the rich cart
     */
    toggleCalculateShipping: function(pEventCodeTracking){
      dojo.debug("Toggling calculate shipping window");
      
      if (!this.availableLink) {
        return;
      }
      
      this.cartWidget.toggleCalculateShipping(pEventCodeTracking);
      this.updateTriggerDisplay();
    },
    /**
     *Set gray color to the link.
     */
     setLinkUnavailable:function(){
       this.calculateShippingTriggerLink.style.color="#666";
       this.availableLink = false;
     },
     /**
      *Set red color to the link.
      */
      setLinkAvailable:function(){
        this.calculateShippingTriggerLink.style.color="#ff6319";
        this.availableLink = true;
      },
    
    /**
     * 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.calculateShippingTriggerLink.innerHTML=this.i18n.hideCalculation;
        this.calculateShippingTriggerLink.title=this.i18n.hideCalculation;
        dojo.html.addClass(this.domNode,this.cartOpenCssClass);
      }
      else {
        // Cart is closed (or closing animation is in progress)
        this.calculateShippingTriggerLink.innerHTML=this.i18n.calculate;
        this.calculateShippingTriggerLink.title=this.i18n.calculate;
        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);
    }
  }
);

