//-----------------------------------------------------------------------
// Module name   : GtGiftReg
// Author        : Paul Battersby
// Creation Date : March 26/10
//
//  $Log:$
//------------------------------------------------------------------------

//---------------------------- INCLUDE FILES -----------------------------

var GtGiftReg = new Class({
  Implements: [Events, Options],

//----------------------------- CONSTANTS --------------------------------


//----------------------------- VARIABLES --------------------------------

  options : {
    // name of the html table that contains the gift registry
    giftRegTableName : "gtGiftReg_table",

    // name of the dbase table that contains the gift registry items
    dbTableName : "gt_giftreg_items",

    emptyTableLength : 1, // # table rows in an empty table
                          //  0 = there is no title row
                          //  1 = only title row remains
                          //  2 = there is a header and footer perhaps

    show : -1 // indicates which form to show initially
              //  only used if an error has occurred and upon page refresh
              //  the same form needs to remain visible
  },

  // this is a lookup table to go from the #link to open an accordion
  // by default (NOTE: gtReg automatically inserts these link names
  //             when creating forms)
  formAnchors : {
    "gtReg_loginForm" : 0,
    "gtReg_forgotPasswordForm" : 1,
    "gtReg_registerForm" : 2,
    "gtReg_loginFormFriends" : 3
  },

  updateAjax : null, // created by the constructor
  deleteAjax : null, // created by the constructor

//----------------------------- FUNCTIONS --------------------------------

  //************************************************************************
  // Name   : updateQuantity
  //  This updates the quantity of the given item by placing an Ajax call
  //
  // Returns : (nothing)
  //************************************************************************
  updateQuantity : function(formField) {
    var value = parseInt(formField.getProperty("value"));

    // default to 0 for invalid values
    if ((value === NaN) || value < 0) {
      value = 0;
    } // endif

    this.updateAjax.POST({
      action : "update",
      tableName : this.options.dbTableName,
      id : formField.getProperty("id"),
      quantity : value
    });
  },

  //************************************************************************
  // Name   : deleteItem
  //  This deletes the item from the database by placing an Ajax call
  //
  // Returns : (nothing)
  //************************************************************************
  deleteItem : function(formField) {
    if (confirm("Delete this item?")) {
      // remove the table row from the table
      formField.getParent().getParent().destroy();

      // remove the item from the database
      this.deleteAjax.POST({
        action : "delete",
        id : formField.getProperty("data-id"),
        tableName : this.options.dbTableName
      });

    } // endif
  },

  //************************************************************************
  // Name   : initUpdate
  //
  // Returns : (nothing)
  //************************************************************************
  initUpdate : function() {

    // loop through all the quantity form fields
    $$(".gtGiftRegQuantity").each(function(item,index) {
      var image = [];

      // add a change event to the form field
      item.addEvent("change",function(event) {
        this.updateQuantity(event.target);
      }.bind(this));

      // insert a delete button beside each form field
      image[index] = new Element("img",{
        src : "gtLib/GtGiftReg/GtGiftReg_delete.gif",
        alt : "delete this item",
        title : "delete this item",
        "data-id" : item.getProperty("id"), // store the id name in the image tag
        styles : {
          "cursor" : "pointer",
          "margin-left" : 4,
          "vertical-align" : "top"
        }
      });

      // insert the image after the form field
      image[index].inject(item,"after");

      // add a click event to the image
      image[index].addEvent("click",function(event) {
        this.deleteItem(event.target);
      }.bind(this));

    }.bind(this));
  },

  //************************************************************************
  // Name   : initAccordion
  //  - creates the accordion effect to hide/reveal the various forms,
  //  - initializes lmooCountrySelect (if so configured)
  //  - creates gtFormmakerVal form validators for all the forms
  //
  // Returns : (nothing)
  //************************************************************************
  initAccordion : function() {
    window.addEvent("domready",function() {
      // allow forms sections to hide and reveal
      new LmooMenuExpand("",".gtGiftRegToggler",".gtGiftRegElement", {
        objId : "login"
      });

    }.bind(this));
  },

  //************************************************************************
  // Name   : initialize (constructor)
  //
  //  (object) options - (optional) configuration options. See options declaration
  //                     above for the available options
  //
  // Returns : (nothing)
  //*************************************************************************
  initialize : function(formCfg,options) {
    this.setOptions(options);
    this.formCfg = formCfg;

    this.updateAjax = new Request.JSON({
      url : "gtLib/GtGiftReg/GtGiftReg_ajax.php",
      onFailure : function() {
        alert("GtGiftReg - failed to update gift registry");
      }
    });

    this.deleteAjax = new Request.JSON({
      url : "gtLib/GtGiftReg/GtGiftReg_ajax.php",
      onSuccess : function() {
        // if table is empty, then reload the page which will
        // presumbly show the empty table html in the custom
        // gift registry display code
        if ($(this.options.giftRegTableName).rows.length == this.options.emptyTableLength) {
          window.location.reload(true);
        } // endif
      }.bind(this),
      onFailure : function() {
        alert("GtGiftReg - failed to delete gift registry item");
      }
    });
  }

});

