var lbl_rated = 'Rated!';
var lbl_error = 'Error';
var lbl_cancel_vote = 'Already rated';
var lbl_error = 'Error';
var txt_minicart_total_note = 'Order subtotal does not cover discounts and extra costs like shipping charges, etc. The final cost of the order will be calculated at the checkout page.';
/* vim: set ts=2 sw=2 sts=2 et: */
/**
 * Rating bar widget
 * 
 * @category   X-Cart
 * @package    X-Cart
 * @subpackage JS Library
 * @author     Ruslan R. Fazlyev <rrf@x-cart.com> 
 * @version    $Id: ajax.rating.js,v 1.2.2.1 2010/11/22 10:27:34 aim Exp $
 * @link       http://www.x-cart.com/
 * @see        ____file_see____
 */

ajax.widgets.rating = function(elm) {
  if (!elm) {
    elm = $('.creviews-rating-box');

  } else {
    elm = $(elm);
  }

  elm.each(
    function() {
      if (!this.ratingWidget)
        new ajax.widgets.rating.obj(this);
    }
  );

  return true;
}

ajax.widgets.rating.obj = function(elm) {
  this.elm = elm;
  this.elm$ = $(elm);

  elm.ratingWidget = this;

  var s = this;

  this._rate = function() {
    return !s.rate(this);
  }

  this._callbackUB = function(responseText, textStatus, XMLHttpRequest) {
    return s._callbackUpdateBar(responseText, textStatus, XMLHttpRequest);
  }

  this._callbackR = function(state, a, b, c, d) {
    return s._callbackRate(state, a, b, c, d);
  }

  this._prepareWidget();

  this.state = 1;
}

// Options
ajax.widgets.rating.obj.prototype.ttl = 3000;

// Property
ajax.widgets.rating.obj.prototype.elm = false;

ajax.widgets.rating.obj.prototype.productid = false;

ajax.widgets.rating.obj.prototype.state = false;
ajax.widgets.rating.obj.prototype.isRated = false;

// Widget :: check widget status
ajax.widgets.rating.obj.prototype.isReady = function() {
  return this.productid;
}

// Widget :: do rate
ajax.widgets.rating.obj.prototype.rate = function(item) {
  if (!item || !this.isReady() || this.isRated)
    return false;

  this.changeState(2);

  var s = this;
  return ajax.query.add(
    {
      type: 'POST',
      data: '',
      url: $(item).attr('href'),
      success: function(a, b, c, d) {
        return s._callbackR(true, a, b, c, d);
      },
      error: function(a, b, c, d) {
        return s._callbackR(false, a, b, c, d);
      }
    }
  ) !== false;
}

// Widget :: update bar
ajax.widgets.rating.obj.prototype.updateBar = function() {
  if (!this.isReady())
    return false;

  if (this.state == 1)
    this.changeState(2);

  if (typeof(window.creviews_hover_loaded) && creviews_hover_loaded)
    creviews_hover_loaded = false;

  var data = {
    productid: this.productid
  };
  if (this.pconf)
    data.pconf = this.pconf;

  return ajax.core.loadBlock(this.elm$, 'rating_bar', data, this._callbackUB);
}

// Widget :: changes widget state
//  1 - idle
//  2 - wait
//  3 - success rated
//  4 - error
//  5 - cancel rate: already rated
ajax.widgets.rating.obj.prototype.changeState = function(state, msg) {
  if (this.state == state)
    return true;

  var res = false;

  switch (this.state) {
    case 2:
      res = this._cleanWaitState();
      break;

    case 3:
      res = this._cleanRatedState();
      break;

    case 4:
      res = this._cleanErrorState();
      break;

    case 5:
      res = this._cleanCancelState();
      break;

    default:
      res = this._cleanIdleState();
  }

  if (!res)
    return false;

  this.state = state;
  var o = this;

  switch (state) {
    case 2:
      res = this._doWaitState();
      break;

    case 3:
      res = this._doRatedState();
      break;

    case 4:
      res = this._doErrorState();
      setTimeout(
        function() {
          return o.changeState(1);
        },
        this.ttl
      );
      break;

    case 5:
      res = this._doCancelState();
      break;

    default:
      res = this._doIdleState();
  }

  return res;
}

