// Generated by CoffeeScript 1.8.0
(function() {
  var loadData;

  loadData = function() {
    var convert, item, swp, top;
    top = '<div class="top"><img src="../../static/images/product/product_top.jpg" />{btnLogin}</div>';
    item = $("#itemTpl")[0].innerHTML;
    swp = new $.compent.swiper("#productList", {   
      tpl: item,
      topHtml: top,
      refreshEnabled: false
    });
    convert = {
      status: function(val, obj) {
        return (val === 1 && "热销") || (val === 0 && "售馨") || (val === 2 && "在售") || (val === 4 && "售完");
      },
      bgColor: function(val, obj) {
        val = obj.status;
        return (val === 1 && "red") || (val === 0 && "gray") || (val === 2 && "yellow") || (val === 4 && "green");
      },
      expectedProfit: function(val, obj) {
        return val / 1000;
      },
      btnLogin: function(val, obj) {
        if (val) {
        	var _id = $.cookie('_id');
        	if(_id != null){
        		return '<div class="btn_login"><button type="button" onclick="javascript: location.href=\'./appointmentList.html\'" class="am-button am-button-red"><i class="icon-info"></i></button></div>';
        	}else{
        		return '<div class="btn_login"><button type="button" onclick="javascript: location.href=\'../user/login.html\'" class="am-button am-button-red"><i class="icon-info"></i></button></div>';
        	}
          
        } else {
          return '';
        }
      }
    };
    swp.convert(convert);
    swp.dataSource = (function(_this) {
      return function(pageIndex, s, opt) {
        return _this.Ajax({
          url: _this.baseUrl + '/api/product/list.do',
          data: {
            page: pageIndex
          },
          type: 'get',
          callback: function(res) {
            opt['topData'] = {
              btnLogin: true
            };
            $.each(res.datas,function(idx,item){
      		  if(item.expectedProfit===null||item.expectedProfit===""||item.expectedProfit===undefined){
      			  item.expectedProfit="浮动";
      		  }else{
      			item.expectedProfit=item.expectedProfit+"%";
      		  }
      	    });
            return swp.dataList(res.datas);
          },
          error: function(res) {
            opt['timeStatu'] = res.status;
            return swp.timeOut();
          }
        });
      };
    })(this);
    return swp.initData();
  };

  $(function() {
    $(document).on("click", ".item", function() {
      return location.href = "./product.html?id=" + $(this).data("id");
    });
    return loadData();
  });

}).call(this);
