// <![CDATA[
/**
 * ¼÷¼Ò
 *
 * $Id: mainHotel.js 14 2009-04-01 09:39:55Z admin $
 */

var cHotelList = Class.create({
  initialize: function(options)
  {
    this.options = {
      oTypeList:  []    // Á¤·Ä¼ø¼­
      , htList:   $H()  // Á¶È¸µÈ ¼÷¼Ò ¸ñ·Ï (ajax·Î Àç Á¶È¸ ÇÏÁö ¾Ê±â À§ÇØ ÀúÀåÇÑ´Ù.)
      , tplHtList:  null  // Ãâ·ÂÇÒ ÇüÅÂ (template)
      , tplSyntax:  /(^|.|\r|\n)(@\s*(\w+)\s*@)/  //matches symbols like '@field@'
      , tplHtImg:   null  // Ãâ·ÂÇÒ ÇüÅÂ - ÀÌ¹ÌÁö (template)
      , containerID:  null  // ½ºÄÉÁÙÀ» Ãâ·ÂÇÒ À§Ä¡ (element id)
      , maxSearchCount:  1  // Á¶È¸ È½¼ö (second)
      , td_per_tr:  5   // ÁöÁ¤µÈ °³¼ö ¸¶´Ù <tr> »ðÀÔ, ÀÌ¿ëÇÏÁö ¾ÊÀ» °æ¿ì '0'À¸·Î ¼³Á¤
    };
    this.setOptions(options);
  } // end of the 'initialize()' function


  // setOption
  , setOptions: function(options)
  {
    Object.extend(this.options, options || {});
  } // end of the 'setOptions()' function


  /**
   * set OrderTypeList
   *
   * @param [array] list
   */
  , setOrderTypeList: function(list)
  {
    if (!Object.isArray(list)) {
      return false;
    } // end if

    this.options.oTypeList = list;
  } // end of the 'setOrderTypeList()' function


  // set template file of airplan
  , setTplHtList: function(tpl, syntax)
  {
    if (!Object.isString(tpl)) {
      return false;
    } // end if

    syntax = Object.isString(syntax) ? syntax : this.options.tplSyntax;

    this.options.tplHtList = new Template(tpl, syntax);
  } // end of the 'setTplHtList()' function


  // set template file of airplan
  , setTplHtImg: function(tpl)
  {
    if (!Object.isString(tpl)) {
      return false;
    } // end if

    this.options.tplHtImg = new Template(tpl, this.options.tplSyntax);
  } // end of the 'setTplHtImg()' function


  // set containerID
  , setContainer: function(id)
  {
    var element = $(id);

    if (element == null) {
      return false;
    } // end if

    this.options.containerID = element;
  } // end of the 'setContainer()' function


  /**
   * search Hotel List
   *
   * @param [string]    htClass   ¼÷¼Ò Á¾·ù
   * @param [string]    orderType Á¤·Ä ¼ø¼­ (default: RECOMMEND)
   * @param [integer]   listNum (default: 15)
   */
  , _getHtList: function(htClass, orderType, listNum)
  {
    var element = this.options.containerID;
    if (element == null) {
      return false;
    } // end if

    listNum = Object.isUndefined(listNum) ? 15 : listNum;

    new Ajax.Request(
      _CZ_PATH + '/_ajax/ajaxSearchHotel.php',
      {
        method: 'post',
        asynchronous: true,
        parameters: 'htType='+htClass+'&orderType='+orderType+'&listNum='+listNum,
        onLoading : element.update('<tr><td align="center"><img src="/_template/images/ajax-loader-c.gif" width="32" height="32" border="0"></td></tr>'),
        onComplete: function(transport) {
          var json = transport.responseText.evalJSON(true);
          if (!json) {
            //alert('Á¶È¸µÈ ³»¿ëÀÌ ¾ø½À´Ï´Ù.');
            return false;
          } // end if

          this._setHtList(htClass, orderType, json);
        }.bind(this)
      });
  } // end of the '_getHtList()' function


  /**
   * set Hotel List
   *
   * @param [string]  htClass
   * @param [string]  orderType
   * @param [array]   list
   */
  , _setHtList: function(htClass, orderType, list)
  {
    var htList = '';

    if (!Object.isArray(list)) {
      return false;
    } // end if

    list.each(function(h, index) {
      var d = {HT_KEY:        h.HT_KEY
              , IMG_MINI:     h.IMG_MINI.empty()
                                ? 'ÀÌ¹ÌÁö ÁØºñÁß'
                                : (this.options.tplHtImg != null ? this.options.tplHtImg.evaluate(h) : h.IMG_MINI)
              , HT_NAME:      h.HT_NAME.truncate(14)
              , ZONE_NAME:    h.ZONE_NAME
              , LEVEL_NAME:   h.LEVEL_NAME
              , AREA:         h.AREA
              , AREA_TITLE:   h.AREA_STRIPED_TAG
              , HONEY_CMT:    h.HONEY_CMT
              , HONEY_CMT_TITLE:  h.HONEY_CMT_STRIPED_TAG
              , CMT:          h.CMT
          };

      if (this.options.td_per_tr > 0 && index % this.options.td_per_tr == 0) {
        htList += '<tr align="center" valign="top">';
      } // end if

      htList += this.options.tplHtList.evaluate(d);

      if (this.options.td_per_tr > 0 && index % this.options.td_per_tr == this.options.td_per_tr - 1) {
        htList += '</tr>';
      } // end if
    }.bind(this));
    if (this.options.td_per_tr > 0 && list.size() % this.options.td_per_tr != 0) {
      $A($R(list.size() % this.options.td_per_tr, this.options.td_per_tr - 1)).each(function() {
        htList += '<td>&nbsp;</td>';
      });
      htList += '</tr>';
    } // end if

    this.options.htList.set(htClass+':'+orderType, htList);
  } // end of the '_setHtList()' function


  /**
   * get Hotel List
   *
   * @oaram [strign]  htClass
   * @param [string]  orderType
   * @param [string]  selecter (CSS rule)
   * @param [string]  elementID (selected element id)
   * @param [string]  imgTail
   */
  , getHtList: function(htClass, orderType, selecter, elementID, imgTail)
  {
    htClass = !htClass || Object.isUndefined(htClass) ? '' : htClass;
    orderType = !orderType || Object.isUndefined(orderType) ? 'RECOMMEND' : orderType;

    var searchCount = 0;
    var htList = this.options.htList.get(htClass+':'+orderType);
    var element = $(elementID);

    // ¼±ÅÃÇÑ TabMenu È°¼ºÈ­ (ÀÌ¹ÌÁö º¯°æ)
    if (element != null) {
      $$(selecter).each(function(ele) {
        var imgSrc = ele.src;
        if (Object.isUndefined(imgSrc)) {
          return;
        } // end if

        ele.src = imgSrc.replace(imgTail+'.gif', '.gif');
        if (ele.id == elementID) {
          ele.src = imgSrc.replace('.gif', imgTail+'.gif');
        } // end if
      });
    } // end if

    // ¼÷¼Ò ¸ñ·Ï Ãâ·Â
    if (Object.isUndefined(htList)) {
      this._getHtList(htClass, orderType);

      new PeriodicalExecuter(function(pe) {
        var htList = this.options.htList.get(htClass+':'+orderType);

        if (!Object.isUndefined(htList)) {
          this.printHtList(htList);
          pe.stop();
        }
        else if (++searchCount > this.options.maxSearchCount) {
          this.printHtList();
          pe.stop();
        } // end if
      }.bind(this), 1);
    }
    else {
      this.printHtList(htList);
    } // end if
  } // end of the 'getHtList()' function


  // °á°ú Ãâ·Â
  , printHtList: function(result)
  {
    var element = this.options.containerID;
    if (element == null) {
      return false;
    } // end if

    if (Object.isUndefined(result)) {
      element.update('<tr><td align="center">Á¶È¸µÈ ³»¿ëÀÌ ¾ø½À´Ï´Ù.</td></tr>');
      return false;
    } // end if

    element.update(result);
  } // end of the 'printHtList()' function
});






/*
// »çÀÌÆ®º° Á¶Á¤
var oHtList = new cHotelList();
    oHtList.setOrderTypeList(['ÃßÃµ', 'ÀÎ±â', '±âÁØÀÎ¿ø', 'ÃÖÀú¿ä±Ý']);   // RECOMMEDN, POPULAR, CAPACITY, MINIMUM
    oHtList.setTplHtList('...@fieldName@...');
    oHtList.tplHtImg('...@fieldName@...');
    oHtList.setContainer('htListBody');
*/

// ]]>