/* Private */

// Widget :: prepare widget
ajax.widgets.rating.obj.prototype._prepareWidget = function() {

  // Check stars
  var links = $('.creviews-vote-bar a', this.elm$);
  if (links.length == 0)
    return false;

  // Detect productid
  var m = links.get(0).href.match(/productid=(\d+)/)
  if (!m)
    return false;

  this.productid = parseInt(m[1]);
  if (isNaN(this.productid) || this.productid < 1) {
    this.productid = false;
    return false;
  }

  // Detect Product Configurator properties
  m = links.get(0).href.match(/pconf=(\d+)/)
  if (m) {
    this.pconf = parseInt(m[1]);
    if (isNaN(this.pconf) || this.pconf < 1)
      this.pconf = false;
  }

  links.click(this._rate);

  return true;
}

// Widget :: callback rate action
ajax.widgets.rating.obj.prototype._callbackRate = function(state, a, b, c, d) {
  var s = 0;

  if (state && c.messages) {
    for (var i = 0; i < c.messages.length; i++) {
      if (c.messages[i].name == 'addVote' && c.messages[i].params.productid == this.productid) {
        s = parseInt(c.messages[i].params.status);
      }
    }
  }

  var o = this;
  switch (s) {
    case 1:
      this.changeState(3);
      setTimeout(
        function() {
          return o.updateBar();
        },
        this.ttl
      );
      break;

    case 2:
      this.changeState(5);
      setTimeout(
        function() {
          return o.updateBar();
        },
        this.ttl
      );
      break;

    default:
      if (!state && a.status == 0) {
        this.changeState(1);
      } else
        this.changeState(4);
  }

  return true;
}

// Widget :: callback update bar
ajax.widgets.rating.obj.prototype._callbackUpdateBar = function(responseText, textStatus, XMLHttpRequest) {
  if (XMLHttpRequest.status == 200) {
    this.savedElm = false;
    this.changeState(1);

  } else {
    this.changeState(4);
  }

  return true;
}

ajax.widgets.rating.obj.prototype._doIdleState = function() {
  if (this.savedElm) {
    var s = this;
    this.savedElm.children().each(
      function() {
        s.elm$.append(this);
      }
    );

    this.savedElm = false;
  }

  this.elm$.width('auto');

  return true;
}

ajax.widgets.rating.obj.prototype._cleanIdleState = function() {
  this.savedWidth = $('li:last', this.elm$).offset().left - $('li:first', this.elm$).offset().left + $('li:last', this.elm$).width();
  var pl = parseInt($('li:last', this.elm$).css('padding-left'));
  if (!isNaN(pl))
    this.savedWidth += pl;

  var pr = parseInt($('li:last', this.elm$).css('padding-right'));
  if (!isNaN(pr))
    this.savedWidth += pr;

  this.savedElm = this.elm$.clone();

  this.elm$.empty();

  return true;
}

ajax.widgets.rating.obj.prototype._doWaitState = function() {
  var block = document.createElement('SPAN');
  block.className = 'progress';

  this.elm$
    .width(this.savedWidth)
    .empty()
    .addClass('wait')
    .append(block);

  return true;
}

ajax.widgets.rating.obj.prototype._cleanWaitState = function() {
  if (this.elm$.children().length == 1)
    this.elm$.empty();

  this.elm$.removeClass('wait');

  return true;
}

ajax.widgets.rating.obj.prototype._doRatedState = function() {
  var block = document.createElement('SPAN');
  block.innerHTML = lbl_rated;

  this.elm$
    .width(this.savedWidth)
    .empty()
    .addClass('message')
    .addClass('rated')
    .append(block);

  return true;
}

ajax.widgets.rating.obj.prototype._cleanRatedState = function() {
  if (this.elm$.children().length == 1)
    this.elm$.empty();

  this.elm$
    .removeClass('message')
    .removeClass('rated');

  return true;
}

