var productInfoAlternateCtrl = {
  bAlternate : false, // a variable determines if an atlernative product will be proposed
  bCheckForm : true,
  bDoSubmit : true,
  oSelWrs : {},
  ini : function (bAlternate, winWr)
  {
    this.bAlternate = typeof(bAlternate) != "undefined" ? bAlternate : false;
    this.winWr = typeof(winWr) != "undefined" ? winWr : _wrapper;
    this.getLoad();
  },
  getLoad : function ()
  {
    var aRes, aSelects;
    this.frmWr = this.winWr.getForm(this.config.formName);
    this.frmWr.addListener(this, "onsubmit", "addToCart");
    //a product Id
    this.iProductId = this.winWr.getFormElement(this.config.formName, this.config.prodIdElm).elm.value;
    //a div to append a popup div
    this.popupDivWr = this.winWr.getElement(this.config.popupDivId);
    this.loadDivWr = this.winWr.getElement(this.config.loadDivId);
    //a glass popup
    this.bIsGlass = this.winWr.checkElement(this.config.glassPopupId);
    if (this.bIsGlass) {
      this.glassPopupWr = this.winWr.getElement(this.config.glassPopupId);
    }
    var oReg = new RegExp("^id\\[(\\d+)\\]$", "");
    oReg.compile("^id\\[(\\d+)\\]$" , "");
    //getting attribute wrapers
    aSelects = this.frmWr.elm.getElementsByTagName('SELECT');
    for (var i = 0; i < aSelects.length; i++) {
      aRes = oReg.exec(aSelects[i].name);
      if (aRes != null) {
        this.oSelWrs[aRes[1]] = this.winWr.getElmWrapper(aSelects[i]);
      }
    }
    this.dataLoader = this.winWr.getLoadWrapper('/product_info_alternate.ajax.php', "get");
    this.dataLoader.addListener(this, "ondataload", "showPopup");
    this.dataLoader.addListener(this, "ondataerror", "onDataError");
  },
  addToCart : function (evtWr)
  {
    //dropping a form submitting if a form was already submitted
    if (!this.bDoSubmit) {
      evtWr.eventDrop();
      return;
    }
    //avoiding a form validation if a form was checked previously
    if (!this.bCheckForm) {
      return;
    }
    //checking if all attributes are set
    var oAttr = {};
    for (var i in this.oSelWrs) {
      if (!parseInt(this.oSelWrs[i].elm.value)) {
        alert(sAttrsError);
        evtWr.eventDrop();
        return;
      }
      oAttr[i] = parseInt(this.oSelWrs[i].elm.value);
    }
    // if a product is a glass the glass popup will be shown
    if (this.bIsGlass && !this.glassPopupWr.isDisplay() && typeof(oProdInfo) != "undefined") {
      oProdInfo.showPopup();
      evtWr.eventDrop();
      return;
    }
    //avoiding a validation if a product is on stock and submitting a form
    if (!this.bAlternate) {
      return;
    }
    this.bDoSubmit = false;
    this.loadDivWr.setDisplay(true);
    this.loadDivWr.move(this.winWr.getScrollX() + this.winWr.getWindowWidth() / 2 - this.loadDivWr.getWidth() / 2, this.winWr.getScrollY() + this.winWr.getWindowHeight() / 2 - this.loadDivWr.getHeight() / 2);
    //checking if a product is on stock
    this.dataLoader.send({
      products_id : this.iProductId,
      attr : oAttr
    });
    evtWr.eventDrop();
  },
  showPopup : function (oData, sHtml)
  {
    if (oData.bOnStock) {
      this.bCheckForm = false;
      this.bDoSubmit = true;
      this.frmWr.elm.submit();
      return;
    }
    this.loadDivWr.setDisplay(false);
    this.removeContent();
    this.popupDivWr.elm.appendChild(sHtml);
    var popupWr = this.winWr.getElement(this.config.popupId);
    popupWr.move(this.winWr.getScrollX() + this.winWr.getWindowWidth() / 2 - popupWr.getWidth() / 2, this.winWr.getScrollY() + this.winWr.getWindowHeight() / 2 - popupWr.getHeight() / 2);
    this.winWr.getElement(this.config.btnChooseId).addListener(this, "onclick", "clickChoose");
    this.winWr.getElement(this.config.btnContinueId).addListener(this, "onclick", "clickContinue");
  },
  removeContent : function () {
    while (this.popupDivWr.elm.hasChildNodes()) {
      this.popupDivWr.elm.removeChild(this.popupDivWr.elm.firstChild);
    }
  },
  clickChoose : function (evtWr)
  {
    this.bCheckForm = true;
    this.bDoSubmit = true;
    this.removeContent();
  },
  clickContinue : function (evtWr)
  {
    this.bCheckForm = false;
    this.bDoSubmit = true;
    this.frmWr.elm.submit();
    return;
  },
  config : {
    popupId : "alternate_popup",
    popupDivId : "alternate_popup_div",
    glassPopupId : "glasses_settings",
    loadDivId : "ajax_loading_div",
    btnChooseId : "btn_choose",
    btnContinueId : "btn_continue",
    formName : "cart_quantity",
    prodIdElm : "products_id",
    moveDelay : 500
  }
}
