﻿Type.registerNamespace("epsilon");

epsilon.cartSummaryBehavior = function(element) {
   epsilon.cartSummaryBehavior.initializeBase(this, [element]);

   this._msg = '{0} ITEMS {1}{2}';
   this._totalItems = 0;
   this._cartTotal = 0;
   this._currency = '';
   this._popEl = null;

   this._closePopTimer = null;
   this._closePopTimerInterval = 3000;
   this._closePopTickHandler = null;
}

epsilon.cartSummaryBehavior.prototype = {

   set_Msg: function(str) {
      this._msg = str;
   },
   get_Msg: function() {
      return this._msg;
   },

   set_TotalItems: function(qty) {
      this._totalItems = qty;
   },
   get_TotalItems: function() {
      return this._totalItems;
   },

   set_CartTotal: function(total) {
      this._cartTotal = total;
   },
   get_CartTotal: function() {
      return this._cartTotal;
   },

   set_Currency: function(str) {
      this._currency = str;
   },
   get_Currency: function() {
      return this._currency;
   },

   set_PopEl: function(str) {
      this._popEl = str;
   },
   get_PopEl: function() {
      return this._popEl;
   },

   _closePopHandler: function() {
      if (this.get_PopEl()) {
         this.get_PopEl().style.display = 'none';
      }
      this._closePopTimer.set_enabled(false);
   },

   _showPopEl: function() {
      if (this._popEl) {
         var cartEl = this.get_element();
         var cartLocation = Sys.UI.DomElement.getLocation(cartEl);
         var cartBounds = Sys.UI.DomElement.getBounds(cartEl);

         this._popEl.style.display = '';
         var popBounds = Sys.UI.DomElement.getBounds(this._popEl);

         this._popEl.style.left = (cartLocation.x - (popBounds.width - cartBounds.width)) + 'px';
         this._popEl.style.top = (cartLocation.y + cartBounds.height) + 'px';

         this._opacity(0, 100, 1000);
         this._closePopTimer.set_enabled(true);
      }
   },

   _opacity: function(opacStart, opacEnd, millisec) {
      this._changeOpacity(100);
//      var speed = Math.round(millisec / 100);
//      var timer = 0;
//      if (opacStart > opacEnd) {
//         for (i = opacStart; i >= opacEnd; i--) {
//            setTimeout(this._changeOpacity(i), (timer * speed));
//            timer++;
//         }
//      } else if (opacStart < opacEnd) {
//         for (i = opacStart; i <= opacEnd; i++) {
//            //this._changeOpacity(i);
//            //setTimeout(this._changeOpacity(i), (timer * speed));
//            //timer++;
//         }
//      }
   },

   _changeOpacity: function(opacity) {
      var obj = this._popEl.style;
      obj.opacity = (opacity / 100);
      obj.MozOpacity = (opacity / 100);
      obj.KhtmlOpacity = (opacity / 100);
      obj.filter = "alpha(opacity=" + opacity + ")";
   },

   addItemToCart: function(qty, amount) {
      this._totalItems += qty;
      this._cartTotal += amount;
      this.get_element().innerHTML = String.format(this._msg, this._totalItems, this._currency, epsilonCommon.formatCurrency(this._cartTotal, ''));
      this._showPopEl();
   },

   dispose: function() {
      if (this._closePopTimer) {
         this._closePopTimer.remove_tick(this._closePopTickHandler);
         this._closePopTimer.dispose();
         this._closePopTimer = null;
      }

      epsilon.cartSummaryBehavior.callBaseMethod(this, 'dispose');
   },

   initialize: function() {

      this._closePopTickHandler = Function.createDelegate(this, this._closePopHandler);
      this._closePopTimer = new epsilon.Timer();
      this._closePopTimer.add_tick(this._closePopTickHandler);
      this._closePopTimer.set_interval(this._closePopTimerInterval);
      this._closePopTimer.set_enabled(false);

      epsilon.cartSummaryBehavior.callBaseMethod(this, 'initialize');
   }
}
epsilon.cartSummaryBehavior.registerClass('epsilon.cartSummaryBehavior', Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