ajax.widgets.rating.obj.prototype._doErrorState = function() {
  var block = document.createElement('SPAN');
  block.innerHTML = lbl_error;

  this.elm$
    .width(this.savedWidth)
    .empty()
    .addClass('message')
    .addClass('error')
    .append(block);

  return true;
}

ajax.widgets.rating.obj.prototype._cleanErrorState = function() {
  if (this.elm$.children().length == 1)
    this.elm$.empty();

  this.elm$
    .removeClass('message')
    .removeClass('error');

  return true;
}

ajax.widgets.rating.obj.prototype._doCancelState = function() {
  var block = document.createElement('SPAN');
  block.innerHTML = lbl_cancel_vote;

  this.elm$
    .width(this.savedWidth)
    .empty()
    .addClass('message')
    .addClass('cancel')
    .append(block);

  return true;
}

ajax.widgets.rating.obj.prototype._cleanCancelState = function() {
  if (this.elm$.children().length == 1)
    this.elm$.empty();

  this.elm$
    .removeClass('message')
    .removeClass('cancel');

  return true;
}

// onload handler
$(ajax).bind(
  'load',
  function() {
    return ajax.widgets.rating();
  }
);
/* vim: set ts=2 sw=2 sts=2 et: */
/**
 * Ajax minicart widget
 * 
 * @category   X-Cart
 * @package    X-Cart
 * @subpackage JS Library
 * @author     Ruslan R. Fazlyev <rrf@x-cart.com> 
 * @version    $Id: ajax.minicart.js,v 1.2 2010/05/27 13:43:06 igoryan Exp $
 * @link       http://www.x-cart.com/
 * @see        ____file_see____
 */

// Facntory
ajax.widgets.minicart = function(elm) {
  if (!elm) {
    elm = $('.menu-minicart');

  } else {
    elm = $(elm);
  }

  elm.each(
    function() {
      if (!this.minicartWidget)
        new ajax.widgets.minicart.obj(this);
    }
  );

  return true;
}

// Class
ajax.widgets.minicart.obj = function(elm) {
  this.elm = $(elm);

  elm.minicartWidget = this;

  this.msie6 = $.browser.msie && parseInt($.browser.version) < 7;

  var s = this;

  $(ajax.messages).bind(
    'cartChanged',
    function(e, data) {
      return s._add2cartListener(data);
    }
  );

  this._minicartReposition = function(e) {
    return s.minicartReposition(e);
  }

  this._callbackMB = function(e) {
    s.click2Minicart = true;
    return s.minicartVisible ? s.hideMinicart() : s.showMinicart();
  }

  this._callbackUM = function(responseText, textStatus, XMLHttpRequest) {
    return s._callbackUpdateMinicart(responseText, textStatus, XMLHttpRequest);
  }

  this._deleteItem = function(e) {
    return !s.deleteItem(this, e);
  }

  this._updateCart = function(e) {
    return !s.updateCart(this, e);
  }

  this._clearCart = function() {
    return !s.clearCart();
  }

  this._showCheckoutPopup = function(e) {
    return s.checkoutPopupVisible ? !s.hideCheckoutPopup(this, e) : !s.showCheckoutPopup(this, e);
  }

  if (this.elm.hasClass('ajax-minicart')) {
    this._constructMinicartButton();
  }

  $('body').click(
    function() {
      if (!s.click2Minicart)
        s.hideMinicart();

      s.click2Minicart = false;
    }
  );

}

// Options
ajax.widgets.minicart.obj.prototype.errorTTL = 3000;
ajax.widgets.minicart.obj.prototype.minicartBorder = 0;

// Properties
ajax.widgets.minicart.obj.prototype.elm = false;
ajax.widgets.minicart.obj.prototype.minicart = false;
ajax.widgets.minicart.obj.prototype.minicartButton = false;

ajax.widgets.minicart.obj.prototype.minicartState = false;
ajax.widgets.minicart.obj.prototype.minicartVisible = false;
ajax.widgets.minicart.obj.prototype.minicartChanged = false;
ajax.widgets.minicart.obj.prototype.checkoutPopupVisible = false;

// Widget :: check widget status
ajax.widgets.minicart.obj.prototype.isReady = function() {
  return this.minicart.length > 0 && this.checkElement();
}

// Widget :: check element
ajax.widgets.minicart.obj.prototype.checkElement = function(elm) {
  if (!elm)
    elm = this.elm;

  return elm && elm.hasClass('menu-minicart');
}

// Widget :: update cart total block
ajax.widgets.minicart.obj.prototype.updateTotal = function() {
  return this.checkElement() && ajax.core.loadBlock($('div.minicart, span.minicart', this.elm), 'minicart_total');
}

// Widget :: update cart/checkout links block
ajax.widgets.minicart.obj.prototype.updateCartLinks = function() {
  return this.checkElement() && ajax.core.loadBlock($('div.cart-checkout-links', this.elm), 'minicart_links');
}

// Widget :: update minicart block
ajax.widgets.minicart.obj.prototype.updateMinicart = function() {
  if (!this.isReady())
    return false;

  this._markMinicartBoxAsLoaded();

  return ajax.core.loadBlock(this.minicart, 'minicart', {}, this._callbackUM);
}

// Widget :: show minicart
ajax.widgets.minicart.obj.prototype.showMinicart = function() {
  this._constructMinicartBox();

  if (this.minicartVisible)
    return false;

  this.minicartButton.addClass('minicart-button-show');

  if (this.minicartState == 1 || this.minicartChanged) {
    this._markMinicartBoxAsLoaded();
    this.updateMinicart();
  }

  if (this._iframe) {
    this._iframe.show();
  }

  this.minicart.show();

  this.minicartVisible = true;

  this.minicartReposition();

  return true;
}

// Widget :: hide minicart
ajax.widgets.minicart.obj.prototype.hideMinicart = function() {
  if (!this.minicart || !this.minicartVisible)
    return false;

  this.minicartButton.removeClass('minicart-button-show');

  if (this.checkoutPopupVisible)
    this.hideCheckoutPopup();

  this.minicart.hide();

  if (this._iframe) {
    this._iframe.hide();
  }

  this.minicartVisible = false;

  return true;
}

// Widget :: minicart reposition
ajax.widgets.minicart.obj.prototype.minicartReposition = function() {
  if (!this.isReady() || !this.minicartVisible)
    return false;

  if (this.elm.parents().filter('#left-bar').length > 0 ||  this.elm.hasClass('left-dir-minicart')) {
    var l = $('.ajax-minicart-icon', this.elm).position().left;
    var ml = $('.ajax-minicart-icon', this.elm).css('margin-left');
    if (ml) {
      ml = parseInt(ml);
      if (isNaN(ml))
        ml = 0;
    }
    l += ml;

    this.minicart.css('left', l - this.minicartBorder);

  } else if (this.elm.parents().filter('#right-bar').length > 0 || this.elm.hasClass('right-dir-minicart')) {
    var rb = $('.ajax-minicart-icon', this.elm).width() + $('.ajax-minicart-icon', this.elm).position().left;
    var ml = $('.ajax-minicart-icon', this.elm).css('margin-left');
    if (ml) {
      ml = parseInt(ml);
      if (isNaN(ml))
        ml = 0;
    }
    rb += ml;

    var pw = $('.ajax-minicart-icon', this.elm).parents().eq(0).width();

    this.minicart.css('right', pw - rb - this.minicartBorder);
  }

  this._iframeReposition();

  return true;
}

// Widget :: delete cart item
ajax.widgets.minicart.obj.prototype.deleteItem = function(item, e) {
  if (!this.isReady() || !item || !item.href)
    return false;

  this._markMinicartBoxAsLoaded();

  return ajax.query.add({ url: item.href }) !== false;
}

// Widget :: update cart
ajax.widgets.minicart.obj.prototype.updateCart = function(item, e) {
  if (!this.isReady() || !item || !item.form)
    return false;

  this._markMinicartBoxAsLoaded();

  return ajax.query.add(
    {
      type: 'POST',
      url: xcart_web_dir + '/cart.php',
      data: $(item.form).serialize()
    }
  ) !== false;
}

// Widget :: clear cart
ajax.widgets.minicart.obj.prototype.clearCart = function() {
  if (!this.isReady())
    return false;

  this._markMinicartBoxAsLoaded();

  return ajax.query.add({ url: xcart_web_dir + '/cart.php?mode=clear_cart' }) !== false;
}

// Widget :: show checkout popup
ajax.widgets.minicart.obj.prototype.showCheckoutPopup = function(item, e) {
  var p = $('.checkout-popup-link .buttons-box', this.minicart);
  if (this.checkoutPopupVisible || p.length == 0)
    return false;

  $('.checkout-popup-link', this.minicart).children('a').addClass('show');

  if (this._iframe_checkout)
    this._iframe_checkout.show();

  p.show();

  this.checkoutPopupVisible = true;

  return true;
}

// Widget :: hide checkout popup
ajax.widgets.minicart.obj.prototype.hideCheckoutPopup = function(item, e) {
  var p = $('.checkout-popup-link .buttons-box', this.minicart);
  if (!this.checkoutPopupVisible || p.length == 0)
    return false;

  $('.checkout-popup-link', this.minicart).children('a').removeClass('show');
  p.hide();

  if (this._iframe_checkout)
    this._iframe_checkout.hide();

  this.checkoutPopupVisible = false;

  return true;
}


/* Private */

// Widget :: add2cart message listener
ajax.widgets.minicart.obj.prototype._add2cartListener = function(data) {
  if (data.status == 1) {
    this._constructMinicartButton();
    this.updateTotal();
    this.updateCartLinks();

    if (data.isEmpty) {

      // Cart is empty
      this._cartIsEmpty();

    } else if (this.minicart && this.minicartVisible) {

      // Update minicart
      this._constructMinicartBox();
      this.updateMinicart();

    } else {

      // Save cart changed status
      this.minicartChanged = true;
    }
  }

  return true;
}

// Widget :: empty is cart
ajax.widgets.minicart.obj.prototype._cartIsEmpty = function() {
  this.hideMinicart();
  this._destructMinicartButton();

  $('.ajax-minicart-icon', this.elm).eq(0)
    .removeClass('full').addClass('empty')
    .parents('.full').removeClass('full').addClass('empty');

  ajax.core.trigger('cartCleaned');

  return true;
}

// Widget :: construct minicart box
ajax.widgets.minicart.obj.prototype._constructMinicartBox = function() {
  if (this.minicart)
    return false;

  var p = $('.ajax-minicart-icon', this.elm).get(0).parentNode;

  if (this.msie6) {
    this._iframe = document.createElement('IFRAME');
    this._iframe.className = 'minicart-bg';
    this._iframe = $(p.appendChild(this._iframe));

    this._iframe_checkout = document.createElement('IFRAME');
    this._iframe_checkout.className = 'minicart-checkout-bg';
    this._iframe_checkout = $(p.appendChild(this._iframe_checkout));
  }

  this.minicart = $(p.appendChild(document.createElement('DIV')));
  this.minicart.addClass('minicart-box');

  $(window).resize(this._minicartReposition);

  var s = this;
  this.minicart.click(
    function(e) {
      if (!s.click2CheckoutPopup)
        s.hideCheckoutPopup();

      s.click2CheckoutPopup = false;
      s.click2Minicart = true;
      s.showMinicart();
      return true;
    }
  );

  this.minicartState = 1;
  this.minicartVisible = false;

  return true;
}

// Widget :: mark minicart box as loaded
ajax.widgets.minicart.obj.prototype._markMinicartBoxAsLoaded = function() {
  if (this.minicart.hasClass('wait'))
    return false;

  var block = document.createElement('DIV');
  block.className = 'progress';

  this.minicart.empty().addClass('wait').append(block);

  this._iframeReposition();

  return true;
}

// Widget :: unmark minicart box as loaded
ajax.widgets.minicart.obj.prototype._unmarkMinicartBoxAsLoaded = function() {
  this.minicart.removeClass('wait').children('.progress').remove();

  this._iframeReposition();

  return true;
}

// Widget :: prepare minicart box
ajax.widgets.minicart.obj.prototype._prepareMinicart = function() {
  var s = this;

  $('.delete', this.minicart).click(this._deleteItem);

  $('.update-cart', this.minicart).click(this._updateCart);

  if ($('.clear-cart a', this.minicart).length > 0) {
    $('.clear-cart', this.minicart).click(
      function() {
        return false;
      }
    );
    $('.clear-cart a', this.minicart).click(this._clearCart);

  } else {
    $('.clear-cart', this.minicart).click(this._clearCart);
  }

  if ($('.checkout-popup-link .buttons-box', this.minicart).length > 0) {
    $('.checkout-popup-link a.link', this.minicart).click(this._showCheckoutPopup);
    $('.checkout-popup-link .buttons-box', this.minicart).click(
      function() {
        s.click2CheckoutPopup = true;
      }
    );
  }

  return true;
}

// Widget :: display error message
ajax.widgets.minicart.obj.prototype._displayMinicartError = function() {
  this.minicart.empty().html(lbl_error).addClass('error');

  return true;
}

// Widget :: construct minicart button
ajax.widgets.minicart.obj.prototype._constructMinicartButton = function() {
  if (this.minicartButton)
    return false;

  this.minicartButton = $('.ajax-minicart-icon', this.elm);
  if (this.minicartButton.length == 0)
    return false;

  this.elm.addClass('ajax-minicart');

  this.minicartButton
    .addClass('minicart-button')
    .click(this._callbackMB);

  return true;  
}

// Widget :: destruct minicart button
ajax.widgets.minicart.obj.prototype._destructMinicartButton = function() {
  if (!this.minicartButton)
    return false;

  this.elm.removeClass('ajax-minicart full-mini-cart');

  this.minicartButton
    .removeClass('minicart-button')
    .unbind('click', this._callbackMB);

  this.minicartButton = false;

  return true;
}

// Widget :: update minicart listener
ajax.widgets.minicart.obj.prototype._callbackUpdateMinicart = function(responseText, textStatus, XMLHttpRequest) {
  this._unmarkMinicartBoxAsLoaded();

  if (this.minicartState == 1) {

    // Minicart exists as empty box
    if (XMLHttpRequest.status == 200) {
      this.minicartState = 2;

    } else {
      this._displayMinicartError();
      var s = this;
      setTimeout(
        function() {
          s.hideMinicart();
          s._destructMinicartButton();
        },
        this.errorTTL
      );
    }
  }

  if (XMLHttpRequest.status == 200) {

    // Display new content
    this.minicartChanged = false;
    this._prepareMinicart();

  } else if (XMLHttpRequest.getResponseHeader('X-Request-Error-Code') == 1) {

    // Cart is empty
    this._cartIsEmpty();

  } else {

    // Error
    this._displayMinicartError();
    var s = this;
    setTimeout(
      function() {
        s.hideMinicart();
        s._destructMinicartButton();
      },
      this.errorTTL
    );
  }

  return true;
}

// Widget :: iframe reposition
ajax.widgets.minicart.obj.prototype._iframeReposition = function() {
  if (!this._iframe)
    return false;

  var pos = this.minicart.position();
  this._iframe
    .css({ top: pos.top + 'px', left: pos.left + 'px' })
    .width(this.minicart.width())
    .height(this.minicart.height());

  var box = $('.checkout-popup-link .buttons-box', this.minicart);
  if (box.length > 0) {
    pos = box.position();
    this._iframe_checkout
      .css({ top: pos.top + 'px', left: pos.left + 'px' })
      .width(box.width())
      .height(box.height());
  }

  return true;
}

// onload handler
$(ajax).bind(
  'load',
  function() {
    return ajax.widgets.minicart();
  }
);

